1.注意原图
接线参考pwm
这里使用的是tft显示屏
接线图
2.下载库文件
https://github.com/adafruit/Adafruit-ST7735-Library
https://github.com/adafruit/Adafruit-GFX-Library
3.测试显示屏
#include <Adafruit_GFX.h> //Libraries required to use the Display
#include <Adafruit_ST7735.h>
#include <SPI.h>
#define TFT_CS D1 //Display pins
#define TFT_RST D0
#define TFT_DC D2
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST); //Those things are for the display
float p = 3.1415926;
void setup() {
Serial.begin(9600); //Start the serial communication and the display
tft.initR(INITR_GREENTAB);
}
void loop()
{
Print_TFT(); //Display to the TFT LCD, this function will vary depending on your display
delay(5000);
}
void Print_TFT(){
tft.fillScreen(ST77XX_BLACK);
tft.setTextWrap(false);
tft.setCursor(20, 10); //Horiz/Vertic
tft.setTextSize(2);
tft.setTextColor(ST77XX_GREEN);
tft.print("USA");
tft.setCursor(15, 30); //Horiz/Vertic
tft.setTextSize(1);
tft.setTextColor(ST77XX_WHITE);
tft.print("COVID-19 Tracker");
}
所有程序:
#include <Adafruit_GFX.h> //Libraries required to use the Display
#include <Adafruit_ST7735.h>
#include <SPI.h>
#include <ESP8266WiFi.h> //Use ESP8266 functions
#include <ESP8266HTTPClient.h>
#define TFT_CS D1 //Display pins
#define TFT_RST D0
#define TFT_DC D2
const char* ssid = "yang"; //Your router SSID and password
const char* password = "19880927";
const char* host = "api.thingspeak.com"; //We read the data from this host
const int httpPortRead = 80;
const char* url1 = "/apps/thinghttp/send_request?api_key=UT0T1VC08JWKL43N"; //Those URLs are mine but I kept them so you can replace the key only
const char* url2 = "/apps/thinghttp/send_request?api_key=D4HW8EAQ3IIL6XRJ";
const char* url3 = "/apps/thinghttp/send_request?api_key=K5IVK4JAGBW24A8P";
const char* url4 = "/apps/thinghttp/send_request?api_key=MR8T0R2CTFZWYWQO";
String Cases,Death,Recover,Data_Raw,total; //Here I keep the numbers that I got
WiFiClient client; //Create a WiFi client and http client
HTTPClient http;
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST); //Those things are for the display
void setup() {
Serial.begin(9600); //Start the serial communication and the display
tft.initR(INITR_GREENTAB);
WiFi.disconnect(); //Disconnect and reconnect to the Wifi you set
delay(1000);
WiFi.begin(ssid, password);
Serial.println("Connected to the WiFi network"); //Display feedback on the serial monitor
Serial.println(WiFi.localIP());
}
//In the loop I read every URL separately and I get the data string then I remove the unecessary characters
//I keep only the values that I can display either on the serial monitor or the display
//I believe that this part is repeated 3 times and can be set on a single function that can be called
//Keep updated on www.SurtrTech.com for an optimized code
void loop()
{
//Reading 1: Reading of cases
if( http.begin(host,httpPortRead,url1)) //Connect to the host and the url
{
int httpCode = http.GET(); //Check feedback if there's a response
if (httpCode > 0)
{
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY)
{
Data_Raw = http.getString(); //Here we store the raw data string
Serial.print("Cases (RAW value) :"); //I choosed to display it on the serial monitor to help you debug
Serial.println(Data_Raw);
Cases=Data_Raw;
}
}
else //If we can't get data
{
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
else //If we can't connect to the HTTP
{
Serial.printf("[HTTP} Unable to connect\n");
}
//Reading 2 is the same thing as reading 1
if( http.begin(host,httpPortRead,url2))
{
int httpCode = http.GET();
if (httpCode > 0)
{
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY)
{
Data_Raw = http.getString();
Serial.print("Deaths (RAW value) :");
Serial.println(Data_Raw);
Death=Data_Raw;
}
}
else
{
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
else
{
Serial.printf("[HTTP} Unable to connect\n");
}
//Reading 3 is the same thing as reading 1
if( http.begin(host,httpPortRead,url3))
{
int httpCode = http.GET();
if (httpCode > 0)
{
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY)
{
Data_Raw = http.getString();
Serial.print("Recoveries (RAW value) :");
Serial.println(Data_Raw);
Recover=Data_Raw;
}
}
else
{
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
else
{
Serial.printf("[HTTP} Unable to connect\n");
}
//http4
if( http.begin(host,httpPortRead,url4)) //Connect to the host and the url
{
int httpCode = http.GET(); //Check feedback if there's a response
if (httpCode > 0)
{
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY)
{
Data_Raw = http.getString(); //Here we store the raw data string
Serial.print("time (RAW value) :"); //I choosed to display it on the serial monitor to help you debug
Serial.println(Data_Raw);
total=Data_Raw;
}
}
else //If we can't get data
{
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
else //If we can't connect to the HTTP
{
Serial.printf("[HTTP} Unable to connect\n");
}
Print_TFT(); //Display to the TFT LCD, this function will vary depending on your display
delay(5000);
while (WiFi.status() != WL_CONNECTED) //In case the Wifi connexion is lost
{
WiFi.disconnect();
delay(1000);
WiFi.begin(ssid, password);
Serial.println("Reconnecting to WiFi..");
delay(10000);
}
}
void Print_TFT(){
tft.fillScreen(ST77XX_BLACK);
tft.setTextWrap(false);
tft.setCursor(0,0);
tft.setTextSize(1);
tft.setTextColor(ST77XX_BLUE);
tft.print("total:");
tft.print(total);
tft.setCursor(20, 10); //Horiz/Vertic
tft.setTextSize(2);
tft.setTextColor(ST77XX_GREEN);
tft.print("USA");
tft.setCursor(15, 30); //Horiz/Vertic
tft.setTextSize(1);
tft.setTextColor(ST77XX_WHITE);
tft.print("COVID-19 Tracker");
tft.setCursor(0, 55); //Horiz/Vertic
tft.setTextSize(2);
tft.setTextColor(ST77XX_RED);
tft.print("Cases");
tft.setCursor(0, 80);
tft.print(Cases);
tft.setCursor(0, 115); //Horiz/Vertic
tft.setTextSize(1);
tft.setTextColor(ST77XX_GREEN);
tft.print("RECOVERED");
tft.setCursor(60, 115);
tft.setTextColor(ST77XX_WHITE);
tft.print(Recover);
tft.fillRect(0, 138 , 38, 10, ST77XX_WHITE);
tft.setCursor(2, 140); //Horiz/Vertic
tft.setTextSize(1);
tft.setTextColor(ST77XX_BLACK);
tft.print("DEATHS");
tft.setCursor(60, 140);
tft.setTextColor(ST77XX_WHITE);
tft.print(Death);
}
·
效果
网友评论