NodeMCU 基于 Arduino IDE 开发
NodeMCU 是一块自带 Esp8266 Wifi 的开发版,可以说是市面上较为便宜的开发板了,我那块之前是 25 块买的,过几天就降价了,降到 19 块了,比起 Arduino Uno 一百多可以算是相当便宜了。
ModeMCU 的引脚图:
NodeMCU 可以使用 Arduino IDE 开发,首先需要在 文件 -> 首选项
中添加: https://arduino.esp8266.com/stable/package_esp8266com_index.json
:
然后在 开发板管理器
中添加 ESP8266 SDK:
连接上 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); }
更改相应的 SSID
和 PASSWORD
,然后上传,打开串口监视器,就能看到消息输出和抓取的网页。
code:
#include <Arduino.h> #include <ESP8266WiFi.h> #include <ESP8266WiFiMulti.h> #include <ESP8266HTTPClient.h> #include <WiFiClient.h> WiFiClient client; HTTPClient http; ESP8266WiFiMulti WiFiMulti; String http_get(char* url); void setup() { Serial.begin(115200); Serial.setDebugOutput(true); WiFi.mode(WIFI_STA); WiFiMulti.addAP("CMCC-PebQ", "t6xxxxxx"); while(WiFiMulti.run() != WL_CONNECTED) { Serial.print("Retry to connect wifi...\n"); delay(2000); } Serial.print("Wifi connected.\n"); } void loop() { String body = http_get("http://api.example.tech/"); Serial.println(body); delay(10000); } String http_get(char* url) { if(http.begin(client, url)) { int http_code = http.GET(); if(http_code < 0) { Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(http_code).c_str()); } if (http_code == HTTP_CODE_OK) { String body = http.getString(); return body; } Serial.printf("[HTTP] status code: %d, body: %s\n", http_code, http.getString().c_str()); return String(""); } }
base64 encode
#include <Arduino.h> #include <base64.h> void setup() { // put your setup code here, to run once: Serial.begin(115200); Serial.setDebugOutput(true); } void loop() { String data = String("hello arduino."); base64 B64; String result = B64.encode(data); Serial.println(result); delay(5000); }
MD5 encode
#include <Arduino.h> #include <MD5Builder.h> void setup() { Serial.begin(115200); Serial.setDebugOutput(true); } void loop() { const char* data = "hello arduino"; MD5Builder builder; builder.begin(); builder.add(data); builder.calculate(); String result = builder.toString(); Serial.println(result); delay(5000); }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论