importsocketimportusslassslimportjsonfromnetwork_esp32importwifiSSID="(set yours)"PASW="(set yours)"KEY_PATH='(set yours)'CERT_PATH='(set yours)'END_POINT='(set yours)'defenable_esp32():ifwifi.isconnected()==False:foriinrange(5):try:# Running within 3 seconds of power-up can cause an SD load error
# wifi.reset(is_hard=False)
wifi.reset(is_hard=True)print('try AT connect wifi...')wifi.connect(SSID,PASW)ifwifi.isconnected():breakexceptExceptionase:print(e)print('network state:',wifi.isconnected(),wifi.ifconfig())defsend():s=socket.socket()ai=socket.getaddrinfo(END_POINT,8883)print("Address infos:",ai)addr=ai[0][-1]withopen(KEY_PATH,'rb')asf1:key=f1.read()withopen(CERT_PATH,'rb')asf2:cert=f2.read()s=ssl.wrap_socket(s,key=key,cert=cert)s.connect(addr)print('Connected to Server')temp=100json_data={'state':{'reported':{'Temperature':temp}}}send_data=json.dumps(json_data)s.write(b"POST /things/ESP32/shadow HTTP/1.1\r\n")s.write(b"Host: "+END_POINT+"\r\n")s.write(b"Content-Length: %d\r\n"%len(send_data))s.write(b"Connection: close\r\n")s.write(b"\r\n")s.write(send_data)print('Send Data to Server')recv_data=s.recv(4096)print(str(recv_data,'utf8'))s.close()if__name__=='__main__':enable_esp32()send()
Top comments (0)
Subscribe
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)