TPYBoard development board construction and Alibaba Cloud service sending data
[Copy link]
Today I will share with you my experience of using a test project on the TPYBoard V202 development board. The test project is to send hardware underlying data to the server, and the data includes product name, WF module MAC address, temperature and humidity data. What isMicroPython? MicroPython is Python that can run on a microcontroller, that is, you can use the Python script language to develop microcontroller programs. MicroPython is open source in compliance with the MIT protocol, and the licensee has the right to copy, modify, distribute and re-authorize. It was designed by George Daming, a theoretical physicist at Cambridge University. It is similar to Arduino, but MicroPython is more powerful. MicroPython is based on ANSIC, and its syntax is basically the same as Python3. It has its own parser, compiler, virtual machine, and class library. Currently, it supports 32-bit ARM processors, such as STM32F405. That is to say, Python language can be run directly on ARM processor STM32F405, and Python language can be used to control the microcontroller. TPYBoard is a MicroPython development board made by TurnipSmart based on MicroPython under the MIT license. It is based on the STM32F405 microcontroller and uses the USB interface for data transmission. The TPYBoard development board has four built-in LED lights and an acceleration sensor, and can work normally with a voltage between 3V and 10V. The TPYBoard development board allows users to easily control various peripherals of the microcontroller through Python code, such as LEDs, read pin voltages, play songs, and connect to other devices. TPYBoard development board supports direct running of Python 3.0 and above, supports gravity acceleration sensor, supports hundreds of peripheral accessories, and supports SWD firmware burning. Zero foundation can also flexibly master single-chip microcomputer technology! [font=微软雅黑,I encountered many problems during use, including the problem between Python3 version and Python2 version, the problem of flashing the development board firmware, and the problem of MAC parsing algorithm. To solve these problems, I almost annoyed the group owner and my friends. I would like to thank the official group owner and that kind friend. Then I won’t waste time and get straight to the point: This is my friend’s TPYBV202. The code is as follows: [font=微软雅黑, import dht import machine import network from machine import Pin import socket import urllib import time # Declare the class libraries used, especially the dht class library import json import sys import utime # Declare the functions used in the class library and set the port # parameters d = dht.DHT22(machine.Pin(4)) led = Pin(2, Pin.OUT) count = 0 def do_connect(): """Define the function for the development board to connect to the wireless network""" wlan = network.WLAN( network.STA_IF) # Set the network mode of the development board wlan.active(True) # Open the network connection if not wlan.isconnected(): # Determine whether there is a network connection print('connecting to network...') # Set the wireless network you want to connect to # #Line name and password wlan.connect('00', 'zzp6330058') while not wlan.isconnected(): # Waiting to connect to the wireless network pass MAC = wlan.config('mac') # Get MAC address #t = ntptime.time() #t = t + 28800 # Eight hours difference #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 to be sent""" data = { 'name': NA_ME, 'id': MA_C, 'time': TIM_E, 'temp': TEM_P, 'symbol': CUT_cf, 'hum': (HU_M + '%') } return data def DHT_collect(): """Temperature and humidity collection module""" d.measure() # Call the function of measuring data in the DHT library# Read temperature data in the measure() function temp = str(d.temperature()) hum = str(d.humidity()) # Read humidity data in the measure() function print('TEMP:' + temp + ' ' + 'HUM:' + hum) return temp, hum def To_obtain_name_time(): """Get the local name and local time""" time_Str = ''.join([str(i) for i in utime.localtime()]) return time_Str def Data_sent(host, port, data):"""Connect to the server to send data""" s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Create a socket s.connect((host, port)) # Actively initialize TCP server connection. . . json_str = json.dumps(data) # Convert the sent data into json data # Send TCP data and send the data in the string to the connected socket. s.send(bytes('%s\r\n' % (json_str), 'utf8')) s.close() # Close the socket def MAC_format_conversion(MA): """MAC format conversion""" MA = [hex(x) for x in bytes(MA)] #Convert the data into hexadecimal and save it in the list Y=0#loop initial value MB =''#stringwhile Y<6: if len(MA[Y])==4: #Judge whether there is a zero pass else: MA[Y] = MA[Y]+'0' if Y<5: #Reconstruct the list 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() # Connect WF MAC = MAC_format_conversion(MAC) print(MAC) while True: """Main loop""" TIME = 0 #local time# MAC = get_mac_address()#Get MAC TEM, HUM = DHT_collect() #Get temperature and humiditydata = connection("TPYBoard v202", MAC, TIME, TEM, 0, HUM) #Create data dictionaryData_sent("119.23.223.146", 2689, data) count += 1 print('Count:', count) #Show the number of times senttime.sleep(15)[/code] Attached is the display effect: This is the data returned by the development board. This is the json data received by the server. MB = MB + MA[Y][2]+MA[Y][3] Y+=1 return MB MAC = do_connect() # Connect to WF MAC = MAC_format_conversion(MAC) print(MAC) while True: """Main loop""" TIME = 0 #Local time# MAC = get_mac_address()#Get MAC TEM, HUM = DHT_collect() # Get temperature and humidity data = connection("TPYBoard v202", MAC, TIME, TEM, 0, HUM) # Create data dictionary Data_sent("119.23.223.146", 2689, data) count += 1 print('Count:', count) # Display the number of times sent time.sleep(15)[/code] Attached is the display effect: This is the data returned by the development board. This is the json data received by the server. MB = MB + MA[Y][2]+MA[Y][3] Y+=1 return MB MAC = do_connect() # Connect to WF MAC = MAC_format_conversion(MAC) print(MAC) while True: """Main loop""" TIME = 0 #Local time# MAC = get_mac_address()#Get MAC TEM, HUM = DHT_collect() # Get temperature and humidity data = connection("TPYBoard v202", MAC, TIME, TEM, 0, HUM) # Create data dictionary Data_sent("119.23.223.146", 2689, data) count += 1 print('Count:', count) # Display the number of times sent time.sleep(15)[/code] Attached is the display effect: This is the data returned by the development board. This is the json data received by the server.
|