Arduino IED 开发环境搭建及ESP8266 WiFi网络时钟+OLED显示


Arduino IDE是由 Arduino 官方提供的支持 C 语言的集成开发环境,主要是针对 Arduino 系列的开发板进行编程。
通过简单的配置,可以在原本的编程环境里添加上对 ESP8266 开发板的支持。对于熟悉 Arduino 函数库和开发流程的用户,基本上没有任何使用上的区别。
 
一、添加ESP8266
首先从 Arduino官网(https://www.arduino.cc/en/software)下载最新版本的 Arduino IDE 软件并安装。
安装完成以后,进入 首选项(Preferences),找到 附加开发板管理器地址(Additional Board Manager URLs),并在其后添加如下信息:
http://arduino.esp8266.com/stable/package_esp8266com_index.json

 

 之后点击工具 - 开发板 - 开发板管理器,进入开发板管理器界面:

 

 在搜索栏找到ESP8266并安装:

 

安装完成后,重启Arduino IED软件。在工具 - 开发板选项中即会看到ESP8266开发板选项:

注意:根据自己的开发板选型

 

 

 二、WiFi网络时钟+OLED

  • 一块Esb8266 NodeMCU CH340开发版
  • 一块0.96寸OLED 屏幕(4针)
  • 4根杜邦线

 

 OLED屏幕 ---------- ESP8266

    GND ---------- G

    VCC ----------- 3V

    SCL ------------ D1

    SDA ------------ D2

 实物图-

 

一定找根带有数据传输功能的数据线接到电脑上。(上次被坑了,一直不显示串口!!)

装CH340驱动(啥,不会?不会找百度!),然后在设备管理器就可以看到了

 

 OK上面步骤都没问题了,上干货

 

  1 /*
  2   注意:
  3   有的库需要自己安装或更新
  4 */
  5 
  6 #include <Arduino.h>
  7 #include <ESP8266WiFi.h>
  8 #include <ESP8266WiFiMulti.h>
  9 #include <ESP8266HTTPClient.h>
 10 #include <WiFiClient.h>
 11 #include <ArduinoJson.h>
 12 #include <U8g2lib.h>
 13 
 14 #ifdef U8X8_HAVE_HW_SPI
 15 #include <SPI.h>
 16 #endif
 17 #ifdef U8X8_HAVE_HW_I2C
 18 #include <Wire.h>
 19 #endif
 20 
 21 U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);   // All Boards without Reset of the Display
 22 
 23 
 24 ESP8266WiFiMulti WiFiMulti;
 25 WiFiClient client;
 26 HTTPClient http;
 27 
 28 String payload = "";
 29 
 30 bool HttpDateFlag = false;
 31 
 32 char ssid[] = "你的WiFi名称";
 33 char pswd[] = "密码";
 34 
 35 void GetHttpDate()
 36 {
 37     //等待连接
 38     while(WiFiMulti.run() != WL_CONNECTED)
 39     {
 40         delay(200);
 41     }
 42 
 43     if ((WiFiMulti.run() == WL_CONNECTED))
 44     {
 45         http.begin(client, "http://quan.suning.com/getSysTime.do");//苏宁网站提供的网络授时
 46 
 47         int httpCode = http.GET();
 48 
 49         if (httpCode > 0)
 50         {
 51             HttpDateFlag = true;
 52             // file found at server
 53             if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY)
 54             {
 55                 payload = http.getString();
 56                 Serial.println(payload);
 57             }
 58         }
 59         else
 60         {
 61             Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
 62         }
 63 
 64         http.end();
 65     }
 66 }
 67 void HttpDateHandle()
 68 {
 69     const size_t capacity = JSON_OBJECT_SIZE(2) + 60;
 70     DynamicJsonBuffer jsonBuffer(capacity);
 71     
 72     JsonObject& root = jsonBuffer.parseObject(payload);
 73 
 74     const char* sysTime2 = root["sysTime2"]; // "2021-07-17 15:26:56"
 75     const char* sysTime1 = root["sysTime1"]; // "20210807170356"
 76     Serial.println(sysTime2);
 77     u8g2.setFont(u8g2_font_wqy15_t_chinese2); 
 78     u8g2.setFontDirection(0);
 79     u8g2.clearBuffer();
 80     u8g2.setCursor(0, 15);
 81     u8g2.println("北京时间");
 82     u8g2.setCursor(0, 40);
 83     u8g2.println(sysTime2);
 84     u8g2.setCursor(0, 60);
 85     u8g2.print(sysTime2+11);
 86     u8g2.sendBuffer();
 87 }
 88 
 89 void setup() 
 90 {
 91     u8g2.begin();
 92     u8g2.enableUTF8Print();   
 93 
 94     Serial.begin(115200);
 95 
 96     Serial.println();
 97 
 98     WiFi.mode(WIFI_STA);
 99     WiFiMulti.addAP(ssid, pswd);
100 }
101 
102 void loop() 
103 {
104     GetHttpDate();
105     if(HttpDateFlag)
106     {
107         HttpDateHandle();
108         HttpDateFlag = false;
109     }
110     //delay(10000);
111 }

 

 

效果图:

 

 

 

 

 

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM