Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I have to create a mini IoT Weather Station. I have 3 types of code that will be run in Visual Studio Code. First code:
I have to create a mini IoT Weather Station. I have types of code that will be run in Visual Studio Code.
First code: main.py
#MQTT subscribe and toggle LED
from machine import Pin,reset,uniqueid
from time import sleep
from ubinascii import hexlify
from umqttsimple import MQTTClient
from json import loads #change json data to python
clientid hexlifyuniqueid
CLIENTNAME 'MAAsubscriber'
BROKERADDR 'test.mosquitto.org'
apikey'syira'
topicf'gpiomsapikey
# mqttc MQTTClientCLIENTNAME, BROKERADDR, keepalive
mqttc MQTTClientclientid BROKERADDR, keepalive
mqttcconnect
def ambilgpiodatatopic msg:
try:
jsondatamsgdecode
pythondataloadsjsondata
printmsg
for data in pythondata:
statedatastate
gpionodatagpiono
ledPinintgpionoPin.OUT
if state:
led.on
printfgpio no gpiono is on
elif state:
led.off
printfgpio no gpiono is off"
except:
printerr there is some error"
# mqtt subscription
mqttcsetcallbackambilgpiodata
mqttcsubscribetopicencode
while True:
try:
mqttccheckmsg
print
except:
# printNetwork problem ke
reset
print
sleep
Second code: umqttsimple.py
try:
import usocket as socket
except:
import socket
import ustruct as struct
from ubinascii import hexlify
class MQTTExceptionException:
pass
class MQTTClient:
def initself clientid server, port userNone, passwordNone, keepalive
sslFalse, sslparams:
if port :
port if ssl else
self.clientid clientid
self.sock None
self.server server
self.port port
self.ssl ssl
self.sslparams sslparams
self.pid
self.cb None
self.user user
self.pswd password
self.keepalive keepalive
self.lwtopic None
self.lwmsg None
self.lwqos
self.lwretain False
def sendstrself s:
self.sock.writestructpackH lens
self.sock.writes
def recvlenself:
n
sh
while :
b self.sock.read
n b & xf sh
if not b & x:
return n
sh
def setcallbackself f:
self.cb f
def setlastwillself topic, msg retainFalse, qos:
assert qos
assert topic
self.lwtopic topic
self.lwmsg msg
self.lwqos qos
self.lwretain retain
def connectself cleansessionTrue:
self.sock socket.socket
addr socket.getaddrinfoselfserver, self.port
self.sock.connectaddr
if self.ssl:
import ussl
self.sock ussl.wrapsocketselfsock, selfsslparams
premsg bytearraybx
msg bytearraybxMQTTxx
sz lenselfclientid
msg cleansession
if self.user is not None:
sz lenselfuser lenselfpswd
msgxC
if self.keepalive:
assert self.keepalive
msg self.keepalive
msg self.keepalive & xFF
if self.lwtopic:
sz lenselflwtopic lenselflwmsg
msgxselflwqos & xselflwqos & x
msg self.lwretain
i
while sz xf:
premsgisz & xfx
sz
i
premsgi sz
self.sock.writepremsg i
self.sock.writemsg
#printhexlenmsg hexlifymsg:
self.sendstrselfclientid
if self.lwtopic:
self.sendstrselflwtopic
self.sendstrselflwmsg
if self.user is not None:
self.sendstrselfuser
self.sendstrselfpswd
resp self.sock.read
assert respx and respx
if resp:
raise MQTTExceptionresp
return resp &
def disconnectself:
self.sock.writebxe
self.sock.close
def pingself:
self.sock.writebxc
def publishself topic, msg retainFalse, qos:
pkt bytearraybx
pkt qos retain
sz lentopic l
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started