Raspberry Pi Pico is only $4, IoT IoT development practice

The Raspberry Pi Foundation has announced the first microcontroller-class product based on a brand new RP2040 chip: the Raspberry Pi Pico , priced at just $4 .

If you have used Arduino or a development board that supports MicroPython , it will be easy to get started with Raspberry Pi Pico, and you can quickly build IoT applications.

  Pi Pico Development Board   

Raspberry Pi Pico Specifications:


  Using MicroPython in Pico  

Install MicroPython

You can program the Pico by connecting the Pico to the PC via USB, then dragging and dropping the program files onto the Pico. The installation steps are as follows:

1. Download MicroPython's UF2 file through the button below.

2. Press and hold the BOOTSEL button on the Pico development board, then insert the Pico into the USB port of the Raspberry Pi or PC, and release the BOOTSEL button.

3. The Pico will be recognized as a mass storage device.

4. Put the downloaded MicroPython UF2 file on the RPI-RP2 volume. Your Pico will reboot automatically, and MicroPython will start running.

  IoT development  

1. Cloud development

We are on the device management page of the IoT console , we need to create a product Pico temperature and humidity meter first, the data communication is in JSON format , the authentication method is the device key , and the function defines the additive model attribute temperature and humidity . As shown below:

On the device management page, based on the Pico temperature and humidity meter product, we register a device and obtain the triplet of device identity authentication . As shown below:

2. Device-side development

Install umqtt via command line

>>> import upip
>>> upip.install('micropython-umqtt.simple')
Installing to: /lib/
Installing micropython-umqtt.simple 1.3.4 from https://files.pythonhosted.org/packages/bd/cf/697e3418b2f44222b3e848078b1e33ee76aedca9b6c2430ca1b1aec1ce1d/micropython-umqtt.simple-1.3.4.tar.gz

Use the device triplet to establish an MQTT connection

import utime
from umqtt.simple import MQTTClient
import ujson

product_key = '产品productKey'
device_name = '设备deviceName'
device_secret = '设备deviceSecret'

client_id = 'pico'
sign_method = 'hmacsha1'

mqtt_client_id = '{0}|securemode=3,signmethod={1}|'.format(client_id,sign_method)
username = '{0}&{1}'.format(device_name, product_key)
password = get_password(client_id,product_key,device_name,device_secret,sign_method)

broker_address = '{0}.iot-as-mqtt.cn-shanghai.aliyuncs.com'.format(product_key)
broker_port = 1883

client = MQTTClient(client_id=mqtt_client_id, 
        server=broker_address, 
        port=broker_port, 
        user=username, 
        password=password, 
        keepalive=300)

client.set_callback(callback)
client.connect()

The device actively reports the object model data:

publish_topic = '/sys/{0}/{1}/thing/event/property/post'.format(product_key, device_name)
data = {
        'id': '13548753493',
        'version': '1.0',
        'params': {
            'temperature': 21,
            'humidity': 68
        }
    }
message = ujson.dumps(data)
client.publish(publish_topic, message)

3. Online operation

After burning the Python program, we can see that the device status is online , and the latest reported temperature and humidity values ​​can be seen in the physical model data .

In the log service of monitoring operation and maintenance , you can also see the log of the data reported by the device. As shown below:

Guess you like

Origin blog.csdn.net/klandor2008/article/details/113211005