Honestly its hard to store Arduino data to MySQL directly so that in addiction to Arduino IDE i used Processing IDE that is similar to Arduino IDE but with lot of different usage and you can able to code it in java.
Note:don't run Arduino serial monitor while running processing code because port conflict will occur as both have to use the same port
You Need:
create table data(sno int(4) primary key auto_increment,LDR int(4),TEMP int(4));
use desc your_table_name to display table details
desc data;
That's all for DB now we can move to processing...
/*
ARDUINO TO MYSQL THROUGH PROCESSING Read Serial messages from Arduino then write it in MySQL. Author : J.V.JohnsonSelva September 2016 */import de.bezier.data.sql.*; //import the MySQL library import processing.serial.*; //import the Serial library
MySQL msql; //Create MySQL Object String[] a; int end = 10; // the number 10 is ASCII for linefeed (end of serial.println), later we will look for this to break up individual messages String serial; // declare a new string called 'serial' . A string is a sequence of characters (data type know as "char") Serial port; // The serial port, this is a new instance of the Serial class (an Object)
void setup() { String user = "root"; String pass = ""; String database = "iot_database"; msql = new MySQL( this, "localhost", database, user, pass ); port = new Serial(this, Serial.list()[0], 9600); // initializing the object by assigning a port and baud rate (must match that of Arduino) port.clear(); // function from serial library that throws out the first reading, in case we started reading in the middle of a string from Arduino serial = port.readStringUntil(end); // function that reads the string from serial port until a println and then assigns string to our string variable (called 'serial') serial = null; // initially, the string will be null (empty) }
void draw() { while (port.available() > 0) { //as long as there is data coming from serial port, read it and store it serial = port.readStringUntil(end); } if (serial != null) { //if the string is not empty, print the following //Note: the split function used below is not necessary if sending only a single variable. However, it is useful for parsing (separating) messages when //reading from multiple inputs in Arduino. Below is example code for an Arduino sketch a = split(serial, ','); //a new array (called 'a') that stores values into separate cells (separated by commas specified in your Arduino program) println(a[0]); //print LDR value println(a[1]); //print LM35 value function(); } }
void function() { if ( msql.connect() ) { msql.query( "insert into data(LDR,Temp)values("+a[0]+","+a[1]+")" ); } else { // connection failed ! } msql.close(); //Must close MySQL connection after Execution }
I would like to thank you for reading my tutorial. I would appreciate if you found it useful and drop a like (favorite) or ask me anything as it keeps me motivated to do these instructables. feel free to ask any questions that you need to know...
Happy Coding Arduino...