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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
| from machine import Timer,Pin,RTC import socket from emp_wifi import Wifi import time import esp32 import usocket
import socket,re,sys,os import urequests as request
import machine, ssd1306, gfx
hea = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36'} rtc = RTC()
port = 10000 listenSocket = None
import network
WIFI = Pin(17, Pin.OUT, value=0) LED_1 = Pin(12, Pin.OUT, value=1) LED_2 = Pin(14, Pin.OUT, value=1) LED_3 = Pin(27, Pin.OUT, value=1) LED_4 = Pin(26, Pin.OUT, value=1) LED_5 = Pin(25, Pin.OUT, value=1) LED_6 = Pin(33, Pin.OUT, value=1) LED_7 = Pin(32, Pin.OUT, value=1) L3 = Pin(13, Pin.OUT, value=1)
def get_time_pa(): time_api_url = """http://quan.suning.com/getSysTime.do""" r = request.get(time_api_url, headers = hea) content = r.text print('爬取时间...') time = re.search( r'{"sysTime2":"(.*?)-(.*?)-(.*?) (.*?):(.*?):(.*?)","sysTime1":"', content ) print(r.text[13:29]) print(int(r.text[27:29])) print(int(r.text[23:26])) mm = int(r.text[27:29]) hh = int(r.text[23:26]) rtc = RTC() time_date = rtc.datetime() print('重置时间!') rtc.datetime((time_date[0], time_date[1], time_date[2], time_date[3], hh, mm, time_date[6], time_date[7])) rtc.datetime()
def wifi_connect(): wifi_led=Pin(23,Pin.OUT) wlan = network.WLAN(network.STA_IF) wlan.active(True) start_time=time.time()
if not wlan.isconnected(): print("当前无线未联网,正在连接中....") wlan.connect('***','***') while not wlan.isconnected(): wifi_led.value(0) time.sleep_ms(1000) wifi_led.value(0) time.sleep_ms(1000) print("正在尝试连接到wifi....") print(time.time()) if time.time()-start_time>15: print("连接失败!!!无线网连接超过15秒,请检查无线网名称和密码是否正确..") break
if wlan.isconnected(): wifi_led.value(0) IP_info=wlan.ifconfig() print("##################################################") print("无线网已经连接,信息如下:") print("IP地址:"+IP_info[0]) print("子网掩码:"+IP_info[1]) print("网关:"+IP_info[2]) print("DNS:"+IP_info[3]) print("##################################################")
oled_str1='' oled_str2='' oled_str3='' oled_str4='' oled_str5=''
def oled_process(str1,ip,oled,graphics):
global oled_str1 global oled_str2 global oled_str3 global oled_str4 global oled_str5 oled_str1=oled_str2 oled_str2=oled_str3 oled_str3=oled_str4 oled_str4=oled_str5 oled_str5=str1 oled.fill(0) graphics._slow_vline(0, 0, 64, 1) graphics._slow_vline(127, 0, 128, 1) graphics._slow_hline(0, 15, 128, 1) graphics._slow_hline(0, 16, 128, 1) graphics._slow_hline(0, 0, 128, 1) graphics._slow_hline(0, 63, 128, 1) oled.text("port:10000 & ip",0+2,0) oled.text(ip,0+2,8) oled.text(oled_str5,0+2,16+2) oled.text(oled_str4,0+2,24+2) oled.text(oled_str3,0+2,32+2) oled.text(oled_str2,0+2,40+2) oled.text(oled_str1,0+2,48+2) oled.show()
def oled_try(): try: i2c = machine.I2C(scl=machine.Pin(18), sda=machine.Pin(19)) oled = ssd1306.SSD1306_I2C(128 ,64 ,i2c) graphics = gfx.GFX(128, 64, oled.pixel) print("get") connect_to_phone_oled(oled,graphics) except: print("false") connect_to_phone()
def connect_to_phone_oled(oled,graphics): try: ip = Wifi.ifconfig()[0][0] s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sockaddr = socket.getaddrinfo(ip, port) print(sockaddr[0][4]) s.bind(sockaddr[0][4]) oled_process(sockaddr[0][4][0],sockaddr[0][4][0],oled,graphics) print("oled") print('tcp waiting...') while True: data, addr = s.recvfrom(1024) print('From %s %s' % addr) print('data is %s' % data) data_utf8 = bytes.decode(data) oled_process(data_utf8,sockaddr[0][4][0],oled,graphics) data_size = s.sendto(data, addr) if data_utf8 == "led_1_on": LED_1.value(0)
if data_utf8 == "led_2_on": LED_2.value(0) if data_utf8 == "led_1_off": LED_1.value(1)
if data_utf8 == "led_2_off": LED_2.value(1) if data_utf8 == "led_3_on": LED_3.value(0) if data_utf8 == "led_4_on": LED_4.value(0) if data_utf8 == "led_3_off": LED_3.value(1) if data_utf8 == "led_4_off": LED_4.value(1) if data_utf8 == "led_5_on": LED_5.value(0) if data_utf8 == "led_6_on": LED_6.value(0) if data_utf8 == "led_5_off": LED_5.value(1) if data_utf8 == "led_6_off": LED_6.value(1)
if data_utf8 == "led_7_on": LED_7.value(1) if data_utf8 == "led_7_off": LED_7.value(1) except: if(listenSocket): listenSocket.close()
def connect_to_phone(): try: ip = Wifi.ifconfig()[0][0] s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sockaddr = socket.getaddrinfo(ip, port) print(sockaddr[0][4]) s.bind(sockaddr[0][4]) print ('tcp waiting...') while True: data, addr = s.recvfrom(1024) print('From %s %s' % addr) print('data is %s' % data) data_utf8 = bytes.decode(data)
data_size = s.sendto(data, addr) if data_utf8 == "led_1_on": LED_1.value(0) if data_utf8 == "led_2_on": LED_2.value(0) if data_utf8 == "led_1_off": LED_1.value(1) if data_utf8 == "led_2_off": LED_2.value(1) if data_utf8 == "led_3_on": LED_3.value(0) if data_utf8 == "led_4_on": LED_4.value(0) if data_utf8 == "led_3_off": LED_3.value(1) if data_utf8 == "led_4_off": LED_4.value(1) if data_utf8 == "led_5_on": LED_5.value(0) if data_utf8 == "led_6_on": LED_6.value(0) if data_utf8 == "led_5_off": LED_5.value(1) if data_utf8 == "led_6_off": LED_6.value(1)
if data_utf8 == "led_7_on": LED_7.value(1) if data_utf8 == "led_7_off": LED_7.value(1) except: if(listenSocket): listenSocket.close()
wifi_connect() oled_try()
|