I recently bought an Arduino Uno Wifi in order to write sensor data right from my Arduino into an existing MySQL Database. I thought since there are some existing libraries (like this one) that do exactly the same thing with the help of a ethernet or wifi shield, it shouldn't be a big problem. The problem I discovered just after I bought the Uno Wifi is, that all of them require libraries of their one (like Ethernet.h or Wifi.h). I would need an extra shield to use them which doesn't make much sense in my opinion, since I already bought an wifi-ready Arduino. I know that there are things like ThingSpeak which is supported, but I don't want to loose the flexibility of my own database and don't need all the extra analytic services, that ThingSpeak offers me.
So my kinda desperate question is - is there a way to write data with a simple INSERT INTO statement to an existing database with my (brand new) Arduino Uno WIFI?
EDIT:
I tried some things today, that's what I came up with so far:
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
#include <UnoWiFiDevEd.h>
IPAddress server_addr(88, 198, 61, 231); // IP of the MySQL *server* here
char user[] = "USERxxxxxx"; // MySQL user login username
char password[] = "xxxxxxxxx"; // MySQL user login password
// Sample query
char INSERT_SQL[] = "INSERT INTO `arduino_test`(`Humidity`, `Temperature`, `DateTime`) VALUES (60, 23, '2017-02-02 20:34:20')";
WiFiClient client;
MySQL_Connection conn((Client *)&client);
void setup() {
Serial.begin(9600);
while (!Serial); // wait for serial port to connect
Wifi.begin();
Serial.println("Connecting");
if (conn.connect(server_addr, 3306, user, password)) {
delay(5000);
Serial.println("Connection successful");
} else {
Serial.println("Connection failed!");
}
}
void loop() {
delay(2000);
Serial.println("Recording data.");
// Initiate the query class instance
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
// Execute the query
cur_mem->execute(INSERT_SQL);
// Note: since there are no results, we do not need to read any data
// Deleting the cursor also frees up memory used
delete cur_mem;
}
This results in the program not being able to establish a connection to server and just constantly printing "Connecting" to the serial monitor.
EDIT 2:
I figured something out, thanks to @cagdas idea to check the connection first. This is my sketch right now:
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
#include <Wire.h>
#include <UnoWiFiDevEd.h>
IPAddress server_addr(88, 198, 61, 231); // IP of the MySQL *server* here
char user[] = "USERxxxxxx"; // MySQL user login username
char password[] = "xxxxxxx"; // MySQL user login password
// Sample query
char INSERT_SQL[] = "INSERT INTO `arduino_test`(`Humidity`, `Temperature`, `DateTime`) VALUES (60, 23, '2017-02-02 20:34:20')";
WifiData client;
MySQL_Connection conn((Client *)&client);
void setup() {
char* connector = "rest";
char* server = "download.arduino.org";
char* method = "GET";
String resource = "/latest.txt";
Serial.begin(9600);
Ciao.begin();
pinMode(2, INPUT);
delay(5000);
doRequest(connector, server, resource, method);
Wifi.begin();
Serial.println("Connecting");
if (conn.connect(server_addr, 3306, user, password)) {
Serial.println("Connection successful");
} else {
Serial.println("Connection failed!");
}
}
void loop() {
delay(2000);
Serial.println("Recording data.");
// Initiate the query class instance
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
// Execute the query
cur_mem->execute(INSERT_SQL);
// Note: since there are no results, we do not need to read any data
// Deleting the cursor also frees up memory used
delete cur_mem;
}
void doRequest(char* conn, char* server, String command, char* method) {
CiaoData data = Ciao.write(conn, server, command, method);
if (!data.isEmpty()) {
Ciao.println( "State: " + String (data.get(1)) );
Ciao.println( "Response: " + String (data.get(2)) );
Serial.println( "State: " + String (data.get(1)) );
Serial.println( "Response: " + String (data.get(2)) );
}
else {
Ciao.println ("Write Error");
Serial.println ("Write Error");
}
}
If the connection works this should print out "State: 200" and "Response: 10709". But it actually printed just a 0. If I remove my loop and just write
void loop() {}
it works fine (but obviously does not write anything into the database). Putting the code from the loop into this if statement "if (conn.connect(server_addr, 3306, user, password))" also does not return the correct result. So at this point I'm still clueless why this code doesn't work but I think it might give deeper insight into a possible solution. Also the Arduino IDE tells me something like this
The sketch is using 50% of the memory
Global variables use 88% of the memory
There is only little RAM left -> stability issues possible (sorry, it's in german)