Last active
March 19, 2019 13:43
-
-
Save aderbas/74bc632a145277620cbfa7f9c9880969 to your computer and use it in GitHub Desktop.
Thingsboard GPIO MQTT for Wemos D1 Mini
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--MQTT params | |
local mqtt_settings = { | |
module = "ESP8266_"..node.chipid(), | |
ip = "<brokerip>", | |
port = 1883, | |
token = "tokendevice" | |
} | |
-- local pins data | |
local gpio_pins = { | |
{pin = 0, value = "false"}, | |
{pin = 1, value = "false"}, | |
{pin = 2, value = "false"}, | |
{pin = 3, value = "false"}, | |
{pin = 4, value = "false"}, | |
{pin = 5, value = "false"}, | |
{pin = 6, value = "false"}, | |
{pin = 7, value = "false"}, | |
{pin = 8, value = "false"} | |
} | |
-- put pin value to table | |
local function pValue(idx, value) | |
gpio_pins[idx].value = value | |
end | |
-- read from cache | |
-- cache.txt is a file containing the pin values: 0,1,0,0,0,0,1,0,0 | |
if file.open("cache.txt") then | |
last = file.read() | |
file.close() | |
t = {} | |
for v in string.gmatch(last, "[^,]") do table.insert(t,v) end | |
for i = 1, 9 do if t[i] == "0" then pValue(i,"false") else pValue(i,"true") end end | |
end | |
-- set gpio to OUTPUT | |
for i = 1, 8 do | |
gpio.mode(gpio_pins[i].pin, gpio.OUTPUT) | |
end | |
-- mount string to cache last value | |
local function cache_last_value() | |
str = "" | |
for i = 1, 9 do if gpio_pins[i].value == "true" then str = str .. "1," else str = str .. "0," end end | |
str = string.sub(str, 0, -2) | |
if file.open("cache.txt", "w") then | |
file.write(str) | |
file.close() | |
end | |
end | |
-- mount string json GPIO data response | |
local function get_gpio_string() | |
str = "{" | |
for i = 1, 9 do | |
str = str .. "\"" ..gpio_pins[i].pin .. "\": " ..gpio_pins[i].value.. ", " | |
end | |
str = string.sub(str, 0, -3) | |
str = str .. "}" | |
return str | |
end | |
-- set GPIO value from MQTT request | |
local function set_gpio_value(data) | |
if data.enabled == true then | |
gpio.write(data.pin, gpio.HIGH) | |
else | |
gpio.write(data.pin, gpio.LOW) | |
end | |
gpio_pins[data.pin+1].value = tostring(data.enabled) | |
-- save cache | |
cache_last_value() | |
end | |
-- handler payload data | |
local function handler_payload(topic, data) | |
print(data) | |
resTopic = topic:gsub("request", "response") | |
payload = sjson.decode(data) | |
print(resTopic) | |
if payload.method == "getGpioStatus" then | |
m:publish(resTopic, get_gpio_string(), 0, 0, function(client) print("Response sent :: getGpioStatus") end) | |
elseif payload.method == "setGpioStatus" then | |
-- set gpio | |
set_gpio_value(payload.params) | |
m:publish(resTopic, get_gpio_string(), 0, 0, function(client) print("Response sent :: setGpioStatus") end) | |
m:publish("v1/devices/me/attributes", get_gpio_string(), 0, 0, function(client) print("Attributes sent") end) | |
else | |
print("No payload method") | |
end | |
end | |
-- create mqtt client | |
m = mqtt.Client(mqtt_settings.module, 41, mqtt_settings.token, "password") | |
m:on("message", function(client, topic, data) | |
print(topic ..":") | |
if data ~= nil then | |
handler_payload(topic, data) | |
end | |
end) | |
-- handler error | |
function handler_mqtt_error(client, reason) | |
print(reason) | |
tmr.create():alarm(10 * 1000, tmr.ALARM_SINGLE, do_mqtt_connect) | |
end | |
-- handler mqtt success connected | |
function handler_mqtt_connect(client) | |
print("Connected") | |
client:subscribe("v1/devices/me/rpc/request/+", 0, function(client) print("subscribe success") end) | |
client:publish("v1/devices/me/attributes", get_gpio_string(), 0, 0, function(client) print("Data sent") end) | |
end | |
-- performs connect mqtt | |
function do_mqtt_connect() | |
print("Connecting "..node.chipid().." to MQTT broker...") | |
m:connect(mqtt_settings.ip, mqtt_settings.port, handler_mqtt_connect, handler_mqtt_error) | |
end | |
-- connect | |
do_mqtt_connect() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
:25 cache.txt is a file containing the pin values: 0,1,0,0,0,0,1,0,0