材料清單:
-
esp8266
-
nokia5110
-
面包板
-
跳線
布線參考 https://www.zonnepanelen.wouterlood.com/25-nokia-5510-8448-lcd-and-nodemcu-esp8266/
ARDUINO IDE下載
Arduino IDE離線安裝esp8266 SDK (8266_package_2.6.3)
下載 https://ifwz.lanzouw.com/i65oOz3on0f
雙擊運行即可解壓,解壓完成后,再打開Arduino IDE,即可在 菜單欄>工具>開發板 中找到你使用的esp8266開發板
**ESP8266-NodeMCU開發板驅動 (CP210X) **
https://ifwz.lanzouw.com/iNKVPz3on2h
可以上傳個blink測試一下板子
點亮 nokia5110
參考 https://microcontrollerslab.com/nokia-5110-lcd-esp8266-nodemcu-tutorial/
安裝 這兩個庫
測試代碼
點擊查看代碼
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
Adafruit_PCD8544 display = Adafruit_PCD8544(D4,D3,D2,D1,D0);
void setup() {
Serial.begin(115200);
display.begin();
display.setContrast(50);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(10,10);
display.println("Welcome!");
display.display();
}
void loop(){
}
顯示圖案
使用 https://javl.github.io/image2cpp/
因為使用的nokia屏幕,致敬一下經典握手開機logo
點擊查看代碼
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
Adafruit_PCD8544 display = Adafruit_PCD8544(D4,D3,D2,D1,D0);
static const uint8_t bitmap_image[1024] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x1f, 0xff, 0xfc, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xf0, 0x03, 0xff, 0xee, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x1f, 0xfc,
0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x07, 0xe0, 0x07, 0xdf, 0xff, 0xff, 0xff,
0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x03,
0x80, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x07, 0x80, 0x00, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xf0, 0x00, 0x03, 0x80, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00,
0x01, 0xa0, 0x02, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x80, 0x00, 0xe0, 0x00, 0x88, 0xff,
0xff, 0xff, 0xff, 0xff, 0xf0, 0xff, 0xc0, 0x78, 0x00, 0x01, 0x9f, 0xee, 0xff, 0xff, 0xff, 0xf0,
0xff, 0xfe, 0x1c, 0x03, 0x30, 0xfd, 0xff, 0x1f, 0xff, 0xff, 0xf0, 0xff, 0xff, 0x0f, 0x0e, 0x71,
0x83, 0xf3, 0xc0, 0xff, 0x80, 0x00, 0xff, 0xff, 0x82, 0x60, 0x31, 0x1f, 0xce, 0xe0, 0x00, 0x00,
0x00, 0xff, 0xff, 0xf0, 0x4c, 0x63, 0xff, 0xbd, 0x78, 0x00, 0x00, 0x00, 0xff, 0xff, 0xfe, 0x00,
0x8f, 0xff, 0xfb, 0x7c, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x91, 0xff, 0xff, 0xf6, 0xf8, 0x1f,
0x80, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xed, 0xc0, 0xff, 0xf8, 0x00, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0x8f, 0xff, 0xff, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0xff, 0xff, 0x83, 0xcc,
0x00, 0xcf, 0x1c, 0xf8, 0x7f, 0xff, 0xf0, 0xff, 0xff, 0x83, 0xcc, 0x7c, 0xce, 0x3c, 0xf0, 0x3f,
0xff, 0xf0, 0xff, 0xff, 0x91, 0xcc, 0xfe, 0xcc, 0x7c, 0xf3, 0x3f, 0xff, 0xf0, 0xff, 0xff, 0x98,
0xcc, 0xfe, 0xc0, 0xfc, 0xe7, 0x1f, 0xff, 0xf0, 0xff, 0xff, 0x9c, 0x6c, 0xfe, 0xcc, 0x7c, 0xc3,
0x1f, 0xff, 0xf0, 0xff, 0xff, 0x9e, 0x0c, 0xfc, 0xce, 0x3c, 0xc0, 0x0f, 0xff, 0xf0, 0xff, 0xff,
0x9f, 0x0c, 0x00, 0xcf, 0x0c, 0x8f, 0xc7, 0xff, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0
};
void setup() {
Serial.begin(115200);
display.begin();
display.setContrast(50);
display.clearDisplay();
display.drawBitmap(0, 0, bitmap_image, 84, 48, 1);
display.display();
}
void loop() {}
ESP8266自動配網 – WiFiManager庫 (強烈推薦,非常好用)
http://www.taichi-maker.com/homepage/esp8266-nodemcu-iot/iot-c/esp8266-tips/wifimanager/
點擊查看代碼
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
void setup() {
Serial.begin(9600);
// 建立WiFiManager對象
WiFiManager wifiManager;
// 自動連接WiFi。以下語句的參數是連接ESP8266時的WiFi名稱
wifiManager.autoConnect("AutoConnectAP");
// 如果您希望該WiFi添加密碼,可以使用以下語句:
// wifiManager.autoConnect("AutoConnectAP", "12345678");
// 以上語句中的12345678是連接AutoConnectAP的密碼
// WiFi連接成功后將通過串口監視器輸出連接成功信息
Serial.println("");
Serial.print("ESP8266 Connected to ");
Serial.println(WiFi.SSID()); // WiFi名稱
Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); // IP
}
void loop() {}
ESP8266獲取時間
點擊查看代碼
#include <NTPClient.h>
// change next line to use with another board/shield
#include <ESP8266WiFi.h>
//#include <WiFi.h> // for WiFi shield
//#include <WiFi101.h> // for WiFi 101 shield or MKR1000
#include <WiFiUdp.h>
const char *ssid = "Mobile WiFi";//輸入Wifi賬號
const char *password = "";//輸入WiFi密碼
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP,"ntp.aliyun.com");//NTP服務器地址
void setup(){
Serial.begin(9600);//串口通訊波特率
WiFi.begin(ssid, password);//連接WiFi
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( "." );
}
timeClient.begin();
// + 1區 偏移3600, +8區 :3600×8 = 28800,如果注釋掉這行代碼會發現通過串口監聽器
//打印出來的時間和電腦中顯示的時間並不同步,相差8小時。
timeClient.setTimeOffset(28800);
}
void loop() {
timeClient.update();
Serial.println(timeClient.getFormattedTime());//打印時間
delay(1000);
}
ESP8266獲取並解析心知天氣數據
http://www.taichi-maker.com/homepage/iot-development/iot-platform/seniverse/esp8266-application/
點擊查看代碼
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
const char* ssid = "chen"; // 連接WiFi名(此處使用taichi-maker為示例)
// 請將您需要連接的WiFi名填入引號中
const char* password = "88888888"; // 連接WiFi密碼(此處使用12345678為示例)
// 請將您需要連接的WiFi密碼填入引號中
const char* host = "api.seniverse.com"; // 將要連接的服務器地址
const int httpPort = 80; // 將要連接的服務器端口
// 心知天氣HTTP請求所需信息
String reqUserKey = "smtq3n0ixdggurox"; // 私鑰
String reqLocation = "handan"; // 城市
String reqUnit = "c"; // 攝氏/華氏
void setup(){
Serial.begin(9600);
Serial.println("");
// 連接WiFi
connectWiFi();
}
void loop(){
// 建立心知天氣API當前天氣請求資源地址
String reqRes = "/v3/weather/now.json?key=" + reqUserKey +
+ "&location=" + reqLocation +
"&language=en&unit=" +reqUnit;
// 向心知天氣服務器服務器請求信息並對信息進行解析
httpRequest(reqRes);
delay(3000);
}
// 向心知天氣服務器服務器請求信息並對信息進行解析
void httpRequest(String reqRes){
WiFiClient client;
// 建立http請求信息
String httpRequest = String("GET ") + reqRes + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n";
Serial.println("");
Serial.print("Connecting to "); Serial.print(host);
// 嘗試連接服務器
if (client.connect(host, 80)){
Serial.println(" Success!");
// 向服務器發送http請求信息
client.print(httpRequest);
Serial.println("Sending request: ");
Serial.println(httpRequest);
// 獲取並顯示服務器響應狀態行
String status_response = client.readStringUntil('\n');
Serial.print("status_response: ");
Serial.println(status_response);
// 使用find跳過HTTP響應頭
if (client.find("\r\n\r\n")) {
Serial.println("Found Header End. Start Parsing.");
}
// 利用ArduinoJson庫解析心知天氣響應信息
parseInfo(client);
} else {
Serial.println(" connection failed!");
}
//斷開客戶端與服務器連接工作
client.stop();
}
// 連接WiFi
void connectWiFi(){
WiFi.begin(ssid, password); // 啟動網絡連接
Serial.print("Connecting to "); // 串口監視器輸出網絡連接信息
Serial.print(ssid); Serial.println(" ..."); // 告知用戶NodeMCU正在嘗試WiFi連接
int i = 0; // 這一段程序語句用於檢查WiFi是否連接成功
while (WiFi.status() != WL_CONNECTED) { // WiFi.status()函數的返回值是由NodeMCU的WiFi連接狀態所決定的。
delay(1000); // 如果WiFi連接成功則返回值為WL_CONNECTED
Serial.print(i++); Serial.print(' '); // 此處通過While循環讓NodeMCU每隔一秒鍾檢查一次WiFi.status()函數返回值
} // 同時NodeMCU將通過串口監視器輸出連接時長讀秒。
// 這個讀秒是通過變量i每隔一秒自加1來實現的。
Serial.println(""); // WiFi連接成功后
Serial.println("Connection established!"); // NodeMCU將通過串口監視器輸出"連接成功"信息。
Serial.print("IP address: "); // 同時還將輸出NodeMCU的IP地址。這一功能是通過調用
Serial.println(WiFi.localIP()); // WiFi.localIP()函數來實現的。該函數的返回值即NodeMCU的IP地址。
}
// 利用ArduinoJson庫解析心知天氣響應信息
void parseInfo(WiFiClient client){
const size_t capacity = JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(1) + 2*JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(6) + 230;
DynamicJsonDocument doc(capacity);
deserializeJson(doc, client);
JsonObject results_0 = doc["results"][0];
JsonObject results_0_now = results_0["now"];
const char* results_0_now_text = results_0_now["text"]; // "Sunny"
const char* results_0_now_code = results_0_now["code"]; // "0"
const char* results_0_now_temperature = results_0_now["temperature"]; // "32"
const char* results_0_last_update = results_0["last_update"]; // "2020-06-02T14:40:00+08:00"
// 通過串口監視器顯示以上信息
String results_0_now_text_str = results_0_now["text"].as<String>();
int results_0_now_code_int = results_0_now["code"].as<int>();
int results_0_now_temperature_int = results_0_now["temperature"].as<int>();
String results_0_last_update_str = results_0["last_update"].as<String>();
Serial.println(F("======Weahter Now======="));
Serial.print(F("Weather Now: "));
Serial.print(results_0_now_text_str);
Serial.print(F(" "));
Serial.println(results_0_now_code_int);
Serial.print(F("Temperature: "));
Serial.println(results_0_now_temperature_int);
Serial.print(F("Last Update: "));
Serial.println(results_0_last_update_str);
Serial.println(F("========================"));
}
ESP8266獲取並解析嗶哩嗶哩API數據 (獲取 粉絲數量)
點擊查看代碼
#include <ESP8266WiFi.h>
#include "ESP8266_BiliBili.h"
const char* ssid = "chen"; // 連接WiFi名(此處使用taichimaker為示例)
// 請將您需要連接的WiFi名填入引號中
const char* password = "88888888"; // 連接WiFi密碼(此處使用12345678為示例)
// 請將您需要連接的WiFi密碼填入引號中
FansInfo fansInfo("30847042"); // 建立對象用於獲取粉絲信息
// 括號中的參數是B站的UUID
void setup(){
Serial.begin(9600);
Serial.println("");
connectWiFi(); // 連接wifi
}
void loop(){
if(fansInfo.update()){ // 更新信息成功
Serial.println("Update OK");
Serial.print("Server Response: ");
Serial.println(fansInfo.getServerCode());
Serial.print(F("Fans Number: "));
Serial.println(fansInfo.getFansNumber());
Serial.print(F("Guan Zhu: "));
Serial.println(fansInfo.getFollowing());
Serial.print(F("Hei Ming Dan: "));
Serial.println(fansInfo.getBlack());
} else { // 更新失敗
Serial.println("Update Fail...");
Serial.print("Server Response: ");
Serial.println(fansInfo.getServerCode());
}
Serial.println(F("======================"));
delay(3000);
}
// 連接WiFi
void connectWiFi(){
WiFi.begin(ssid, password); // 啟動網絡連接
Serial.print("Connecting to "); // 串口監視器輸出網絡連接信息
Serial.print(ssid); Serial.println(" ..."); // 告知用戶NodeMCU正在嘗試WiFi連接
int i = 0; // 這一段程序語句用於檢查WiFi是否連接成功
while (WiFi.status() != WL_CONNECTED) { // WiFi.status()函數的返回值是由NodeMCU的WiFi連接狀態所決定的。
delay(1000); // 如果WiFi連接成功則返回值為WL_CONNECTED
Serial.print(i++); Serial.print(' '); // 此處通過While循環讓NodeMCU每隔一秒鍾檢查一次WiFi.status()函數返回值
} // 同時NodeMCU將通過串口監視器輸出連接時長讀秒。
// 這個讀秒是通過變量i每隔一秒自加1來實現的。
Serial.println(""); // WiFi連接成功后
Serial.println("Connection established!"); // NodeMCU將通過串口監視器輸出"連接成功"信息。
Serial.print("IP address: "); // 同時還將輸出NodeMCU的IP地址。這一功能是通過調用
Serial.println(WiFi.localIP()); // WiFi.localIP()函數來實現的。該函數的返回值即NodeMCU的IP地址。
}
將以上代碼綜合一下
點擊查看代碼
#include <NTPClient.h>
// change next line to use with another board/shield
#include <ESP8266WiFi.h>
//#include <WiFi.h> // for WiFi shield
//#include <WiFi101.h> // for WiFi 101 shield or MKR1000
#include <WiFiUdp.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
#include <ArduinoJson.h>
#include "ESP8266_BiliBili.h"
Adafruit_PCD8544 display = Adafruit_PCD8544(D4,D3,D2,D1,D0);
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP,"ntp.aliyun.com");//NTP服務器地址
// 心知天氣HTTP請求所需信息
String reqUserKey = "smtq3n0ixdggurox"; // 私鑰
String reqLocation = "handan"; // 城市
String reqUnit = "c"; // 攝氏/華氏
const char* host = "api.seniverse.com"; // 將要連接的服務器地址
const int httpPort = 80; // 將要連接的服務器端口
String temperature = "";
FansInfo fansInfo("30847042"); // 建立對象用於獲取粉絲信息
// 括號中的參數是B站的UUID
void setup(){
Serial.begin(9600);//串口通訊波特率
// 建立WiFiManager對象
WiFiManager wifiManager;
// 自動連接WiFi。以下語句的參數是連接ESP8266時的WiFi名稱
wifiManager.autoConnect("AutoConnectAP");
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( "." );
}
timeClient.begin();
// + 1區 偏移3600, +8區 :3600×8 = 28800,如果注釋掉這行代碼會發現通過串口監聽器
//打印出來的時間和電腦中顯示的時間並不同步,相差8小時。
timeClient.setTimeOffset(28800);
}
void loop() {
timeClient.update();
display.begin();
display.setContrast(50);
// 建立心知天氣API當前天氣請求資源地址
String reqRes = "/v3/weather/now.json?key=" + reqUserKey +
+ "&location=" + reqLocation +
"&language=en&unit=" +reqUnit;
// 向心知天氣服務器服務器請求信息並對信息進行解析
httpRequest(reqRes);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(0,5);
display.println("Time " + timeClient.getFormattedTime());
display.display();
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(0,20);
display.println("Temp " + temperature);
display.display();
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(0,35);
display.println("Fans ");
display.display();
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(30,35);
display.println(fansInfo.getFansNumber());
display.display();
if(fansInfo.update()){ // 更新信息成功
Serial.println("Update OK");
Serial.print("Server Response: ");
Serial.println(fansInfo.getServerCode());
Serial.print(F("Fans: "));
Serial.println(fansInfo.getFansNumber());
Serial.print(F("Guan Zhu: "));
Serial.println(fansInfo.getFollowing());
Serial.print(F("Hei Ming Dan: "));
Serial.println(fansInfo.getBlack());
} else { // 更新失敗
Serial.println("Update Fail...");
Serial.print("Server Response: ");
Serial.println(fansInfo.getServerCode());
}
delay(300);
}
// 向心知天氣服務器服務器請求信息並對信息進行解析
void httpRequest(String reqRes){
WiFiClient client;
// 建立http請求信息
String httpRequest = String("GET ") + reqRes + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n";
Serial.println("");
Serial.print("Connecting to "); Serial.print(host);
// 嘗試連接服務器
if (client.connect(host, 80)){
Serial.println(" Success!");
// 向服務器發送http請求信息
client.print(httpRequest);
Serial.println("Sending request: ");
Serial.println(httpRequest);
// 獲取並顯示服務器響應狀態行
String status_response = client.readStringUntil('\n');
Serial.print("status_response: ");
Serial.println(status_response);
// 使用find跳過HTTP響應頭
if (client.find("\r\n\r\n")) {
Serial.println("Found Header End. Start Parsing.");
}
// 利用ArduinoJson庫解析心知天氣響應信息
parseInfo(client);
} else {
Serial.println(" connection failed!");
}
//斷開客戶端與服務器連接工作
client.stop();
}
// 利用ArduinoJson庫解析心知天氣響應信息
void parseInfo(WiFiClient client){
const size_t capacity = JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(1) + 2*JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(6) + 230;
DynamicJsonDocument doc(capacity);
deserializeJson(doc, client);
JsonObject results_0 = doc["results"][0];
JsonObject results_0_now = results_0["now"];
const char* results_0_now_text = results_0_now["text"]; // "Sunny"
const char* results_0_now_code = results_0_now["code"]; // "0"
const char* results_0_now_temperature = results_0_now["temperature"]; // "32"
const char* results_0_last_update = results_0["last_update"]; // "2020-06-02T14:40:00+08:00"
// 通過串口監視器顯示以上信息
String results_0_now_text_str = results_0_now["text"].as<String>();
int results_0_now_code_int = results_0_now["code"].as<int>();
int results_0_now_temperature_int = results_0_now["temperature"].as<int>();
String results_0_last_update_str = results_0["last_update"].as<String>();
Serial.println(F("======Weahter Now======="));
Serial.print(F("Weather Now: "));
Serial.print(results_0_now_text_str);
Serial.print(F(" "));
Serial.println(results_0_now_code_int);
Serial.print(F("Temperature: "));
Serial.println(results_0_now_temperature_int);
Serial.print(F("Last Update: "));
Serial.println(results_0_last_update_str);
Serial.println(F("========================"));
temperature=results_0_now_temperature_int;
}