From 9a1b49d49a6fdc3011be097936ff0f02df213511 Mon Sep 17 00:00:00 2001 From: esp8266nu Date: Sat, 15 Aug 2015 13:50:23 +0000 Subject: [PATCH] Added configurable message delay in ms (delay between messages to controller) Changed login password field to type 'password' Fixed io pin settings for Dallas/DHT for inconsecutive pin configuration (ESP-01) Support for ThingsSpeak "controller" (webservice) --- Controller.ino | 123 ++++++++++++++++++++++++++++++++++++++++++++++ Devices.ino | 12 ++++- ESPEasy.ino | 18 ++++--- Serial.ino | 1 + WebServer.ino | 34 ++++++++++--- _ReleaseNotes.ino | 6 +++ 6 files changed, 180 insertions(+), 14 deletions(-) diff --git a/Controller.ino b/Controller.ino index c3d214b63..78fd69f95 100644 --- a/Controller.ino +++ b/Controller.ino @@ -73,8 +73,19 @@ boolean sendData(byte sensorType, int idx, byte varIndex) break; case 3: NodoTelnet_sendData(sensorType, idx, varIndex); + case 4: + ThingsSpeak_sendData(sensorType, idx, varIndex); break; } + if (Settings.MessageDelay != 0) + { + char log[30]; + sprintf_P(log, PSTR("HTTP : Delay %u ms"), Settings.MessageDelay); + addLog(LOG_LEVEL_DEBUG_MORE,log); + unsigned long timer = millis() + Settings.MessageDelay; + while (millis() < timer) + backgroundtasks(); + } } //#endif @@ -466,6 +477,118 @@ struct NodeStruct } Nodes[32]; +/*********************************************************************************************\ + * Send data to Domoticz using http url querystring +\*********************************************************************************************/ +boolean ThingsSpeak_sendData(byte sensorType, int idx, byte varIndex) +{ + char log[80]; + boolean success = false; + char host[20]; + sprintf_P(host, PSTR("%u.%u.%u.%u"), Settings.Controller_IP[0], Settings.Controller_IP[1], Settings.Controller_IP[2], Settings.Controller_IP[3]); + + sprintf_P(log, PSTR("%s%s"), "HTTP : connecting to ", host); + addLog(LOG_LEVEL_DEBUG,log); + if (printToWeb) + { + printWebString += log; + printWebString += "
"; + } + // Use WiFiClient class to create TCP connections + WiFiClient client; + if (!client.connect(host, Settings.ControllerPort)) + { + connectionFailures++; + addLog(LOG_LEVEL_ERROR,(char*)"HTTP : connection failed"); + if (printToWeb) + printWebString += F("connection failed
"); + return false; + } + if (connectionFailures) + connectionFailures--; + + String postDataStr = Settings.ControllerPassword; // "0UDNN17RW6XAS2E5" // api key + + switch (sensorType) + { + case 1: // single value sensor, used for Dallas, BH1750, etc + postDataStr +="&field"; + postDataStr += idx; + postDataStr += "="; + postDataStr += String(UserVar[varIndex - 1]); + break; + case 2: // dual value + case 3: + postDataStr +="&field"; + postDataStr += idx; + postDataStr += "="; + postDataStr += String(UserVar[varIndex - 1]); + postDataStr +="&field"; + postDataStr += idx+1; + postDataStr += "="; + postDataStr += String(UserVar[varIndex]); + break; + case 10: // switch + break; + } + postDataStr += "\r\n\r\n"; + + String postStr = F("POST /update HTTP/1.1\n"); + postStr += F("Host: api.thingspeak.com\n"); + postStr += F("Connection: close\n"); + postStr += F("X-THINGSPEAKAPIKEY: "); + postStr += Settings.ControllerPassword; + postStr += "\n"; + postStr += F("Content-Type: application/x-www-form-urlencoded\n"); + postStr += F("Content-Length: "); + postStr += postDataStr.length(); + postStr += F("\n\n"); + postStr += postDataStr; + + if (Settings.SerialLogLevel >= LOG_LEVEL_DEBUG_MORE) + Serial.println(postStr); + + // This will send the request to the server + client.print(postStr); + + unsigned long timer = millis() + 200; + while (!client.available() && millis() < timer) + delay(1); + + // Read all the lines of the reply from server and print them to Serial + while (client.available()) { + if (Settings.SerialLogLevel >= LOG_LEVEL_DEBUG_MORE) + { + sprintf_P(log,PSTR("C1 WS %u FM %u"),WiFi.status(),FreeMem()); + Serial.println(log); + } + String line = client.readStringUntil('\n'); + if (Settings.SerialLogLevel >= LOG_LEVEL_DEBUG_MORE) + { + sprintf_P(log,PSTR("C2 WS %u FM %u"),WiFi.status(),FreeMem()); + Serial.println(log); + } + line.toCharArray(log,79); + addLog(LOG_LEVEL_DEBUG_MORE,log); + if (line.substring(0, 15) == "HTTP/1.1 200 OK") + { + addLog(LOG_LEVEL_DEBUG,(char*)"HTTP : Succes!"); + if (printToWeb) + printWebString += F("Success
"); + success = true; + } + delay(1); + } + addLog(LOG_LEVEL_DEBUG,(char*)"HTTP : closing connection"); + if (printToWeb) + printWebString += F("closing connection
"); + + client.flush(); + client.stop(); + return success; +} + + /*********************************************************************************************\ * Send data to other ESP node \*********************************************************************************************/ diff --git a/Devices.ino b/Devices.ino index 0b4fa9e94..98d32dfa7 100644 --- a/Devices.ino +++ b/Devices.ino @@ -278,7 +278,10 @@ boolean dallas(byte Par1, byte Par2) byte var = Par2; // Variable to be set. byte RelativePort = Par1 - 1; - DallasPin = Settings.Pin_wired_out_1 + Par1 - 1; + if (Par1 == 1) + DallasPin = Settings.Pin_wired_out_1; + else + DallasPin = Settings.Pin_wired_out_2; noInterrupts(); while (!(bitRead(Call_Status, RelativePort))) @@ -336,12 +339,17 @@ byte read_dht_dat(void) boolean dht(byte type, byte Par1, byte Par2) { boolean success = false; - DHT_Pin = Settings.Pin_wired_out_1 + Par1 - 1; + byte dht_dat[5]; byte dht_in; byte i; byte Retry = 0; + if (Par1 == 1) + DHT_Pin = Settings.Pin_wired_out_1; + else + DHT_Pin = Settings.Pin_wired_out_2; + do { pinMode(DHT_Pin, OUTPUT); diff --git a/ESPEasy.ino b/ESPEasy.ino index 7fda474a8..c6d728a61 100644 --- a/ESPEasy.ino +++ b/ESPEasy.ino @@ -98,7 +98,7 @@ #define ESP_PROJECT_PID 2015050101L #define ESP_EASY #define VERSION 3 -#define BUILD 12 +#define BUILD 13 #define REBOOT_ON_MAX_CONNECTION_FAILURES 30 #define LOG_LEVEL_ERROR 1 @@ -174,6 +174,7 @@ struct SettingsStruct char ControllerUser[26]; char ControllerPassword[26]; char Password[26]; + unsigned long MessageDelay; } Settings; struct LogStruct @@ -263,10 +264,6 @@ void setup() void loop() { - WebServer.handleClient(); - - MQTTclient.loop(); - if (Serial.available()) serial(); @@ -352,7 +349,9 @@ void loop() delay(100); } } - delay(10); + + backgroundtasks(); + } void inputCheck() @@ -424,4 +423,11 @@ void SensorSend() } +void backgroundtasks() +{ + WebServer.handleClient(); + MQTTclient.loop(); + delay(10); +} + diff --git a/Serial.ino b/Serial.ino index cf1b973c5..c47e423ed 100644 --- a/Serial.ino +++ b/Serial.ino @@ -449,6 +449,7 @@ void ResetFactory(void) Settings.ControllerUser[0] = 0; Settings.ControllerPassword[0] = 0; Settings.Password[0] = 0; + Settings.MessageDelay=2000; Save_Settings(); WifiDisconnect(); diff --git a/WebServer.ino b/WebServer.ino index 164fa2543..ca7cc7457 100644 --- a/WebServer.ino +++ b/WebServer.ino @@ -6,8 +6,8 @@ void WebServerInit() // Prepare webserver pages WebServer.on("/", handle_root); WebServer.on("/config", handle_config); - WebServer.on("/devices", handle_devices); WebServer.on("/hardware", handle_hardware); + WebServer.on("/devices", handle_devices); WebServer.on("/json.htm", handle_json); #ifdef ESP_CONNEXIO WebServer.on("/eventlist", handle_eventlist); @@ -275,13 +275,14 @@ void handle_config() { reply += F("'>Protocol:"); byte choice = Settings.Protocol; - String options[4]; + String options[5]; options[0] = F(""); options[1] = F("Domoticz HTTP"); options[2] = F("Domoticz MQTT"); options[3] = F("Nodo Telnet"); + options[4] = F("ThingsSpeak HTTP"); reply += F("
Device SettingsIDX/VariableConnect to
"); reply += F("
Delay:
Message Delay (ms):
Dallas:Output 1
DHT:"); reply += F("
Password"); - reply += F("
"); reply += F("
"); diff --git a/_ReleaseNotes.ino b/_ReleaseNotes.ino index d941aefba..823e63f3d 100644 --- a/_ReleaseNotes.ino +++ b/_ReleaseNotes.ino @@ -88,4 +88,10 @@ // Added hardware custom gpio pin selection // Added sanity check on BMP085 pressure values +// R013 15-08-2015 +// Added configurable message delay in ms (delay between messages to controller) +// Changed login password field to type 'password' +// Fixed io pin settings for Dallas/DHT for inconsecutive pin configuration (ESP-01) +// Support for ThingsSpeak "controller" (webservice) +