hello-iot

iot,internet of things

环境搭建

使用真实环境
Single-board computer - Raspberry Pi
Arduino - Wio Terminal
或者虚拟环境
Single-board computer - Virtual device
个人使用了虚拟环境

pip install CounterFit
pip install counterfit-connection
pip install counterfit-shims-grove
//启动虚拟环境
counterfit

lesson1

sensor

Sensors are either analog or digital.
传感器,模拟和数字两种,是用来从物理世界收集信息的器件。
通过虚拟环境启动一个传感器。

counterfit
//然后打开浏览器访问127.0.0.1:5000,也可以通过-port指定端口


类型选择light,units选择nounits,pin选1
创建后如下所示

测试代码

import time
from counterfit_connection import CounterFitConnection
from counterfit_shims_grove.grove_light_sensor_v1_2 import GroveLightSensor

CounterFitConnection.init('127.0.0.1', 5000)
light_sensor = GroveLightSensor(0)

while True:
    light = light_sensor.light
    print('Light level:', light)
    time.sleep(1)

运行,并在创建的传感器那里设置数值,结果如下

$ python3 ./app.py 
Light level:500
Light level:500
Light level:500
Light level:500
Light level:500
Light level:500
Light level:800
Light level:800
Light level:800
Light level:800
Light level:100
Light level:100
Light level:100
Light level:100
Light level:100

actuator

they convert an electrical signal from your IoT device into an interaction with the physical world such as emitting light or sound, or moving a motor.
从虚拟环境启动actuator

测试代码

import time
from counterfit_connection import CounterFitConnection
from counterfit_shims_grove.grove_led import GroveLed
from counterfit_shims_grove.grove_light_sensor_v1_2 import GroveLightSensor

CounterFitConnection.init("127.0.0.1", 5000)

led = GroveLed(5)
sensor = GroveLightSensor(0)

while(1):
    light = sensor.light
    print('Light level:', light)
    if light < 500 :
        led.on()
    else:
        led.off()

    time.sleep(1)

通过在传感器设置不同的值,可以控制led的打开与关闭

通信协议

通信模型

MQTT 是物联网设备最流行的通信协议,本课程将对此进行介绍。其他协议包括 AMQP 和 HTTPS。
将虚拟设备连接到MQTT代理,使用公开的MQTT代理https://test.mosquitto.org/
首先安装MQTT

pip3 install paho-mqtt

测试代码

import time
from counterfit_connection import CounterFitConnection
from counterfit_shims_grove.grove_light_sensor_v1_2 import GroveLightSensor

CounterFitConnection.init('127.0.0.1', 5000)
light_sensor = GroveLightSensor(0)

#mqtt相关
id = '<ID>' #因为代理是公开的所以需要一个唯一的标识符来分别,可以去生成一个唯一的uid在这里使用
client_name = id + 'nightlight_client'
import paho.mqtt.client as mqtt
mqtt_client = mqtt.Client(client_name)
mqtt_client.connect('test.mosquitto.org')
mqtt_client.loop_start()
print("MQTT connected!")

while True:
    light = light_sensor.light
    print('Light level:', light)
    
    time.sleep(1)

iot设备向MQTT发送数据

数据以json格式发送
测试代码

import time
import json
import paho.mqtt.client as mqtt
from counterfit_connection import CounterFitConnection
from counterfit_shims_grove.grove_light_sensor_v1_2 import GroveLightSensor

CounterFitConnection.init('127.0.0.1', 5000)
light_sensor = GroveLightSensor(0)


id = '<0a82aeb8-7817-4640-81ef-b6bcc667f242>'
client_telemetry_topic = id + '/telemetry'
client_name = id + 'nightlight_client'

mqtt_client = mqtt.Client(client_name)
mqtt_client.connect('test.mosquitto.org')
mqtt_client.loop_start()
print("MQTT connected!")


while True:
    light = light_sensor.light
    telemetry = json.dumps({'light' : light})
    print('send Light level:', light)
    mqtt_client.publish(client_telemetry_topic, telemetry)

    time.sleep(1)

从MQTT代理接收遥测数据

效果如下

测试代码

import json
import time

import paho.mqtt.client as mqtt

id = '<ID>'

client_telemetry_topic = id + '/telemetry'
client_name = id + 'nightlight_server'

mqtt_client = mqtt.Client(client_name)
mqtt_client.connect('test.mosquitto.org')

mqtt_client.loop_start()

def handle_telemetry(client, userdata, message):
    payload = json.loads(message.payload.decode())
    print("Message received:", payload)

mqtt_client.subscribe(client_telemetry_topic)
mqtt_client.on_message = handle_telemetry

while True:
    time.sleep(2)

在实际使用时要考虑到多久发送一次或者接受一次数据比较合适,还要考虑如果因为网络原因数据丢失时应怎样处理

MQTT代理向IOT设备发送命令

测试代码

import json
import time

import paho.mqtt.client as mqtt

id = '<ID>'

client_telemetry_topic = id + '/telemetry'
server_command_topic = id + '/commands'
client_name = id + 'nightlight_server'

mqtt_client = mqtt.Client(client_name)
mqtt_client.connect('test.mosquitto.org')

mqtt_client.loop_start()

def handle_telemetry(client, userdata, message):
    payload = json.loads(message.payload.decode())
    print("Message received:", payload)

    command = { 'led_on' : payload['light'] < 300 }
    print("Sending message:", command)

    client.publish(server_command_topic, json.dumps(command))

mqtt_client.subscribe(client_telemetry_topic)
mqtt_client.on_message = handle_telemetry

while True:
    time.sleep(2)

IOT设备处理MQTT发送的命令

测试代码

import time
from counterfit_connection import CounterFitConnection
from counterfit_shims_grove.grove_light_sensor_v1_2 import GroveLightSensor
from counterfit_shims_grove.grove_led import GroveLed
import json
import paho.mqtt.client as mqtt

CounterFitConnection.init('127.0.0.1', 5000)

light_sensor = GroveLightSensor(0)
led = GroveLed(5)

id = '<ID>'

client_telemetry_topic = id + '/telemetry'
server_command_topic = id + '/commands'
client_name = id + 'nightlight_client'

mqtt_client = mqtt.Client(client_name)
mqtt_client.connect('test.mosquitto.org')

mqtt_client.loop_start()

def handle_command(client, userdata, message):
    payload = json.loads(message.payload.decode())
    print("Message received:", payload)

    if payload['led_on']:
        led.on()
    else:
        led.off()

mqtt_client.subscribe(server_command_topic)
mqtt_client.on_message = handle_command

while True:
    light = light_sensor.light
    print('Light level:', light)

    mqtt_client.publish(client_telemetry_topic, json.dumps({'light' : light}))
    time.sleep(3)

这里sleep(3)是因为网络的原因,两端消息不太同步,所以放慢了一点
测试效果如下

posted @ 2022-10-07 23:18  岁云暮  阅读(111)  评论(0编辑  收藏  举报