I have a project to try and get the Arduino to upload data to an SQL server!

The following code works just fine on Arduino UNO:

#include <Wire.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
#include <ESP8266_Lib.h>
#include "SHTSensor.h"
SHTSensor sht;
#include "WiFiEsp.h"
#include "SoftwareSerial.h"
SoftwareSerial Serial1(7, 8); // RX, TX
long sendetid;
double temp;
double fuktighet;

int status = WL_IDLE_STATUS;
#define EspSerial Serial1
#define ESP8266_BAUD 19200

ESP8266 wifi(&EspSerial);

IPAddress server_addr(10,0,0,208);  // IP of the MySQL *server* here
char user[] = "ArdTest";              // MySQL user login username
char password[] = "testPass";        // MySQL user login password

// Sample query
char INSERT_SQL[] = "INSERT INTO test_arduino.hello_arduino (message) VALUES ('Hello, Arduino!')";
// char INSERT_SQL[] = "INSERT INTO test_arduino.sensor (temperature, humidity) VALUES ('temp', fuktighet)";

// WiFi card example
char ssid[] = "NextGenTel_81";         // your SSID
char pass[] = "hidden";     // your SSID Password

WiFiEspClient client;
MySQL_Connection conn(&client);
MySQL_Cursor* cursor;

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  // initialize serial for ESP module
  Serial1.begin(19200);
  // initialize ESP module
  WiFi.init(&Serial1);
  delay(1600);

  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);
  }
  
/*  // Begin WiFi section
  Serial.printf("\nConnecting to %s", ssid);
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  } */

  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }

  Serial.println("You're connected to the network");


  // print out info about the connection:
  Serial.println("\nConnected to network");
  Serial.print("My IP address is: ");
  Serial.println(WiFi.localIP());

  Serial.print("Connecting to SQL...  ");
  if (conn.connect(server_addr, 3306, user, password))
    Serial.println("OK.");
  else
    Serial.println("FAILED.");
  
  // create MySQL cursor object
  cursor = new MySQL_Cursor(&conn);

  if (sht.init()) {
    Serial.print("init(): success\n");
  }
  else {
    Serial.print("init(): failed\n");
  }

  sht.setAccuracy(SHTSensor::SHT_ACCURACY_MEDIUM);
  printWifiStatus();
  sendetid = millis();
  sjekker();
  sender();
}

void loop()
{
  if ((millis() - sendetid) > 10350L) {
    sjekker();
    sender();
  }

}

void sender()
{
  sendetid = millis();
  Serial.println("Sending..");

/*  client.stop();
  delay(400); */

  if (conn.connected())
    cursor->execute(INSERT_SQL);

  Serial.println("Sending complete..");
}

void printWifiStatus()
{
  // print the SSID of the network you're attached to
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}

void sjekker()
{
  if (sht.readSample()) {

    temp = sht.getTemperature();  // get temp from SHT
    fuktighet = sht.getHumidity(); // get temp from SHT
  
  } 
  
  else {
    Serial.print("Error in readSample()\n");
  }
  
  delay(500);

  Serial.print("Temperatur = ");
  Serial.print(temp);
  Serial.print(" - Fuktighet = ");
  Serial.println(fuktighet);
}

When I try to run this on Arduino MEGA and after removing the Software serial, I get gibberish login info on the SQL part.. Works just fine on WiFi login! But for some reason I get this in the serial monitor (on the UNO the login is correct and working), (the "square" is a symbol of a cube, doesn't seem to be possible to paste it in):

[WiFiEsp] Initializing ESP module
[WiFiEsp] Initilization successful - 2.0.0
Attempting to connect to WPA SSID: NextGenTel_81
[WiFiEsp] Connected to NextGenTel_81
You're connected to the network

Connected to network
My IP address is: 10.0.0.227
Connecting to SQL...  [WiFiEsp] Connecting to 10.0.0.208
[WiFiEsp] TIMEOUT: 58
ERROR: Timeout waiting for client.
[WiFiEsp] TIMEOUT: 45
ERROR: Timeout waiting for client.
Error: 76 = Access denied for user P L  "square" #28000Access <R!"square": "square""square"s"square"b+C[ "square"T, 
FAILED.

Just ignore the temperature stuff, it will be added once I get this working..

Post the code which fails. You have almost certainly made an error “removing” softwareSerial.

Any advice on how to make the code better is also accepted.. And the reason why I use the Mega now is that when I set up the code to do what I want, it uses a lot more space!

#include <Wire.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
#include <ESP8266_Lib.h>
#include "SHTSensor.h"
SHTSensor sht;
#include "WiFiEsp.h"
long sendetid;
double temp;
double fuktighet;

int status = WL_IDLE_STATUS;
#define EspSerial Serial1
#define ESP8266_BAUD 19200

ESP8266 wifi(&EspSerial);

IPAddress server_addr(10,0,0,208);  // IP of the MySQL *server* here
char user[] = "ArdTest";              // MySQL user login username
char password[] = "testPass";        // MySQL user login password

// Sample query
char INSERT_SQL[] = "INSERT INTO test_arduino.hello_arduino (message) VALUES ('Hello, Arduino!')";
// char INSERT_SQL[] = "INSERT INTO test_arduino.sensor (temperature, humidity) VALUES ('temp', fuktighet)";

// WiFi card example
char ssid[] = "NextGenTel_81";         // your SSID
char pass[] = "hidden";     // your SSID Password

WiFiEspClient client;
MySQL_Connection conn(&client);
MySQL_Cursor* cursor;

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  // initialize serial for ESP module
  Serial1.begin(19200);
  // initialize ESP module
  WiFi.init(&Serial1);
  delay(1600);

  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);
  }
  
/*  // Begin WiFi section
  Serial.printf("\nConnecting to %s", ssid);
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  } */

  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }

  Serial.println("You're connected to the network");


  // print out info about the connection:
  Serial.println("\nConnected to network");
  Serial.print("My IP address is: ");
  Serial.println(WiFi.localIP());

  Serial.print("Connecting to SQL...  ");
  if (conn.connect(server_addr, 3306, user, password))
    Serial.println("OK.");
  else
    Serial.println("FAILED.");
  
  // create MySQL cursor object
  cursor = new MySQL_Cursor(&conn);

  if (sht.init()) {
    Serial.print("init(): success\n");
  }
  else {
    Serial.print("init(): failed\n");
  }

  sht.setAccuracy(SHTSensor::SHT_ACCURACY_MEDIUM);
  printWifiStatus();
  sendetid = millis();
  sjekker();
  sender();
}

void loop()
{
  if ((millis() - sendetid) > 10350L) {
    sjekker();
    sender();
  }

}

void sender()
{
  sendetid = millis();
  Serial.println("Sending..");

/*  client.stop();
  delay(400); */

  if (conn.connected())
    cursor->execute(INSERT_SQL);

  Serial.println("Sending complete..");
}

void printWifiStatus()
{
  // print the SSID of the network you're attached to
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}

void sjekker()
{
  if (sht.readSample()) {

    temp = sht.getTemperature();  // get temp from SHT
    fuktighet = sht.getHumidity(); // get temp from SHT
  
  } 
  
  else {
    Serial.print("Error in readSample()\n");
  }
  
  delay(500);

  Serial.print("Temperatur = ");
  Serial.print(temp);
  Serial.print(" - Fuktighet = ");
  Serial.println(fuktighet);
}

I'm using the TX and RX on the board as Serial1.. And I had to change RX to TX and TX to RX on the ESP or it would not work at all..

Do I have to define them?

Edit: Also, I like your Electron Vacuum Tube Avatar.. lol

Nobody knows what the problem is? Actually I need to know how to properly send data from temperature and humidity with the SQL command too.. Do you just do as in the commented out code?

char INSERT_SQL[] = "INSERT INTO test_arduino.sensor (temperature, humidity) VALUES ('temp', fuktighet)";

Post your error from your debug screen.

It looks like you are having problems with the ESP8266 talking back to the Mega. The other way appears to have worked because it appears this statement wast sent right trhrough to the SQL server and and understood:

if (conn.connect(server_addr, 3306, user, password))

It appears, however that the answer transmitted from the ESP8266 to the mega was garbled.

I don't know the libraries:

#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
#include <ESP8266_Lib.h>

and I always use mySQL from a PHP server, not a direct connection as you are attempting, but this seems strange:

#define EspSerial Serial1
. . .
ESP8266 wifi(&EspSerial);
. . .
WiFi.init(&Serial1);

You are making Serial1 available two 2 different objects. Can you post a link to the tutorial you have followed for using ESP8266_Lib.h ?

chiques:
Post your error from your debug screen.

If you mean the Serial monitor I already posted that? What other debug screens is there? I dunno..

6v6gt:
if (conn.connect(server_addr, 3306, user, password))

It appears, however that the answer transmitted from the ESP8266 to the mega was garbled.

I don't know the libraries:

#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
#include <ESP8266_Lib.h>

and I always use mySQL from a PHP server, not a direct connection as you are attempting, but this seems strange:

#define EspSerial Serial1

. . .
ESP8266 wifi(&EspSerial);
. . .
WiFi.init(&Serial1);




You are making Serial1 available two 2 different objects. Can you post a link to the tutorial you have followed for using ESP8266_Lib.h ?

Somehow I managed to get it working one time.. It logged in and sent the value to the SQL, but then it stopped working again and I haven't got it to work after that.. I have no clue..

I don't have the tutorial, I used the code from a friend of mine and it should be working, he got a Arduino that has been running for years now.. I guess I have to read on GitHub to find out how it works, but it's only to get connection to the ESP and using the WiFi, and that should be working..

What I'm thinking about now is that it may be that the encryption of the password is messed up somehow.. Or the Wifi does not understand the symbols.. But that's just a guess! I mean I had it working on the UNO.. It worked every time.. But further testing is required..

Is it the compiler debug your asking about?

Archiving built core (caching) in: C:\Users\BLUELI~1\AppData\Local\Temp\arduino_cache_457239\core\core_arduino_avr_mega_cpu_atmega2560_453d43f0eee558934e02dd05628a3f9f.a
Sketch uses 21462 bytes (8%) of program storage space. Maximum is 253952 bytes.
Global variables use 1647 bytes (20%) of dynamic memory, leaving 6545 bytes for local variables. Maximum is 8192 bytes.

I’ve just noticed this:

I'm using the TX and RX on the board as Serial1.. And I had to change RX to TX and TX to RX on the ESP or it would not work at all..

You mean TX1 and RX1 on the Arduino Mega ? These are used by Serial1. Crossing over RX and TX is normal.

Maybe post a wiring diagram where pin numbering and labeling is clear.

Edit:

If you are having reliability problems with the serial link, you can lower the baud rate on the ESP to 9600

I just got the USB to Serial device and used the string "AT+UART_DEF=9600,8,1,0,0" to lower the Baud rate to 9600.. It seems there is still a problem, and it looks like it's in my code..

Here is the output from the Serial Monitor:

[WiFiEsp] Initializing ESP module
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] Initilization successful - 2.0.0
Attempting to connect to WPA SSID: NextGenTel_81
[WiFiEsp] Connected to NextGenTel_81
You're connected to the network

Connected to network
My IP address is: 10.0.0.222
Connecting to SQL...  [WiFiEsp] Connecting to 10.0.0.208
[WiFiEsp] TIMEOUT: 4
ERROR: Timeout waiting for client.
[WiFiEsp] TIMEOUT: 23
ERROR: Timeout waiting for client.
Error: 76 = Access denied for userssword: YES)3,CLOSED

Second try:

[WiFiEsp] Initializing ESP module
[WiFiEsp] Initilization successful - 2.0.0
Attempting to connect to WPA SSID: NextGenTel_81
[WiFiEsp] Connected to NextGenTel_81
You're connected to the network

Connected to network
My IP address is: 10.0.0.222
Connecting to SQL...  [WiFiEsp] Connecting to 10.0.0.208
[WiFiEsp] TIMEOUT: 33
ERROR: Timeout waiting for client.
[WiFiEsp] TIMEOUT: 23
ERROR: Timeout waiting for client.
Error: 76 = Access denied for userssword: YES)3,CLOSED

As you can see it is combining the words.. Like Username and Password is "userssword", and I have now tested with a second ESP8266 and this is still the same issue.. Theres more in the Monitor but it's just temperature readings and stuff.. The Forum did a cut where there was random symbols that was noted as a wrong password..

Must be some sort of flaw in the code.. Posting the code here:

/*
  MySQL Connector/Arduino Example : connect by wifi

  This example demonstrates how to connect to a MySQL server from an
  Arduino using an Arduino-compatible Wifi shield. Note that "compatible"
  means it must conform to the Ethernet class library or be a derivative
  thereof. See the documentation located in the /docs folder for more
  details.

  INSTRUCTIONS FOR USE

  1) Change the address of the server to the IP address of the MySQL server
  2) Change the user and password to a valid MySQL user and password
  3) Change the SSID and pass to match your WiFi network
  4) Connect a USB cable to your Arduino
  5) Select the correct board and port
  6) Compile and upload the sketch to your Arduino
  7) Once uploaded, open Serial Monitor (use 115200 speed) and observe

  If you do not see messages indicating you have a connection, refer to the
  manual for troubleshooting tips. The most common issues are the server is
  not accessible from the network or the user name and password is incorrect.

  Created by: Dr. Charles A. Bell
*/
#include <Wire.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
#include <ESP8266_Lib.h>
#include "SHTSensor.h"
SHTSensor sht;
#include "WiFiEsp.h"
long sendetid;
double temp;
double fuktighet;

int status = WL_IDLE_STATUS;
#define EspSerial Serial1
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

IPAddress server_addr(10,0,0,208);  // IP of the MySQL *server* here
char user[] = "ArdTest";              // MySQL user login username
char password[] = "testPass";        // MySQL user login password

// Sample query
char INSERT_SQL[] = "INSERT INTO test_arduino.hello_arduino (message) VALUES ('Hello, Arduino!')";
// char INSERT_SQL[] = "INSERT INTO test_arduino.sensor (temperature, humidity) VALUES ('temp', fuktighet)";

// WiFi card example
char ssid[] = "NextGenTel_81";         // your SSID
char pass[] = "hidden";     // your SSID Password

WiFiEspClient client;
MySQL_Connection conn(&client);
MySQL_Cursor* cursor;

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  // initialize serial for ESP module
  Serial1.begin(9600);
  // initialize ESP module
  WiFi.init(&Serial1);
  delay(1600);

  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);
  }
  
/*  // Begin WiFi section
  Serial.printf("\nConnecting to %s", ssid);
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  } */

  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }

  Serial.println("You're connected to the network");


  // print out info about the connection:
  Serial.println("\nConnected to network");
  Serial.print("My IP address is: ");
  Serial.println(WiFi.localIP());

  Serial.print("Connecting to SQL...  ");
  if (conn.connect(server_addr, 3306, user, password))
    Serial.println("OK.");
  else
    Serial.println("FAILED.");
  
  // create MySQL cursor object
  cursor = new MySQL_Cursor(&conn);

  if (sht.init()) {
    Serial.print("init(): success\n");
  }
  else {
    Serial.print("init(): failed\n");
  }

  sht.setAccuracy(SHTSensor::SHT_ACCURACY_MEDIUM);
  printWifiStatus();
  sendetid = millis();
  sjekker();
  sender();
}

void loop()
{
  if ((millis() - sendetid) > 10350L) {
    sjekker();
    sender();
  }

}

void sender()
{
  sendetid = millis();
  Serial.println("Sending..");

/*  client.stop();
  delay(400); */

  if (conn.connected())
    cursor->execute(INSERT_SQL);

  Serial.println("Sending complete..");
}

void printWifiStatus()
{
  // print the SSID of the network you're attached to
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}

void sjekker()
{
  if (sht.readSample()) {

    temp = sht.getTemperature();  // get temp from SHT
    fuktighet = sht.getHumidity(); // get temp from SHT
  
  } 
  
  else {
    Serial.print("Error in readSample()\n");
  }
  
  delay(500);

  Serial.print("Temperatur = ");
  Serial.print(temp);
  Serial.print(" - Fuktighet = ");
  Serial.println(fuktighet);
}

It's really strange that it worked before.. I dunno what to do.. lol