| #include "DHT.h" |
| #include <ESP8266WiFi.h> |
| #include <WiFiClientSecure.h> |
| #include <ESP8266HTTPClient.h> |
|
|
| #define DHTPIN 5 |
| #define DHTTYPE DHT11 |
|
|
| |
| const char* ssid = "WIFI_SSID"; |
| const char* password = "WIFI_PASSWORD"; |
|
|
| |
| String API_URL = "API_URL"; |
| String API_KEY = "API_KEY"; |
| String TableName = "maintable"; |
| const int httpsPort = 443; |
|
|
| |
| int sendinginterval = 1200; |
| |
|
|
| HTTPClient https; |
| WiFiClientSecure client; |
| DHT dht(DHTPIN, DHTTYPE); |
|
|
| float h; |
| float t; |
| int m; |
|
|
| void setup() { |
| |
| pinMode(LED_BUILTIN, OUTPUT); |
| digitalWrite(LED_BUILTIN, HIGH); |
|
|
| |
| client.setInsecure(); |
|
|
| |
| Serial.begin(115200); |
| dht.begin(); |
| |
| Serial.print("Connecting to "); |
| Serial.println(ssid); |
| WiFi.begin(ssid, password); |
| while (WiFi.status() != WL_CONNECTED) { |
| delay(500); |
| Serial.print("."); |
| } |
| |
| |
| Serial.println(""); |
| Serial.println("WiFi connected."); |
| Serial.println("IP address: "); |
| Serial.println(WiFi.localIP()); |
| } |
|
|
| void loop() { |
|
|
| |
| if (WiFi.status() == WL_CONNECTED) { |
| digitalWrite(LED_BUILTIN, LOW); |
|
|
| |
| h = dht.readHumidity(); |
| t = dht.readTemperature(); |
| m = analogRead(A0); |
|
|
| |
| https.begin(client,API_URL+"/rest/v1/"+TableName); |
| https.addHeader("Content-Type", "application/json"); |
| https.addHeader("Prefer", "return=representation"); |
| https.addHeader("apikey", API_KEY); |
| https.addHeader("Authorization", "Bearer " + API_KEY); |
| int httpCode = https.POST("{\"temperature\":" + String(t)+ ",\"humidity\":"+ String(h)+",\"moisture\":" + String(1024 - m)+"}" ); |
| String payload = https.getString(); |
| Serial.println(httpCode); |
| Serial.println(payload); |
| https.end(); |
|
|
| |
| digitalWrite(LED_BUILTIN, HIGH); |
| }else{ |
| Serial.println("Error in WiFi connection"); |
| } |
| delay(1000*sendinginterval); |
| |
| } |