本帖最后由 云天 于 2022-3-26 21:43 编辑
【项目背景】
将由传感器获取的室内信息,通过屏幕显示。如温湿度、光照强度、噪音分贝等。
主板使用LattePanda配合7英寸触摸屏与10英寸IPS高分辨率显示屏,通过阿里云IOT,与笔记本电脑可同步显示。
【LattePanda 拿铁熊猫 Win10 开发板】
LattePanda是市面上首款支持完整Windows10操作系统的单板计算机。此版LattePanda预装Windows 10企业版LTSB操作系统,附带正版激活码,专为工业领域应用设计。LattePanda板载Intel Cherry Trail Z8350四核处理器,极小的体积却有强悍的性能。拥有USB2.0、USB3.0、3.5mm音频、HDMI等常用PC接口,自带蓝牙、WIFI模块,给您带来无比便捷的连接体验。LattePanda板载Arduino芯片,兼容数千种传感器及外设,无缝掌控物理世界。
【硬件组装】
【开机】
【登陆DF论坛】
【传感器连接电路图】
【阿里云 IOT配置】
1、新建项目:https://studio.iot.aliyun.com/projects
2、新建WEB应用:https://studio.iot.aliyun.com/p/a123NLToMdCVQRNF/project/detail
3、新建大屏
4、大屏组件样式调整:https://studio.iot.aliyun.com/we ... 20CTqKwdWuj8tF/edit
大屏美化
5、创建产品
6、添加设备
7、获取Python程序的参数:https://studio.iot.aliyun.com/project/a123NLToMdCVQRNF/devices/a1RIQsYJqq8/LattePanda__class/1
options = {
'productKey':'你的productKey',
'deviceName':'你的deviceName',
'deviceSecret':'你的deviceSecret',
'regionId':'cn-shanghai'
}
8、功能定义:https://iot.console.aliyun.com/p ... RIQsYJqq8?current=8
9、数据源
10、设备模拟器:https://studio.iot.aliyun.com/pr ... LattePanda__class/1
11、大屏预览
13、Python设备接入:https://www.yuque.com/cloud-dev/iot-tech/rz6fpl?spm=a2c6h.12873639.0.0.30bcb47frAT3ro
测试程序
-
- # -*- coding: utf-8 -*-
- import paho.mqtt.client as mqtt
- import time
- import hashlib
- import hmac
- import random
- import json
-
- options = {
- 'productKey':'a1RIQsYJqq8',
- 'deviceName':'LattePanda__class',
- 'deviceSecret':'c86bdcbdee4feec23ef6db01af0898e0',
- 'regionId':'cn-shanghai'
- }
- HOST = options['productKey'] + '.iot-as-mqtt.'+options['regionId']+'.aliyuncs.com'
- PORT = 1883
- PUB_TOPIC = "/sys/" + options['productKey'] + "/" + options['deviceName'] + "/thing/event/property/post";
-
-
- # The callback for when the client receives a CONNACK response from the server.
- def on_connect(client, userdata, flags, rc):
- print("Connected with result code "+str(rc))
- # client.subscribe("the/topic")
-
- # The callback for when a PUBLISH message is received from the server.
- def on_message(client, userdata, msg):
- print(msg.topic+" "+str(msg.payload))
-
- def hmacsha1(key, msg):
- return hmac.new(key.encode(), msg.encode(), hashlib.sha1).hexdigest()
-
- def getAliyunIoTClient():
- timestamp = str(int(time.time()))
- CLIENT_ID = "paho.py|securemode=3,signmethod=hmacsha1,timestamp="+timestamp+"|"
- CONTENT_STR_FORMAT = "clientIdpaho.pydeviceName"+options['deviceName']+"productKey"+options['productKey']+"timestamp"+timestamp
- # set username/password.
- USER_NAME = options['deviceName']+"&"+options['productKey']
- PWD = hmacsha1(options['deviceSecret'],CONTENT_STR_FORMAT)
- client = mqtt.Client(client_id=CLIENT_ID, clean_session=False)
- client.username_pw_set(USER_NAME, PWD)
- return client
-
-
- if __name__ == '__main__':
-
- client = getAliyunIoTClient()
- client.on_connect = on_connect
- client.on_message = on_message
-
- client.connect(HOST, 1883, 300)
- client.loop_start()
- while True:
- time.sleep(5)
- payload_json = {
- 'id': int(time.time()),
- 'params': {
- 'temperature': random.randint(20, 30),
- 'notice': "测试信息"+str(random.randint(40, 50)),
- 'SoundDecibelValue':random.randint(10, 60),
- 'LightLux':random.randint(10, 60),
- 'Humidity':random.randint(10, 60)/100
- },
- 'method': "thing.event.property.post"
- }
- print('send data to iot server: ' + str(payload_json))
-
- client.publish(PUB_TOPIC,payload=str(payload_json),qos=1)
-
复制代码
14、WEB应用预览
【编写Arduino程序】
- #include <dht11.h>
- dht11 DHT;
- #define DHT11_PIN 9
- String str1;
- void setup()
- {
- Serial.begin(115200);
-
- pinMode(DHT11_PIN, INPUT);
- }
-
- void loop()
- { int chk;
- //Serial.print("DHT11, \t");
- chk = DHT.read(DHT11_PIN); // READ DATA
- int light=analogRead(A0);
- int sound=analogRead(A2);
- switch (chk){
- case DHTLIB_OK:
- //Serial.print("OK,\t");
- break;
- case DHTLIB_ERROR_CHECKSUM:
- //Serial.print("Checksum error,\t");
- break;
- case DHTLIB_ERROR_TIMEOUT:
- //Serial.print("Time out error,\t");
- break;
- default:
- //Serial.print("Unknown error,\t");
- break;
- }
- // DISPLAT DATA
- //Serial.write();
- str1=String(DHT.humidity);
- str1.concat(",");
- str1.concat(String(DHT.temperature));
- str1.concat(",");
- str1.concat(String(sound));
- str1.concat(",");
- str1.concat(String(light));
-
- Serial.println(str1);
- delay(3000);
-
-
-
- }
复制代码
【编写Python程序】
-
- import serial
-
- # -*- coding: utf-8 -*-
- import paho.mqtt.client as mqtt
- import time
- import hashlib
- import hmac
- import random
- import json
-
- ser = serial.Serial('COM4', 115200, timeout=0)
- options = {
- 'productKey':'a1RIQsYJqq8',
- 'deviceName':'LattePanda__class',
- 'deviceSecret':'c86bdcbdee4feec23ef6db01af0898e0',
- 'regionId':'cn-shanghai'
- }
- HOST = options['productKey'] + '.iot-as-mqtt.'+options['regionId']+'.aliyuncs.com'
- PORT = 1883
- PUB_TOPIC = "/sys/" + options['productKey'] + "/" + options['deviceName'] + "/thing/event/property/post";
-
-
- # The callback for when the client receives a CONNACK response from the server.
- def on_connect(client, userdata, flags, rc):
- print("Connected with result code "+str(rc))
- # client.subscribe("the/topic")
-
- # The callback for when a PUBLISH message is received from the server.
- def on_message(client, userdata, msg):
- print(msg.topic+" "+str(msg.payload))
-
- def hmacsha1(key, msg):
- return hmac.new(key.encode(), msg.encode(), hashlib.sha1).hexdigest()
-
- def getAliyunIoTClient():
- timestamp = str(int(time.time()))
- CLIENT_ID = "paho.py|securemode=3,signmethod=hmacsha1,timestamp="+timestamp+"|"
- CONTENT_STR_FORMAT = "clientIdpaho.pydeviceName"+options['deviceName']+"productKey"+options['productKey']+"timestamp"+timestamp
- # set username/password.
- USER_NAME = options['deviceName']+"&"+options['productKey']
- PWD = hmacsha1(options['deviceSecret'],CONTENT_STR_FORMAT)
- client = mqtt.Client(client_id=CLIENT_ID, clean_session=False)
- client.username_pw_set(USER_NAME, PWD)
- return client
-
-
- if __name__ == '__main__':
-
- client = getAliyunIoTClient()
- client.on_connect = on_connect
- client.on_message = on_message
-
- client.connect(HOST, 1883, 300)
- client.loop_start()
- while True:
- time.sleep(2)
- count = ser.inWaiting()
- if count > 0:
- data = ser.read(count)
- if data != b'':
- data=str(data,"utf-8")
- data=data.replace("\r\n","")
- data=data.split(",")
- payload_json = {
- 'id': int(time.time()),
- 'params': {
- 'temperature': float(data[1]),
- 'notice': "测试信息"+str(random.randint(40, 50)),
- 'SoundDecibelValue':int(data[2]),
- 'LightLux':int(data[3]),
- 'Humidity':float(data[0])/100
- },
- 'method': "thing.event.property.post"
- }
- print('send data to iot server: ' + str(payload_json))
-
- client.publish(PUB_TOPIC,payload=str(payload_json),qos=1)
-
-
-
-
-
复制代码
【显示数据】
【演示视频】
|