使用ESP8266显示实时公交

引言

最近在学习物联网方面的知识,由于正好最近天天要坐公交,老是打开微信再启动一个小程序来查看,这可不是我的风格。
于是从零开始学习ESP8622 WIFI模块,再配上一块OLED显示屏,因为USB供电一般是5.1v,再买了一个3.3v降压稳压模块。

亮相

esp8622
oled
3.3v
usb

简单说一下接线

使用USB TO TTL上传代码时,IO0需要接GND才能进入刷机模式
line

代码

/*
       Next Bus
      86: -1;-1
      23: -1;-1
   Update 16:26:07
*/

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecureBearSSL.h>
#include <ArduinoJson.h>

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

ESP8266WiFiMulti WiFiMulti;

// https 证书指纹 aecd3b*********************************feab
const uint8_t fingerprint[20] = {0xae, 0xcd, 0x3b, ..., 0xfe, 0xab};
std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);

Adafruit_SSD1306 display(4);

const String zero("0");
// current timestamp
int timestamp = 0;
String crtShow = "";


void setup() {
  Wire.begin(0, 2);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.setTextColor(WHITE);
  display.clearDisplay();
  display.setTextSize(2);

  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFiMulti.addAP("WIFI_SSID", "WIFI_PASS");

  client->setFingerprint(fingerprint);

  println("Connecting wifi.");
  delay(1000);
  while (WiFiMulti.run() != WL_CONNECTED) {
    println("Connecting wifi..");
    delay(500);
  }
}

void loop() {

  // wait for WiFi connection
  while (WiFiMulti.run() != WL_CONNECTED) {
    println("Connecting wifi...");
  }

  if (timestamp % 10 == 0) {
    String bus86 = httpGet("https://busapi.proxy.itmx.xyz/*********");
    String bus23 = httpGet("https://busapi.proxy.itmx.xyz/*********");
    crtShow = " Next Bus/n86:" + bus86 + "23:" + bus23 + " ";
  } else {
    timestamp++;
  }
  println(crtShow + getCrtTime());

  delay(1000);
}

void println(String line) {
  display.clearDisplay();
  display.setCursor(0, 0);
  display.println(line);
  display.display();
  Serial.println(line);
}

void setCrtTime(String date) {
  int pos = date.indexOf(':');
  if (pos == -1) {
    return;
  }
  timestamp = atoi(date.substring(pos - 2, pos).c_str()) * 3600 +
              atoi(date.substring(pos + 1, pos + 3).c_str()) * 60   +
              atoi(date.substring(pos + 4, pos + 6).c_str());
}

String getCrtTime() {
  int h = (timestamp / 3600 + 8) % 24, m = timestamp % 3600 / 60, s = timestamp % 60;
  return String(h < 10 ? zero + h : h) + ":" + (m < 10 ? zero + m : m) + ":" + (s < 10 ? zero + s : s);
}

String httpGet(String url) {
  HTTPClient https;
  String result;

  // 请求接口
  if (https.begin(*client, url)) {
    https.addHeader("content-type", "application/json");
    https.addHeader("user-agent", "");
    https.addHeader("connection", "close");
    const char *keys[] = {"Date"};
    https.collectHeaders(keys, 1);

    if (https.GET() == HTTP_CODE_OK) {
      setCrtTime(https.header("Date"));

      String payload = https.getString();
      int posS = payload.indexOf('[');
      int posE = payload.indexOf('}', posS);
      if (posS != -1 && posE != -1) {
        DynamicJsonDocument doc(JSON_OBJECT_SIZE(6) + 60);
        payload = payload.substring(posS + 1, posE + 1);
        if (!deserializeJson(doc, payload)) {
          int c = doc["count"].as<int>(); // 获取距离当前站还有几个站
          int t = doc["time"].as<int>();  // 大约还有多少分钟到达本站(家门口的公交站)
          result = (c < 10 && c >= 0 ? zero + c : c) + "s " + (t < 10 && t >= 0 ? zero + t : t) + "m";
        } else {
          result = "-2;-2";
        }
      } else {
        result = "-3;-3";
      }
    } else {
      result = "-4;-4";
    }
  } else {
    result = "-5;-5";
  }
  https.end();
  return result;
}

参考资料

WIFI模块ESP8266使用方法
【Arduino教程】第十三课:Arduino通过ESP8266接入互联网
怎样使用ESP8266-01引脚和指示灯
Github仓库,大量示例

成品

result0
result2
result1

害,但凡有个3D打印机,也不至于丑成这样啊。
之前在学习的时候跟朋友一起买了个3D打印机,没用几次就成装饰品在角落落灰了。。。

版权声明:
作者:Zad
链接:https://www.techfm.club/p/1043.html
来源:TechFM
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>