Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <WiFiClient.h>
- #include <MySQL_Connection.h>
- #include <MySQL_Cursor.h>
- //byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
- IPAddress server_addr(192,168,43,132); // IP of the MySQL *server* here
- char user[] = "sensor_node_user"; // MySQL user login username
- char password[] = "admin1234"; // MySQL user login password
- // Wi-Fi Settings
- const char* ssid = "CEEDTEDPWIFI"; // your wireless network name (SSID)
- const char* pass = "tedpiniot"; // your Wi-Fi network password
- // Sample query
- //char query[] = "SELECT temperature FROM new_sample.tbl_datas WHERE id = 1";
- //char query[] = "SELECT * FROM new_sample.tbl_datas";
- char INSERT_DATA[] = "INSERT INTO new_sample.tbl_datas (temperature, humidity) VALUES ('%s','%s')";
- char query[128];
- char temperature[10];
- char humidity[10];
- //EthernetClient client;
- WiFiClient client;
- MySQL_Connection conn((Client *)&client);
- // Create an instance of the cursor passing in the connection
- //MySQL_Cursor cur = MySQL_Cursor(&conn);
- void setup() {
- Serial.begin(115200);
- Serial.print ("Connecting to..");
- //while (!Serial); // wait for serial port to connect
- // Ethernet.begin(mac_addr);
- WiFi.begin(ssid, pass); // initializing the WIFI library
- while ( WiFi.status() != WL_CONNECTED ) { // while loop to write dots during connecting
- delay (500);
- Serial.print (".");
- }
- // print out information about the WIFI connection
- Serial.println ("");
- Serial.print ("Connected to ");
- Serial.println (ssid);
- Serial.print ("IP address: ");
- Serial.println (WiFi.localIP());
- // connecting to the MySQL server
- Serial.println("Connecting to DB...");
- /*
- if (conn.connect(server_addr, 3306, user, password)) {
- delay(1000);
- }
- else
- Serial.println("Connection failed.");
- */
- while (conn.connect(server_addr, 3306, user, password) != true) {
- delay(500);
- Serial.print ( "." );
- }
- }
- void loop() {
- delay(5000);
- Serial.println("Preparing Sensor Data...");
- // Initiate the query class instance
- MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
- //Double to String
- dtostrf(50.125, 3, 3, temperature);
- dtostrf(32.115, 3, 3, humidity);
- sprintf(query, INSERT_DATA, temperature, humidity);
- // 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("Data recorded.");
- delay(500);
- }
Add Comment
Please, Sign In to add comment