The printer is awesome and while it already operates remotely via Octoprint on Cubietruck (see previous posts), I did find it useful to add a cooling system to it as well as some lighting inside the chassis, both to be remotely controlled as well.
This post is all about the lua scripts loaded to the ESP8266-01 wifi module, in order to achieve this.
First off, I had several issues with memory usage while using a stock nodeMCU firmware, meaning, a build containing all the available modules.
Trimming the fat from the firmware proved quite valuable in memory terms (check previous posts).
Then, moving on to the lua scripts, I managed to devise a basic management system where I can now update the wifi module configurations for WiFi and MQTT broker over the air, using the MQTT messaging system.
config_wifi.lua:
wifi_ssid='
wifiHotspot
'
wifi_pass='
w1f1P4ss
'
config_mqtt.lua:
mqtt_host='192.168.1.2'
mqtt_port=1883
mqtt_topic='topic-'..node.chipid()
bootup.lua:
function bootup()
wifi.setmode(wifi.STATION)
wifi.sta.config(wifi_ssid,wifi_pass)
end
mqtt_connect.lua:
function mqttActivate()
m:connect(mqtt_host, mqtt_port, 0, function(conn)
print("connected to mqtt broker!")
m:subscribe("/"..mqtt_topic,0, function(conn)
tmr.stop(3)
tmr.stop(2)
end)
end)
end
function mqttConnect()
m = mqtt.Client(wifi.sta.getmac(), 120, "user", "password")
m:lwt("/lwt", wifi.sta.getmac(), 0, 0)
m:on("offline", function(con)
tmr.alarm(2, 10000, 1, function()
print ("reconnecting to mqtt broker...")
mqttActivate()
end)
end)
tmr.alarm(3, 10000, 1, function()
print("connecting to mqtt broker...")
mqttActivate()
end)
mqttParse()
end
mqtt_parse.lua:
function mqttParse()
m:on("message", function(conn, topic, data)
if data ~= nil then
local i=1
local offset=0
while string.find(data,";") do
field = string.sub(data, offset, string.find(data,":")-1)
dataArray[field] = string.sub(data, string.find(data,":")+1, string.find(data,";")-1)
data = string.sub(data, string.find(data, ";") + 1, string.len(data))
i = i + 1
tmr.wdclr()
end
if dataArray['type'] == 'config-update' then
confUpdate()
elseif dataArray['type'] == 'gpio-control' then
gpioControl()
end
end
end)
end
config_update.lua:
function confUpdate()
print('changing configuration for module: '..dataArray['setting'])
if dataArray['setting'] == 'wifi' then
file.open("config_wifi.lua", "w+")
file.writeline('wifi_ssid=\''..dataArray['ssid']..'\'')
file.writeline('wifi_pass=\''..dataArray['pass']..'\'')
file.close()
elseif dataArray['setting'] == 'mqtt' then
file.open("config_mqtt.lua", "w+")
file.writeline('mqtt_host=\''..dataArray['host']..'\'')
file.writeline('mqtt_port='..dataArray['port'])
file.writeline('mqtt_topic=\'topic-'..node.chipid()..'\'')
file.close()
end
node.restart()
end
gpio_control.lua:
function gpioControl()
print('setting GPIO pin: '..dataArray['gpio'])
pin=0
if dataArray['gpio'] == '0' then
pin=3
elseif dataArray['gpio'] == '2' then
pin=4
end
if dataArray['state'] == 'on' then
gpio.write(pin, gpio.HIGH)
elseif dataArray['state'] == 'off' then
gpio.mode(pin, gpio.INPUT)
end
end
init.lua:
require 'config_wifi'
require 'config_mqtt'
require 'bootup'
require 'mqtt_connect'
require 'mqtt_parse'
require 'config_update'
require 'gpio_control'
dataArray={}
bootup()
gpio.mode(3, gpio.INPUT)
gpio.mode(4, gpio.INPUT)
mqttConnect()
The WiFi module will try to connect the the WiFi network and from there, to the
MQTT broker. Once connected, it will register to a new topic based on its ID,
which you'll be able to catch in the MQTT broker log file.
You'll want to compile all scripts, except for config_wifi.lua, config_mqtt.lua
and init.lua.
I'm still learning Lua, hadn't used it before, which is to say that there's
definitely room for improvement here - however, as they stand, these
scripts do the job in allowing for remote, on demand control!
And finally, at this point, you'll be able to publish messages
to your MQTT broker server in order to operate your wifi module:
mosquitto_pub -h 192.168.1.2 -p 1883 -t /topic-12345678 -m 'type:gpio-control;gpio:0;state:on;'
mosquitto_pub -h
192.168.1.2
-p 1883
-t /topic-12345678 -m 'type:gpio-control;gpio:0;state:off;'
mosquitto_pub -h
192.168.1.2
-p 1883
-t /topic-12345678 -m 'type:gpio-control;gpio:2;state:on;'
mosquitto_pub -h
192.168.1.2
-p 1883
-t /topic-12345678 -m 'type:gpio-control;gpio:2;state:off;'
mosquitto_pub -h
192.168.1.2
-p 1883
-t /topic-12345678 -m '
type:config-update;setting:mqtt;host:192.168.1.3;port:1883;'
mosquitto_pub -h
192.168.1.2
-p 1883
-t /topic-12345678 -m '
type:config-update;setting:wifi;ssid:wifiHotspot;pass:w1f1P4ss;
'
No comments :
Post a Comment