Hi im new to arduino coding. Im using a nodemcu esp8266 board and i want to make a project with it. I write a code it works but i t gives an error while working. I need help pls.
There is my code:
#include <ESP8266WiFi.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <Adafruit_BMP085.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <SPI.h>
#include <SD.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
const char* ssid = "Karabiberim"; // Wi-Fi ağınızın adı
const char* password = "XXXXXXXXXXXX"; // Wi-Fi şifreniz
const int chipSelect = D8; // CS pin for the SD card
// GPS modülünün bağlı olduğu pinler
const int rxPin = D3; // GPS TX pini
const int txPin = D4; // GPS RX pini
const char *mqtt_broker = "broker.emqx.io"; // Enter your WiFi or Ethernet IP
const char *topic = "XXXXXXXXXXXX";
const int mqtt_port = 1883;
WiFiClient espClient;
PubSubClient client(espClient);
// GPS verilerini saklamak için TinyGPS++ nesnesi oluşturuluyor
TinyGPSPlus gps;
Adafruit_BMP085 bmp;
Adafruit_MPU6050 mpu;
Adafruit_Sensor *mpu_temp, *mpu_accel, *mpu_gyro;
unsigned long previousMillis = 0;
const long interval = 1000; // 1 saniye (1000 milisaniye)
String latitude = "*****";
String longitude = "*****";
String altitude = "*****";
String satallites = "*****";
String accx = "*";
String accy = "*";
String accz = "*" ;
String gyrox = "*";
String gyroy = "*";
String gyroz = "*";
String mputemp = "*";
String pressure = "**";
String bmptemp = "**";
String freeheap = "**";
// SoftwareSerial nesnesi oluşturuluyor
SoftwareSerial gpsSerial(rxPin, txPin);
void setup() {
Serial.begin(9600); // Seri haberleşme başlatılıyor
delay(200);
gpsSerial.begin(9600); // GPS ile haberleşme başlatılıyor
client.setServer(mqtt_broker, mqtt_port);
client.setCallback(callback);
if (!bmp.begin()) {
Serial.println("BMP180 sensörü bulunamadı, lütfen bağlantıları kontrol edin!");
yield();
}
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
yield();
}
smartDelay(10);
if (!SD.begin(chipSelect)) {
Serial.println("SD card initialization failed!");
yield();
return;
}
Serial.println("SD card initialized.");
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
mpu.setFilterBandwidth(MPU6050_BAND_5_HZ);
}
void loop() {
yield();
connectWifi();
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
// GPS verilerini oku
if (gpsSerial.available() > 0 && WiFi.status() == WL_CONNECTED){
gps.encode(gpsSerial.read());
// GPS verisi geçerli ise
if (gps.location.isValid()) {
Serial.print("Enlem: ");
Serial.println(gps.location.lat(), 6); // 6 hane hassasiyet
latitude = String(gps.location.lat(),6);
Serial.print("Boylam: ");
Serial.println(gps.location.lng(), 6); // 6 hane hassasiyet
longitude = String(gps.location.lng(),6);
//Serial.print("Yükseklik: ");
//Serial.println(gps.altitude.meters());
//altitude = String(gps.altitude.meters());
//Serial.print("Uydusayısı: ");
//Serial.println(gps.satellites.value());
//satallites = String(gps.satellites.value());
}
else {
// GPS verisi geçersiz ise
Serial.println("Geçersiz GPS verisi!");
}
if (mpu.begin()) {
Serial.print("Acceleration X: ");
Serial.print(a.acceleration.x);
accx = String(a.acceleration.x);
Serial.print(", Y: ");
Serial.print(a.acceleration.y);
accy = String(a.acceleration.y);
Serial.print(", Z: ");
Serial.print(a.acceleration.z);
accz = String(a.acceleration.z);
Serial.println(" m/s^2");
Serial.print("Rotation X: ");
Serial.print(g.gyro.x);
gyrox = String(g.gyro.x);
Serial.print(", Y: ");
Serial.print(g.gyro.y);
gyroy = String(g.gyro.y);
Serial.print(", Z: ");
Serial.print(g.gyro.z);
gyroz = String(g.gyro.z);
Serial.println(" rad/s");
Serial.print("Temperature: ");
Serial.print(temp.temperature);
mputemp = String(temp.temperature);
Serial.println(" degC");
}
else{
Serial.println("MPU bulunamadı.");
}
if (bmp.begin()) {
Serial.print("Sıcaklık: ");
Serial.println(bmp.readTemperature());
bmptemp = String(bmp.readTemperature());
Serial.print("Free Heap: ");
Serial.println(ESP.getFreeHeap());
freeheap = String(ESP.getFreeHeap());
Serial.print("basınc: ");
Serial.println(bmp.readPressure());
pressure = String(bmp.readPressure());
}
else{
Serial.println("BMP bulunamadı.");
}
StaticJsonDocument<200> doc; // JSON belge boyutu
doc["accx"] = accx;
doc["accy"] = accy;
doc["accz"] = accz;
doc["gyrox"] = gyrox;
doc["gyroy"] = gyroy;
doc["gyroz"] = gyroz;
doc["mputemp"] = mputemp;
doc["pressure"] = pressure;
doc["bmptemp"] = bmptemp;
doc["freeheap"] = freeheap;
String jsonString;
serializeJson(doc, jsonString);
StaticJsonDocument<00> doc2;
doc2["latitude"] = latitude;
doc2["longitude"] = longitude;
//doc2["altitude"] = altitude;
//doc2["satallites"] = satallites;
String jsongpsString;
serializeJson(doc2, jsongpsString);
smartDelay(10);
writeFile("json.txt", jsonString.c_str());
smartDelay(10);
writeFile("jsongps.txt", jsongpsString.c_str());
smartDelay(20);
client.connect("admin");
client.loop();
client.publish(topic, jsonString.c_str());
client.publish(topic, jsongpsString.c_str());
//client.disconnect();
//WiFi.disconnect();
}
smartDelay(300);
// Diğer işlemleri buraya ekleyin, GPS verileri işlenirken döngü devam eder
}
void smartDelay(unsigned long ms) {
unsigned long start = millis(); // Başlangıç zamanını al
do {
// Bekleme süresi kadar zaman geçene kadar diğer işlemleri yap
while (gpsSerial.available() > 0) {
gps.encode(gpsSerial.read());
}
} while (millis() - start < ms);
}
void writeFile(const char* filename, const char* message) {
File file = SD.open(filename, FILE_WRITE);
if (file) {
file.println(message);
file.close();
Serial.println("Write to SD card successful.");
} else {
Serial.println("Error opening file for writing.");
}
Serial.println();
}
void connectWifi(){
yield();
if (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("Wi-Fi'ye bağlanıldı");
Serial.println("IP Adresi: " + WiFi.localIP().toString());
}
}
void callback(char *topic1, byte *payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
Serial.println(topic1);
Serial.print("Message:");
String s;
for (int i = 0; i < length; i++) {
Serial.print((char) payload[i]);
s += (char)payload[i];
}
}
And this is the error:
15:26:50.800 -> SDK:2.2.2-dev(38a443e)/Core:3.1.0=30100000/lwIP:STABLE-2_1_3_RELEASE/glue:1.2-65-g06164fb/BearSSL:b024386
15:26:51.698 -> SD card initialized.
15:26:51.698 -> Fatal exception 0(IllegalInstructionCause):
15:26:51.783 -> epc1=0x40210728, epc2=0x00000000, epc3=0x00000000, excvaddr=0x00000000, depc=0x00000000
15:26:51.850 ->
15:26:51.850 -> --------------- CUT HERE FOR EXCEPTION DECODER ---------------
15:26:51.916 ->
15:26:51.916 -> Exception (0):
15:26:51.949 -> epc1=0x40210728 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000
15:26:52.050 ->
15:26:52.050 -> >>>stack>>>
15:26:52.050 ->
15:26:52.050 -> ctx: cont
15:26:52.050 -> sp: 3ffff5c0 end: 3ffffd20 offset: 0190
15:26:52.116 -> 3ffff750: feefeffe feefeffe feefeffe feefeffe
15:26:52.149 -> 3ffff760: feefeffe feefeffe feefeffe feefeffe
15:26:52.216 -> 3ffff770: feefeffe feefeffe 00000001 feefeffe
15:26:52.249 -> 3ffff780: feefeffe feefeffe feefeffe c0038035
15:26:52.316 -> 3ffff790: 00000000 00000001 00000000 40100bbc
15:26:52.383 -> 3ffff7a0: 00000020 feefeffe feefeffe feefeffe
15:26:52.416 -> 3ffff7b0: feefeffe feefeffe feefeffe 00000022
15:26:52.483 -> 3ffff7c0: 3fffc200 40100af8 3fffc258 4000050c
15:26:52.516 -> 3ffff7d0: 40004378 00000030 0000001c ffffffff
15:26:52.582 -> 3ffff7e0: 60000200 00000008 09051a17 80000000
15:26:52.616 -> 3ffff7f0: 20000000 3ffefe98 80000000 203fc040
15:26:52.682 -> 3ffff800: 80000000 3fffc6fc 00000001 3ffefe9c
15:26:52.715 -> 3ffff810: 00000234 003fc040 00000045 00000030
15:26:52.783 -> 3ffff820: feefeffe feefeffe feefeffe feefeffe
15:26:52.849 -> 3ffff830: feefeffe feefeffe feefeffe feefeffe
15:26:52.883 -> 3ffff840: feefeffe feefeffe feefeffe feefeffe
15:26:52.947 -> 3ffff850: feefeffe feefeffe feefeffe feefeffe