Data Logging with PHP + MySQL + ESP8266
This is simple alternative for Blynk, Cayenne, Thingspeak etc. This is not MQTT project. Works with PHP GET method to get and send data.
Components and supplies
1
DHT11 Temperature & Humidity Sensor (3 pins)
1
NodeMCU ESP8266 Breakout Board
1
ESP8266 ESP-01
1
Adafruit Waterproof DS18B20 Digital temperature sensor
Apps and platforms
1
Arduino IDE
Project description
Code
ESP8266 Code
arduino
You have to edit your wifi information and php file link
1#include <ESP8266WiFi.h> 2#include <ESP8266WebServer.h> 3#include "DHT.h" 4 5#include <OneWire.h> 6#include <DallasTemperature.h> 7#define ONE_WIRE_BUS 2 8OneWire oneWire(ONE_WIRE_BUS); 9DallasTemperature sensors(&oneWire); 10 11#define DHTTYPE DHT11 // DHT 11 12 13// DHT Sensor 14uint8_t DHTPin = 14; 15 16// Initialize DHT sensor. 17DHT dht(DHTPin, DHTTYPE); 18 19float Temperature; 20float Humidity; 21float Probe; 22int Charge; 23int delayTime; 24String device = "1"; 25 26#include <Arduino.h> 27#include <ESP8266WiFi.h> 28#include <ESP8266WiFiMulti.h> 29 30#include <ESP8266HTTPClient.h> 31#include <WiFiClientSecureBearSSL.h> 32// Fingerprint for demo URL, expires on June 2, 2019, needs to be updated well before this date 33// const uint8_t fingerprint[20] = {0x5A, 0xCF, 0xFE, 0xF0, 0xF1, 0xA6, 0xF4, 0x5F, 0xD2, 0x11, 0x11, 0xC6, 0x1D, 0x2F, 0x0E, 0xBC, 0x39, 0x8D, 0x50, 0xE0}; 34 35ESP8266WiFiMulti WiFiMulti; 36String slp, st1, st2, st3, st4; 37 38void setup() { 39 40 pinMode(DHTPin, INPUT); 41 42 dht.begin(); 43 sensors.begin(); 44 45 Serial.begin(115200); 46 Serial.println(); 47 Serial.println(); 48 Serial.println(); 49 50 for (uint8_t t = 4; t > 0; t--) { 51 Serial.printf("[SETUP] WAIT %d...\ 52", t); 53 Serial.flush(); 54 delay(1000); 55 } 56 57 WiFi.mode(WIFI_STA); 58 WiFiMulti.addAP("*****", "*******"); 59 WiFi.setAutoReconnect(true); 60 WiFi.persistent(true); 61} 62 63void loop() { 64 // wait for WiFi connection 65 if ((WiFiMulti.run() == WL_CONNECTED)) { 66 67 std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure); 68 69 //client->setFingerprint(fingerprint); 70 client->setInsecure(); 71 72 HTTPClient https; 73 74 Temperature = dht.readTemperature(); // Gets the values of the temperature 75 Humidity = dht.readHumidity(); // Gets the values of the humidity 76 77 sensors.requestTemperatures(); 78 Probe = sensors.getTempCByIndex(0); 79 80 Charge = analogRead(A0); 81 Charge = map(Charge, 400, 830, 0, 100); 82 83 String TempVal, HumVal, ProbVal, getData, Link, ChargeVal; 84 TempVal = String(Temperature); //String to interger conversion 85 HumVal = String(Humidity); 86 ProbVal = String(Probe); 87 ChargeVal = String(Charge); 88 89 //GET Data 90 getData = "?temp=" + TempVal + "&hum=" + HumVal + "&probe=" + ProbVal + "&charge=" + ChargeVal + "&device=" + device; //Note "?" added at front 91 Link = "https://yilmazyurdakul.com/projects/esp/espget.php" + getData; 92 93 Serial.print("[HTTPS] begin...\ 94"); 95 if (https.begin(*client, Link)) { // HTTPS 96 97 Serial.print("[HTTPS] GET...\ 98"); 99 // start connection and send HTTP header 100 int httpCode = https.GET(); 101 102 // httpCode will be negative on error 103 if (httpCode > 0) { 104 // HTTP header has been send and Server response header has been handled 105 Serial.printf("[HTTPS] GET... code: %d\ 106", httpCode); 107 Serial.println(Link); 108 // file found at server 109 if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) { 110 String payload = https.getString(); 111 112 slp = payload.substring(0, 1); 113 st1 = payload.substring(1, 2); 114 st2 = payload.substring(2, 3); 115 st3 = payload.substring(3, 4); 116 st4 = payload.substring(4); 117 delayTime = (st4.toInt() * 60000); 118 119 Serial.println(slp); 120 Serial.println(st1); 121 Serial.println(st2); 122 Serial.println(st3); 123 Serial.println(st4); 124 125 if (st1 == "1") { 126 //Do something... 127 } else { 128 // 129 } 130 131 } 132 if (slp == "1") { 133 Serial.println("deep sleep"); 134 ESP.deepSleep(delayTime*1000); 135 } 136 } else { 137 Serial.printf("[HTTPS] GET... failed, error: %s\ 138", https.errorToString(httpCode).c_str()); 139 } 140 141 https.end(); 142 } else { 143 Serial.printf("[HTTPS] Unable to connect\ 144"); 145 } 146 } 147 148 Serial.println("Wait before next round..."); 149 Serial.println(delayTime); 150 delay(delayTime); 151}
Comments
Only logged in users can leave comments