lbenson
Senior Member
Here is code to run an MQTT node on a PICAXE 20M2, sending the data using an ESP-01 flashed with Annex RDS (https://sites.google.com/site/annexwifi/home). (See post 11 for 14M2 PCB.)
The 20M2 has a DS18B20 on pin B.7, an LED On B.0, and a pushbutton on pinB.3. I set up a mosquitto MQTT broker on a Raspberry Pi-ZW following this tutorial:
I set up mosquitto clients on another pi-zw and on a Dockstar running openwrt.
I use a topic of "broadcast", with the intention that each picaxe node will recognize a message for itself by matching a SERIN qualifier, like "MsgForA" (through "MsgForZ").
Sending from the Dockstar MQTT client of a message of "MsgForA1" turns on the LED; "MsgForA0" turns it off, as with this command:
mosquitto_pub -h 192.168.2.40 -t broadcast -m "MsgForA1"
When the button is depressed, the picaxe sends serial to the ESP-01 and that sends a message, e.g., FromA=B (indicating the button was pressed). When the temperature changes, "FromA=T:25" is sent.
Here is the Annex code on the ESP-01:
Here's a photo:
Eaglecad files for the PCB for this setup are here--zip file may be sent directly to fab (e.g., JLCPCB.com) or .brd file can be sent to oshpark.com.
Code:
'20mqtt
#picaxe 20M2
#no_data
#terminal 4800
symbol baud=t2400_4
symbol pLED=B.0
symbol action=b4
symbol temp=b5
symbol lasttemp=b6
pullup %00001000 ' pullup on b.3
'setfreq M8
pause 2000
readtemp B.7,b1
sertxd(ppp_filename," ", ppp_datetime," starting; temp = ",#b1,cr,lf)
do
serin [1000,tempread],b.6,baud,("MsgForA"),action
select action
case "0": low pLED
case "1": high pLED
endselect
tempread:
readtemp B.7,temp
if lasttemp <> temp then
lasttemp = temp
sertxd(#temp," ")
serout C.0,baud,("T ",#temp,13,10) ' temperature changed message
endif
if pinB.3 = 0 then
pause 100 ' debounce
bit0 = 1-bit0 ' toggle bit
do while pinB.3=0: loop
serout C.0,baud,("B 1",13,10) ' button depressed message
endif
loop
do
if pinB.3 = 0 then
pause 100 ' debounce
bit0 = 1-bit0 ' toggle bit
do while pinB.3=0: loop
endif
if bit0=1 then: toggle B.0: else: low B.0: endif
pause 1000
readtemp B.7,temp
if b1 <> b2 then: b2 = b1: sertxd(#temp," "): endif
loop
Install Mosquitto Broker Raspberry Pi | Random Nerd Tutorials
This tutorial shows how to install the Mosquitto Broker for MQTT communication on a Raspberry Pi board.
randomnerdtutorials.com
I use a topic of "broadcast", with the intention that each picaxe node will recognize a message for itself by matching a SERIN qualifier, like "MsgForA" (through "MsgForZ").
Sending from the Dockstar MQTT client of a message of "MsgForA1" turns on the LED; "MsgForA0" turns it off, as with this command:
mosquitto_pub -h 192.168.2.40 -t broadcast -m "MsgForA1"
When the button is depressed, the picaxe sends serial to the ESP-01 and that sends a message, e.g., FromA=B (indicating the button was pressed). When the temperature changes, "FromA=T:25" is sent.
Here is the Annex code on the ESP-01:
Code:
' MQTT test program
serial2.mode 2400,1,3 ' set up serial on the Tx & Rx pins
wlog "Connecting to 192.168.2.40"
pause 1000
print "Setup: 2.40",mqtt.setup("192.168.2.40")
print "Connect: ",mqtt.connect("", "") ' No login / pass required
topic$="broadcast"
print "Subscribe: ",mqtt.subscribe(topic$) ' subscribe to the topic /AnnexTest
onmqtt mqtt_msg
onSerial2 ser2in
print "Ram ",ramfree
wlog "Ram ",ramfree
wait
ser2in:
pause 50 ' accept characters
in$=serial2.input$
code$=mid$(in$,1,1)
if code$="B" then
ret=mqtt.publish(topic$, "FromA=B")
endif
if code$="T" then
msg$="FromA=T:"+mid$(in$,3)
ret=mqtt.publish(topic$, msg$)
endif
return
' receive messages from the broker
mqtt_msg:
wlog "Topic/Message : "; mqtt.topic$;" "; mqtt.message$
if instr(mqtt.message$,"MsgForA") <> 0 then
print2 mqtt.message$ ' e.g., "MsgForA1" turns on LED; "MsgForA0" turns off LED
endif
Eaglecad files for the PCB for this setup are here--zip file may be sent directly to fab (e.g., JLCPCB.com) or .brd file can be sent to oshpark.com.
Attachments
-
14 KB Views: 11
Last edited: