环境:
ESPlorer工具和nodemcu刷固态工具,编程用的语言是lua语言
链接:https://pan.baidu.com/s/1djJv_mgt9cKGqJMSZZnF0w
提取码:rzj8
两个重要的网站:
下载刷固态的网址:https://nodemcu-build.com/
api文档:https://nodemcu.readthedocs.io/en/master/en/modules/net/#netudpsocketon
java的SDK在线文档阅读:http://tool.oschina.net/apidocs/apidoc?api=jdk-zh
开发板:
一.连接网络操作:
--连接网络--
wifi.setmode(wifi.STATION)
cfg = {}
cfg.ssid = "OPPO"
cfg.pwd = "123456789"
wifi.sta.config(cfg)
tmr.alarm(1,1000,1,function()
if wifi.sta.getip() == nil then
print("please...wait...")
else
print(wifi.sta.getip()) --打印连接的网络的ip地址
tmr.stop(1) --取消定时器
end
end)
打印输出:
二.ESP8266创建服务器操作:
1.创建服务器:
--创建服务器--
led = 4
gpio.mode(led,gpio.OUTPUT)
gpio.write(led,gpio.HIGH)
wifi.setmode(wifi.STATIONAP)
cfg = {}
cfg.ssid = "OPPO"
cfg.pwd = "123456789"
wifi.sta.config(cfg)
--上面这段用来连接网网络--
--创建一个服务器--
sv = net.createServer(net.TCP,30)
function receiver(sck,data)
print(data)
if data == "0" then
gpio.write(led,0)
else
gpio.write(led,1)
end
--sck:close()
end
tmr.alarm(1,1000,1,function()
if wifi.sta.getip() == nil then
print("please....wait.....")
else
print(wifi.sta.getip())
if sv then
sv:listen(80, function(conn)
conn:on("receive", receiver)
conn:send("hello world")
end)
end
end
end)
2.这里通过java创建客户端,可以向服务器发送数据,代码如下:
public class kuhuduanServer {
public static void main(String[] args) {
String ip = "192.168.43.71"; //服务器端ip地址
int port = 8080; //端口号
Socket sck = null;
Scanner in=new Scanner(System.in);
try {
sck = new Socket(ip, port);
} catch (IOException e) {
e.printStackTrace();
}
while (true) {
String ds =in.nextLine();
neirong(ds,sck);
}
}
static void neirong(String content, Socket sc) {
//2.传输内
byte[] bstream = new byte[0]; //转化为字节流
try {
bstream = content.getBytes("GBK");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
OutputStream os = null; //输出流
os = sc.getOutputStream();
os.write(bstream);
} catch (IOException e) {
e.printStackTrace();
}
}
}
java有关Socket编程学习:
客户端:
1.新建Socket
Socket socket = new Socket("127.0.0.1",8080);
2.获取输入流,输出流
OutputStream out = socket.getOutputStream();
InputStream in = socket.getInputStream();
3.通过socket输入流和输出流向服务器发送数据
out.write("客户端发送的数据:");
out.flush();
三:ESP8266创建客户端:
1.创建客户端:
--连接一个服务器--
led = 4
gpio.mode(led,gpio.OUTPUT)
gpio.write(led,gpio.HIGH)
wifi.setmode(wifi.STATIONAP)
cfg = {}
cfg.ssid = "OPPO"
cfg.pwd = "123456789"
wifi.sta.config(cfg)
--连接网络--
--创建一个客户端--
skk = net.createConnection(net.TCP, 0)
function f_method(sk)
skk:on("receive", function(sck, c) print(c) end)
skk:connect(1220,"192.168.43.108")
end
tmr.alarm(1,1000,1,function()
if wifi.sta.getip() == nil then
--如果为空表示没连上--
print("please.....wait....")
else
--连接成功
print("连接成功"..wifi.sta.getip())
f_method(skk)
tmr.stop(1)
end
end)
2:这里通过java创建服务器,代码如下:
import java.io.*;
import java.net.Socket;
public class ServerThread extends Thread{
private Socket socket = null;
public ServerThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
InputStream is=null;
InputStreamReader isr=null;
BufferedReader br=null;
OutputStream os=null;
PrintWriter pw=null;
try {
is = socket.getInputStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String info = null;
while((info=br.readLine())!=null){
System.out.println("我是服务器,客户端说:"+info);
}
socket.shutdownInput();
os = socket.getOutputStream();
pw = new PrintWriter(os);
pw.write("服务器欢迎你");
pw.flush();
} catch (Exception e) {
// TODO: handle exception
} finally{
//关闭资源
try {
if(pw!=null)
pw.close();
if(os!=null)
os.close();
if(br!=null)
br.close();
if(isr!=null)
isr.close();
if(is!=null)
is.close();
if(socket!=null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public class fuwuqi {
public static void main(String[] args) {
try {
// 创建服务端socket
ServerSocket serverSocket = new ServerSocket(1220);
// 创建客户端socket
Socket socket = new Socket();
//循环监听等待客户端的连接
while(true){
// 监听客户端
socket = serverSocket.accept();
ServerThread thread = new ServerThread(socket);
thread.start();
InetAddress address=socket.getInetAddress();
System.out.println("当前客户端的IP:"+address.getHostAddress());
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}