Hello,
I am a beginner Arduino programmer and I have a W5500 Ethernet module, an Arduino Uno board, and a NEO 6M GPS module. I am programming in Arduino IDE 2.2.1. I set myself the goal of sending to the database information obtained through GPS. The problem is that in the serial monitor, the message "...trying..." is displayed, indicating an attempt to connect to the database, and then the program freezes. Below, I present the code that I uploaded to Arduino. I will also add a screenshot of the CPU usage. I checked the GPS module itself - it works. I also sent constant values to the database - it was successful without any problem. The problem appeared when I wanted to send information directly from the GPS module.
#include <Ethernet.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x99 };
IPAddress server_addr(xxx,xxx,xxx,xx); // Adres IP serwera MySQL
char user[] = ""; // Użytkownik MySQL
char password[] = ""; // Hasło MySQL
EthernetClient client;
MySQL_Connection conn((Client *)&client);
// Konfiguracja GPS
static const int RXPin = 4, TXPin = 3;
static const uint32_t GPSBaud = 9600;
TinyGPSPlus gps;
SoftwareSerial ss(RXPin, TXPin);
void setup() {
Serial.begin(115200);
ss.begin(GPSBaud);
while (!Serial); // czekaj na połączenie portu szeregowego
Ethernet.begin(mac_addr);
Serial.println("Connecting...");
if (conn.connect(server_addr, 3306, user, password)) {
Serial.println("Connected to MySQL server.");
} else {
Serial.println("Connection failed.");
}
}
void loop() {
// Odczyt danych GPS
while (ss.available() > 0) {
if (gps.encode(ss.read())) {
if (gps.location.isValid()) {
// Przygotowanie zapytania SQL
char INSERT_SQL[255];
sprintf(INSERT_SQL, "INSERT INTO mtbscjchhr_GPS.GPS (dlugosc, szerokosc) VALUES (%f, %f)", gps.location.lat(), gps.location.lng());
Serial.println("Recording data.");
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
cur_mem->execute(INSERT_SQL);
delete cur_mem;
}
}
}
delay(5000); // Czekaj 5 sekund przed kolejnym odczytem
}