周末翻抽屉,发现一块n年前买的espduino板子,老实说其实当时是买错了,一直丢在那。今天刚好要在pc/pi上给arduino发指令,手上没有多余的esp8266模块,就拿这个板子来用吧,顺便把esp8266模块的使用方法整理一下。
*注:以下方法适用于任何使用esp8266模块的arduino板子,不论是集成还是外置esp8266的情形(开发版根据实际情况选择即可)。
esp8266模块使用方法如下:
1.安装依赖
brew install gnu-sed --with-default-names
brew install binutils coreutils automake autoconf wget gawk libtool help2man gperf grep
2.找到esp8266在arduino的最新releases(用于IDE识别开发版)
https://github.com/esp8266/Arduino/releases
3.附加开发版设置
*IDE和CH341驱动的安装方法详见:http://blog.yanjingang.com/?p=721
打开Arduino IDE,“Arduino-Preferences-附加开发版管理网址”,填写esp8266开发包配置地址:
https://github.com/esp8266/Arduino/releases/download/2.5.2/package_esp8266com_index.json
或
http://arduino.esp8266.com/stable/package_esp8266com_index.json
4.安装esp8266开发版插件
打开Arduino IDE,“工具-开发版-开发版管理器”,搜索esp8266,点击安装。安装完毕后重启IDE。
5.测试wifi通信
开发版选择“ESPDuino(ESP-13 Module)”,Reset Medth选“ESPduino V2”,选择端口后测试烧录。
5.1 arduino向外发送http请求:
//http请求示例
#include
#include
#include
#include
#include
ESP8266WiFiMulti WiFiMulti;
void setup() {
Serial.begin(115200);
// Serial.setDebugOutput(true);
Serial.println();
Serial.println();
Serial.println();
/*
for (uint8_t t = 4; t > 0; t--) {
Serial.printf("[SETUP] WAIT %d...\n", t);
Serial.flush();
delay(1000);
}
*/
WiFi.mode(WIFI_STA);
WiFiMulti.addAP("MARS.Y", "lovezhu1314"); //wifi 名称/密码
}
void loop() {
// wait for WiFi connection
if ((WiFiMulti.run() == WL_CONNECTED)) {
WiFiClient client;
HTTPClient http;
Serial.print("[HTTP] begin...\n");
String url = "http://www.yanjingang.com/chengyu/api/s.php?word=qinnengbuzhuo";
if (http.begin(client, url)) { // HTTP
Serial.printf("[HTTP] GET: %s\n", url.c_str());
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] HTTP Status: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = http.getString();
Serial.printf("[HTTP] HTTP Res: %s\n", payload.c_str());
}
} else {
Serial.printf("[HTTP] GET failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
Serial.print("[HTTP] end...\n");
} else {
Serial.printf("[HTTP