http://www.gaohaiyan.com/3771.html#4.使用VSCode
以下指令在交互模式逐行输入,执行。通常,esp8266-01s模块永久存储其中的设置,重新通电后这个ap仍然有效。如果无效尝试刷其它版本的micropython固件。
1 2 3 4 5 |
import network ap = network.WLAN(network.AP_IF) # 热点模式 ap.active(True) # 激活 ap.config(essid='Micro8266-AP', password="12345678") # 热点名,密码(至少8位) |
下面代码是将模块作为一个服务端使用,接收客户端socket的消息进行解析。结合machine模块对针脚数据控制。
模块通电,首先执行的是boot.py文件-MicroPython系统自行创建。然后寻找并执行main.py文件-这个我们手动创建,因此可以将下面代码保存为main.py上传到模块。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import socket import threading import machine sema = threading.Semaphore(1) addrlist = socket.getaddrinfo('0.0.0.0', 8888) # [ # (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('0.0.0.0', 8888)), # 面向连接的、基于TCP的。数据流,一般是tcp/ip协议的编程 # (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('0.0.0.0', 8888)) # 基于UDP的,专门用于局域网,基于广播。数据包,是udp协议网络编程 # ] af = addrlist[0] local = af[-1] # 解析本机ip print("本地ip", local) # ('0.0.0.0', 8888) tcpService = socket.socket() # 默认AF_INET、SOCK_STREAM tcpService.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) tcpService .bind(local) tcpService .listen(3) # 最大等待接入访问个数-不是正在响应个数。正响应个数+等待个数=socket最大连接数 def echo(): print("子程开始") with sema: clientSocket, clientAddr = tcpService.accept() print("客户ip", clientAddr) try: while True: msg = clientSocket.recv(1024) if not msg: break msg = msg.decode('utf-8') if 'byebye' == msg: break print("客户消息", msg) if msg.startswith('pin'): # 指令格式 pin_数字_on/off codes = msg.split('_') num = codes[1] pow = codes[2] p = machine.Pin(int(num), machine.Pin.OUT) # 设置某个针脚为输出信号模式 if 'on' == pow: p.on() # 输出高电平-通电 else: p.off() # 输出低电平 clientSocket.send("收到你的消息了:{}".format(msg).encode()) # 发消息格式是bytes print("{} 对话结束".format(clientAddr)) finally: clientSocket.close() # 关闭与客户端的连接 print("子程结束") tcpService.close() t = threading.Thread(target=echo) t.start() print("主程结束") |
客户端使用java的例子。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; public class Main { public static void main(String[] args) { try { Socket socket = new Socket("192.168.4.1", 8888); // 连接 esp8266的ip:端口 PrintWriter writer = new PrintWriter(socket.getOutputStream()); // 写数据 BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); // 读数据 writer.println("你好python"); // 发送数据 writer.flush(); String line = reader.readLine(); // 接收数据 System.out.println("服务端返回:" + line); writer.println("pin_2_on"); writer.flush(); line = reader.readLine(); System.out.println("服务端返回:" + line); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } writer.println("pin_2_off"); writer.flush(); line = reader.readLine(); System.out.println("服务端返回:" + line); writer.println("byebye"); writer.flush(); line = reader.readLine(); System.out.println("服务端返回:" + line); // 关闭资源 writer.close(); reader.close(); socket.close(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } |
- end
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/3839.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设