Skip to content

Commit 481fe3b

Browse files
authored
Merge pull request #4856 from chromoxdor/reply_event
[sendtohttp] thingspeak only reply event
2 parents 8e7c162 + c215acf commit 481fe3b

File tree

6 files changed

+88
-7
lines changed

6 files changed

+88
-7
lines changed

docs/source/Rules/Rules.rst

+42
Original file line numberDiff line numberDiff line change
@@ -1969,6 +1969,48 @@ Added: 2022/07/23
19691969
* Host name can contain user credentials. For example: ``http://username:pass@hostname:portnr/foo.html``
19701970
* HTTP user credentials now can handle Basic Auth and Digest Auth.
19711971

1972+
Added: 2023/10/26
1973+
1974+
* ``SendToHTTP`` now generates an event with the response of a thingspeak request (https://de.mathworks.com/help/thingspeak/readlastfieldentry.html & // https://de.mathworks.com/help/thingspeak/readdata.html)
1975+
* There are two options:
1976+
1977+
1. Get the value of a single field:
1978+
1979+
- Example command:
1980+
``SendToHTTP,api.thingspeak.com,80,/channels/1637928/fields/5/last.csv``
1981+
- Example of the resulting event:
1982+
``"EVENT: ThingspeakReply=1637928,5,24.2"``
1983+
1984+
| channel number = ``%eventvalue1%``
1985+
| field number = ``%eventvalue2%``
1986+
| value = ``%eventvalue3%``
1987+
1988+
2. Get the values of all fields:
1989+
1990+
- Example command:
1991+
``SendToHTTP,api.thingspeak.com,80,/channels/1637928/feeds/last.csv``
1992+
- Example of the resulting event:
1993+
``"EVENT:ThingspeakReply=1637928,5929,353,42.0,177,19.1,995.6,,"``
1994+
1995+
| channel number = ``%eventvalue1%``
1996+
| values = ``%eventvalue2%`` to ``%eventvalue9%``
1997+
1998+
.. warning:: When using the command for all fields, the reply can become extremely big and can lead to memory issues which results in instabilities of your device (especially when all eight fields are filled with very big numbers)
1999+
2000+
* Example for two single field events in rules:
2001+
2002+
.. code:: none
2003+
2004+
on ThinkspeakReply do
2005+
LogEntry,'The channel number is: %eventvalue1%'
2006+
if %eventvalue2% = 5 //when the field number is 5
2007+
LogEntry,'%eventvalue3%°C in Berlin'
2008+
elseif %eventvalue2% = 6 //when the field number is 6
2009+
LogEntry,'%eventvalue3%°C in Paris'
2010+
endif
2011+
endon
2012+
2013+
19722014
19732015
Convert curl POST command to PostToHTTP
19742016
---------------------------------------

src/Custom-sample.h

+6-5
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@
2222
// --- Feature Flagging ---------------------------------------------------------
2323
// Can be set to 1 to enable, 0 to disable, or not set to use the default (usually via define_plugin_sets.h)
2424

25-
#define FEATURE_RULES_EASY_COLOR_CODE 1 // Use code highlighting, autocompletion and command suggestions in Rules
26-
#define FEATURE_ESPEASY_P2P 1 // (1/0) enables the ESP Easy P2P protocol
27-
#define FEATURE_ARDUINO_OTA 1 //enables the Arduino OTA capabilities
28-
// #define FEATURE_SD 1 // Enable SD card support
29-
// #define FEATURE_DOWNLOAD 1 // Enable downloading a file from an url
25+
#define FEATURE_RULES_EASY_COLOR_CODE 1 // Use code highlighting, autocompletion and command suggestions in Rules
26+
#define FEATURE_ESPEASY_P2P 1 // (1/0) enables the ESP Easy P2P protocol
27+
#define FEATURE_ARDUINO_OTA 1 // enables the Arduino OTA capabilities
28+
#define FEATURE_THINGSPEAK_EVENT 1 // generate an event when requesting last value of a field in thingspeak via SendToHTTP(e.g. sendToHTTP,api.thingspeak.com,80,/channels/1667332/fields/5/last)
29+
// #define FEATURE_SD 1 // Enable SD card support
30+
// #define FEATURE_DOWNLOAD 1 // Enable downloading a file from an url
3031

3132
#ifdef BUILD_GIT
3233
# undef BUILD_GIT

src/src/CustomBuild/define_plugin_sets.h

+7
Original file line numberDiff line numberDiff line change
@@ -3316,5 +3316,12 @@ To create/register a plugin, you have to :
33163316
*/
33173317

33183318

3319+
#ifndef FEATURE_THINGSPEAK_EVENT
3320+
#ifdef LIMIT_BUILD_SIZE
3321+
#define FEATURE_THINGSPEAK_EVENT 0
3322+
#else
3323+
#define FEATURE_THINGSPEAK_EVENT 1
3324+
#endif
3325+
#endif
33193326

33203327
#endif // CUSTOMBUILD_DEFINE_PLUGIN_SETS_H

src/src/Helpers/Networking.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -1561,7 +1561,38 @@ int http_authenticate(const String& logIdentifier,
15611561
// Generate event with the HTTP return code
15621562
// e.g. http#hostname=401
15631563
eventQueue.addMove(strformat(F("http#%s=%d"), host.c_str(), httpCode));
1564+
1565+
#if FEATURE_THINGSPEAK_EVENT
1566+
// Generate event with the response of a
1567+
// thingspeak request (https://de.mathworks.com/help/thingspeak/readlastfieldentry.html &
1568+
// https://de.mathworks.com/help/thingspeak/readdata.html)
1569+
// e.g. command for a specific field: "sendToHTTP,api.thingspeak.com,80,/channels/1637928/fields/5/last.csv"
1570+
// command for all fields: "sendToHTTP,api.thingspeak.com,80,/channels/1637928/feeds/last.csv"
1571+
// where first eventvalue is the channel number and the second to the nineth event values
1572+
// are the field values
1573+
// Example of the event: "EVENT: ThingspeakReply=1637928,5,24.2,12,900,..."
1574+
// ^ ^ └------┬------┘
1575+
// channel number ┘ | └ received values
1576+
// field number (only available for a "single-value-event")
1577+
// In rules you can grep the reply by "On ThingspeakReply Do ..."
1578+
1579+
if (httpCode == 200 && equals(host, F("api.thingspeak.com")) && uri.endsWith(F("/last.csv"))) {
1580+
String result = http.getString();
1581+
const int posTimestamp = result.lastIndexOf(':');
1582+
if (posTimestamp >= 0) {
1583+
result = parseStringToEndKeepCase(result.substring(posTimestamp), 3);
1584+
if (uri.indexOf(F("fields")) >= 0) { //when there is a single field call add the field number before the value
1585+
result = parseStringKeepCase(uri, 4, '/') + "," + result;
1586+
}
1587+
eventQueue.addMove(strformat(
1588+
F("ThingspeakReply=%s,%s"),
1589+
parseStringKeepCase(uri, 2, '/').c_str(),
1590+
result.c_str()));
1591+
}
1592+
}
1593+
#endif
15641594
}
1595+
15651596
#ifndef BUILD_NO_DEBUG
15661597
log_http_result(http, logIdentifier, host + ':' + port, HttpMethod, httpCode, EMPTY_STRING);
15671598
#endif

static/espeasy.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var commonCommands = ["AccessInfo", "Background", "Build", "ClearAccessBlock", "
88
"ControllerEnable", "DateTime", "Debug", "Dec", "DeepSleep", "DisablePriorityTask", "DNS", "DST", "EraseSDKWiFi", "ExecuteRules", "Gateway", "I2Cscanner", "Inc",
99
"IP", "Let", "Load", "LogEntry", "LogPortStatus", "LoopTimerSet", "LoopTimerSet_ms", "MemInfo", "MemInfoDetail", "Name", "Password", "PostToHTTP", "Publish",
1010
"Reboot", "Reset", "Save", "SendTo", "SendToHTTP", "SendToUDP", "Settings", "Subnet", "Subscribe", "TaskClear", "TaskClearAll",
11-
"TaskDisable", "TaskEnable", "TaskRun", "TaskValueSet", "TaskValueSetAndRun", "TimerPause", "TimerResume", "TimerSet", "TimerSet_ms", "TimeZone",
11+
"TaskDisable", "TaskEnable", "TaskRun", "TaskValueSet", "TaskValueSetAndRun", "ThingspeakReply", "TimerPause", "TimerResume", "TimerSet", "TimerSet_ms", "TimeZone",
1212
"UdpPort", "UdpTest", "Unit", "UseNTP", "WdConfig", "WdRead", "WiFi", "WiFiAPkey", "WiFiAllowAP", "WiFiAPMode", "WiFiConnect", "WiFiDisconnect", "WiFiKey",
1313
"WiFiKey2", "WiFiScan", "WiFiSSID", "WiFiSSID2", "WiFiSTAMode", "WiFi#Disconnected",
1414
"Event", "AsyncEvent",

0 commit comments

Comments
 (0)