硬件清单:
Wemos D1(支持AP模式也就是路由模式和STA模式也就是上网设备)、超声波模块、小车、L9110s步进电机控制器
软件:
eclipse、arduino IDE
ESP8266WiFi库:
从上图中可以看出ESP8266WiFi库主要包含Station、Soft AP、config、WiFiClient、WiFiServer、scan、hostname,本节主要介绍Station相关的函数使用。
动态IP方式连接WiFi热点
#include "ESP8266WiFi.h"
char* ssid = "sand";
char* passwd = "12345678";
void setup() {
Serial.begin(115200);
// Set WiFi to station mode
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, passwd);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.println("connecting......");
}
Serial.println("Connect to router successfully!");
Serial.println(String("mac address is ")+WiFi.macAddress());
}
void loop() {
if (WiFi.status() == WL_CONNECTED)
{
Serial.println("Connect to router success");
Serial.println(String("Subnet mask is ")+WiFi.subnetMask().toString());
Serial.println(String("gatewayIP address is ")+WiFi.gatewayIP().toString());
Serial.println(String("dnsIP address is ")+WiFi.dnsIP().toString());
Serial.println(String("SSID is ") + WiFi.SSID());
Serial.println(String("PSK is ") + WiFi.psk());
Serial.println(String("BSSID is ") + WiFi.BSSIDstr());
Serial.println(String("RSSI is ") + WiFi.RSSI());
Serial.println(WiFi.localIP());
}else
{
Serial.println("Connect to router failed");
}
// Wait a bit before scanning again
delay(1000);
}
串口输出如下:
Connect to router success
Subnet mask is 255.255.255.0
gatewayIP address is 192.168.43.1
dnsIP address is 192.168.43.1
SSID is HUAWEI
PSK is FHN12345
BSSID is F4:63:1F:7D:AB:69
RSSI is -51
192.168.43.5//这个是想要固定的IP地址
Wemos作为设备动态接入WiFi:
#include <ESP8266WiFi.h>
char *ssid="HUAWEI";
char *passwd="FHN12345";
void initWifiSta()//wifi初始化函数
{
WiFi.mode(WIFI_STA);//设置STA模式
WiFi.begin(ssid,passwd);//连接网络
while(WiFi.status()!=WL_CONNECTED)//判断是否接入WiFi,如果接入WiFi.status()=WL_CONNECTED
{
Serial.print(".");
delay(500);
Serial.println(WiFi.localIP());//通过串口打印wemos的IP地址
delay(500);
}
}
void setup() {
Serial.begin(115200);
initWifiSta();
}
void loop() {
Serial.println(WiFi.localIP());
delay(500);
}
Wemos作为设备静态接入WiFi:
#include "ESP8266WiFi.h"
char* ssid = "HUAWEI";
char* passwd = "FHN12345";
IPAddress local_static_ip(192,168,43,5);
IPAddress local_gateway(192,168,43,1);
IPAddress local_subnet(255,255,255,0);
IPAddress local_dns_ip(192,168,43,1);
void setup() {
Serial.begin(115200);
// Set WiFi to station mode
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, passwd);
WiFi.config(local_static_ip, local_gateway, local_subnet, local_dns_ip);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.println("connecting......");
}
Serial.println("Connect to router successfully!");
Serial.println(String("mac address is ")+WiFi.macAddress());
}
void loop() {
if (WiFi.status() == WL_CONNECTED)
{
Serial.println("Connect to router success");
Serial.println(String("IP address is ")+WiFi.localIP().toString());
Serial.println(String("Subnet mask is ")+WiFi.subnetMask().toString());
Serial.println(String("gatewayIP address is ")+WiFi.gatewayIP().toString());
Serial.println(String("dnsIP address is ")+WiFi.dnsIP().toString());
Serial.println(String("SSID is ") + WiFi.SSID());
Serial.println(String("PSK is ") + WiFi.psk());
Serial.println(String("BSSID is ") + WiFi.BSSIDstr());
Serial.println(String("RSSI is ") + WiFi.RSSI());
}else
{
Serial.println("Connect to router failed");
}
// Wait a bit before scanning again
delay(1000);
}
串口打印如下:
Connect to router success
IP address is 192.168.43.5
Subnet mask is 255.255.255.0
gatewayIP address is 192.168.43.1
dnsIP address is 192.168.43.1
SSID is HUAWEI
PSK is FHN12345
BSSID is F4:63:1F:7D:AB:69//MAC
RSSI is -54
研发常说的C/S模式:
其实就是服务器和客户端的关系,服务器两大要素:1、地址(IP地址、域名等)2、端口号:(一台电脑或者设备有多个服务,每个服务用端口号区别)。比如:医院地址就可以类比为服务器地址,不同的科室去不同的地方类比端口号。
编程Wemos服务器,手机TCP工具控制舵机的旋转:
#include <Servo.h>
#include "ESP8266WiFi.h"
#define DuoPin 4
Servo myduoji;
char* ssid = "HUAWEI";
char* passwd = "FHN12345";
int port=4466;//端口号
WiFiServer server(port);//设置服务器端口号,将Wemos作为服务器,为其他设备接入提供端口号,server是自己定义的
IPAddress local_static_ip(192,168,43,5);
IPAddress local_gateway(192,168,43,1);
IPAddress local_subnet(255,255,255,0);
IPAddress local_dns_ip(192,168,43,1);
void setup() {
Serial.begin(115200);
// Set WiFi to station mode
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, passwd);
WiFi.config(local_static_ip, local_gateway, local_subnet, local_dns_ip);
server.begin();//启动服务器开始服务
myduoji.attach(DuoPin);//连接舵机
myduoji.write(98);
}
void loop() {
char cmd;
WiFiClient client=server.available();//初始化服务器
while(client.connected()){//当没有接入的时候client.connected()是0,当接入的时候client.connected()是非零进入循环
while(client.available()>0){//判断有没有数据的到来
cmd=client.read();//将命令读取到cmd
if(cmd=='1'){
myduoji.write(98);
}
else if(cmd=='2'){
myduoji.write(180);
}
}
}
}
编程Wemos服务器,手机TCP工具控制电机的旋转:
#include <Servo.h>
#include "ESP8266WiFi.h"
#define DuoPin 4
#define BehindLight1 5
#define BehindLight0 16
#define BehindRight1 0
#define BehindRight0 2
Servo myduoji;
char* ssid = "HUAWEI";
char* passwd = "FHN12345";
int port=4466;//端口号
WiFiServer server(port);//设置服务器端口号,将Wemos作为服务器,为其他设备接入提供端口号,server是自己定义的
IPAddress local_static_ip(192,168,43,5);
IPAddress local_gateway(192,168,43,1);
IPAddress local_subnet(255,255,255,0);
IPAddress local_dns_ip(192,168,43,1);
void initL9()//初始化L9电机驱动模块
{
pinMode(BehindLight1,OUTPUT);
pinMode(BehindLight0,OUTPUT);
pinMode(BehindRight1,OUTPUT);
pinMode(BehindRight0,OUTPUT);
}
void initSg()
{
myduoji.write(105);
}
void forward()
{
digitalWrite(BehindLight1,HIGH);
digitalWrite(BehindLight0,LOW);
digitalWrite(BehindRight1,HIGH);
digitalWrite(BehindRight0,LOW);
}
void retreat()
{
digitalWrite(BehindLight1,LOW);
digitalWrite(BehindLight0,HIGH);
digitalWrite(BehindRight1,LOW);
digitalWrite(BehindRight0,HIGH);
}
void Stop()
{
digitalWrite(BehindLight1,LOW);
digitalWrite(BehindLight0,LOW);
digitalWrite(BehindRight1,LOW);
digitalWrite(BehindRight0,LOW);
}
void setup() {
Serial.begin(115200);
// Set WiFi to station mode
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, passwd);
WiFi.config(local_static_ip, local_gateway, local_subnet, local_dns_ip);
server.begin();//启动服务器开始服务
myduoji.attach(DuoPin);//连接舵机
initSg();
initL9();
}
void loop() {
char cmd;
WiFiClient client=server.available();//初始化服务器
while(client.connected()){//当没有接入的时候client.connected()是0,当接入的时候client.connected()是非零进入循环
while(client.available()>0){//判断有没有数据的到来
cmd=client.read();//将命令读取到cmd
if(cmd=='1'){
forward();
delay(2000);
Stop();
}
else if(cmd=='2'){
retreat();
delay(2000);
Stop();
}
}
}
Serial.println(WiFi.localIP());
}
代码的综合实现:
#include <Servo.h>
#include "ESP8266WiFi.h"
#define DuoPin 4
#define BehindLight1 5
#define BehindLight0 16
#define BehindRight1 0
#define BehindRight0 2
#define Beep D5
Servo myduoji;
char* ssid = "HUAWEI";
char* passwd = "FHN12345";
int port = 4466; //端口号
WiFiServer server(port);//设置服务器端口号,将Wemos作为服务器,为其他设备接入提供端口号,server是自己定义的
IPAddress local_static_ip(192, 168, 43, 5);
IPAddress local_gateway(192, 168, 43, 1);
IPAddress local_subnet(255, 255, 255, 0);
IPAddress local_dns_ip(192, 168, 43, 1);
void initL9()//初始化L9电机驱动模块
{
pinMode(BehindLight1, OUTPUT);
pinMode(BehindLight0, OUTPUT);
pinMode(BehindRight1, OUTPUT);
pinMode(BehindRight0, OUTPUT);
}
void initSg()
{
myduoji.write(105);
}
void forward()
{
digitalWrite(BehindLight1, HIGH);
digitalWrite(BehindLight0, LOW);
digitalWrite(BehindRight1, HIGH);
digitalWrite(BehindRight0, LOW);
}
void retreat()
{
digitalWrite(BehindLight1, LOW);
digitalWrite(BehindLight0, HIGH);
digitalWrite(BehindRight1, LOW);
digitalWrite(BehindRight0, HIGH);
}
void Stop()
{
digitalWrite(BehindLight1, LOW);
digitalWrite(BehindLight0, LOW);
digitalWrite(BehindRight1, LOW);
digitalWrite(BehindRight0, LOW);
}
void Lift()
{
myduoji.write(135);
}
void Right()
{
myduoji.write(75);
}
void initBeep()
{
pinMode(Beep, OUTPUT);
digitalWrite(Beep, HIGH);
}
void Bee()
{
while (WiFi.status() != WL_CONNECTED) //判断是否接入WiFi,如果接入WiFi.status()=WL_CONNECTED
{
digitalWrite(Beep, LOW);
delay(1000);
digitalWrite(Beep, HIGH);
delay(1000);
}
}
void setup() {
Serial.begin(115200);
// Set WiFi to station mode
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, passwd);
WiFi.config(local_static_ip, local_gateway, local_subnet, local_dns_ip);
server.begin();//启动服务器开始服务
myduoji.attach(DuoPin);//连接舵机
initBeep();
initSg();
initL9();
}
void loop() {
char cmd;
Bee();//如果处于未连接状态,蜂鸣器响
WiFiClient client = server.available(); //初始化服务器
while (client.connected()) { //当没有接入的时候client.connected()是0,当接入的时候client.connected()是非零进入循环
while (client.available() > 0) { //判断有没有数据的到来
cmd = client.read(); //将命令读取到cmd
switch (cmd) {
case'q':
forward();
break;
case'h':
retreat();
break;
case'z':
Lift();
break;
case'y':
Right();
break;
case's':
Stop();
break;
case'd':
initSg();
break;
}
}
}
Serial.println(WiFi.localIP());
}
安卓app的设置:
- 导入工程文件
- ①
- ②
- ③
- ④在电脑的模拟器上面运行
- ⑤如果不能在虚拟机上运行,则按以下步骤执行,打开Eclipse的路径找到platform-tools
- ⑥找到SDK
- ⑦找到adb.exe并复制路径
- ⑧找到对应的盘,输入adb的路径,dir查看路径下的文件。并输入adb connect 127.0.0.1:62001,这里127.0.0.1是本机地址,62001是模拟器端口号。
将程序发送到手机:程序在bin文件夹下面。