Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <dht11.h>
- #include <MySQL_Connection.h>
- #include <MySQL_Cursor.h>
- const char* ssid = "WLAN_Name"; // Enter SSID here
- const char* password = "WLAN_Password"; //Enter Password here
- IPAddress server_addr(192,168,178,27); // IP of the MySQL *server* here
- char* user = "DB_user"; // MySQL user login username
- char* dbPassword = "DB_Pass"; // MySQL user login password
- WiFiClient client; // Use this for WiFi instead of EthernetClient
- MySQL_Connection conn(&client);
- MySQL_Cursor* cursor;
- // Das Insert Statement
- char INSERT_SQL[] = "insert into home.feuchte(ip,sensor,temperatur,feuchte) values('%s', %d, %d,%d)";
- char query[128];
- char temperature[10];
- char feuchte[10];
- dht11 DHT11;
- void setup() {
- DHT11.attach(2);
- Serial.begin(115200);
- delay(10);
- // Begin WiFi section
- Serial.printf("\nConnecting to %s", ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- // print out info about the connection:
- Serial.println("\nConnected to network");
- Serial.print("My IP address is: ");
- Serial.println(WiFi.localIP());
- Serial.print("Connecting to SQL... ");
- if (conn.connect(server_addr, 3306, user, dbPassword))
- Serial.println("OK.");
- else
- Serial.println("FAILED.");
- // create MySQL cursor object
- cursor = new MySQL_Cursor(&conn);
- }
- void connectWiFI(){
- // Begin WiFi section
- Serial.printf("\nConnecting to %s", ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- // print out info about the connection:
- Serial.println("\nConnected to network");
- Serial.print("My IP address is: ");
- Serial.println(WiFi.localIP());
- }
- void connectMysql(){
- Serial.print("Connecting to SQL... ");
- if (conn.connect(server_addr, 3306, user, dbPassword))
- Serial.println("OK.");
- else
- Serial.println("FAILED.");
- // create MySQL cursor object
- cursor = new MySQL_Cursor(&conn);
- }
- void loop() {
- // Besteht eine WLAN Verbindung
- if (WiFi.status() != WL_CONNECTED){
- connectWiFI();
- } else{
- // Besteht eine Verbindung zur Datenbank
- if (! conn.connected()){
- connectMysql();
- }else{
- Serial.println("\n");
- // Sensor lesen
- int chk = DHT11.read();
- Serial.print("Read sensor: ");
- switch (chk)
- {
- case 0: Serial.println("OK"); break;
- case -1: Serial.println("Checksum error"); break;
- case -2: Serial.println("Time out error"); break;
- default: Serial.println("Unknown error"); break;
- }
- IPAddress ip = WiFi.localIP();
- char buf[16];
- sprintf(buf, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
- sprintf(query, INSERT_SQL, buf, 1, DHT11.temperature,DHT11.humidity);
- //sprintf(query, INSERT_SQL, "addr", 1, temperature,feuchte);
- //Serial.println(temperature);
- //Serial.println(feuchte);
- Serial.println(query);
- Serial.println("writing...");
- if (conn.connected()){
- cursor->execute(query);
- }
- }
- }
- delay(60000); //delay for reread
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement