久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 6829|回復(fù): 4
打印 上一主題 下一主題
收起左側(cè)

ESP8266+LCD2004+DS18B20 實現(xiàn)網(wǎng)絡(luò)時鐘+室溫

  [復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:798252 發(fā)表于 2020-8-12 18:00 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
本帖最后由 laiycx 于 2020-8-13 13:06 編輯

半吊子新手,程序肯定有不少可以優(yōu)化的地方,請指教,謝謝
注意:這圖的BS18B20的Gnd和IO腳接到D1的時候畫反了,不好意思。


  1. /*  ---By gsmcable---
  2.               Connect
  3.         Arduino       I2C_LCD
  4.           5V            VCC
  5.           GND           GND
  6.       D2  SDA           SDA
  7.       D1  SCL           SCL
  8.       D4  DS18B20
  9. */

  10. #include <ESP8266WiFi.h>
  11. #include <OneWire.h>
  12. #include <DallasTemperature.h>
  13. #include <WiFiUdp.h>
  14. #include <WiFiClientSecure.h>
  15. #include <NTPClient.h>
  16. #include <Time.h>
  17. #include <TimeLib.h>
  18. #include <Timezone.h>
  19. #include <Wire.h>
  20. #include <LiquidCrystal_I2C.h>
  21. LiquidCrystal_I2C lcd(0x27, 20, 4);

  22. #define ONE_WIRE_BUS 2  // DS18B20 pin
  23. OneWire oneWire(ONE_WIRE_BUS);
  24. DallasTemperature DS18B20(&oneWire);

  25. float oldTemp;

  26. // Define NTP properties
  27. #define NTP_OFFSET   60 * 60      // In seconds
  28. #define NTP_INTERVAL 60 * 1000    // In miliseconds
  29. #define NTP_ADDRESS  "asia.pool.ntp.org"  // change this to whatever pool is closest (see ntp.org)

  30. // Set up the NTP UDP client
  31. WiFiUDP ntpUDP;
  32. NTPClient timeClient(ntpUDP, NTP_ADDRESS, NTP_OFFSET, NTP_INTERVAL);

  33. const char* ssid = "xxx";                 // insert your own ssid
  34. const char* password = "xxx";        // and your wifi password

  35. String date; //create the string for the date which will be printed on the lcd screen below
  36. String t;    // create the string for the time

  37. //顯示字符
  38. #if defined(ARDUINO) && ARDUINO >= 100
  39. #define printByte(args)  write(args);
  40. #else
  41. #define printByte(args)  print(args,BYTE);
  42. #endif

  43. //要顯示的漢字編碼,定義為一個數(shù)組
  44. uint8_t nian[8] = {0x08,0x0f,0x12,0x0f,0x0a,0x1f,0x02,0x02,};//年
  45. uint8_t yue[8]  = {0x0f,0x09,0x0f,0x09,0x0f,0x09,0x0b,0x11,};//月
  46. uint8_t ri[8]   = {0x1F,0x11,0x11,0x1F,0x11,0x11,0x1F,0x00,};//日
  47. uint8_t dian[8] = {0x02,0x05,0x02,0x00,0x00,0x00,0x00,0x00,};//點(diǎn)

  48. const char * days[] = {"Sunday", "Monday", "Tuesday", "**Wed**", "*Thurs*", "Friday", "**Sat*"} ;//my screen is 20 across to can't fit in Wednesday
  49. const char * months[] = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"} ;
  50. const char * ampm[] = {"AM", "PM"} ;//not used in my version

  51. void setup()
  52. {

  53.   Serial.begin(115200); // most D1 use 115200 but this could vary. Included for serial monitor debugging
  54.   timeClient.begin();   // Start the NTP UDP client

  55.   oldTemp = -1;

  56.   int cursorPosition = 0;

  57.   lcd.init();                      // initialize the lcd
  58.   lcd.backlight();
  59.   lcd.setCursor(0, 0);
  60.   lcd.print("Connect");
  61.   lcd.setCursor(0, 2);
  62.   lcd.print(ssid);

  63.   // Connect to wifi
  64.   Serial.println("");
  65.   Serial.print("Connecting to ");
  66.   Serial.print(ssid);

  67.   WiFi.begin(ssid, password);
  68.   while (WiFi.status() != WL_CONNECTED)
  69.   {
  70.     delay(500);
  71.     Serial.print(".");
  72.   }
  73.   Serial.println("");
  74.   Serial.print("Connected to WiFi at ");
  75.   Serial.print(WiFi.localIP());
  76.   Serial.println("");
  77.   delay(1000);

  78.   lcd.clear();
  79. }

  80. void loop()
  81. {

  82.   float temp;
  83.   
  84.     DS18B20.requestTemperatures();
  85.     temp = DS18B20.getTempCByIndex(0);
  86.     Serial.print("Temperature: ");
  87.     Serial.println(temp);
  88.   
  89.   //////////////////////// The first part of the loop is for the internet clock
  90.   
  91.   if (WiFi.status() == WL_CONNECTED) //Check WiFi connection status
  92.   {
  93.     date = "";  // clear the variables
  94.     t = "";

  95.     // update the NTP client and get the UNIX UTC timestamp
  96.     timeClient.update();
  97.     unsigned long epochTime =  timeClient.getEpochTime();

  98.   // convert received time stamp to time_t object
  99.     time_t local, utc;
  100.     utc = epochTime;
  101.     TimeChangeRule BST = {"BST", Last, Sun, Mar, 1, 420};   //British Summer Time - change these variables for your local time
  102.     TimeChangeRule GMT = {"GMT", Last, Sun, Oct, 2, 360};   //Standard Time
  103.     Timezone CN(BST, GMT);
  104.     local = CN.toLocal(utc);

  105.   // format the time to 12-hour format with AM/PM and add seconds. t (time) is made up of the variables below which are then printed as a string
  106.     t += hour(local);
  107.     t += ":";
  108.     if (minute(local) < 10) // add a zero if minute is under 10
  109.     t += "0";
  110.     t += minute(local);
  111.     t += ":";
  112.     if (second(local) < 10)
  113.     t += "0";
  114.     t += second(local);
  115.   //t += ampm[isPM(local)];

  116.   // Display the date and time
  117.     Serial.println("");
  118.     Serial.print("Local date: ");
  119.     Serial.print(date);
  120.     Serial.println("");
  121.     Serial.print("Local time: ");
  122.     Serial.print(t);

  123.     lcd.createChar(1, nian);
  124.     lcd.createChar(2, yue);
  125.     lcd.createChar(3, ri);
  126.     lcd.createChar(4, dian);
  127.    
  128.     lcd.setCursor(1, 0);
  129.     lcd.print(year(local));
  130.     lcd.setCursor(5, 0);
  131.     lcd.printByte(1); //年
  132.     lcd.setCursor(6, 0);
  133.     lcd.print(months[month(local) - 1]);
  134.     lcd.setCursor(8, 0);
  135.     lcd.printByte(2); //月
  136.     lcd.setCursor(9, 0);
  137.     lcd.print(day(local));
  138.     lcd.setCursor(11, 0);
  139.     lcd.printByte(3); //日
  140.     lcd.setCursor(13, 0);
  141.     lcd.print(days[weekday(local) - 1]);
  142.     lcd.setCursor(3, 1);
  143.     lcd.print("Time:");
  144.     lcd.setCursor(9, 1);
  145.     lcd.print(t);
  146.     lcd.setCursor(1, 2);
  147.     lcd.print("------------------");
  148.     lcd.setCursor(3, 3);
  149.     lcd.print("Temp:");
  150.     lcd.setCursor(9, 3);
  151.     lcd.print(temp);
  152.     lcd.setCursor(14, 3);
  153.     lcd.printByte(4); //點(diǎn)
  154.     lcd.setCursor(15, 3);
  155.     lcd.print("C");
  156.   }
  157.   else // this part is a step to attempt to connect to wifi again if disconnected
  158.   {
  159.     lcd.setCursor(0, 0);
  160.     lcd.print("Connect");

  161.     //display.display();
  162.     WiFi.begin(ssid, password);
  163.     //display.drawString(0, 24, "Connected.");

  164.     lcd.clear();
  165.     lcd.setCursor(0, 1);
  166.     lcd.print("Connected.");

  167.     delay(1000);
  168.    
  169.   }
  170. }
復(fù)制代碼



評分

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

查看全部評分

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

使用道具 舉報

沙發(fā)
ID:56960 發(fā)表于 2020-8-13 08:28 | 只看該作者
點(diǎn)贊+收藏+頂帖!用ESP8266 作為主控
回復(fù)

使用道具 舉報

板凳
ID:28992 發(fā)表于 2021-3-24 07:31 | 只看該作者
great job, thank u!
回復(fù)

使用道具 舉報

地板
ID:390236 發(fā)表于 2021-7-14 08:20 來自手機(jī) | 只看該作者
esp8266我還玩不轉(zhuǎn)
回復(fù)

使用道具 舉報

5#
ID:960470 發(fā)表于 2021-8-14 10:41 | 只看該作者
向你學(xué)習(xí)
回復(fù)

使用道具 舉報

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

本版積分規(guī)則

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

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

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 中文字幕综合在线 | 中文字幕在线观看第一页 | 国产精品日韩一区 | 久久久久久久久久久久91 | 国产高清精品一区二区三区 | 国产激情视频网站 | 综合在线视频 | 日韩一区二区视频 | 精品伊人| 久久国产精品一区二区三区 | 一级毛片在线播放 | 一级a性色生活片久久毛片波多野 | www国产成人免费观看视频,深夜成人网 | 亚洲国产激情 | 四虎成人在线播放 | 91精品91久久久 | 一区二区视频在线 | 91视频在线 | 老子午夜影院 | 国产成人高清视频 | 色婷婷av久久久久久久 | 国产免费一级片 | 亚洲一区二区三区视频 | 91精品国产一区二区三区 | 中文字幕一区二区三区不卡在线 | 久久精品欧美一区二区三区不卡 | 成人美女免费网站视频 | 国产精品综合色区在线观看 | 国产综合在线视频 | 国产成人精品一区二区三区四区 | 羞羞视频在线观看网站 | aaa在线观看 | 尤物视频在线免费观看 | 日韩国产中文字幕 | 在线视频 亚洲 | 免费a大片 | 伊人久久一区二区 | 国内精品久久久久 | 自拍偷拍欧美 | 久久久久成人精品 | 欧美一级欧美三级在线观看 |