Arduino MYSQL connection problem

I get this error messages when I try conecting to MYSQL server with Arduino Uno R4 Wifi device.

...trying...
ERROR: Timeout waiting for client.
Error: -1 = ERROR: Class requires connected server.
ERROR: Class requires connected server.

But with Arduino 1010 MKR device, I can connect to MYSQL server without any problems.
Arduino code can be found below this text.

Can someone help me?

#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
#include <MySQL_Encrypt_Sha1.h>
#include <MySQL_Packet.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
#include <MySQL_Encrypt_Sha1.h>
#include <MySQL_Packet.h>
#include <Ethernet.h>
#include <DHT.h>
#include <DHT_U.h>
#include <dht.h>
//#include <WiFiNINA.h>
#include <WiFiClient.h>
#include "wifiinfo.h"
#include <avr/dtostrf.h>
// #include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <hd44780.h>                      
#include <hd44780ioClass/hd44780_I2Cexp.h>
#include <WiFiS3.h>

hd44780_I2Cexp lcd;

byte mac_addr[] = { 0x50, 0x81, 0x40, 0x9A, 0xD5, 0x6F };

#define DHTPIN 6  
#define DHTPIN2 7    
#define DHTTYPE 22
DHT deviceMonitor(DHTPIN,DHTTYPE);
DHT deviceMonitor2(DHTPIN2,DHTTYPE);
IPAddress server_addr(192,168,50,123);

char database[] ="weatherstation";
char user[] = "person";              
char password2[] = "test1";      

char ssid[] = SECRET_SSID;        
char password[] = SECRET_PASS;  

int status = WL_IDLE_STATUS;    
int blueLed = 3;
int redLed = 4;
int greenLed = 5;
int switchButton = 6;

int choose = 1;
int checkTemp;
int timer1_counter;
long period;
long setPeriod = 600;
int delayNumber = 10;
int stopTimer = 0;
bool beginOver = true;
unsigned long time_now = 0;
long timercounter = 0;
long timercounter2 = 0;

float hum;
float temp;
float hum2;
float temp2;
float humError;
float tempError;

char tempNew[15];
char humNew[15];
char tempNew2[15];
char humNew2[15];
char INSERT_DATA[] = "INSERT INTO dataview (outtemp, outhum, intemp, inhum) VALUES ('%s','%s','%s','%s')";
char query[128];
char temperature[10];

WiFiClient client;
MySQL_Connection conn((Client *)&client);

void setup()
{
  Serial.begin(115200);
  Serial.setTimeout(5000);
 
  pinMode(blueLed, OUTPUT);
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(switchButton, INPUT_PULLUP);

  digitalWrite(blueLed, LOW);
  digitalWrite(redLed, LOW);
  digitalWrite(greenLed, HIGH);  

  attachInterrupt(digitalPinToInterrupt(switchButton), interuptPin, CHANGE);
  lcd.init();
  lcd.begin(8, 2);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Starting");
  lcd.setCursor(0,1);
  lcd.print(" device!");
  deviceMonitor.begin();
  deviceMonitor2.begin();
 // while (!Serial); // wait for serial port to connect
  Ethernet.begin(mac_addr);

  Serial.println("Connecting wifi!");
  Serial.println();

  status = WiFi.begin(ssid, password);
 // delay(10000);
  if (status != WL_CONNECTED)
  {
    Serial.println("Couldn't get a wifi connection");
    while(true);
   
  }
  else
  {
    Serial.println("You're connected to the network");
    printData();
   
  }
  period = 300;
   conn.connect(server_addr, 3306, user, password2, database);
}
void delayTime()
{
  time_now = millis();
  while(millis() - time_now < period){
     if (stopTimer == 1){
       // period = 0;
        stopTimer = 0;
        beginOver = true;
        break;
      }  
   }
 
}
void loop()
{
delay(1000);
timercounter2++;  


 if (timercounter2 == 1)
    {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Delay:");
      lcd.setCursor(0, 1);
      lcd.print(delayNumber);
      lcd.setCursor(6, 1);
      lcd.print("min");
     
    }

    if (timercounter2 == 15)
    {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Temp:");
      lcd.setCursor(0, 1);
      lcd.print(temp);
      lcd.setCursor(6, 1);
      lcd.print("C");
       
    }


    if (timercounter2 == 30)
    {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Hum:");
      lcd.setCursor(0, 1);
      lcd.print(hum);
      lcd.setCursor(6, 1);
      lcd.print("%");

    }

   
    if (timercounter2 == 45)
    {
        timercounter2 = 0;
    }


  /*hum = deviceMonitor2.readHumidity();
  temp = deviceMonitor2.readTemperature();
  hum2 = deviceMonitor.readHumidity();
  temp2 = deviceMonitor.readTemperature(); */


  hum = 11;
  temp = 22;
  hum2 = 33;
  temp2 = 44;
  timercounter++;
     
   // checkTemp = deviceMonitor.read(DHTPIN);


    if (timercounter == 5){
      MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
      if (isnan(hum) || isnan(temp))
      {
        lcd.clear();
        lcd.print("Reading error!");
      }
      else
      {
        dtostrf(temp,-4, 1, tempNew);
        dtostrf(hum,-4, 1, humNew);
        dtostrf(temp2,-4, 1, tempNew2);
        dtostrf(hum2,-4, 1, humNew2);
        sprintf(query, INSERT_DATA, tempNew, humNew, tempNew2, humNew2);
        cur_mem->execute(query);
        delete cur_mem;
        timercounter = 0;
      }  
    }
   // delay(1000);
}

void printData()
{
  Serial.println();
  Serial.println("Network Information:");
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.println(rssi);

  byte encryption = WiFi.encryptionType();
  Serial.print("Encryption Type:");
  Serial.println(encryption, HEX);
  Serial.println();
}

Disclaimer
No experience with the Uno R4 WiFi nor with the MKR1010; I also don't have experience with the MySql connector. There however are some potential issues in your code.

conn.connect(server_addr, 3306, user, password2, database);

You should check the result of the operation. I'm not able to tell you why it fails. Any chance that your MySql server does not allow connections from the IP address of the R4?

MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);

This creates a MySql cursor that needs to be deleted; you however only delete it if hum and temp are valid so if these are not valid you don't delete the cursor and eventually will run out of memory.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.