Hello!I 've build a coronavirus program which uses an api and downloads data to my screen.I m displaying data every 10 seconds from the world and my country.I have a question though.I want to compare the last day's value of critically ill patients to the current day value.The thing is that the current day value might change a lot of times during the day or might not change at all.So I can't do it tradisionally by giving the new variable the value of the old one,if they are different..
So I was thinking.Is there a way to store a value to a var(like crit_old) for 24 hours and while my program runs (every 10 seconds) compare the var (crit_new) to the var (crit_old) and tells me the percentage difference?But (and here is the tricky part) after 24 hours (lets say in the morning) crit_new turns to 0 and crit_old takes the value of crit_new?
Can you post the sketch you have already? Have you attempted the modifications yourself yet? You have a pretty good outline of what needs to be done.
ToddL1962:
Can you post the sketch you have already?
...in code tags, please!
I m modifying the code from a project I already found..
It's not ready yet,BUT it's close to be..(I want to keep only the http request part)
So I'm here.
Trying this code,(with bold the new addition of percentage thing) which I am changing.
#include <ESP8266HTTPClient.h>
#include "json_parser.h"
#include "WifiConnect.h"
#include "SSD1306Wire.h"
#include "time.h"
#include "Wire.h"
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#define country_code "Greece"
#define country_code2 "all"
int timezone = 3; //Timezone (2 for Greece)
int dst = 0; //Daylight saving time
[b]//first variable declaration
old_crit=0; //i ll give the current crit value
old_crit2=0; //i ll give the current crit value
new_crit=0;
new_crit2=0;[/b]
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for SSD1306 display connected using software SPI (default case):
#define OLED_MOSI 13
#define OLED_CLK 14
#define OLED_DC 12
#define OLED_CS 15
#define OLED_RESET 16
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
#define LOGO_HEIGHT 16
#define LOGO_WIDTH 16
static String build_url_from_country(String country)
{
String url = "http://coronavirus-19-api.herokuapp.com/countries/";
url = url + country;
return url;
}
static String build_url_from_country2(String country)
{
String url = "http://coronavirus-19-api.herokuapp.com/";
url = url + country;
return url;
}
void setup() {
// initialize and clear display
display.begin(SSD1306_SWITCHCAPVCC);
display.clearDisplay();
display.display();
// update display with all of the above graphics
display.display();
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(15,30);
display.print("CORONA VIRUS");
display.display();
delay (3000);
// TIME Read from NTP server
configTime(timezone * 3600, dst * 0, "pool.ntp.org", "time.nist.gov");
Serial.println("\nWaiting for time");
while (!time(nullptr)) {
Serial.print(".");
delay(1000);
}
#if defined JSON_DEBUG
Serial.begin(9600);
#endif
JSON_LOG("Connecting...");
display.clearDisplay();
display.setCursor(15,30);
display.print("Connecting...");
display.setCursor(15,40);
display.print("Latest data for " + String(country_code));
display.display();
delay(3000);
vConnWifiNetworkViaSdk();
}
void loop() {
Greece();
delay(10000);
all();
delay(10000);
}
void Greece(){
HTTPClient http;
http.begin(build_url_from_country(country_code));
int httpCode = http.GET();
if(httpCode > 0)
{
String payload = http.getString();
char* JsonArray = (char *)malloc(payload.length() + 1);
if (!JsonArray) JSON_LOG("nop");
payload.toCharArray(JsonArray, payload.length() + 1);
JSON_LOG(JsonArray);
time_t now = time(nullptr); //Get Local time
if (json_validate(JsonArray))
{
int confirmed = (int)get_json_value(JsonArray, "cases", INT);
int deaths = (int)get_json_value(JsonArray, "deaths", INT);
int recovered = (int)get_json_value(JsonArray, "recovered", INT);
int critical = (int)get_json_value(JsonArray, "critical", INT);
JSON_LOG(confirmed);
JSON_LOG(deaths);
JSON_LOG(recovered);
JSON_LOG(critical);
[b]//takes new value
new_crit=critical;[/b]
//Display latest Data
//-------------------
display.clearDisplay();
display.setCursor(5, 5);
display.print("Confirmed :");
display.setCursor(75, 5);
display.print(String(confirmed));
display.setCursor(5, 20);
display.print("Recovered ");
display.setCursor(75, 20);
display.print(String(recovered));
display.setCursor(5, 30);
display.print("Critical ");
display.setCursor(75, 30);
display.print(String(critical));
[b]//percentage
display.setCursor(110, 30);
display.print((new_crit-old_crit)/old_crit);
[/b]
display.setCursor(0, 45);
display.print(String(ctime(&now)));
display.display();
}
free(JsonArray);
}
http.end();
}
void all(){ if(bGotIpFlag) bGotIp();
HTTPClient http;
http.begin(build_url_from_country2(country_code2));
int httpCode = http.GET();
Serial.println(httpCode);
if(httpCode == HTTP_CODE_OK)
{
String payload = http.getString();
char* JsonArray = (char *)malloc(payload.length() + 1);
if (!JsonArray) JSON_LOG("nop");
payload.toCharArray(JsonArray, payload.length() + 1);
JSON_LOG(JsonArray);
time_t now = time(nullptr); //Get Local time
if (json_validate(JsonArray))
{
int confirmed = (int)get_json_value(JsonArray, "cases", INT);
int deaths = (int)get_json_value(JsonArray, "deaths", INT);
int recovered = (int)get_json_value(JsonArray, "recovered", INT);
int critical = (int)get_json_value(JsonArray, "critical", INT);
JSON_LOG(confirmed);
JSON_LOG(deaths);
JSON_LOG(recovered);
JSON_LOG(critical);
[b]//takes new value
new_crit2=critical;
[/b]
//Display latest Data
//-------------------
display.clearDisplay();
display.setCursor(5, 5);
display.print("Confirmed :");
display.setCursor(75, 5);
display.print(String(confirmed));
display.setCursor(5, 20);
display.print("Recovered ");
display.setCursor(75, 20);
display.print(String(recovered));
[b]//percentage
display.setCursor(110, 30);
display.print((new_crit-old_crit)/old_crit);
[/b]
display.setCursor(0, 45);
display.print(String(ctime(&now)));
display.display();
}
free(JsonArray);
}
http.end();
}
[b]void percentage(){
//this is where I have to put the control for the given amount of time
//if 24 times have passed
old_crit=new_crit;
old_crit2=new_crit2;
}[/b]