Skip to content

Instantly share code, notes, and snippets.

@rodolfofadino
Created January 24, 2022 21:15
Show Gist options
  • Save rodolfofadino/5236e62464e6ff83a420948dd3cc1714 to your computer and use it in GitHub Desktop.
Save rodolfofadino/5236e62464e6ff83a420948dd3cc1714 to your computer and use it in GitHub Desktop.
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <Wire.h>
#include "SSD1306Wire.h"
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecureBearSSL.h>
unsigned long myTime;
ESP8266WiFiMulti WiFiMulti;
SSD1306Wire display(0x3c, SDA, SCL);
void setup() {
Serial.begin(115200);
// Serial.setDebugOutput(true);
Serial.println();
Serial.println();
Serial.println();
for (uint8_t t = 4; t > 0; t--) {
Serial.printf("[SETUP] WAIT %d...\n", t);
Serial.flush();
delay(1000);
}
WiFi.mode(WIFI_STA);
WiFiMulti.addAP("WIFINAME", "WIFIPASSWORD");
display.init();
display.flipScreenVertically();
ProgressBar();
}
void ProgressBar()
{
for (int counter = 0; counter <= 100; counter++)
{
display.clear();
//Desenha a barra de progresso
display.drawProgressBar(0, 32, 120, 10, counter);
//Atualiza a porcentagem completa
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.drawString(64, 15, String(counter) + "%");
display.display();
delay(10);
}
}
void loop() {
if ((WiFiMulti.run() == WL_CONNECTED)) {
std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
//client->setFingerprint(fingerprint);
// Or, if you happy to ignore the SSL certificate, then use the following line instead:
client->setInsecure();
HTTPClient https;
myTime = millis();
Serial.print("[HTTPS] begin...\n");
if (https.begin(*client, "https://url-for-chech.com.br")) { // HTTPS
Serial.print("[HTTPS] GET...\n");
// start connection and send HTTP header
int httpCode = https.GET();
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTPS] GET... code: %d\n", httpCode);
//Apaga o display
display.clear();
display.setColor(WHITE);
display.setTextAlignment(TEXT_ALIGN_CENTER);
//Seleciona a fonte
display.setFont(ArialMT_Plain_16);
display.drawString(63, 0, "GET API");
display.drawString(63, 26, "CODE: " + String(httpCode));
unsigned long myFinishTime = millis()- myTime;
// file found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = https.getString();
Serial.println(payload);
}
Serial.println("Total Time");
Serial.println(myFinishTime);
display.drawString(63, 45, String(myFinishTime)+ "ms");
display.display();
} else {
Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
display.clear();
display.setColor(WHITE);
display.setTextAlignment(TEXT_ALIGN_CENTER);
//Seleciona a fonte
display.setFont(ArialMT_Plain_16);
display.drawString(63, 0, "GET API");
display.drawString(63, 26, "ERROR: " + String(https.errorToString(httpCode).c_str()));
display.display();
}
https.end();
} else {
Serial.printf("[HTTPS] Unable to connect\n");
display.clear();
display.setColor(WHITE);
display.setTextAlignment(TEXT_ALIGN_CENTER);
//Seleciona a fonte
display.setFont(ArialMT_Plain_16);
display.drawString(63, 0, "GET API");
display.drawString(63, 26, "Unable to connect");
display.display();
}
}else{
Serial.println("Wifi Error");
display.clear();
display.setColor(WHITE);
display.setTextAlignment(TEXT_ALIGN_CENTER);
//Seleciona a fonte
display.setFont(ArialMT_Plain_16);
display.drawString(63, 0, "WIFI Error");
display.drawString(63, 26, "Unable to connect");
display.display();
}
Serial.println("Wait 10s before next round...");
delay(3000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment