Inspiriert von dem Projekt des Covid-Trackers mit dem Arduino Touch TFT-Display, habe ich mir überlegt: es müsste doch auch möglich sein, dies mit dem kleineren 1,7″ TFT-Display zu lösen.

Und es geht. Hat nur eine Weile gedauert, bis die Bibliotheken gepasst haben.

Die Verkabelung des Displays und die notwendigen Anpassungen am Code sind hier dokumentiert:

https://www.az-delivery.de/blogs/azdelivery-blog-fur-arduino-und-raspberry-pi/1-8-toll-tft-am-esp-32-dev-kit-c-betreiben

Danke an:

https://www.az-delivery.de/blogs/azdelivery-blog-fur-arduino-und-raspberry-pi/covid19-tracker-esp32?page=1

https://github.com/HWHardsoft/COVID19-Tracker-ESP32

https://programmer.ink/think/color-setting-and-text-display-esp32-learning-tour-arduino-version.html

Und hier deer Code:

/*
 *  Application note: Covid 19 tracker for AZ-Touch and ESP32 DEV KIT 3
 *  Version 1.1
 *  Copyright (C) 2020  Hartmut Wendt  www.zihatec.de
 *  
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * angepasst für 1,7" TFT Display
 * 2020/09/01
 *
*/ 

/*______Import Libraries_______*/
#include <Arduino.h> 
#include <SPI.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>  
#include <ArduinoHttpClient.h>
#include <TFT_eSPI.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
//

/*______End of Libraries_______*/
  
/*______End of Definitions _______*/


/*____Wifi _____________________*/
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
#define WIFI_SSID "ssid"       // Enter your SSID here
#define WIFI_PASS "password"    // Enter your WiFi password here

// Number of milliseconds to wait without receiving any data before we give up
const int kNetworkTimeout = 30*1000;
// Number of milliseconds to wait if no data is available before trying again
const int kNetworkDelay = 2000; 
/*______End of Wifi______*/


int status = WL_IDLE_STATUS;
int infected=0;
int recovered=0;
int deaths=0;


WiFiClientSecure client;
HttpClient http(client,"www.worldometers.info", 443); 

TFT_eSPI tft = TFT_eSPI();  // Pins defined in User_Setup.h

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  // initialize the TFT
  Serial.println("Init TFT ...");
  tft.begin();          
  // tft.setRotation(2); // portrait mode
  tft.fillScreen(TFT_BLACK);// clear screen 


  tft.setTextColor(TFT_WHITE);
  tft.setTextSize(1);
  tft.setCursor(10,10);
  tft.print("COVID-19-Tacker");
  tft.setCursor(10,25);
  tft.print("Data live from:");
  tft.setCursor(10,40);
  tft.print("worldometer.info");
  tft.setCursor(10,55);
  tft.print("Connecting...");
 

 // Set WiFi to station mode and disconnect from an AP if it was Previously
  // connected
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);

  // Attempt to connect to Wifi network:
  Serial.print("Connecting Wifi: ");
  Serial.println(WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  IPAddress ip = WiFi.localIP();
  Serial.println(ip); 
 

}

void loop() {
 check_country("Germany");
 delay(3000);
check_country("Italy");
 delay(3000); 
 check_country("France");
 delay(3000); 
 check_country("UK");
 delay(3000);
  check_country("Spain");
 delay(3000); 
 check_country("Russia");
 delay(3000); 
 check_country("US");
 delay(3000); 
 check_country("Brazil");
 delay(3000); 
 check_country("India");
 delay(3000);  
}


void draw_country_screen(String sCountry){
  tft.fillScreen(TFT_BLACK);// clear screen

  // headline
  tft.setCursor(5,5);
  tft.setTextColor(TFT_WHITE);
  tft.setTextSize(2);
  tft.print(sCountry + ":");
  tft.setTextSize(1);


  // infected
#define TFT_mycolor 0x061F
  tft.setCursor(10,30);
  tft.setTextColor(TFT_mycolor);
  tft.print("Infected:");
  tft.setCursor(30,45);
  tft.setTextSize(2);
  tft.print(infected);
  tft.setTextSize(1);

  // recovered
  tft.setCursor(10,75);
  tft.setTextColor(TFT_GREEN);
  tft.print("Recovered:");
  tft.setCursor(30,90);
  tft.setTextSize(2);
  tft.print(recovered);
  tft.setTextSize(1);

  // deaths
  tft.setCursor(10,120);
  tft.setTextColor(TFT_LIGHTGREY);
  tft.print("Deaths:");
  tft.setCursor(30,135);
  tft.setTextSize(2);
  tft.print(deaths); 
  tft.setTextSize(1);   
}

void check_country(String sCountry) {
  int err =0;
  int readcounter = 0;
  int read_value_step = 0;
  String s1 = "";
  String s2 = "";
  
  err = http.get("/coronavirus/country/" + sCountry +"/");
  if (err == 0)
  {
    Serial.println("startedRequest ok");

    err = http.responseStatusCode();
    if (err >= 0)
    {
      Serial.print("Got status code: ");
      Serial.println(err);

      // Usually you'd check that the response code is 200 or a
      // similar "success" code (200-299) before carrying on,
      // but we'll print out whatever response we get

      // If you are interesting in the response headers, you
      // can read them here:
      //while(http.headerAvailable())
      //{
      //  String headerName = http.readHeaderName();
      //  String headerValue = http.readHeaderValue();
      //}

      Serial.print("Request data for ");
      Serial.println(sCountry);
    
      // Now we've got to the body, so we can print it out
      unsigned long timeoutStart = millis();
      char c;
      // Whilst we haven't timed out & haven't reached the end of the body
      while ( (http.connected() || http.available()) &&
             (!http.endOfBodyReached()) &&
             ((millis() - timeoutStart) < kNetworkTimeout) )
      {
          if (http.available())
          {
              c = http.read();
              s2 = s2 + c;
              if (readcounter < 300) {
                readcounter++;
              } else {
                readcounter = 0;
                String tempString = "";
                tempString.concat(s1);
                tempString.concat(s2);
                // check infected first 
                if (read_value_step == 0) {                               
                  int place = tempString.indexOf("Coronavirus Cases:");
                  if ((place != -1) && (place < 350)) { 
                    read_value_step = 1;
                    s2 = tempString.substring(place + 15);
                    tempString = s2.substring(s2.indexOf("#aaa") + 6);
                    s1 = tempString.substring(0, (tempString.indexOf("</")));
                    s1.remove(s1.indexOf(","),1);  
                    s1.remove(s1.indexOf(","),1); // for large 7 digit numbers
                    Serial.print("Coronavirus Cases: ");
                    Serial.println(s1);
                    infected = s1.toInt();
                  }
                  
                }
                // check deaths               
                if (read_value_step == 1) {
                  int place = tempString.indexOf("Deaths:");
                  if ((place != -1) && (place < 350)) { 
                    read_value_step = 2;
                    s2 = tempString.substring(place + 15);
                    tempString = s2.substring(s2.indexOf("<span>") + 6);
                    s1 = tempString.substring(0, (tempString.indexOf("</")));
                    s1.remove(s1.indexOf(","),1);  
                    s1.remove(s1.indexOf(","),1); // for large 7 digit numbers
                    Serial.print("Deaths: ");
                    Serial.println(s1);
                    deaths = s1.toInt();
                  }
                }                
                // check recovered               
                if (read_value_step == 2) {
                  int place = tempString.indexOf("Recovered:");
                  if ((place != -1) && (place < 350)) {                   
                    s2 = tempString.substring(place + 15);
                    tempString = s2.substring(s2.indexOf("<span>") + 6);
                    s1 = tempString.substring(0, (tempString.indexOf("</")));
                    s1.remove(s1.indexOf(","),1);  
                    s1.remove(s1.indexOf(","),1); // for large 7 digit numbers
                    Serial.print("Recovered: ");
                    Serial.println(s1);
                    recovered = s1.toInt();
                    draw_country_screen(sCountry);
                    http.stop();
                    return;
                  }
                }                
      
                s1 = s2;
                s2 = ""; 
              }              
              
              // We read something, reset the timeout counter
              timeoutStart = millis();
          }
          else
          {
              // We haven't got any data, so let's pause to allow some to
              // arrive
              delay(kNetworkDelay);
          }
      }
    }
    else
    {    
      Serial.print("Getting response failed: ");
      Serial.println(err);
    }
  }
  else
  {
    Serial.print("Connect failed: ");
    Serial.println(err);
  }
  http.stop();
  
}


void printWiFiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your board's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

 

Covid-Tracker mit ESP32 und TFT-Display

Beitragsnavigation


Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert