ModeMCU 基于 Arduino IDE 开发

NodeMCU 是一块自带 Esp8266 Wifi 的开发版,可以说是市面上较为便宜的开发板了,我那块之前是 25 块买的,过几天就降价了,降到 19 块了,比起 Arduino Uno 一百多可以算是相当便宜了。

ModeMCU 的引脚图:

6540285-8cae02535047304e

NodeMCU 可以使用 Arduino IDE 开发,首先需要在 文件 -> 首选项 中添加:https://arduino.esp8266.com/stable/package_esp8266com_index.json

nodemcu

然后在开发板管理器中添加 ESP8266 SDK:

board

连接上 NodeMCU 开发板,开发板选择: NodeMCU 1.0 (ESP-12E Module),然后选择相应COM端口,就可以开发了。

首先尝试一下点亮 LED 灯,代码:

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);   
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, LOW);   
  delay(1000);                  
  digitalWrite(LED_BUILTIN, HIGH); 
  delay(2000);                    
}

然后上传,一切顺利的话应该可以看到板子上的 Led 灯开始闪烁了。

显然上面的例子太简单了,也没有用到 Wifi 功能,下面尝试一下发起 HTTP 请求,代码(可在示例中找到):

#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

#include <ESP8266HTTPClient.h>

#include <WiFiClient.h>

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("SSID", "PASSWORD");

}

void loop() {
  // wait for WiFi connection
  if ((WiFiMulti.run() == WL_CONNECTED)) {

    WiFiClient client;

    HTTPClient http;

    Serial.print("[HTTP] begin...\n");
    if (http.begin(client, "http://www.baidu.com")) {  // HTTP

      Serial.print("[HTTP] GET...\n");
      // 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] GET... code: %d\n", httpCode);

        // file found at server
        if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
          String payload = http.getString();
          Serial.println(payload);
        }
      } else {
        Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
      }

      http.end();
    } else {
      Serial.printf("[HTTP} Unable to connect\n");
    }
  }

  delay(10000);
}

更改相应的 SSIDPASSWORD,然后上传,打开串口监视器,就能看到消息输出和抓取的网页。