赞
踩
MicroPython ESP32 开发
1.ESP32刷入MicroPython以及连接WiFi
2.ESP32 GPIO
3.ESP32 GPIO 的高低电平与PWM
4.ESP32 控制继电器与读取DHT22温湿度数据
5.sokcet 通信与AES-CBC加密通信 (本文)
更新中……
esp32 通过socket 与电脑通信
提示:以下是本篇文章正文内容,下面案例可供参考
代码如下(示例):
import socket
import _thread
import json
import array
# socket的连接实例化 AF_INET = IPV4 SOCK_STREAM = TCP
sock_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#服务端ipv4地址
sock_tcp.connect(("192.168.0.5",60000))
#向服务端发送json打包后的字典数据
# 字典内容 = {
# "type":"exec":
# "cmd":"g5.value(1)"
# }
execDictDump = json.dumps({"type":"exec","cmd":"g5.value(1)"})
sock_tcp.sendall(bytes(execDictDump, "utf-8"))
代码如下(示例):
import socket import _thread import json import array def tcplink(conn, addr): global g5 print("addr:",addr) print("conn",conn) while 1: # MTU最大为1024byte data = conn.recv(1024) # 防止对面掉线,连接无法正常关闭 if not data: break recvDict = json.loads(data) #判断字典内容为执行命令后执行cmd的内容 if recvDict["type"] == "exec": exec(recvDict["cmd"]) print(g5.value()) conn.close() # socket实例化 sock_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 允许所有设备连接 sock_tcp.bind(("0.0.0.0", 60000)) # 最大连接数 sock_tcp.listen(100) while 1: print("Start Listening") # 如果设备连接过来之后,将连接的实例复制给conn 将ip与端口赋值给addr conn, addr = sock_tcp.accept() # 启动新线程连接,如果不用新线程将会一直停留在阻塞状态 _thread.start_new_thread(tcplink, (conn,addr))
以下仅展示esp32向window发送温度数据
windows下的python代码:
import socket import _thread import json import array from Crypto import AES import os import binascii import hashlib def tcplink(conn, addr): print("addr:", addr) print("conn", conn) while 1: data = conn.recv(1024) if not data: break keystr = "lihz" key = hashlib.sha256(keystr.encode("utf-8")).digest()[0:16] data = aes_decrypt(bytes(data), key) print(str(data, "utf-8")) conn.close() def aes_decrypt(data: bytes, eKey: bytes): iv_ = data[0:16] aes = AES.new(eKey, 2, iv_) plain = aes.decrypt(data[16:]).rstrip() return plain sock_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock_tcp.bind(("0.0.0.0", 60000)) sock_tcp.listen(5) while 1: print("Start Listening...") conn, addr = sock_tcp.accept() _thread.start_new_thread(tcplink, (conn, addr))
运行后效果如下:
在micropython中运行如下代码:
from dht import DHT22 from machine import Pin import socket import _thread import json import array import ucryptolib import os import ubinascii # base64 import uhashlib def aes_encrypt(data: bytes, eKey: bytes): padding_len = 16 - len(data) % 16 data = data + b" "*padding_len iv_ = os.urandom(16) aes = ucryptolib.aes(eKey, 2, iv_) encrypt_data = aes.encrypt(data) final = iv_ + encrypt_data return final key = uhashlib.sha256("lihz").digest()[0:16] g5 = DHT22(Pin(5)) g5.measure() sock_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock_tcp.connect(("192.168.0.6", 60000)) #连接windows send1='temp:'+str(g5.temperature())+' humi:'+str(g5.humidity()) sends=aes_encrypt(bytes(send1, "utf-8"), key) sock_tcp.sendall(sends) #发送sends加密数据
运行后Windows端收到发送的信息,效果如下:
然后就可以通过WiFi在客户端发送消息给服务端了,可以从esp32向服务端发送各种传感器数据,还能在服务端发送控制命令来控制esp32
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。