micropython(esp8266)adc采集通过tcp发送采集的数据
Posted _WILLPOWER_
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了micropython(esp8266)adc采集通过tcp发送采集的数据相关的知识,希望对你有一定的参考价值。
引脚
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() #关闭套接字
break
adcVal = 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 time
port = 10000 #端口号
listenSocket = None #套接字
def do_connect():
import network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print('connecting to network...')
wlan.connect('CQUPT', '')
while not wlan.isconnected():
pass
print('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() #关闭套接字
#break
inVal = 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() #关闭套接字
break
if(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
以上是关于micropython(esp8266)adc采集通过tcp发送采集的数据的主要内容,如果未能解决你的问题,请参考以下文章