当前位置: 代码迷 >> 综合 >> micropython(esp8266)adc采集通过tcp发送采集的数据
  详细解决方案

micropython(esp8266)adc采集通过tcp发送采集的数据

热度:60   发布时间:2023-11-14 08:01:23.0

引脚

A0为adc采集端。

tcp连接

ip:192.168.4.1 端口:9999

代码

import network, socket
from machine import ADC, Pin
import time
ip = '192.168.4.1'
port = 9999  #端口号ap = network.WLAN(network.AP_IF)
# create access-point interface
ap.config(essid='ESP-AP',password='12345678') # set the ESSID of the access point
ap.active(True)         # activate the interface
while True:if(ap.isconnected() == True):time.sleep(4);print("connected!")break
adc = ADC(0)listenSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
addr = (ip, port)
listenSocket.bind(addr)
listenSocket.listen(5)   #监听套接字, 最多允许一个连接
while True:print("accepting.....")conn, addr = listenSocket.accept()   #接收连接请求,返回收发数据的套接字对象和客户端地址print(addr, "connected")while True:time.sleep(1)if(ap.isconnected() == False):   #判断客户端是否断开连接print("close socket")conn.close()   #关闭套接字breakadcVal = str(3.3/1024*adc.read())print('voltage is '+ adcVal +'v')ret = conn.send(adcVal + '\r\n')   #发送数据

连接wifi产生tcp服务器

""" ESP32 TCP Server """import socket
import network
from machine import Pin
import timeport = 10000  #端口号
listenSocket = None  #套接字def do_connect():import networkwlan = network.WLAN(network.STA_IF)wlan.active(True)if not wlan.isconnected():print('connecting to network...')wlan.connect('CQUPT', '')while not wlan.isconnected():passprint('network config:', wlan.ifconfig())return(wlan.ifconfig())
p2 = Pin(2, Pin.IN)
wifiinfo = do_connect()
ip = wifiinfo[0]  #获取IP地址
listenSocket = socket.socket()   #创建套接字
listenSocket.bind((ip, port))   #绑定地址和端口号
listenSocket.listen(1)   #监听套接字, 最多允许一个连接
listenSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)   #设置套接字
print ('tcp waiting...')while True:print("accepting.....")conn, addr = listenSocket.accept()   #接收连接请求,返回收发数据的套接字对象和客户端地址print(addr, "connected")while True:time.sleep(1)#if(ap.isconnected() == False): #判断客户端是否断开连接#print("close socket")#conn.close() #关闭套接字#breakinVal = p2.value()print(inVal)ret = conn.send(str(inVal) + '\r\n')   #发送数据

tcp服务器通过接受值控制舵机(主动断开)

import network, socket
from machine import ADC, Pin
from machine import Pin, PWM
import time
ip = '192.168.4.1'
port = 9999  #端口号ap = network.WLAN(network.AP_IF)
# create access-point interface
ap.config(essid='ESP-AP',password='12345678') # set the ESSID of the access point
ap.active(True)         # activate the interface
pwm0 = PWM(Pin(2))      # create PWM object from a pin
pwm0.freq(50)         # set frequency
pwm0.duty(200)          # set duty cycle
pwm0.duty(int((2.5)/20*1023))
listenSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
addr = (ip, port)
listenSocket.bind(addr)
listenSocket.listen(1)   #监听套接字
while True:print("accepting.....")conn, addr = listenSocket.accept()   #接收连接请求,返回收发数据的套接字对象和客户端地址print(addr, "connected") while True: data = conn.recv(1024)   #接收数据(1024字节大小)if(len(data) == 0):   #判断客户端是否断开连接print("close socket")conn.close()   #关闭套接字breakif(str(data) == "b'1'"):print(data)ret = conn.send(data)   #发送数据pwm0.duty(int((1.3)/20*1023))time.sleep(5)pwm0.duty(int((2.5)/20*1023))conn.close()   #关闭套接字break