Arduino Uno Wifi and SQL

Hey everyone,

I´m currently using the Arduino Uno Wifi (not a Wifi Shield). I want to send sensor data to an existing MySQL database, but do not really know how to do it. I found quite a lot of tutorials, but all of them use Wifi- or Ethernet-Shields and libraries like Ethernet.h or Wifi.h, which I don't need.
Isn´t there a simple way to make an SQL connection, without using any libraries which require additional hardware?

Thanks in advance!

Isn´t there a simple way to make an SQL connection, without using any libraries which require additional hardware?

I think you would have a much better chance of taking the existing MySQL Connector and adapting it to work with your ethernet library. The ethernet piece is just the transport and will be conceptually the same no matter which ethernet board you use. Somebody else has already worked out the protocols for talking to a MySQL server (it's not simple), so I would start with that.

It's on github, so you can fork the repo and make your changes and then contribute it back to the community when you have it working.

Hello,
I have the same issue. Did you found a solution? I also need to do some select queries..
Thank you.

yari92:
Hello,
I have the same issue. Did you found a solution? I also need to do some select queries..
Thank you.

I am actually talking to the web-server (php-script) with GET request, php-script parse the data, validate it and send to mysql server. Is this suitable for you? Select also could be done on similar way.

Hi, thank you for reply.
Sure, if it works, it sounds good!
Can you share it?
Thank you.

yari92:
Hi, thank you for reply.
Sure, if it works, it sounds good!
Can you share it?
Thank you.

I have all files here: http://sergeskor.no-ip.org/Arduino/project/All_arduino_DataLogg_WebServer_and_client.zip

It is kind of aged, I did many changes since that, but still should work for you.
All you need is in function senREQ() in .ino file, this is the function used to send GET request to web-server, and in SaveArduino2.php file, this is the file which process this request. Actually, if you can read russian :-), the more detailed description (but still aged) is here: Arduino DataLogger (web-server and client), project is online, so links are accesible, but i may shut it down at any time if i need to work with it.

Hi, the problem is that i have the "Arduino Uno WiFi", which have an integrated wireless adapter.
As i could see from your sketch, you have used an Ethernet shield, which i don't have..
I think that in your case you could use mysql connector library..
Am I wrong?

yari92:
Hi, the problem is that i have the "Arduino Uno WiFi", which have an integrated wireless adapter.
As i could see from your sketch, you have used an Ethernet shield, which i don't have..
I think that in your case you could use mysql connector library..
Am I wrong?

Yes, i am using arduino uno with ethernet shield and wired connection. I do not know about mysql connector library, i've never use it and i dont want to talk directly to mysql server because mysql server is not accessible from outside of my home network, but my web server is.

SergeS:
Yes, i am using arduino uno with ethernet shield and wired connection. I do not know about mysql connector library, i've never use it and i dont want to talk directly to mysql server because mysql server is not accessible from outside of my home network, but my web server is.

Got you. Thank you for your reply.

I think that I found a working solution for me, and I share it. Don't know how it is elegant or memory efficient.. but still.. it works.

I have combined a RestClient example with Arduino Uno Wi Fi and a ArduinoJsonParser adding a php page which implements a query and replies with a JSON data.

Here is the sketch:

#include <Wire.h>
#include <UnoWiFiDevEd.h>
#include <ArduinoJson.h>

void setup() {

 const char* connector = "rest";
 const char* server = "192.168.1.103"; // webserver ip
 const char* method = "GET";
 const char* resource = "/~yari/prova/select_arduino.php";  // php page location
 
 
 
 
 Serial.begin(9600);
 Ciao.begin();
  
 pinMode(2, INPUT);


 doRequest(connector, server, resource, method);
}

void loop() {

}

void doRequest(const char* conn, const char* server, const char* command, const char* method){
 CiaoData data = Ciao.write(conn, server, command, method);
 if (!data.isEmpty()){
  DynamicJsonBuffer jsonBuffer;
 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)) );
 
 JsonObject& root = jsonBuffer.parseObject(String (data.get(2)));


  // Saving data:
 const char* nome = root["nome"];
 const char* cognome = root["cognome"];

 // Print data:
 Serial.println(cognome);
 Serial.println(nome);
 
 
 }
 else{
 Ciao.println ("Write Error");
 Serial.println ("Write Error");
 }
}

Here is the "select_arduino.php":

<?php

// Host definition:
$host = "Your Host";
// Database name definition:
$nomeDB = "Your DB Name";
// Database username definition:
$nomeUtente = "Your DB User";
// Database password definition:
$passwordUtente = "Your DB Password";

// Connection:
$connessione=new mysqli($host, $nomeUtente, $passwordUtente, $nomeDB);

// Se ci sono degli errori:
if (mysqli_connect_errno($connessione)) {
	
	
	printf("Error:
	%s\n",mysqli_connect_error($connessione));
	
	// Uscita:
	exit();
}

$query = "SELECT nome, cognome FROM Utenti WHERE codice='0d8dc'";
$persone = $connessione->query($query);
$persona = $persone->fetch_assoc();

echo json_encode($persona);

which does a simple query to a database with a "Utenti" table.

Hi - I know that I'm gravedigging this thread - but I have some kind of the same problem

My case:
15 Arduino UNO WiFi
15 DHT-22 humidity sensors
Need to read temp/humid from 15 different locations onsite, and upload them to an SQL database

My prototype:
1 Arduino Leonardo
1 Ethernet shield
1 DHT-22 sensor
Works perfectly uploading the data from the sensor to SQL (via a PHP-file)

My problem:
How do I adapt the code to work with the Arduino UNO WiFi? Again, there's no shield, it's the build in wifi-module

Any help would be nice :slight_smile:

//  Hartmann fugtighedsmåler v 0.2
//  Lavet af Jan Andreasen
//  Skriver til DB på WDKTO560
    
    #include <Ethernet.h>
    #include <SPI.h>
    #include <DHT.h>
    #define DHTPIN 2 // Siger sig selv
    #define DHTTYPE DHT22 // Typen af sensor. DHT11 og DHT22 kan bruges med dette DHT lib
 
    float h = 0;
    float t = 0;
    byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02}; // Macadresse på kortet
    IPAddress dnServer(10, 16, 2, 89);
    IPAddress gateway(10, 16, 10, 1);
    IPAddress subnet(255, 255, 255, 0);
    IPAddress ip(10, 16, 10, 63);
    IPAddress server(10,16,9,229); // Server adressen på SQL'en
 
    EthernetClient client;
    DHT dht(DHTPIN, DHTTYPE);
 
    void setup() {
      Serial.begin(9600);
      Serial.println("Starting...");
      Ethernet.begin(mac, ip, dnServer, gateway, subnet);
      Serial.print("IP = ");
      Serial.println(Ethernet.localIP());
      dht.begin();
    }
 
    void loop() {
      readTempHum();
      delay(300000); // Loop timer i millis - 5 minutter
    }
 
    void get_request(float t, float h)
      {
        Serial.println("Connecting to Client...");      
        if (client.connect(server, 10080)) {
          Serial.println("--> connection ok\n");
          client.print("GET /test.php?"); // Placering af PHP script til upload til DB
          client.print("t="); // Temp
          client.print(t); 
          client.print("&h="); // Fugtighed
          client.print(h);
          client.println(" HTTP/1.1"); 
          client.print( "Host: " );
          client.println(server);
          client.println("Connection: close"); 
          client.println(); 
          client.println(); 
          client.stop();    
          Serial.println("--> finished transmission\n");  
        }
        else {
          Serial.println("--> connection failed\n");
        }
      }
 
    void readTempHum() {
      h = dht.readHumidity();
      t = dht.readTemperature();
        {
        Serial.print("Humidity: ");
        Serial.print(h);
        Serial.print("%\t");
        Serial.print("Temperature:");
        Serial.print(t);
        Serial.println("*C");
        get_request(t,h);
        }
      }

the factory firmware of Uno WiFi and the corresponding UnoWiFiDevEd library doesn't support general networking with standard Arduino networking API like the Ethernet or WiFi library.

you can change the firmware of the Uno WiFi to WiFi link and use the WiFi Link library which is easy to port to from Ethernet library.

my Uno WiFi and WiFi Link doc