一.所需器件:1.USB TO TTL 2.LED灯 3.面包板 4.连接线 5.电脑
二.配置Arduino IDE环境
1.安装ESP8266开发板软件包
使用1.6.4及以上版本的Arduino, 打开Arduino IDE,打开 文件->首选项 在 附加开发板管理器网址 一栏写入: http://arduino.esp8266.com/stable/package_esp8266com_index.json添加完以后点击 好。
做完这步以后重启Arduino IDE,然后依次点击 工具->开发板->开发板管理器 打开后在搜索框输入esp,然后能找到类似'esp8266 by ESP8266 Community',点击并安装。
安装完后重启Arduino IDE,然后依次点击 工具->开发板->Generic ESP8266 Module,接着按照下面的信息在工具栏找到对应项进行配置
- Flash Mode: DIO
- Flash Frequency: 40 MHz
- Upload Using: Serial
- CPU Frequency: 80 MHz
- Flash Size: 4M (1M SPIFFS)
- Upload Speed: 115200
- Port: 对应的USB 端口 (当你一将Arduino连接电脑时,在设备管理器中会冒出端口号)
- Programmer: AVRISP mkll
其他的设置就按照默认的选择不变
2.安装ArduinoJSON库
在我们的HTML网页中将使用到json数据,所以我们需要添加ArduinoJson库到我们的Arduino IDE中。打开 项目->加载库->管理库 在搜索框输入json,可能会得到类似 'ArduinoJSON by Benolt Blanchon' 点击它并安装,如果安装出现问题,可以访问如下链接: installing ArduinoJson library 在这步设置以后,你可以通过 #include<ArduinoJson.h> 在项目中添加库 了
三.配置HTML网页程序
在自己电脑上新建一个web project,例如工程名起为wifiarduino,里面放一个light.json文件,内容为{"light":"off"},然后放到tomcat中发布,然后就能通过在浏览器端输入http://localhost/wifiarduino/light.json查看到了。
四.连线
ESP8266 USB TO TTL
- VCC 3.3 V
- GND GND
- CH_PD 3.3 V
- TX TX
- RX RX
- GPIO2 3.3 V
- GPIO0 GND
将ESP8266设定为FLASH(烧写)模式:当GPIO0(上图紫色线)接地线时,ESP8266是以bootloader模式(编程模式)启动的,这也就是你可以烧写ESP8266的时候。在Arduino代码加载完毕时,你将看到Arduino IDE底部提示“上传结束”的信息,然后代码就会开始运行了。
程序上传结束后,将紫色线移除:当你不想将ESP8266一直设定为烧写模式时,将紫色线移除即可,程序将会一直在ESP8266上执行。
五.编写Arduino代码
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
const char* ssid = "mywifi"; //修改成你可访问的wifi名称
const char* password = "mywifipassword"; // 修改成wifi密码
const char* host = "192.168.1.10"; // 你的网点域名或IP
String path = "/wifiarduino/light.json"; // 文件路径
const int pin = 2;
void setup() {
pinMode(pin, OUTPUT);
pinMode(pin, HIGH);
Serial.begin(115200);
delay(10);
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
int wifi_ctr = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.println("IP address: " + WiFi.localIP());
}
void loop() {
Serial.print("connecting to ");
Serial.println(host);
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
client.print(String("GET ") + path + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: keep-alive\r\n\r\n");
delay(500); // wait for server to respond
// read response
String section="header";
while(client.available()){
String line = client.readStringUntil('\r');
// Serial.print(line); // we’ll parse the HTML body here
if (section=="header") { // headers..
Serial.print(".");
if (line=="\n") { // skips the empty space at the beginning
section="json";
}
}
else if (section=="json") { // print the good stuff
section="ignore";
String result = line.substring(1); // Parse JSON
int size = result.length() + 1;
char json[size];
result.toCharArray(json, size);
StaticJsonDocument<200> jsonDocument;
deserializeJson(jsonDocument, json);
auto error = deserializeJson(jsonDocument, json);
if (error)
{
Serial.println("parseObject() failed");
return;
}
// Make the decision to turn off or on the LED
if (strcmp(jsonDocument["light"], "on") == 0) {
digitalWrite(pin, HIGH);
Serial.println("LED ON");
}
else {
digitalWrite(pin, LOW);
Serial.println("led off");
}
}
}
Serial.print("closing connection. ");
}
五.打开Arduino IDE中 工具->串口监视器,并将右下角的波特率设定为115200,然后它将会显示你是否连上了WiFi,如果你看到ESP8266上有红灯亮起,且有蓝灯闪烁时,说明你的无线模块准备完毕。