Dopo aver esaminato la connessione con il sensore di temperatura ed umidità
Mqtt con Beaglebone ed ESP8266-MQTT su ESP8266 per sensore temperatura
in questo articolo si imposta l’esp8266 per gestire un relé. Di seguito i collegamenti utilizzati
Anche in questo esempio metteremo il nodemcu in sleep. Perché il sistema possa riavviarsi dopo il comando di sleep è necessario collegare il PIN D0 (GPIO16) al PIN RST come specificato nella documentazione nodemcu
Passiamo ad analizzare il codice da caricare sul dispositivo; consta del init.lua e del file relay.lua.
Analizziamo i punti principali di ciascun file.
- init.lua
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
-- init.lua SSID="SSID" -- SSID PASSWORD="password" -- SSID password TIMEOUT=10000 -- timeout to check the network status EXSENSOR1="relay.lua" -- module to run MQTTSERVER="mqttaddress" -- mqtt broker address MQTTPORT="1883" -- mqtt broker port MQTTQOS="0" -- qos used -- Set the netowrk parameters and get ip address wifi.setmode(wifi.STATION) wifi.sta.config(SSID, PASSWORD) wifi.sta.getip() -- Check the network status after the TIMEOUT interval tmr.alarm(0, TIMEOUT,tmr.ALARM_SINGLE, function() status=wifi.sta.status() -- if esp8266 got the ip if (status == wifi.STA_GOTIP ) then dofile(EXSENSOR1) else -- Without up restart the node print("Error acquiring ip address=",status) node.restart() end end ) |
Nel file sono impostate globalmente i parametri dell’access point, l’indirizzo e porta del broker mqtt ed il file da eseguire sull’esp dopo la connessione all’access point. Nel caso non riesca ad acquisire l’indirizzo il sistema viene fatto ripartire.
- relay.lua
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
-- relay.lua CNAME="relay1" -- Client name AMBIENT="home" -- Ambient name STOPIC = AMBIENT.."/"..CNAME.."/status" -- Status topic CTOPIC = AMBIENT.."/"..CNAME.."/command" --Command topic MTOPIC = AMBIENT.."/"..CNAME.."/monitor" --Monitor topic sleep_in_seconds= nil --Sleep in seconds TUPDATE = 20 -- Time interval for update monitor in seconds RPIN =1 -- Relay Pin TGEN = 5 -- Time interval in seconds usend in various trm gpio.mode(RPIN,gpio.OUTPUT) -- Set Relay pin mode gpio.write(RPIN,gpio.LOW) -- Default output is OFF -- Define Client Name and connection parameters mqt = mqtt.Client(CNAME, 60, "", "", 1) -- Subscribe to the topics and send status to the status topic mqt:on("connect", function() mqt:subscribe(STOPIC, MQTTQOS) mqt:subscribe(CTOPIC, MQTTQOS) mqt:subscribe(MTOPIC, MQTTQOS) send_status("Relay ready again") end ) -- Callback when mqtt is offline mqt:on("offline", function() print("mqtt offline"); end) --Callback to manage messages mqt:on("message", function(client, topic, data) manage_message(client, topic, data) end) -- Function to filter the messages function manage_message(client, topic, data) -- If CTOPIC: command topic if topic == CTOPIC then -- If the command is Restart if data == "Restart" then send_status("Restarting") send_mstatus("Restarting") tmr.alarm(2, TGEN*1000, tmr.ALARM_SINGLE, node.restart) -- If the command is null elseif data == nil then send_status("Command not valid") -- Command to turn on the Relay elseif data == "ON" then relay_on() -- Command to turn off the Relay elseif data == "OFF" then relay_off() -- Command to check the state of the Relay elseif data == "Status" then relay_status() else -- Check the Sleep command i=string.len(data) dato=string.sub(data,1,5) if dato == "Sleep" then ndato=string.sub(data,6) TSLEEP=tonumber(ndato) if TSLEEP ~= nil then if TSLEEP >= 1 then -- Sets the interval in seconds to sleep sleep_in_seconds=TSLEEP*60 esp_sleep() else -- Send an error in Sleep command send_status("Sleep command error") end else -- Send an error in Sleep command send_status("Sleep command error") end else -- Command not managed send_status("Command not valid") end end end end -- Function to turn on the relay function relay_on() gpio.write(RPIN,gpio.HIGH) send_status("Relay ON") end -- Function to turn off the relay function relay_off() gpio.write(RPIN,gpio.LOW) send_status("Relay OFF") end -- Function to check if the the relay is on or off function relay_status() risultato=gpio.read(RPIN) if(risultato==1) then send_status("Relay ON") elseif(risultato==0) then send_status("Relay OFF") end end -- Function to send messages to to STOPIC: status topic function send_status(message) mqt:publish(STOPIC, message, MQTTQOS, 0) end -- Fucntion to send messages to MTOPIC: monitor topic function send_mstatus(message) mqt:publish(MTOPIC, message, MQTTQOS, 0) end -- Function that sleeps esp8266 function esp_sleep() send_status("Relay going to sleep now") send_mstatus("Relay going to sleep now") -- Delayd sleep to send the message above. tmr.alarm(3, TGEN*1000, tmr.ALARM_SINGLE, function() node.dsleep(sleep_in_seconds*1000000) end) end -- Connection to the mqtt server at the msttport mqt:connect(MQTTSERVER, MQTTPORT, 0, 1) -- Send periodically a message to the monitor topic tmr.alarm(4, TUPDATE*1000, tmr.ALARM_AUTO, function() send_mstatus("Relay Ready") end) |
Il file relay.lua gestisce il relay, la sottoscrizione sul broker mqtt con invio dello stato con il topic monitor. Sono stati impostati dei parametri globali per gestire i topic, i tempi di attesa in tmr.alarm. Sono presenti in particolare i seguenti topic
- /home/relay1/status : viene inviato lo stato del relé all’avvio ed in risposta dei comandi nel topic /home/relay1/command.
- /home/relay1/monitor: periodicamente viene inviato lo stato del relé.
- /home/relay1/command: Da altri client è possibile inviare dei comandi all’esp8266.
- Restart: riavvia l’esp.
- Sleep n: Tempo di sleep di n secondi.
- ON: Accende il relay.
- OFF: Spegne il relay.
- Status: Richiede lo stato del relé (Acceso o Spento).
Vi sono varie funzioni di servizio che gesticono il tutto. I commenti all’interno del codice descrivono ciascuna funzionalità.
Nel prossimo articolo tratteremo la configurazione del webserver sul Beaglebone e la configurazione del websocket per Mosquitto sempre sul Beaglebone
Mqtt con Beaglebone ed ESP8266-Web server e Mqtt Websocket