Sending data to Microsoft Sql Server

Hello everyone,

For my school project i have to connect my arduino with a microsoft Sql database (I don't run this database local). I'm using a esp8266 wifi module, and can already connect to the wifi network. The question is now how to connect it to the sql server.

This here is the old code that i used to connect to a local Xampp server and sended data with. This all worked, I also connected from Inteliji Idea to the sql database but there I used a Connection string. But couldn't find anything like that for arduino.

the arduino code keeps saying: Error: Class requires connected server.
or:
Connecting to SQL... ...trying...
...got: 0 retrying...
...trying...
...got: 0 retrying...
...trying...
...got: 0 retrying...
FAILED.

Can't add file yet, so here is the code:

/*
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
with the same classes and methods.

For more information and documentation, visit the wiki:
Home · ChuckBell/MySQL_Connector_Arduino Wiki · GitHub.

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 <ESP8266WiFi.h> // Use this for WiFi instead of Ethernet.h
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>

// for ESP8266 microcontroller
int startTimer = D1;
int buttonstate = 0;
int oldmillis = 0;
int timepassed = 0;
unsigned long chrono = 0;
int seconds = 0;
int minutes = 0;

IPAddress server_addr(//); // IP of the MySQL server here
char user[] = "svc_stagebalgenklem"; // MySQL user login username
char password[] = "//"; // MySQL user login password

// Sample query
//char INSERT_SQL[] = "INSERT INTO downtime (downtimebalgenpers) VALUES ('1');";
//char SQL_INSERT2[] = "INSERT INTO SensorData(db_rotaties, ``) VALUES ([]);";
// WiFi card example
char ssid[] = "VBintern"; // your SSID
char pass[] = "//"; // your SSID Password
WiFiClient client; // Use this for WiFi instead of EthernetClient
MySQL_Connection conn(&client);
MySQL_Cursor* cursor;

void setup()
{
pinMode (startTimer, INPUT);

chrono = millis();
Serial.begin(9600);
while (!Serial); // wait for serial port to connect. Needed for Leonardo only

// Begin WiFi section
Serial.printf("\nConnecting to %s", ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

// 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);
}

void loop() {
int val = digitalRead(D1);
if ((val == 1) && millis() - chrono >= 1000) {
chrono = millis();
result();
}
//else {
//Serial.println(" signaal niet actief");
//}

}
void result() {
seconds++;
// if (seconds == 60) {
// seconds = 0;
// minutes ++;
// }
// Serial.print (minutes);
// Serial.print (" M ");
Serial.print (seconds);
Serial.println(" Seconden");
delay (1000);
versturen();
}

void versturen() {
int laatstewaarde = digitalRead(seconds);
int val = digitalRead(D1);
if (val == 0) {
String sql = "INSERT INTO foutenregistratiebalgenpers.downtime(tijd) VALUES (" + String(seconds) + ")";
char INSERT_SQL[254];
sql.toCharArray(INSERT_SQL, 254);
if (conn.connected())
cursor->execute(INSERT_SQL);
Serial.println("Waarde verstuurd");
delay(200);
seconds = 0;
minutes = 0;
Serial.println("Tijd gereset");
}

}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.