Advertisement
TomParts

Lesen eines DHT11 und schreiben in eine MySQL DB

Mar 6th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.17 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <dht11.h>
  3. #include <MySQL_Connection.h>
  4. #include <MySQL_Cursor.h>
  5.  
  6. const char* ssid = "WLAN_Name";  // Enter SSID here
  7. const char* password = "WLAN_Password";  //Enter Password here
  8.  
  9. IPAddress server_addr(192,168,178,27);  // IP of the MySQL *server* here
  10. char* user = "DB_user";              // MySQL user login username
  11. char* dbPassword = "DB_Pass";        // MySQL user login password
  12.  
  13. WiFiClient client;                 // Use this for WiFi instead of EthernetClient
  14. MySQL_Connection conn(&client);
  15. MySQL_Cursor* cursor;
  16.  
  17. // Das Insert Statement
  18. char INSERT_SQL[] = "insert into home.feuchte(ip,sensor,temperatur,feuchte) values('%s', %d, %d,%d)";
  19. char query[128];
  20. char temperature[10];
  21. char feuchte[10];
  22.  
  23. dht11 DHT11;
  24.  
  25. void setup() {
  26.     DHT11.attach(2);
  27.     Serial.begin(115200);
  28.     delay(10);
  29.    
  30.     // Begin WiFi section
  31.       Serial.printf("\nConnecting to %s", ssid);
  32.       WiFi.begin(ssid, password);
  33.       while (WiFi.status() != WL_CONNECTED) {
  34.         delay(500);
  35.         Serial.print(".");
  36.       }
  37.  
  38.       // print out info about the connection:
  39.       Serial.println("\nConnected to network");
  40.       Serial.print("My IP address is: ");
  41.       Serial.println(WiFi.localIP());
  42.  
  43.       Serial.print("Connecting to SQL...  ");
  44.         if (conn.connect(server_addr, 3306, user, dbPassword))
  45.           Serial.println("OK.");
  46.         else
  47.           Serial.println("FAILED.");
  48.  
  49.         // create MySQL cursor object
  50.         cursor = new MySQL_Cursor(&conn);
  51. }
  52.  
  53.  
  54. void connectWiFI(){
  55.     // Begin WiFi section
  56.           Serial.printf("\nConnecting to %s", ssid);
  57.           WiFi.begin(ssid, password);
  58.           while (WiFi.status() != WL_CONNECTED) {
  59.             delay(500);
  60.             Serial.print(".");
  61.           }
  62.  
  63.           // print out info about the connection:
  64.           Serial.println("\nConnected to network");
  65.           Serial.print("My IP address is: ");
  66.           Serial.println(WiFi.localIP());
  67. }
  68.  
  69.  
  70. void connectMysql(){
  71.      Serial.print("Connecting to SQL...  ");
  72.             if (conn.connect(server_addr, 3306, user, dbPassword))
  73.               Serial.println("OK.");
  74.             else
  75.               Serial.println("FAILED.");
  76.  
  77.             // create MySQL cursor object
  78.             cursor = new MySQL_Cursor(&conn);
  79. }
  80.  
  81.  
  82. void loop() {
  83. // Besteht eine WLAN Verbindung
  84.     if (WiFi.status() != WL_CONNECTED){
  85.         connectWiFI();
  86.     } else{
  87. // Besteht eine Verbindung zur Datenbank
  88.         if (! conn.connected()){
  89.             connectMysql();
  90.         }else{
  91.           Serial.println("\n");
  92. // Sensor lesen
  93.           int chk = DHT11.read();
  94.           Serial.print("Read sensor: ");
  95.           switch (chk)
  96.           {
  97.             case 0: Serial.println("OK"); break;
  98.             case -1: Serial.println("Checksum error"); break;
  99.             case -2: Serial.println("Time out error"); break;
  100.             default: Serial.println("Unknown error"); break;
  101.           }
  102.          
  103.           IPAddress ip = WiFi.localIP();
  104.           char buf[16];
  105.           sprintf(buf, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
  106.  
  107.           sprintf(query, INSERT_SQL, buf, 1, DHT11.temperature,DHT11.humidity);
  108.  
  109.           //sprintf(query, INSERT_SQL, "addr", 1, temperature,feuchte);
  110.  
  111.           //Serial.println(temperature);
  112.           //Serial.println(feuchte);
  113.           Serial.println(query);
  114.           Serial.println("writing...");
  115.           if (conn.connected()){
  116.               cursor->execute(query);
  117.           }
  118.         }
  119.     }
  120. delay(60000); //delay for reread
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement