Created
March 22, 2020 04:45
-
-
Save mpsparrow/a213629ac02fdb8089e4a0441e6d8d55 to your computer and use it in GitHub Desktop.
Revisions
-
Matthew created this gist
Mar 22, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,83 @@ /* Created by Matthew S on March 22, 2020 Simple script to log DHT11 sensor data into a mySQL database. This is done by directly connecting to the database. */ #include <ESP8266WiFi.h> #include <WiFiUdp.h> #include <WiFiClient.h> #include <MySQL_Connection.h> #include <MySQL_Cursor.h> #include "DHT.h" #define DHTPIN 2 #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE); // MySQL IPAddress server_addr(xx,xx,xx,xx); // MySQL SERVER char user[] = ""; // MySQL USERNAME char password[] = ""; // MySQL PASSWORD char INSERT_SQL[] = "INSERT INTO databaseName.tableName (temperature, humidity, heatIndex) VALUES (%s, %s, %s)"; char query[255]; // WiFi char ssid[] = ""; // SSID NAME char pass[] = ""; // SSID PASSWORD // Data char temp[10]; char hum[10]; char heat[10]; WiFiClient client; MySQL_Connection conn((Client *)&client); void setup() { Serial.begin(115200); // Seriel begin WiFi.begin(ssid, pass); // WiFi begin // WiFi connected while ( WiFi.status() != WL_CONNECTED ) { delay(500); Serial.print("."); } // Seriel output connection data Serial.println(""); Serial.print("Connected to: "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); Serial.println("DB - Connecting..."); // mySQL connection if (conn.connect(server_addr, 3306, user, password)) { delay(1000); dht.begin(); } else Serial.println("Connection failed."); } void loop() { MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn); // MySQL cursor // get dht11 temp, humidity, heat index float t = dht.readTemperature(); float h = dht.readHumidity(); float hic = dht.computeHeatIndex(t, h, false); // convert data to string dtostrf(t, 1, 1, temp); dtostrf(h, 1, 1, hum); dtostrf(hic, 1, 1, heat); // complete query and execute sprintf(query, INSERT_SQL, temp, hum, heat); cur_mem->execute(query); delete cur_mem; delay(60000); // delay between readings }