1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| from mpython import *
import network
my_wifi = wifi()
my_wifi.connectWiFi('Erised', 'yiruosi88')
import ntptime
from umqtt.simple import MQTTClient
from machine import Timer
import machine
import ubinascii
def OneNET_recv(_msg):pass _OneNET_msg_list = [] def OneNET_callback(_topic, _msg): global _OneNET_msg_list try: _msg = _msg.decode('utf-8', 'ignore') except: print(_msg);return OneNET_recv(_msg) if _msg in _OneNET_msg_list: eval('OneNET_recv_' + bytes.decode(ubinascii.hexlify(_msg)) + '()')
tim14 = Timer(14)
_iot_count = 0 def timer14_tick(_): global _iot, _iot_count _iot_count = _iot_count + 1 if _iot_count == 1000: _iot.ping(); _iot_count = 0 try: _iot.check_msg() except: machine.reset()
_iot = None def OneNET_setup(): global _iot _iot = MQTTClient('408137', '183.230.40.39', 6002, '692155207', 'CoOilgoe0bL3oACmQqFj8VZPgB8=', keepalive=300) _iot.set_callback(OneNET_callback) if 1 == _iot.connect(): print('Successfully connected to MQTT server.') tim14.init(period=200, mode=Timer.PERIODIC, callback=timer14_tick)
import json
def pubdata(_dic): print(_dic) _list = [] for _key in list(_dic.keys()): _d = {'id':_key,'datapoints':[{'value':_dic[_key]}]} _list.append(_d) _data = {'datastreams': _list} j_d = json.dumps(_data) j_l = len(j_d) arr = bytearray(j_l + 3) arr[0] = 1 arr[1] = int(j_l / 256) arr[2] = j_l % 256 arr[3:] = j_d.encode('ascii') return arr
import ujson
import time
def OneNET_recv(_msg): global x, j, i, p_state, y_state, w1, t_state, w2, h_sate, w3, o_state, tempoure, my_dict, temp my_dict = ujson.loads(_msg) temp = my_dict.get('speed') print(temp) if temp == 0: rgb.fill( (0, 0, 0) ) rgb.write() time.sleep_ms(1) elif temp == 1: rgb.fill((int(255), int(0), int(0))) rgb.write() time.sleep_ms(1) ntptime.settime(8, "time.windows.com") OneNET_setup() _iot.publish('$dp', pubdata({'speed':1})) while True: pass
|