From 8777536cc01ec0fef223a5e6bd1029142478b080 Mon Sep 17 00:00:00 2001 From: Adrian Terranova Date: Wed, 12 Aug 2020 22:41:42 -0500 Subject: [PATCH 1/8] Update HTTPSRedirect.cpp Created initial method for custom header injection - this will take one for now. --- HTTPSRedirect/HTTPSRedirect.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/HTTPSRedirect/HTTPSRedirect.cpp b/HTTPSRedirect/HTTPSRedirect.cpp index fae281c..796e850 100644 --- a/HTTPSRedirect/HTTPSRedirect.cpp +++ b/HTTPSRedirect/HTTPSRedirect.cpp @@ -155,6 +155,25 @@ void HTTPSRedirect::createPostRequest(const String& url, const char* host, const return; } +void HTTPSRedirect::createPostRequestcustomHeader(const String& url, const char* host, const String& customHeader, const String& payload){ + // Content-Length is mandatory in POST requests + // Body content will include payload and a newline character + unsigned int len = payload.length() + 1; + + _Request = String("POST ") + url + " HTTP/1.1\r\n" + + "Host: " + host + "\r\n" + + "User-Agent: ESP8266\r\n" + + customHeader + "\r\n" + + (_keepAlive ? "" : "Connection: close\r\n") + + "Content-Type: " + _contentTypeHeader + "\r\n" + + "Content-Length: " + len + "\r\n" + + "\r\n" + + payload + + "\r\n\r\n"; + + return; +} + bool HTTPSRedirect::getLocationURL(void){ From 66acaa72eacf2094ca141411f7ec2bf7d325875f Mon Sep 17 00:00:00 2001 From: Adrian Terranova Date: Wed, 12 Aug 2020 22:46:19 -0500 Subject: [PATCH 2/8] Update HTTPSRedirect.h added prototype for customheader method .. need to pull this in and test with tago.io connection --- HTTPSRedirect/HTTPSRedirect.h | 1 + 1 file changed, 1 insertion(+) diff --git a/HTTPSRedirect/HTTPSRedirect.h b/HTTPSRedirect/HTTPSRedirect.h index 2f24b3f..16b909b 100644 --- a/HTTPSRedirect/HTTPSRedirect.h +++ b/HTTPSRedirect/HTTPSRedirect.h @@ -54,6 +54,7 @@ class HTTPSRedirect : public WiFiClientSecure { void InitResponse(void); void createGetRequest(const String&, const char*); void createPostRequest(const String&, const char*, const String&); + void createPostRequestcustomHeader(const String&, const char*, const String& , const String&); #ifdef EXTRA_FNS void fetchBodyRaw(void); From 905fb0d571951f24d849155fffe78080e820cfe0 Mon Sep 17 00:00:00 2001 From: Adrian Terranova Date: Thu, 13 Aug 2020 20:17:32 -0500 Subject: [PATCH 3/8] Updated to include the custom headers required for tago.io It's a kludge for the esp, but it works. --- HTTPSRedirect/HTTPSRedirect.cpp | 39 +++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/HTTPSRedirect/HTTPSRedirect.cpp b/HTTPSRedirect/HTTPSRedirect.cpp index 796e850..da5e6ee 100644 --- a/HTTPSRedirect/HTTPSRedirect.cpp +++ b/HTTPSRedirect/HTTPSRedirect.cpp @@ -163,9 +163,9 @@ void HTTPSRedirect::createPostRequestcustomHeader(const String& url, const char* _Request = String("POST ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "User-Agent: ESP8266\r\n" + - customHeader + "\r\n" + (_keepAlive ? "" : "Connection: close\r\n") + "Content-Type: " + _contentTypeHeader + "\r\n" + + customHeader + "\r\n" + "Content-Length: " + len + "\r\n" + "\r\n" + payload + @@ -396,9 +396,12 @@ bool HTTPSRedirect::POST(const String& url, const char* host, const String& payl return POST(url, host, payload, _printResponseBody); } -bool HTTPSRedirect::POST(const String& url, const char* host, const String& payload, const bool& disp){ + + +bool HTTPSRedirect::POST(const String& url, const char* host, const String& payload, const bool& disp){ bool retval; bool oldval; + // set _printResponseBody temporarily to argument passed oldval = _printResponseBody; @@ -409,12 +412,41 @@ bool HTTPSRedirect::POST(const String& url, const char* host, const String& payl // which did not have redirection _redirHost = host; _redirUrl = url; - InitResponse(); // Create request packet + //createPostRequest(url, host, payload); createPostRequest(url, host, payload); + // Call request handler + retval = printRedir(); + + _printResponseBody = oldval; + return retval; +} + +bool HTTPSRedirect::POSTcustomHeader(const String& url, const char* host, const String& customHeader, const String& payload){ + return POSTcustomHeader(url, host, customHeader, payload, _printResponseBody); +} + +bool HTTPSRedirect::POSTcustomHeader(const String& url, const char* host, const String& customHeader, const String& payload, const bool& disp){ + bool retval; + bool oldval; + + // set _printResponseBody temporarily to argument passed + oldval = _printResponseBody; + _printResponseBody = disp; + + // redirected Host and Url need to be initialized in case a + // reConnectFinalEndpoint() request is made after an initial request + // which did not have redirection + _redirHost = host; + _redirUrl = url; + InitResponse(); + + // Create request packet + //createPostRequest(url, host, payload); + createPostRequestcustomHeader(url, host, customHeader, payload); // Call request handler retval = printRedir(); @@ -503,4 +535,3 @@ void HTTPSRedirect::printHeaderFields(void){ DPRINTLN(_hF.contentType); } #endif - From 73d36032c928ebf8ff9e33e8ec678e0593da9504 Mon Sep 17 00:00:00 2001 From: Adrian Terranova Date: Thu, 13 Aug 2020 20:18:41 -0500 Subject: [PATCH 4/8] updated methods in support of tago.io Custom header Required to support the API key for u/l. From 3a2bebd0adb137bcbc10117528c01bc24fdb3273 Mon Sep 17 00:00:00 2001 From: Adrian Terranova Date: Sun, 31 Jan 2021 10:35:35 -0600 Subject: [PATCH 5/8] Update README.md --- README.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5638081..08107ed 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,12 @@ # ESP8266 -Stuff I do with this tiny wireless wonder +@peril99 added a post method to this fork for usa in the arduino projects which need an https / post method. My use case was IOT projects based on arduino clones like esp8266 / esp32. These are the key sensors / affectors which sit outside and controls pumps teeny pumps (affectors) and ph sensors (atlas ezo). + +These have all been tied together in a rudimentary dashboard for visualization @ https://admin.tago.io/public/dashboard/5f35d9868411cc00274b6a8a/e155a708-01a8-4fbc-8227-74e2b4e79855 + + +to use this library with the arduino ide + +1) clone a copy of this repo in a spot where you arduino ide can reference the libaries folder for me that was C:\Users\xxx\Documents\Arduino\libraries\HTTPSRedirect +2) include this library in the project you are including with a "#include " + From 8784e882c791ffb46f6d27095ddb58b76a069064 Mon Sep 17 00:00:00 2001 From: Adrian Terranova Date: Fri, 7 Jan 2022 11:47:09 -0600 Subject: [PATCH 6/8] Add files via upload Upload example for bob --- .../1710MonacoDHTWifiGdriveWriterCleaned.ino | 216 ++++++++++++++++++ 1 file changed, 216 insertions(+) create mode 100644 1710MonacoDHTWifiGdriveWriterCleaned/1710MonacoDHTWifiGdriveWriterCleaned.ino diff --git a/1710MonacoDHTWifiGdriveWriterCleaned/1710MonacoDHTWifiGdriveWriterCleaned.ino b/1710MonacoDHTWifiGdriveWriterCleaned/1710MonacoDHTWifiGdriveWriterCleaned.ino new file mode 100644 index 0000000..b538674 --- /dev/null +++ b/1710MonacoDHTWifiGdriveWriterCleaned/1710MonacoDHTWifiGdriveWriterCleaned.ino @@ -0,0 +1,216 @@ + +/** + * Example for reading temperature and humidity + * using the DHT22 and ESP8266 + * + * Copyright (c) 2016 Losant IoT. All rights reserved. + * https://www.losant.com + */ + +#define DHTPIN 4 +// what digital pin the DHT22 is conected to +#define DHTTYPE DHT22 // there are multiple kinds of DHT sensors +//#define DEBUG + +#include "DHT.h" +#include +#include +#include +#include + +const char* mqttServer = "192.168.20.18"; +const int mqttPort = 1883; +const char* mqttUser = "XXX"; +const char* mqttPassword = "XXX"; + +// your wifi ap info +const char* ssid = "XXX"; +const char* password = "XXX"; + +//your gsscript sheet id + +const char *GScriptId = "AKfycbzE7WE04utWYeHtCVf2PyQpZnosTiAyIvk3_3UleDMsEnNaoXZn"; +const char* host = "script.google.com"; + +const int dataPostDelay = 900000; // 15 minutes = 15 * 60 * 1000 +const int httpsPort = 443; + +String url = String("/macros/s/") + GScriptId + "/exec?"; + + +HTTPSRedirect client(httpsPort); +PubSubClient mqclient; + +DHT dht(DHTPIN, DHTTYPE); + +void setup() { + Serial.begin(9600); + Serial.setTimeout(2000); + // Wait for serial to initialize. + while(!Serial) { } + + Serial.println("setup: Device Started"); + Serial.println("setup: -------------------------------------"); + Serial.println("setup: Running DHT!"); + Serial.println("setup: -------------------------------------"); + Serial.println(ssid); + Serial.flush(); + dht.begin(); + WiFi.mode(WIFI_STA); + WiFi.begin(ssid, password); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println("setup:IP address: "); + Serial.println(WiFi.localIP()); + + //Insecure for TLS cert checking + client.setInsecure(); + client.setContentTypeHeader("application/json"); + + Serial.print("setup: Connecting to "); + Serial.println(host); + + bool flag = false; + + for (int i=0; i<5; i++){ + int retval = client.connect(host, httpsPort); + if (retval == 1) { + flag = true; + break; + } + else + Serial.println("setup: Initial https Connection failed. Retrying..."); + } + + if (!flag){ + Serial.print("setup: Could not connect to server: "); + Serial.println(host); + Serial.println("setup: Exiting..."); + return; + } + Serial.println("setup: Before MQTT"); + mqclient.setServer(mqttServer, mqttPort); + mqclient.setCallback(callback); + while (!mqclient.connected()) { + Serial.println("setup: Connecting to MQTT..."); + + if (mqclient.connect("ESP8266Client", mqttUser, mqttPassword )) { + + Serial.println("connected"); + + } else { + + Serial.print("failed with state "); + Serial.print(mqclient.state()); + delay(2000); + + } + } + + mqclient.publish("esp/test", "hello"); //Topic name + mqclient.subscribe("esp/test"); +} +void postData(String tag, float value){ + // this method is cruft and exist in case there is a desire for a Post payload, it was cloned from the Get fucntion, and never taken further + if (!client.connected()){ + Serial.println("postData: Connecting to client again…"); + client.connect(host, httpsPort); + } + String payload = "tag=" + tag + "&value=" + String(value); + Serial.print("url"); + //Serial.print(url); + Serial.print(" host "); + //Serial.print(host); + Serial.print("payload "); + //Serial.print(payload); + client.POST(url, host, payload); + Serial.print("getStatusCode: "); + Serial.print(client.getStatusCode()); + //Serial.print("getReasonPhrase"); + //Serial.print(client.getReasonPhrase()); + Serial.print(client.getResponseBody()); +} + +//void getData(String tag, float value){ +void getData(String Sensorname, float Humidity, float Temperature){ + if (!client.connected()){ + Serial.println("getData:Connecting to client again…"); + client.connect(host, httpsPort); + } + //String payload = "tag=" + tag + "&value=" + String(value); + String payload = "Sensorname=" + Sensorname + "&Humidity=" + String(Humidity)+ "&Temperature=" + String(Temperature); + String url2 = url + payload; + //Serial.print("GetData: url2\n"); + //Serial.print(url2); + //Serial.print(" host "); + //Serial.print(host); + //Serial.print("payload "); + //Serial.print(payload); + client.GET(url2, host); + Serial.print("getData: getStatusCode: "); + Serial.print(client.getStatusCode()); + //Serial.print("getReasonPhrase"); + //Serial.print(client.getReasonPhrase()); + //Serial.print(client.getResponseBody()); +} + +void callback(char* topic, byte* payload, unsigned int length) { + + Serial.print("Message arrived in topic: "); + Serial.println(topic); + + Serial.print("Message:"); + for (int i = 0; i < length; i++) { + Serial.print((char)payload[i]); + } + + Serial.println(); + Serial.println("-----------------------"); + +} + +void loop() { + // Wait a few seconds between measurements. + + + // Reading temperature or humidity takes about 250 milliseconds! + // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) + float h = dht.readHumidity(); + // Read temperature as Celsius (the default) + float t = dht.readTemperature(); + // Read temperature as Fahrenheit (isFahrenheit = true) + float f = dht.readTemperature(true); + + // Check if any reads failed and exit early (to try again). + if (isnan(h) || isnan(t) || isnan(f)) { + Serial.println(F("loop: Failed to read from DHT sensor!")); + return; + } + + // Compute heat index in Fahrenheit (the default) + float hif = dht.computeHeatIndex(f, h); + // Compute heat index in Celsius (isFahreheit = false) + float hic = dht.computeHeatIndex(t, h, false); + + Serial.print(F("loop: Humidity: ")); + Serial.print(h); + Serial.print(F("% Temperature: ")); + Serial.print(t); + Serial.print(F("°C ")); + Serial.print(f); + Serial.print(F("°F Heat index: ")); + Serial.print(hic); + Serial.print(F("°C ")); + Serial.print(hif); + Serial.println(F("°F")); + getData("1710Office01" ,h, f); + // postData("Humidity" ,h); + // #postData("TempC",t); + // #postData("TempF",f); + delay(300000); + + +} From 44135486fcb91caf40a8cf136c0f3f30201bac3f Mon Sep 17 00:00:00 2001 From: Adrian Terranova Date: Fri, 7 Jan 2022 11:48:20 -0600 Subject: [PATCH 7/8] Delete 1710MonacoDHTWifiGdriveWriterCleaned directory --- .../1710MonacoDHTWifiGdriveWriterCleaned.ino | 216 ------------------ 1 file changed, 216 deletions(-) delete mode 100644 1710MonacoDHTWifiGdriveWriterCleaned/1710MonacoDHTWifiGdriveWriterCleaned.ino diff --git a/1710MonacoDHTWifiGdriveWriterCleaned/1710MonacoDHTWifiGdriveWriterCleaned.ino b/1710MonacoDHTWifiGdriveWriterCleaned/1710MonacoDHTWifiGdriveWriterCleaned.ino deleted file mode 100644 index b538674..0000000 --- a/1710MonacoDHTWifiGdriveWriterCleaned/1710MonacoDHTWifiGdriveWriterCleaned.ino +++ /dev/null @@ -1,216 +0,0 @@ - -/** - * Example for reading temperature and humidity - * using the DHT22 and ESP8266 - * - * Copyright (c) 2016 Losant IoT. All rights reserved. - * https://www.losant.com - */ - -#define DHTPIN 4 -// what digital pin the DHT22 is conected to -#define DHTTYPE DHT22 // there are multiple kinds of DHT sensors -//#define DEBUG - -#include "DHT.h" -#include -#include -#include -#include - -const char* mqttServer = "192.168.20.18"; -const int mqttPort = 1883; -const char* mqttUser = "XXX"; -const char* mqttPassword = "XXX"; - -// your wifi ap info -const char* ssid = "XXX"; -const char* password = "XXX"; - -//your gsscript sheet id - -const char *GScriptId = "AKfycbzE7WE04utWYeHtCVf2PyQpZnosTiAyIvk3_3UleDMsEnNaoXZn"; -const char* host = "script.google.com"; - -const int dataPostDelay = 900000; // 15 minutes = 15 * 60 * 1000 -const int httpsPort = 443; - -String url = String("/macros/s/") + GScriptId + "/exec?"; - - -HTTPSRedirect client(httpsPort); -PubSubClient mqclient; - -DHT dht(DHTPIN, DHTTYPE); - -void setup() { - Serial.begin(9600); - Serial.setTimeout(2000); - // Wait for serial to initialize. - while(!Serial) { } - - Serial.println("setup: Device Started"); - Serial.println("setup: -------------------------------------"); - Serial.println("setup: Running DHT!"); - Serial.println("setup: -------------------------------------"); - Serial.println(ssid); - Serial.flush(); - dht.begin(); - WiFi.mode(WIFI_STA); - WiFi.begin(ssid, password); - - while (WiFi.status() != WL_CONNECTED) { - delay(500); - Serial.print("."); - } - Serial.println("setup:IP address: "); - Serial.println(WiFi.localIP()); - - //Insecure for TLS cert checking - client.setInsecure(); - client.setContentTypeHeader("application/json"); - - Serial.print("setup: Connecting to "); - Serial.println(host); - - bool flag = false; - - for (int i=0; i<5; i++){ - int retval = client.connect(host, httpsPort); - if (retval == 1) { - flag = true; - break; - } - else - Serial.println("setup: Initial https Connection failed. Retrying..."); - } - - if (!flag){ - Serial.print("setup: Could not connect to server: "); - Serial.println(host); - Serial.println("setup: Exiting..."); - return; - } - Serial.println("setup: Before MQTT"); - mqclient.setServer(mqttServer, mqttPort); - mqclient.setCallback(callback); - while (!mqclient.connected()) { - Serial.println("setup: Connecting to MQTT..."); - - if (mqclient.connect("ESP8266Client", mqttUser, mqttPassword )) { - - Serial.println("connected"); - - } else { - - Serial.print("failed with state "); - Serial.print(mqclient.state()); - delay(2000); - - } - } - - mqclient.publish("esp/test", "hello"); //Topic name - mqclient.subscribe("esp/test"); -} -void postData(String tag, float value){ - // this method is cruft and exist in case there is a desire for a Post payload, it was cloned from the Get fucntion, and never taken further - if (!client.connected()){ - Serial.println("postData: Connecting to client again…"); - client.connect(host, httpsPort); - } - String payload = "tag=" + tag + "&value=" + String(value); - Serial.print("url"); - //Serial.print(url); - Serial.print(" host "); - //Serial.print(host); - Serial.print("payload "); - //Serial.print(payload); - client.POST(url, host, payload); - Serial.print("getStatusCode: "); - Serial.print(client.getStatusCode()); - //Serial.print("getReasonPhrase"); - //Serial.print(client.getReasonPhrase()); - Serial.print(client.getResponseBody()); -} - -//void getData(String tag, float value){ -void getData(String Sensorname, float Humidity, float Temperature){ - if (!client.connected()){ - Serial.println("getData:Connecting to client again…"); - client.connect(host, httpsPort); - } - //String payload = "tag=" + tag + "&value=" + String(value); - String payload = "Sensorname=" + Sensorname + "&Humidity=" + String(Humidity)+ "&Temperature=" + String(Temperature); - String url2 = url + payload; - //Serial.print("GetData: url2\n"); - //Serial.print(url2); - //Serial.print(" host "); - //Serial.print(host); - //Serial.print("payload "); - //Serial.print(payload); - client.GET(url2, host); - Serial.print("getData: getStatusCode: "); - Serial.print(client.getStatusCode()); - //Serial.print("getReasonPhrase"); - //Serial.print(client.getReasonPhrase()); - //Serial.print(client.getResponseBody()); -} - -void callback(char* topic, byte* payload, unsigned int length) { - - Serial.print("Message arrived in topic: "); - Serial.println(topic); - - Serial.print("Message:"); - for (int i = 0; i < length; i++) { - Serial.print((char)payload[i]); - } - - Serial.println(); - Serial.println("-----------------------"); - -} - -void loop() { - // Wait a few seconds between measurements. - - - // Reading temperature or humidity takes about 250 milliseconds! - // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) - float h = dht.readHumidity(); - // Read temperature as Celsius (the default) - float t = dht.readTemperature(); - // Read temperature as Fahrenheit (isFahrenheit = true) - float f = dht.readTemperature(true); - - // Check if any reads failed and exit early (to try again). - if (isnan(h) || isnan(t) || isnan(f)) { - Serial.println(F("loop: Failed to read from DHT sensor!")); - return; - } - - // Compute heat index in Fahrenheit (the default) - float hif = dht.computeHeatIndex(f, h); - // Compute heat index in Celsius (isFahreheit = false) - float hic = dht.computeHeatIndex(t, h, false); - - Serial.print(F("loop: Humidity: ")); - Serial.print(h); - Serial.print(F("% Temperature: ")); - Serial.print(t); - Serial.print(F("°C ")); - Serial.print(f); - Serial.print(F("°F Heat index: ")); - Serial.print(hic); - Serial.print(F("°C ")); - Serial.print(hif); - Serial.println(F("°F")); - getData("1710Office01" ,h, f); - // postData("Humidity" ,h); - // #postData("TempC",t); - // #postData("TempF",f); - delay(300000); - - -} From 63b40eb880d57dd42089ed0b2692a240f55f4dfe Mon Sep 17 00:00:00 2001 From: Adrian Terranova Date: Fri, 7 Jan 2022 11:49:16 -0600 Subject: [PATCH 8/8] Add files via upload --- .../1710MonacoDHTWifiGdriveWriterCleaned.ino | 216 ++++++++++++++++++ 1 file changed, 216 insertions(+) create mode 100644 1710MonacoDHTWifiGdriveWriterCleaned/1710MonacoDHTWifiGdriveWriterCleaned.ino diff --git a/1710MonacoDHTWifiGdriveWriterCleaned/1710MonacoDHTWifiGdriveWriterCleaned.ino b/1710MonacoDHTWifiGdriveWriterCleaned/1710MonacoDHTWifiGdriveWriterCleaned.ino new file mode 100644 index 0000000..ec19c72 --- /dev/null +++ b/1710MonacoDHTWifiGdriveWriterCleaned/1710MonacoDHTWifiGdriveWriterCleaned.ino @@ -0,0 +1,216 @@ + +/** + * Example for reading temperature and humidity + * using the DHT22 and ESP8266 + * + * Copyright (c) 2016 Losant IoT. All rights reserved. + * https://www.losant.com + */ + +#define DHTPIN 4 +// what digital pin the DHT22 is conected to +#define DHTTYPE DHT22 // there are multiple kinds of DHT sensors +//#define DEBUG + +#include "DHT.h" +#include +#include +#include +#include + +const char* mqttServer = "192.168.20.18"; +const int mqttPort = 1883; +const char* mqttUser = "XXX"; +const char* mqttPassword = "XXX"; + +// your wifi ap info +const char* ssid = "XXX"; +const char* password = "XXX"; + +//your gsscript sheet id + +const char *GScriptId = "XXX"; +const char* host = "script.google.com"; + +const int dataPostDelay = 900000; // 15 minutes = 15 * 60 * 1000 +const int httpsPort = 443; + +String url = String("/macros/s/") + GScriptId + "/exec?"; + + +HTTPSRedirect client(httpsPort); +PubSubClient mqclient; + +DHT dht(DHTPIN, DHTTYPE); + +void setup() { + Serial.begin(9600); + Serial.setTimeout(2000); + // Wait for serial to initialize. + while(!Serial) { } + + Serial.println("setup: Device Started"); + Serial.println("setup: -------------------------------------"); + Serial.println("setup: Running DHT!"); + Serial.println("setup: -------------------------------------"); + Serial.println(ssid); + Serial.flush(); + dht.begin(); + WiFi.mode(WIFI_STA); + WiFi.begin(ssid, password); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println("setup:IP address: "); + Serial.println(WiFi.localIP()); + + //Insecure for TLS cert checking + client.setInsecure(); + client.setContentTypeHeader("application/json"); + + Serial.print("setup: Connecting to "); + Serial.println(host); + + bool flag = false; + + for (int i=0; i<5; i++){ + int retval = client.connect(host, httpsPort); + if (retval == 1) { + flag = true; + break; + } + else + Serial.println("setup: Initial https Connection failed. Retrying..."); + } + + if (!flag){ + Serial.print("setup: Could not connect to server: "); + Serial.println(host); + Serial.println("setup: Exiting..."); + return; + } + Serial.println("setup: Before MQTT"); + mqclient.setServer(mqttServer, mqttPort); + mqclient.setCallback(callback); + while (!mqclient.connected()) { + Serial.println("setup: Connecting to MQTT..."); + + if (mqclient.connect("ESP8266Client", mqttUser, mqttPassword )) { + + Serial.println("connected"); + + } else { + + Serial.print("failed with state "); + Serial.print(mqclient.state()); + delay(2000); + + } + } + + mqclient.publish("esp/test", "hello"); //Topic name + mqclient.subscribe("esp/test"); +} +void postData(String tag, float value){ + // this method is cruft and exist in case there is a desire for a Post payload, it was cloned from the Get fucntion, and never taken further + if (!client.connected()){ + Serial.println("postData: Connecting to client again…"); + client.connect(host, httpsPort); + } + String payload = "tag=" + tag + "&value=" + String(value); + Serial.print("url"); + //Serial.print(url); + Serial.print(" host "); + //Serial.print(host); + Serial.print("payload "); + //Serial.print(payload); + client.POST(url, host, payload); + Serial.print("getStatusCode: "); + Serial.print(client.getStatusCode()); + //Serial.print("getReasonPhrase"); + //Serial.print(client.getReasonPhrase()); + Serial.print(client.getResponseBody()); +} + +//void getData(String tag, float value){ +void getData(String Sensorname, float Humidity, float Temperature){ + if (!client.connected()){ + Serial.println("getData:Connecting to client again…"); + client.connect(host, httpsPort); + } + //String payload = "tag=" + tag + "&value=" + String(value); + String payload = "Sensorname=" + Sensorname + "&Humidity=" + String(Humidity)+ "&Temperature=" + String(Temperature); + String url2 = url + payload; + //Serial.print("GetData: url2\n"); + //Serial.print(url2); + //Serial.print(" host "); + //Serial.print(host); + //Serial.print("payload "); + //Serial.print(payload); + client.GET(url2, host); + Serial.print("getData: getStatusCode: "); + Serial.print(client.getStatusCode()); + //Serial.print("getReasonPhrase"); + //Serial.print(client.getReasonPhrase()); + //Serial.print(client.getResponseBody()); +} + +void callback(char* topic, byte* payload, unsigned int length) { + + Serial.print("Message arrived in topic: "); + Serial.println(topic); + + Serial.print("Message:"); + for (int i = 0; i < length; i++) { + Serial.print((char)payload[i]); + } + + Serial.println(); + Serial.println("-----------------------"); + +} + +void loop() { + // Wait a few seconds between measurements. + + + // Reading temperature or humidity takes about 250 milliseconds! + // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) + float h = dht.readHumidity(); + // Read temperature as Celsius (the default) + float t = dht.readTemperature(); + // Read temperature as Fahrenheit (isFahrenheit = true) + float f = dht.readTemperature(true); + + // Check if any reads failed and exit early (to try again). + if (isnan(h) || isnan(t) || isnan(f)) { + Serial.println(F("loop: Failed to read from DHT sensor!")); + return; + } + + // Compute heat index in Fahrenheit (the default) + float hif = dht.computeHeatIndex(f, h); + // Compute heat index in Celsius (isFahreheit = false) + float hic = dht.computeHeatIndex(t, h, false); + + Serial.print(F("loop: Humidity: ")); + Serial.print(h); + Serial.print(F("% Temperature: ")); + Serial.print(t); + Serial.print(F("°C ")); + Serial.print(f); + Serial.print(F("°F Heat index: ")); + Serial.print(hic); + Serial.print(F("°C ")); + Serial.print(hif); + Serial.println(F("°F")); + getData("1710Office01" ,h, f); + // postData("Humidity" ,h); + // #postData("TempC",t); + // #postData("TempF",f); + delay(300000); + + +}