3

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)

Community
  • 1
  • 1
Felix Bockemuehl
  • 157
  • 1
  • 3
  • 14
  • does the answer makes sense? – cagdas Feb 02 '17 at 15:30
  • @cagdas Do you've any idea where this strange behavior is coming from? Or any idea what to do next? – Felix Bockemuehl Feb 04 '17 at 19:53
  • Where are your wifi credentials? – cagdas Feb 05 '17 at 03:32
  • @cagdas I'm not sure if I need them, since the Uno WiFi is already connected to my wifi via the [web interface](http://www.arduino.org/images/tutorials/wifi4.png). I tested the connection with another sketch and it worked. – Felix Bockemuehl Feb 05 '17 at 12:27
  • Yeah, the lib u use looks like handle much more than i expected. Maybe you can try to establish a webserver connection first which will be easier and gonna prove the network connectivity. Than we can talk about the mysql connector. – cagdas Feb 05 '17 at 21:08
  • @cagdas I tried to implement some code that proofs the network connectivity first and it gave me some strange results (like you can see in the edit 2 of my initial post). – Felix Bockemuehl Feb 05 '17 at 23:08
  • Actually I have no ex with Ciao. But I saw you are doing frequent jobs in loop. Lets try for just one MySQL shot. – cagdas Feb 08 '17 at 18:13

1 Answers1

1

You are eligible to use ESP8266's overloaded WiFiClient by including it.

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

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

Network clients have supper inheritances on Arduino side, so WiFiClient need to implement the things you need.

You will continue to your code with :

WiFi.begin(SSID,PASS);
cagdas
  • 1,634
  • 2
  • 14
  • 27
  • Hey cagdas, thanks for your answer and sorry for the late reply. I tried it and unfortunately it doesn't really work for me. The ESP8266WiFi.h throws the exception "/Arduino/libraries/ESP8266WiFi/src/ESP8266WiFiType.h:26:19: fatal error: queue.h: No such file or directory #include " – Felix Bockemuehl Feb 02 '17 at 21:04