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..