ChuckBell / MySQL_Connector_Arduino

Database connector library for using MySQL with your Arduino projects.
332 stars 133 forks source link

How can I set my mysql variables sensorValue and voltage inside the code? #193

Open ilsochrist opened 2 years ago

ilsochrist commented 2 years ago

include // Use this for WiFi instead of Ethernet.h

include

include

define sensorPin A0

byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress server_addr(10,0,1,35); // IP of the MySQL server here char user[] = "XXXXXX"; // MySQL user login username char password[] = "XXXXX"; // MySQL user login password

// WiFi card example char ssid[] = "XXXXX"; // your SSID char pass[] = "XXXXXXX"; // your SSID Password

int sensorValue = 0; float voltage;

WiFiClient client; // Use this for WiFi instead of EthernetClient MySQL_Connection conn((Client *)&client);

void setup() { Serial.begin(115200); while (!Serial); // wait for serial port to connect. Needed for Leonardo only

// Begin WiFi section int status = WiFi.begin(ssid, pass); if ( status != WL_CONNECTED) { Serial.println("Couldn't get a wifi connection"); while(true); } // print out info about the connection: else { Serial.println("Connected to network"); IPAddress ip = WiFi.localIP(); Serial.print("My IP address is: "); Serial.println(ip); } // End WiFi section

Serial.println("Connecting..."); if (conn.connect(server_addr, 3306, user, password)) { delay(1000); } else Serial.println("Connection failed."); conn.close(); }

void loop() { sensorValue = analogRead(sensorPin); voltage = sensorValue * (5.0 / 1024); }

ilsochrist commented 2 years ago

image

That is my variables in my db. i want to pass the reading values to my mysql db.

ilsochrist commented 2 years ago

include // Use this for WiFi instead of Ethernet.h

include

include

define sensorPin A0

byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress server_addr(54, 39, 75, 7); // IP of the MySQL server here char user[] = "XXXX"; // MySQL user login username char password[] = "XXXXXX"; // MySQL user login password

// WiFi card example char ssid[] = "XXXXXX"; // your SSID char pass[] = "CCCCXXXX1"; // your SSID Password char INSERT_DATA[] = "INSERT INTO >blnrpjswshaaortwjhlh<.Temperatura (sensorValue, voltage) VALUES (%d,%s)";

WiFiClient client; // Use this for WiFi instead of EthernetClient MySQL_Connection conn((Client *)&client);

void setup() { Serial.begin(115200); while (!Serial); // wait for serial port to connect. Needed for Leonardo only

// Begin WiFi section int status = WiFi.begin(ssid, pass); if ( status != WL_CONNECTED) { Serial.println("Couldn't get a wifi connection"); while(true); } // print out info about the connection: else { Serial.println("Connected to network"); IPAddress ip = WiFi.localIP(); Serial.print("My IP address is: "); Serial.println(ip); } // End WiFi section

Serial.println("Connecting..."); if (conn.connect(server_addr, 3306, user, password)) { delay(1000); } else Serial.println("Connection failed."); conn.close(); }

void loop() {

int sensorValue = 0; float voltage;

AguardaDados(); Leitura(&sensorValue, &voltage);

EnviaDados(sensorValue, voltage); Serial.println(); }

void AguardaDados() { while (!(Serial.available() > 0)) {} }

void Leitura(int sensorValue, float voltage) { char mensagem[20]; byte atual, i = 0; atual = 255; if (Serial.available() > 0) { while (atual != 10) { if (Serial.available() > 0) { atual = Serial.read(); // Serial.print((char)leitura); mensagem[i] = (char)atual; i++; } } i = 0; sensorValue = atoi(strtok(mensagem, "|")); voltage = atof(strtok(NULL, "|")); } } void EnviaDados(int sensorValue, float voltage) { char query[128]; char temperatura[10]; if (conn.connect(server_addr, 3306, user, password)) { delay(1000); MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn); // Save dtostrf(voltage, 1, 1, temperatura); sprintf(query, INSERT_DATA, sensorValue, temperatura); // Execute the query cur_mem->execute(query); // 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; Serial.println(); Serial.println("Data recorded."); // } else Serial.println(); Serial.println("Connection failed."); // conn.close(); }

Chopan25 commented 1 year ago

Hi, what is the problem with your code? This should be reading values from the serial port and inserting them into a database. Note: Remember to put code formatted as code so it is easier to read.