demo scripts and publish command

pull/16/head
Martin Ger 2017-08-22 21:46:45 +02:00
rodzic 34b225620f
commit 671a543ce3
8 zmienionych plików z 102 dodań i 14 usunięć

Wyświetl plik

@ -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 _password_: Password for authentication
- 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
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
gpio_out 12 $relay_status
gpio_out 13 not ($relay_status)
endif
if $this_data = "off" then
else
if $this_data = "off" then
setvar $relay_status = 0
setvar $blink = 0
gpio_out 12 $relay_status
gpio_out 13 not ($relay_status)
endif
endif
if $this_data = "toggle" then
setvar $relay_status = not ($relay_status)
@ -183,7 +185,6 @@ on timer 1
do
if $blink = 1 then
publish local $command_topic "toggle"
settimer 1 500
endif
@ -218,8 +219,8 @@ In general, scripts have the following BNF:
<action> ::= publish (local|remote) <topic-id> <expr> [retained] |
subscribe (local|remote) <topic-id> |
unsubscribe (local|remote) <topic-id> |
settimer <num> <num> |
setvar $<num> = <expr> |
settimer <num> <expr> |
setvar $[any ASCII]* = <expr> |
gpio_pinmode <num> [pullup]
gpio_out <num> <expr> |
if <expr> then <action> [else <action>] endif |

Plik binarny nie jest wyświetlany.

Plik binarny nie jest wyświetlany.

Wyświetl plik

@ -1,6 +1,12 @@
#include "user_interface.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
#undef os_free
#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
#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
#define QUEUE_BUFFER_SIZE 2048
#endif

67
scripts/script.pir 100644
Wyświetl plik

@ -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

Wyświetl plik

@ -1,5 +1,5 @@
% Config params, overwrite any previous settings from the commandline
config ap_ssid MyAP
config ap_ssid MQTTbroker
config ap_password stupidPassword
config ntp_server 1.de.pool.ntp.org
config broker_user Martin

Wyświetl plik

@ -368,7 +368,7 @@ void ICACHE_FLASH_ATTR console_handle_command(struct espconn *pespconn) {
if (strcmp(tokens[0], "help") == 0) {
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);
#ifdef SCRIPTED
os_sprintf(response, "|script <port>");
@ -747,6 +747,24 @@ void ICACHE_FLASH_ATTR console_handle_command(struct espconn *pespconn) {
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 (config.locked) {
os_sprintf(response, INVALID_LOCKED);

Wyświetl plik

@ -8,7 +8,7 @@ void ICACHE_FLASH_ATTR user_init() {
// Initialize the UART
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
wifi_set_opmode(STATIONAP_MODE);