tpyboard v 202はtcp通信をテストして、i 2 cのoledプログラム、呼吸灯のソースコード、みんなに役に立つことを望みます
2744 ワード
1、 main.py
oled ,
import time, math
import machine
import network
# from ssd1306 import SSD1306_I2C
# def oled_text(str,x=0,y=0):
# from machine import I2C
# i2c=machine.I2C(-1, sda=machine.Pin(4), scl=machine.Pin(5), freq=100000)
#
# oled = SSD1306_I2C(128,64, i2c)
# oled.text(str, x, y)
# oled.show()
# now use socket as usual
import socket
def socket_server_test():
s = socket.socket()
host = socket.gethostname() #
port = 12345 #
s.bind((host, port)) #
s.listen(5) #
while True:
c, addr = s.accept() #
print(' :', addr)
c.send('tpyboard tcp server !')
c.close() #
def socket_clent_test():
s = socket.socket()
try :
s.connect(('192.168.31.244',55555))
s.send(b'test success')
data = s.recv(1000)
print (data)
s.close()
except Exception as e :
print (e)
def do_connect():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print('connecting to network...')
wlan.connect('pang', 'ldx19891025')
while not wlan.isconnected():
print("wifi connecting")
# oled_text ("wifi connect success")
print('network config:', wlan.ifconfig())
def pulse( t):
led = machine.PWM(machine.Pin(2), freq=100)
for i in range(20):
led.duty(int(math.sin(i / 10 * math.pi) * 500 + 500))
time.sleep_ms(t)
# oled_text('--display test--', y=56)
while True:
do_connect()
time.sleep(1)
socket_clent_test()
print('-------')
# oled_text('--display test--',y=8)
pulse( 30)
2、
#!/usr/bin/python3
# :server.py
# socket、sys
import socket
import sys
# socket
serversocket = socket.socket()
#
serversocket.bind(("192.168.31.244", 55555))
# ,
serversocket.listen(5)
while True:
#
clientsocket, addr = serversocket.accept()
# print(clientsocket,addr)
print(" : %s" % str(addr))
buf = clientsocket.recv(1024)
print (buf)
msg = 'test success !' + "\r
"
clientsocket.send(msg.encode('utf-8'))
clientsocket.close()