找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5658|回复: 4
打印 上一主题 下一主题
收起左侧

ESP8266+LCD2004+DS18B20 实现网络时钟+室温

  [复制链接]
跳转到指定楼层
楼主
本帖最后由 laiycx 于 2020-8-13 13:06 编辑

半吊子新手,程序肯定有不少可以优化的地方,请指教,谢谢
注意:这图的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. //要显示的汉字编码,定义为一个数组
  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,};//点

  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); //点
  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. }
复制代码



评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏9 分享淘帖 顶4 踩
回复

使用道具 举报

沙发
ID:56960 发表于 2020-8-13 08:28 | 只看该作者
点赞+收藏+顶帖!用ESP8266 作为主控
回复

使用道具 举报

板凳
ID:28992 发表于 2021-3-24 07:31 | 只看该作者
great job, thank u!
回复

使用道具 举报

地板
ID:390236 发表于 2021-7-14 08:20 来自手机 | 只看该作者
esp8266我还玩不转
回复

使用道具 举报

5#
ID:960470 发表于 2021-8-14 10:41 | 只看该作者
向你学习
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表