前言
????????之前发布了一篇esp32采集数据发布到阿里云IOT平台的教程,有些同学觉得太复杂,那么接下来教大家一种更加简单的方法替代阿里云IOT让下位机和上位机进行数据交互。
一、本地搭建mqtt服务器(超简单)
网址链接:免费下载、试用 EMQ 产品
1.进入emqx官网,找到emqx开源版服务下载,选择你要安装的系统及版本,下载就行。
?2.启动mqtt服务。? ?windows版的启动命令 :? ./bin/emqx start
3.然后打开浏览器 进入 http://127.0.0.1:18083?Mqtt服务端管理后台。初始账号密码都是admin,在这里可以查看到有哪些用户接入订阅什么主题等信息。
?
?4.然后这就替代阿里云的IOT平台了,下位机esp32及上位机客户端都连接这个mqtt服务器进行数据传输。看下架构:
?
?
二、Esp32采集数据,转发到mqtt服务的具体实现(简单)
1.采集传感器数据上发
代码如下(示例):
from umqtt.simple import MQTTClient
from machine import ADC, Pin
import network
import time
import machine
import dht
import json
from machine import Timer
#wifi账号密码
SSID="dmx"
PASSWORD="dmx123456"
#mqtt服务器域名
SERVER ='8.134.117.41' #本地的填 127.0.0.1
#mqtt连接信息
CLIENT_ID = "1840707149" #连接id要唯一,不然会相互挤下线
username='dmx123'
password='dmx111'
publish_TOPIC = 'dmx/1840707149' #发布信息主题(上传数据)
subscribe_TOPIC ='dmx/sub123' #订阅主题(接收上位机的命令)
client=None
mydht=mylight=adc=rain=beep=led=None
beepmsg = []
def sub_cb(topic, msg):
print(json.loads(msg))
#print(json.loads(msg)['Led'])
if 'Led' in json.loads(msg):
if json.loads(msg)['Led'] == 1:
led.value(1)
if json.loads(msg)['Led'] == 0:
led.value(0)
if 'Beep' in json.loads(msg):
if json.loads(msg)['Beep'] == 1:
beep.value(1)
if json.loads(msg)['Beep'] == 0:
beep.value(0)
if 'L298N' in json.loads(msg):
if json.loads(msg)['L298N'] == 1:
IN2.value(1)
time.sleep(0.8)
IN2.value(0)
if json.loads(msg)['L298N'] == 0:
IN2.value(1)
time.sleep(0.8)
IN2.value(0)
def ConnectWifi(ssid,passwd): #连接wifi
global wlan
wlan=network.WLAN(network.STA_IF) #create a wlan object
wlan.active(True) #Activate the network interface
#wlan.disconnect() #Disconnect the last connected WiFi
wlan.connect(ssid,passwd) #connect wifi
while not wlan.isconnected():
pass
print(wlan.ifconfig())
def apptimerevent(mytimer):
try:
wsData=ReadTemHum()
Light=ReadLight()
Led=led.value()
Beep=beep.value()
#rain = Pin(23,Pin.IN)
print(ReadRain())
Rain=ReadRain()
#mymessage='{"data": {"name":%d,"temp": %d ,"hum": %d ,"light": %d,"led":%d,"beep":%d}, "method": "thing.event.property.post"}'%(1,wsData[0],wsData[1],Light,Led,Beep)
mymessage = {"data":{"name":1,"hum":wsData[1],"temp":wsData[0],"light":Light,"led":Led,"beep":Beep,"rain":Rain,"beep_msg":beepmsg,"L298N":0}}
client.publish(topic=publish_TOPIC,msg = json.dumps(mymessage), retain=False, qos=0)
except Exception as ex_results2:
print('exception',ex_results2)
mytimer.deinit()
def ReadTemHum(): #获取温湿度
mydht.measure()
tem=mydht.temperature()
hum=mydht.humidity()
data=[tem,hum]
print(data)
return data
def ReadLight():
adcData=mylight.read()
Lightvalu=10240000/(adcData*1.1)-2500 #adc转换
Light=round(Lightvalu) #取整
print(Light)
return Light
def ReadRain():
adc.atten(ADC.ATTN_11DB) #设置衰减比
adc.width(ADC.WIDTH_12BIT) #设置12位数据宽度
adcwww=adc.read()
#adcwww = ((3.3-adcwww*(3.3/4095))*100/3.3)
return(adcwww)
if __name__=='__main__':
try:
mydht=dht.DHT11(machine.Pin(4))
mylight=ADC(machine.Pin(34))
adc = ADC(Pin(35)) #设置IO35为模拟IO口
led = Pin(19, Pin.OUT, value=0)
EN = Pin(22,Pin.OUT,value = 1) #EN
IN1 = Pin(12,Pin.OUT, value=0) #IN1
IN2 = Pin(14,Pin.OUT,value=0) #IN2
beep=Pin(5,Pin.OUT,value=0)
rain = Pin(23, Pin.IN)
ConnectWifi(SSID,PASSWORD)
client = MQTTClient(CLIENT_ID, SERVER,0,username,password,60) #create a mqtt client
#print(client)
client.set_callback(sub_cb) #set callback
client.connect() #connect mqtt
client.subscribe(subscribe_TOPIC) #client subscribes to a topic
mytimer=Timer(0)
mytimer.init(mode=Timer.PERIODIC, period=3000,callback=apptimerevent) #初始化回调函数
while True:
client.wait_msg() #wait message
except Exception as ex_results:
print('exception1',ex_results)
finally:
if(client is not None):
client.disconnect()
#wlan.disconnect()
#wlan.active(False)
2.读入数据
????????上位机要读数据只需要连接到同一个mqtt服务端上,订阅esp32的发布主题就能拿到数据了。
|