kopia lustrzana https://github.com/martin-ger/esp_mqtt
demo scripts and publish command
rodzic
34b225620f
commit
671a543ce3
11
README.md
11
README.md
|
@ -78,6 +78,7 @@ By default the "remote" MQTT client is disabled. It can be enabled by setting th
|
||||||
- set mqtt_user _username_: Username for authentication ("none" if no authentication is required at the broker)
|
- set mqtt_user _username_: Username for authentication ("none" if no authentication is required at the broker)
|
||||||
- set mqtt_user _password_: Password for authentication
|
- set mqtt_user _password_: Password for authentication
|
||||||
- set mqtt_id _clientId_: Id of the client at the broker (default: "ESPRouter_xxxxxx" derived from the MAC address)
|
- set mqtt_id _clientId_: Id of the client at the broker (default: "ESPRouter_xxxxxx" derived from the MAC address)
|
||||||
|
- publish [local|remote] [topic] [data]: this publishes a topic (mainly for testing)
|
||||||
|
|
||||||
# Scripting
|
# Scripting
|
||||||
The esp_uMQTT_broker comes with a build-in scripting engine. A script enables the ESP not just to act as a passive broker but to react on events (publications and timing events) and to send out its own items.
|
The esp_uMQTT_broker comes with a build-in scripting engine. A script enables the ESP not just to act as a passive broker but to react on events (publications and timing events) and to send out its own items.
|
||||||
|
@ -147,12 +148,13 @@ do
|
||||||
setvar $blink = 0
|
setvar $blink = 0
|
||||||
gpio_out 12 $relay_status
|
gpio_out 12 $relay_status
|
||||||
gpio_out 13 not ($relay_status)
|
gpio_out 13 not ($relay_status)
|
||||||
endif
|
else
|
||||||
if $this_data = "off" then
|
if $this_data = "off" then
|
||||||
setvar $relay_status = 0
|
setvar $relay_status = 0
|
||||||
setvar $blink = 0
|
setvar $blink = 0
|
||||||
gpio_out 12 $relay_status
|
gpio_out 12 $relay_status
|
||||||
gpio_out 13 not ($relay_status)
|
gpio_out 13 not ($relay_status)
|
||||||
|
endif
|
||||||
endif
|
endif
|
||||||
if $this_data = "toggle" then
|
if $this_data = "toggle" then
|
||||||
setvar $relay_status = not ($relay_status)
|
setvar $relay_status = not ($relay_status)
|
||||||
|
@ -183,7 +185,6 @@ on timer 1
|
||||||
do
|
do
|
||||||
if $blink = 1 then
|
if $blink = 1 then
|
||||||
publish local $command_topic "toggle"
|
publish local $command_topic "toggle"
|
||||||
|
|
||||||
settimer 1 500
|
settimer 1 500
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
@ -218,8 +219,8 @@ In general, scripts have the following BNF:
|
||||||
<action> ::= publish (local|remote) <topic-id> <expr> [retained] |
|
<action> ::= publish (local|remote) <topic-id> <expr> [retained] |
|
||||||
subscribe (local|remote) <topic-id> |
|
subscribe (local|remote) <topic-id> |
|
||||||
unsubscribe (local|remote) <topic-id> |
|
unsubscribe (local|remote) <topic-id> |
|
||||||
settimer <num> <num> |
|
settimer <num> <expr> |
|
||||||
setvar $<num> = <expr> |
|
setvar $[any ASCII]* = <expr> |
|
||||||
gpio_pinmode <num> [pullup]
|
gpio_pinmode <num> [pullup]
|
||||||
gpio_out <num> <expr> |
|
gpio_out <num> <expr> |
|
||||||
if <expr> then <action> [else <action>] endif |
|
if <expr> then <action> [else <action>] endif |
|
||||||
|
|
Plik binarny nie jest wyświetlany.
Plik binarny nie jest wyświetlany.
|
@ -1,6 +1,12 @@
|
||||||
#include "user_interface.h"
|
#include "user_interface.h"
|
||||||
#include "mem.h"
|
#include "mem.h"
|
||||||
|
|
||||||
|
#include "mqtt_server.h"
|
||||||
|
#include "mqtt_topics.h"
|
||||||
|
#include "mqtt_topiclist.h"
|
||||||
|
#include "mqtt_retainedlist.h"
|
||||||
|
#include "debug.h"
|
||||||
|
|
||||||
/* Mem Debug
|
/* Mem Debug
|
||||||
#undef os_free
|
#undef os_free
|
||||||
#define os_free(x) {os_printf("F:%d-> %x\r\n", __LINE__,(x));vPortFree(x, "", 0);}
|
#define os_free(x) {os_printf("F:%d-> %x\r\n", __LINE__,(x));vPortFree(x, "", 0);}
|
||||||
|
@ -12,14 +18,10 @@ return _v;
|
||||||
}
|
}
|
||||||
#undef os_zalloc
|
#undef os_zalloc
|
||||||
#define os_zalloc(x) my_os_zalloc(x, __LINE__)
|
#define os_zalloc(x) my_os_zalloc(x, __LINE__)
|
||||||
|
#undef os_malloc
|
||||||
|
#define os_malloc(x) my_os_zalloc(x, __LINE__)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "mqtt_server.h"
|
|
||||||
#include "mqtt_topics.h"
|
|
||||||
#include "mqtt_topiclist.h"
|
|
||||||
#include "mqtt_retainedlist.h"
|
|
||||||
#include "debug.h"
|
|
||||||
|
|
||||||
#ifndef QUEUE_BUFFER_SIZE
|
#ifndef QUEUE_BUFFER_SIZE
|
||||||
#define QUEUE_BUFFER_SIZE 2048
|
#define QUEUE_BUFFER_SIZE 2048
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -0,0 +1,67 @@
|
||||||
|
%
|
||||||
|
% Demo script for an ESP-01 module with a PIR (movement) sensor connected to GPIO 2.
|
||||||
|
% An indicator LED is connected to GPIO 0.
|
||||||
|
% The sensor connects to the Sonoff switch modul running the esp_uMQTT_broker.
|
||||||
|
%
|
||||||
|
|
||||||
|
% Config params, overwrite any previous settings from the commandline
|
||||||
|
% No AP, no broker
|
||||||
|
config ap_on 0
|
||||||
|
config broker_access 0
|
||||||
|
|
||||||
|
config ssid MQTTbroker
|
||||||
|
config password stupidPassword
|
||||||
|
|
||||||
|
% Give us a time
|
||||||
|
config ntp_server 1.de.pool.ntp.org
|
||||||
|
|
||||||
|
% Connect to the broker on the Sonoff
|
||||||
|
config mqtt_host 192.168.178.32
|
||||||
|
config mqtt_user Martin
|
||||||
|
config mqtt_password secret
|
||||||
|
config speed 160
|
||||||
|
|
||||||
|
% Now the initialization, this is done once after booting
|
||||||
|
on init
|
||||||
|
do
|
||||||
|
println "Starting the PIR script"
|
||||||
|
|
||||||
|
% Delay constanst in secs
|
||||||
|
setvar $delay = 10;
|
||||||
|
|
||||||
|
% Device number
|
||||||
|
setvar $device_number = 2
|
||||||
|
|
||||||
|
% Status of the PIR
|
||||||
|
setvar $pir_status=0
|
||||||
|
gpio_out 0 $pir_status
|
||||||
|
|
||||||
|
% Command topic of the switch
|
||||||
|
setvar $command_topic="/martinshome/switch/1/command"
|
||||||
|
|
||||||
|
% Status topic
|
||||||
|
setvar $status_topic="/martinshome/pir/" | $device_number | "/status"
|
||||||
|
|
||||||
|
|
||||||
|
% Now the events, checked whenever something happens
|
||||||
|
|
||||||
|
% The PIR
|
||||||
|
on gpio_interrupt 2 pullup
|
||||||
|
do
|
||||||
|
println "New state GPIO 2: " | $this_gpio
|
||||||
|
setvar $pir_status = $this_gpio
|
||||||
|
gpio_out 0 $pir_status
|
||||||
|
publish remote $status_topic $pir_status retained
|
||||||
|
if $pir_status = 1 then
|
||||||
|
publish remote $command_topic "on"
|
||||||
|
endif
|
||||||
|
settimer 1 $delay*1000
|
||||||
|
|
||||||
|
|
||||||
|
% Turn off again if nothing happens
|
||||||
|
on timer 1
|
||||||
|
do
|
||||||
|
if $pir_status = 0 then
|
||||||
|
publish remote $command_topic "off"
|
||||||
|
endif
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
% Config params, overwrite any previous settings from the commandline
|
% Config params, overwrite any previous settings from the commandline
|
||||||
config ap_ssid MyAP
|
config ap_ssid MQTTbroker
|
||||||
config ap_password stupidPassword
|
config ap_password stupidPassword
|
||||||
config ntp_server 1.de.pool.ntp.org
|
config ntp_server 1.de.pool.ntp.org
|
||||||
config broker_user Martin
|
config broker_user Martin
|
|
@ -368,7 +368,7 @@ void ICACHE_FLASH_ATTR console_handle_command(struct espconn *pespconn) {
|
||||||
|
|
||||||
if (strcmp(tokens[0], "help") == 0) {
|
if (strcmp(tokens[0], "help") == 0) {
|
||||||
os_sprintf(response,
|
os_sprintf(response,
|
||||||
"show [config|stats|mqtt|script]\r\n|set [ssid|password|auto_connect|ap_ssid|ap_password|network|dns|ip|netmask|gw|ap_on|ap_open|speed|config_port|config_access|broker_subscriptions|broker_retained_messages|broker_user|broker_password|broker_access] <val>\r\n|quit|save [config]|reset [factory]|lock [<password>]|unlock <password>");
|
"show [config|stats|mqtt|script]\r\n|set [ssid|password|auto_connect|ap_ssid|ap_password|network|dns|ip|netmask|gw|ap_on|ap_open|speed|config_port|config_access|broker_subscriptions|broker_retained_messages|broker_user|broker_password|broker_access] <val>|\r\npublish [local|remote] <topic> <data>|quit|save [config]|reset [factory]|lock [<password>]|unlock <password>");
|
||||||
to_console(response);
|
to_console(response);
|
||||||
#ifdef SCRIPTED
|
#ifdef SCRIPTED
|
||||||
os_sprintf(response, "|script <port>");
|
os_sprintf(response, "|script <port>");
|
||||||
|
@ -747,6 +747,24 @@ void ICACHE_FLASH_ATTR console_handle_command(struct espconn *pespconn) {
|
||||||
goto command_handled;
|
goto command_handled;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (strcmp(tokens[0], "publish") == 0)
|
||||||
|
{
|
||||||
|
if (nTokens != 4) {
|
||||||
|
os_sprintf(response, INVALID_NUMARGS);
|
||||||
|
goto command_handled;
|
||||||
|
}
|
||||||
|
if (strcmp(tokens[1], "local") == 0) {
|
||||||
|
MQTT_local_publish(tokens[2], tokens[3], os_strlen(tokens[3]), 0, 0);
|
||||||
|
}
|
||||||
|
#ifdef MQTT_CLIENT
|
||||||
|
else if (strcmp(tokens[1], "remote") == 0 && mqtt_connected) {
|
||||||
|
MQTT_Publish(&mqttClient, tokens[2], tokens[3], os_strlen(tokens[3]), 0, 0);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
os_sprintf(response, "Published topic\r\n");
|
||||||
|
goto command_handled;
|
||||||
|
}
|
||||||
|
|
||||||
if (strcmp(tokens[0], "set") == 0) {
|
if (strcmp(tokens[0], "set") == 0) {
|
||||||
if (config.locked) {
|
if (config.locked) {
|
||||||
os_sprintf(response, INVALID_LOCKED);
|
os_sprintf(response, INVALID_LOCKED);
|
||||||
|
|
|
@ -8,7 +8,7 @@ void ICACHE_FLASH_ATTR user_init() {
|
||||||
// Initialize the UART
|
// Initialize the UART
|
||||||
uart_div_modify(0, UART_CLK_FREQ / 115200);
|
uart_div_modify(0, UART_CLK_FREQ / 115200);
|
||||||
|
|
||||||
os_printf("\r\n\r\nMQTT Broker starting\r\n", espconn_tcp_get_max_con());
|
os_printf("\r\n\r\nMQTT Broker starting\r\n");
|
||||||
|
|
||||||
// Setup STA
|
// Setup STA
|
||||||
wifi_set_opmode(STATIONAP_MODE);
|
wifi_set_opmode(STATIONAP_MODE);
|
||||||
|
|
Ładowanie…
Reference in New Issue