Guest User

Untitled

a guest
Feb 20th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <MySQL_Connection.h>
  4. #include <MySQL_Cursor.h>
  5.  
  6. //byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  7.  
  8. IPAddress server_addr(192,168,43,132); // IP of the MySQL *server* here
  9. char user[] = "sensor_node_user"; // MySQL user login username
  10. char password[] = "admin1234"; // MySQL user login password
  11.  
  12. // Wi-Fi Settings
  13. const char* ssid = "CEEDTEDPWIFI"; // your wireless network name (SSID)
  14. const char* pass = "tedpiniot"; // your Wi-Fi network password
  15.  
  16. // Sample query
  17. //char query[] = "SELECT temperature FROM new_sample.tbl_datas WHERE id = 1";
  18. //char query[] = "SELECT * FROM new_sample.tbl_datas";
  19.  
  20. char INSERT_DATA[] = "INSERT INTO new_sample.tbl_datas (temperature, humidity) VALUES ('%s','%s')";
  21. char query[128];
  22. char temperature[10];
  23. char humidity[10];
  24.  
  25. //EthernetClient client;
  26. WiFiClient client;
  27. MySQL_Connection conn((Client *)&client);
  28. // Create an instance of the cursor passing in the connection
  29. //MySQL_Cursor cur = MySQL_Cursor(&conn);
  30.  
  31. void setup() {
  32. Serial.begin(115200);
  33. Serial.print ("Connecting to..");
  34. //while (!Serial); // wait for serial port to connect
  35.  
  36. // Ethernet.begin(mac_addr);
  37. WiFi.begin(ssid, pass); // initializing the WIFI library
  38. while ( WiFi.status() != WL_CONNECTED ) { // while loop to write dots during connecting
  39. delay (500);
  40. Serial.print (".");
  41. }
  42.  
  43. // print out information about the WIFI connection
  44. Serial.println ("");
  45. Serial.print ("Connected to ");
  46. Serial.println (ssid);
  47. Serial.print ("IP address: ");
  48. Serial.println (WiFi.localIP());
  49.  
  50. // connecting to the MySQL server
  51. Serial.println("Connecting to DB...");
  52. /*
  53. if (conn.connect(server_addr, 3306, user, password)) {
  54. delay(1000);
  55. }
  56. else
  57. Serial.println("Connection failed.");
  58. */
  59. while (conn.connect(server_addr, 3306, user, password) != true) {
  60. delay(500);
  61. Serial.print ( "." );
  62. }
  63. }
  64.  
  65. void loop() {
  66. delay(5000);
  67.  
  68. Serial.println("Preparing Sensor Data...");
  69.  
  70. // Initiate the query class instance
  71. MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
  72.  
  73. //Double to String
  74. dtostrf(50.125, 3, 3, temperature);
  75. dtostrf(32.115, 3, 3, humidity);
  76. sprintf(query, INSERT_DATA, temperature, humidity);
  77.  
  78. // Execute the query
  79. cur_mem->execute(query);
  80.  
  81. // Note: since there are no results, we do not need to read any data
  82. // Deleting the cursor also frees up memory used
  83. delete cur_mem;
  84. Serial.println("Data recorded.");
  85.  
  86.  
  87. delay(500);
  88.  
  89. }
Add Comment
Please, Sign In to add comment