TPYBoard development board construction and Alibaba Cloud service sending data

   What I bring to you today is the experience of using a test project of the TPYBoard V202 development board. The test item is to send the underlying hardware data to the server, and the data includes the product name, the MAC address of the WF module, and the temperature and humidity data.

 

     What is MicroPython ?

    MicroPython is a Python that can run on a microcontroller, that is to say, you can use the Python script

This language develops microcontroller programs.

    MicroPython is open sourced under the MIT protocol, and the licensee has the right to copy, modify, distribute and sublicense

profit. Designed by theoretical physicist George Damien of the University of Cambridge. Similar to Arduino, but MicroPython

More powerful.

    MicroPython is based on ANSIC, the syntax is basically the same as Python3, and it has its own parser and compiler

, virtual machines and class libraries. Currently it supports 32-bit based ARM processors such as STM32F405. That is

It means that the ARM processor STM32F405 can run the Python language directly, and use the Python language to control the microcontroller.


 

What is TPYBoard ?

    TPYBoard is based on MicroPython under the MIT license and made by TurnipSmart.

This MicroPython development board is based on the STM32F405 microcontroller and transmits data through the USB interface. Should

The development board has 4 built-in LED lights and an acceleration sensor, which can work normally at voltages between 3V-10V.

    The TPYBoard development board allows users to easily control various peripherals of the microcontroller through Python code, such as

LEDs, etc., read pin voltages, play songs, network with other devices, etc.

    The TPYBoard development board supports the direct operation of Python 3.0 and above, supports the gravitational acceleration sensor,

Support hundreds of peripheral accessories, support SWD programming firmware. Zero foundation can also flexibly master single-chip technology!

 

I encountered many problems during use, including the Python3 version, and the python2 version

problem, the problem of flashing the firmware of the development board, and the problem of the MAC parsing algorithm. To solve these problems, the editor will soon

The group owner, I’m tired of asking my friends, and the editor is here to thank you, the official group owner, and that kind friend.

     Then I don't waste time and go straight to the topic:

     This is my friend's TPYBV202.

code show as below:

 

 
import dht

import machine

import network

from machine import Pin

import socket

import urllib

import time  # 声明用到的类库,尤其是dht的类库

import json

import sys

import utime




# 声明用到类库中的函数,并设端口                                                    #置参数
d = dht.DHT22(machine.Pin(4))

led = Pin(2, Pin.OUT)

count = 0


def do_connect():
    """定义开发板连接无线网络的函数"""
    wlan = network.WLAN(
        network.STA_IF)  # 设置开发板的网                                                                          #络模式

    wlan.active(True)  # 打开网络连接

    if not wlan.isconnected():  # 判断是否有网络连接

        print('connecting to network...')

        # 设置想要连接的无线网络
        # #线名称和密码
        wlan.connect('00', 'zzp6330058')

        while not wlan.isconnected():  # 等待连接上无线网络

            pass
    MAC = wlan.config('mac')      # 获得MAC地址
    #t = ntptime.time()
    #t = t + 28800  # 相差八小时
    #tm = tm[0:3] + (0,) + tm[3:6] + (0,)
    #machine.RTC().datetime(tm)
    print('network config:', wlan.ifconfig())
    return MAC


def connection(NA_ME, MA_C, TIM_E, TEM_P, CUT_cf, HU_M):
    """要发送的数据"""
    data = {
        'name': NA_ME,
        'id': MA_C,
        'time': TIM_E,
        'temp': TEM_P,
        'symbol': CUT_cf,
        'hum': (HU_M + '%')
    }
    return data


def DHT_collect():
    """温湿度采集模块"""
    d.measure()  # 调用DHT类库中测量数据的函数
    # 读取measure()函数中的温度数据
    temp = str(d.temperature())
    hum = str(d.humidity())  # 读取measure()函数中的湿度数据
    print('TEMP:' + temp + ' ' + 'HUM:' + hum)
    return temp, hum


def To_obtain_name_time():
    """获取本机名称,本地时间"""
    time_Str = ''.join([str(i) for i in utime.localtime()])
    return time_Str


def Data_sent(host, port, data):
    """连接服务器发送数据"""
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  # 创建套接字
    s.connect((host, port))  # 主动初始化TCP服务器连接。。。

    json_str = json.dumps(data)  # 将发送的数据转换成json数据
    # 发送TCP数据,将string中的数据发送到连接的套接字。
    s.send(bytes('%s\r\n' % (json_str), 'utf8'))

    s.close()  # 关闭套接字

def MAC_format_conversion(MA):
         """MAC格式换算"""
         MA = [hex(x) for x in bytes(MA)] #将数据转换成16进制保存到列表中
         Y=0#循环初值
         MB =''#字符串

         while Y<6:
                   if len(MA[Y])==4: #判断是否有零
                            pass
                   else:
                            MA[Y] = MA[Y]+'0'
                   if Y<5: #对列表重构5c:cf:7f:d0:85:65
                            MB = MB + MA[Y][2]+MA[Y][3]+':'
                   else:
                            MB = MB + MA[Y][2]+MA[Y][3]
                   Y+=1

         return MB


MAC = do_connect()  # 连接WF
MAC = MAC_format_conversion(MAC)
print(MAC)

while True:
    """主循环"""
    TIME = 0  #本地时间
    # MAC = get_mac_address()#获取MAC
    TEM, HUM = DHT_collect()  # 获取温湿度
    data = connection("TPYBoard v202", MAC,
                      TIME, TEM, 0, HUM)  # 建立数据字典

    Data_sent("119.23.223.146", 2689, data)

    count += 1
    print('Count:', count)  # 显示发送的次数

    time.sleep(15)

 

Attach the displayed effect: this is the data returned by the development board.

 

This is the json data received by the server.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325132381&siteId=291194637