reset wifi data on button push, mqtt device config
reset wifi data on button push, mqtt device config

file:a/init.lua -> file:b/init.lua
--- a/init.lua
+++ b/init.lua
@@ -32,3 +32,14 @@
     end
 end)
 
+pin = 3
+function pin3cb()
+    print("Resetting wireless configuration and restarting")
+    station_cfg = {}
+    station_cfg.ssid = "1234567890"
+    station_cfg.pwd = "1234567890"
+    station_cfg.save = true
+    wifi.sta.config(station_cfg)
+    node.restart()
+end
+gpio.trig(pin, "down", pin3cb)

file:a/mqtt.lua -> file:b/mqtt.lua
--- a/mqtt.lua
+++ b/mqtt.lua
@@ -2,6 +2,15 @@
 
 prefix = "/IoTmanager"
 device = "/relay01"
+
+mqttConfig = {}
+mqttConfig.host = '192.168.0.50'
+mqttConfig.port = '9443'
+mqttConfig.user = 'iot'
+mqttConfig.pass = 'iot'
+mqttConfig.secure = 1
+
+print ("Trying to connect to MQTT host " .. mqttConfig.host .. " on port " .. mqttConfig.port)
 
 local status1 = 1
 gpio12 = 6
@@ -17,10 +26,10 @@
 
 -- initiate the mqtt client and set keepalive timer to 120sec
 
-mqtt = mqtt.Client("nodemcu", 120, "qdcaxgqc", "eQurLLKpesr9", 1)
+mqtt = mqtt.Client(device, 120, mqttConfig.user, mqttConfig.pass, 1)
 
 mqtt:on("connect", function(con)
-    print ("connected to MQTT broker\n")
+    print ("connected to MQTT " .. mqttConfig.host .. " broker\n")
 end)
 mqtt:on("offline", function(con)
     print ("\tdisconected from MQTT broker, reconnecting\n")
@@ -78,8 +87,8 @@
     end
 end)
 
-mqtt:connect("m20.cloudmqtt.com", 29338, 1, 1, function(conn) 
-    print ("connected to MQTT broker\n")
+mqtt:connect(mqttConfig.host, mqttConfig.port, mqttConfig.secure, 1, function(conn) 
+    print ("connected to " .. mqttConfig.host  .. " MQTT broker\n")
     -- subscribe topic with qos = 2
     mqtt:subscribe({[prefix .. "/+/+/control"]=1, [prefix .. "/+/request"]=1}, function(conn)
     end)

comments