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)
This commit is contained in:
esp8266nu
2015-08-15 13:50:23 +00:00
parent 24e3b8aea5
commit 9a1b49d49a
6 changed files with 180 additions and 14 deletions
+123
View File
@@ -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 += "<BR>";
}
// 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<BR>");
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<BR>");
success = true;
}
delay(1);
}
addLog(LOG_LEVEL_DEBUG,(char*)"HTTP : closing connection");
if (printToWeb)
printWebString += F("closing connection<BR>");
client.flush();
client.stop();
return success;
}
/*********************************************************************************************\
* Send data to other ESP node
\*********************************************************************************************/
+10 -2
View File
@@ -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);
+12 -6
View File
@@ -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);
}
+1
View File
@@ -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();
+28 -6
View File
@@ -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("'><TR><TD>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("<TD><select name='protocol'>");
for (byte x = 0; x < 4; x++)
for (byte x = 0; x < 5; x++)
{
reply += F("<option value='");
reply += x;
@@ -308,7 +309,7 @@ void handle_config() {
reply += Settings.ControllerUser;
}
if (Settings.Protocol == 3)
if (Settings.Protocol == 3 or Settings.Protocol == 4)
{
reply += F("'><TR><TD>Controller Password:<TD><input type='text' name='controllerpassword' value='");
reply += Settings.ControllerPassword;
@@ -366,6 +367,7 @@ void handle_devices() {
String boardtype = WebServer.arg("boardtype");
String sensordelay = WebServer.arg("delay");
String messagedelay = WebServer.arg("messagedelay");
String dallas = WebServer.arg("dallas");
String dht = WebServer.arg("dht");
String dhttype = WebServer.arg("dhttype");
@@ -379,6 +381,7 @@ void handle_devices() {
if (sensordelay.toInt() != 0)
{
Settings.Delay = sensordelay.toInt();
Settings.MessageDelay = messagedelay.toInt();
Settings.Dallas = dallas.toInt();
Settings.DHT = dht.toInt();
Settings.DHTType = dhttype.toInt();
@@ -404,21 +407,29 @@ void handle_devices() {
{
eventAddTimer((char*)"DallasRead 1,1");
eventAddVarSend(1, 1, Settings.Dallas);
if (Settings.MessageDelay != 0)
eventAddDelay();
}
if (Settings.DHT != 0)
{
eventAddTimer((char*)"DHTRead 2,2");
eventAddVarSend(2, 2, Settings.DHT);
if (Settings.MessageDelay != 0)
eventAddDelay();
}
if (Settings.BMP != 0)
{
eventAddTimer((char*)"BMP085Read 4");
eventAddVarSend(4, 3, Settings.BMP);
if (Settings.MessageDelay != 0)
eventAddDelay();
}
if (Settings.LUX != 0)
{
eventAddTimer((char*)"LuxRead 6");
eventAddVarSend(6, 1, Settings.LUX);
if (Settings.MessageDelay != 0)
eventAddDelay();
}
if (Settings.Analog != 0)
{
@@ -434,6 +445,8 @@ void handle_devices() {
reply += F("<form method='post'><table bgcolor='#ddeeff'><tr bgcolor='#55bbff'><td>Device Settings<td>IDX/Variable<TD>Connect to<TR><TD>");
reply += F("<TR><TD>Delay:<TD><input type='text' name='delay' value='");
reply += Settings.Delay;
reply += F("'><TR><TD>Message Delay (ms):<TD><input type='text' name='messagedelay' value='");
reply += Settings.MessageDelay;
reply += F("'><TR><TD>Dallas:<TD><input type='text' name='dallas' value='");
reply += Settings.Dallas;
reply += F("'><TD>Output 1<TR><TD>DHT:<TD><input type='text' name='dht' value='");
@@ -469,6 +482,8 @@ void eventAddVarSend(byte var, byte sensortype, int idx)
strcpy(strProtocol, "MQTT");
if (Settings.Protocol == 3)
strcpy(strProtocol, "TELNET");
if (Settings.Protocol == 4)
strcpy(strProtocol, "TSPK");
sprintf_P(cmd, PSTR("eventlistwrite; Timer 1; VariableSend %u,%s,%u,%u"), var, strProtocol, sensortype, idx);
ExecuteLine(cmd, VALUE_SOURCE_SERIAL);
}
@@ -479,6 +494,13 @@ void eventAddTimer(char* event)
sprintf_P(cmd, PSTR("eventlistwrite; Timer 1; %s"), event);
ExecuteLine(cmd, VALUE_SOURCE_SERIAL);
}
void eventAddDelay()
{
char cmd[80];
sprintf_P(cmd, PSTR("eventlistwrite; Timer 1; Delay %u"), Settings.MessageDelay);
ExecuteLine(cmd, VALUE_SOURCE_SERIAL);
}
#endif
@@ -1024,9 +1046,9 @@ void handle_login() {
webrequest.toCharArray(command, 79);
String reply = "";
reply += F("<form>");
reply += F("<form method='post'>");
reply += F("<table bgcolor='#ddeeff'><tr bgcolor='#55bbff'><td>Password<TD>");
reply += F("<input type='text' name='password' value='");
reply += F("<input type='password' name='password' value='");
reply += webrequest;
reply += F("'><TR><TD><TD><input class=\"button-link\" type='submit' value='Submit'><TR><TD>");
reply += F("</table></form>");
+6
View File
@@ -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)