找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

帖子
查看: 4820|回復(fù): 2
打印 上一主題 下一主題
收起左側(cè)

esp8266+lcd1602四線驅(qū)動接法(arduino程序)

  [復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:598987 發(fā)表于 2022-8-3 20:39 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
  • 硬件
  • esp8266開發(fā)板(ch340g)其它也差不多
  • lcd1602

  • 代碼(lcd1602四線驅(qū)動接法,省線,速度不如八線驅(qū)動)
    1. # include <LiquidCrystal.h>

    2. // 對應(yīng)gpio5,4,0,2,14,12口,5 ----> rs,4 ----> en,d4-d7 ----> 0,2,14,12
    3. LiquidCrystal lcd(5,4,0,2,14,12);//實例化,并初始化引腳
    4. void setup() {
    5.   lcd.begin(16,2);//設(shè)置行列
    6.   lcd.print("hello,word!");//打印信息
    7. }

    8. void loop() {
    9.   lcd.setCursor(0,1);//設(shè)置光標位置
    10.   lcd.print("time:");
    11.   lcd.print(millis()/1000);//計算運行時間
    12. }
    復(fù)制代碼

  • 調(diào)用了arduino的庫 ----> LiquidCrystal
  • 4位數(shù)據(jù)線接法:LiquidCrystal(rs,enable,d4,d5,d6,d7)
    • LiquidCrystal(rs,rw,enable,d4,d5,d6,d7)
    8位數(shù)據(jù)線接法:
    • LiquidCrystal(rs,enable,d0,d1,d2,d3,d4,d5,d6,d7)
    • LiquidCrystal(rs,rw,enable,d0,d1,d2,d3,d4,d5,d6,d7)
    參數(shù):
    • rs,連接到RS的Arduino引腳。
    • rw,連接到R/W的Arduino引腳。
    • enable,連接到E的Arduino引腳。
    • d0,d1,d2,d3,d4,d5,d6,d7數(shù)據(jù)引腳。
  • 接線
  • 注釋:最后把背光接上,因為esp8266輸出是3.3v,lcd1602需要5v才顯示清楚,所以顯示效果會很淡
  • 或者,直接把lcd1602的vdd接到esp8266的vin,因為vin輸出是5v。如果屏幕顯示不清晰,把lcd1602的v0(vee)和esp8266的gnd串聯(lián)一個可調(diào)電阻,調(diào)一下就清晰了

  • 運行效果

評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏7 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

沙發(fā)
ID:598987 發(fā)表于 2022-8-4 20:48 | 只看該作者
  • 附上代碼二,在上面的基礎(chǔ)上。添加 心知天氣 (api申請)的顯示,增加一點實用性
  • 主要代碼來自于 太極創(chuàng)客,
    1. /**********************************************************************
    2.   項目名稱/Project          : 零基礎(chǔ)入門學(xué)用物聯(lián)網(wǎng)
    3.   程序名稱/Program name     : weather_now
    4.   團隊/Team                : 太極創(chuàng)客團隊 / Taichi-Maker
    5.   作者/Author              : CYNO朔
    6.   日期/Date(YYYYMMDD)     : 20200602
    7.   程序目的/Purpose          :
    8.   通過心知天氣免費服務(wù)獲取實時天氣信息。
    9.   -----------------------------------------------------------------------
    10.   其它說明 / Other Description
    11.   心知天氣API文檔說明:

    12.   本程序為太極創(chuàng)客團隊制作的免費視頻教程《零基礎(chǔ)入門學(xué)用物聯(lián)網(wǎng) 》中一部分。該教程系統(tǒng)的
    13.   向您講述ESP8266的物聯(lián)網(wǎng)應(yīng)用相關(guān)的軟件和硬件知識。
    14. ***********************************************************************/
    15. #include <ArduinoJson.h>
    16. #include <ESP8266WiFi.h>
    17. #include <LiquidCrystal.h>

    18. const char* ssid     = "bonfire";       // 連接WiFi名(此處使用taichi-maker為示例)
    19. // 請將您需要連接的WiFi名填入引號中
    20. const char* password = "1234567800";          // 連接WiFi密碼(此處使用12345678為示例)
    21. // 請將您需要連接的WiFi密碼填入引號中

    22. const char* host = "api.知心天氣的服務(wù)器";     // 將要連接的服務(wù)器地址
    23. const int httpPort = 80;                    // 將要連接的服務(wù)器端口

    24. // 心知天氣HTTP請求所需信息
    25. String reqUserKey = "xxxxxxxxxxxxxx";   // 私鑰
    26. String reqLocation = "22.98486:114.7199";  // 城市
    27. String reqUnit = "c";                      // 攝氏/華氏

    28. LiquidCrystal lcd(5, 4, 0, 2, 14, 12);  // 實例化lcd驅(qū)動

    29. void setup() {
    30.   lcd.begin(16, 2); //設(shè)置行列
    31.   lcd.print("Hello Joie ^v^");//打印信息
    32.   Serial.begin(9600);
    33.   Serial.println("");

    34.   // 連接WiFi
    35.   connectWiFi();
    36. }

    37. void loop() {
    38.   // 建立心知天氣API當前天氣請求資源地址
    39.   String reqRes = "/v3/weather/now.json?key=" + reqUserKey +
    40.                   + "&location=" + reqLocation +
    41.                   "&language=en&unit=" + reqUnit;

    42.   // 向心知天氣服務(wù)器服務(wù)器請求信息并對信息進行解析
    43.   httpRequest(reqRes);
    44.   delay(3000);

    45. //  lcd.clear();
    46. //  lcd.setCursor(0, 1); //設(shè)置光標位置
    47. //  lcd.print("time:");
    48. //  lcd.print(millis() / 1000); //計算運行時間
    49. }

    50. // 向心知天氣服務(wù)器服務(wù)器請求信息并對信息進行解析
    51. void httpRequest(String reqRes) {
    52.   WiFiClient client;

    53.   // 建立http請求信息
    54.   String httpRequest = String("GET ") + reqRes + " HTTP/1.1\r\n" +
    55.                        "Host: " + host + "\r\n" +
    56.                        "Connection: close\r\n\r\n";
    57.   Serial.println("");
    58.   Serial.print("Connecting to "); Serial.print(host);

    59.   // 嘗試連接服務(wù)器
    60.   if (client.connect(host, 80)) {
    61.     Serial.println(" Success!");

    62.     // 向服務(wù)器發(fā)送http請求信息
    63.     client.print(httpRequest);
    64.     Serial.println("Sending request: ");
    65.     Serial.println(httpRequest);

    66.     // 獲取并顯示服務(wù)器響應(yīng)狀態(tài)行
    67.     String status_response = client.readStringUntil('\n');
    68.     Serial.print("status_response: ");
    69.     Serial.println(status_response);

    70.     // 使用find跳過HTTP響應(yīng)頭
    71.     if (client.find("\r\n\r\n")) {
    72.       Serial.println("Found Header End. Start Parsing.");
    73.     }

    74.     // 利用ArduinoJson庫解析心知天氣響應(yīng)信息
    75.     parseInfo(client);
    76.   } else {
    77.     Serial.println(" connection failed!");
    78.   }
    79.   //斷開客戶端與服務(wù)器連接工作
    80.   client.stop();
    81. }

    82. // 連接WiFi
    83. void connectWiFi() {
    84.   WiFi.begin(ssid, password);                  // 啟動網(wǎng)絡(luò)連接
    85.   Serial.print("Connecting to ");              // 串口監(jiān)視器輸出網(wǎng)絡(luò)連接信息
    86.   Serial.print(ssid); Serial.println(" ...");  // 告知用戶NodeMCU正在嘗試WiFi連接

    87.   int i = 0;                                   // 這一段程序語句用于檢查WiFi是否連接成功
    88.   while (WiFi.status() != WL_CONNECTED) {      // WiFi.status()函數(shù)的返回值是由NodeMCU的WiFi連接狀態(tài)所決定的。
    89.     delay(1000);                               // 如果WiFi連接成功則返回值為WL_CONNECTED
    90.     Serial.print(i++); Serial.print(' ');      // 此處通過While循環(huán)讓NodeMCU每隔一秒鐘檢查一次WiFi.status()函數(shù)返回值
    91.     lcd.setCursor(0,1);
    92.     lcd.print(i++);
    93.   }                                            // 同時NodeMCU將通過串口監(jiān)視器輸出連接時長讀秒。
    94.   // 這個讀秒是通過變量i每隔一秒自加1來實現(xiàn)的。
    95.   Serial.println("");                          // WiFi連接成功后
    96.   Serial.println("Connection established!");   // NodeMCU將通過串口監(jiān)視器輸出"連接成功"信息。
    97.   Serial.print("IP address:    ");             // 同時還將輸出NodeMCU的IP地址。這一功能是通過調(diào)用
    98.   Serial.println(WiFi.localIP());              // WiFi.localIP()函數(shù)來實現(xiàn)的。該函數(shù)的返回值即NodeMCU的IP地址。
    99. }

    100. // 利用ArduinoJson庫解析心知天氣響應(yīng)信息
    101. void parseInfo(WiFiClient client) {
    102.   const size_t capacity = JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(1) + 2 * JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(6) + 230;
    103.   DynamicJsonDocument doc(capacity);

    104.   deserializeJson(doc, client);

    105.   JsonObject results_0 = doc["results"][0];

    106.   JsonObject results_0_now = results_0["now"];
    107.   const char* results_0_now_text = results_0_now["text"]; // "Sunny"
    108.   const char* results_0_now_code = results_0_now["code"]; // "0"
    109.   const char* results_0_now_temperature = results_0_now["temperature"]; // "32"

    110.   const char* results_0_last_update = results_0["last_update"]; // "2020-06-02T14:40:00+08:00"

    111.   // 通過串口監(jiān)視器顯示以上信息
    112.   String results_0_now_text_str = results_0_now["text"].as<String>();
    113.   int results_0_now_code_int = results_0_now["code"].as<int>();
    114.   String results_0_now_temperature_str = results_0_now["temperature"].as<String>();

    115.   String results_0_last_update_str = results_0["last_update"].as<String>();

    116.   Serial.println(F("======Weahter Now======="));
    117.   Serial.print(F("Weather Now: "));
    118.   Serial.print(results_0_now_text_str);
    119.   Serial.print(F(" "));
    120.   Serial.println(results_0_now_code_int);
    121.   Serial.print(F("Temperature: "));
    122.   Serial.print(results_0_now_temperature_str);
    123.   Serial.println(F(" C"));
    124.   Serial.print(F("Last Update: "));
    125.   Serial.println(results_0_last_update_str);
    126.   Serial.println(F("========================"));

    127.   // 設(shè)置lcd1602顯示信息
    128.   lcd.clear();
    129. //  delay(1000);
    130.   lcd.setCursor(0, 0);
    131.   lcd.print(results_0_now_text_str + " " + results_0_now_temperature_str + "c");
    132.   lcd.setCursor(0, 1);
    133.   lcd.print(results_0_last_update_str);
    134. }
    復(fù)制代碼


評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 回帖助人的獎勵!

查看全部評分

回復(fù)

使用道具 舉報

板凳
ID:830316 發(fā)表于 2023-12-31 13:35 | 只看該作者
時間哪里沒有實時更新呢,博主
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表