OK, here is the full code, its just getting large and thought it would be more confusing to include it all. But thanks, I do appreciate your help.
Makes connection with wifi manager
API looks up multiple currencies & displays it. Works.
Have to reduce frequency of api polls so wanted to copy payload to a variable and just cycle through displaying those in between api polls, this is where I am stuck.
#include <Arduino.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecureBearSSL.h>
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C
///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
ESP8266WiFiMulti WiFiMulti;
// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request
String header;
// Auxiliar variables to store the current output state
String output5State = "off";
String output4State = "off";
char Atlas_temp[5] = "---";
char Polis_temp[5] = "---";
char temp1[5] = "";
const char* c_str() const { return buffer; }
// Assign output variables to GPIO pins
const int output5 = 5;
const int output4 = 4;
int Not_First_Pass = 0;
int Cycle=0;
int Poll=0;
void setup() {
Serial.begin(115200);
// Initialize the output variables as outputs
pinMode(output5, OUTPUT);
pinMode(output4, OUTPUT);
// Set outputs to LOW
digitalWrite(output5, LOW);
digitalWrite(output4, LOW);
// WiFiManager
// Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
// Uncomment and run it once, if you want to erase all the stored information
//wifiManager.resetSettings();
// set custom ip for portal
//wifiManager.setAPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
// fetches ssid and pass from eeprom and tries to connect
// if it does not connect it starts an access point with the specified name
// here "AutoConnectAP"
// and goes into a blocking loop awaiting configuration
//
//
wifiManager.autoConnect("Crypto Buddi");
// wifiManager.autoConnect("AutoConnectAP");
// or use this for auto generated name ESP + ChipID
//wifiManager.autoConnect();
// if you get here you have connected to the WiFi
//Serial.println("Connected.");
server.begin();
}
void loop(){
WiFiClient client = server.available(); // Listen for incoming clients
if (client) { // If a new client connects,
Serial.println("New Client."); // print a message out in the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// turns the GPIOs on and off
if (header.indexOf("GET /5/on") >= 0) {
Serial.println("GPIO 5 on");
output5State = "on";
digitalWrite(output5, HIGH);
} else if (header.indexOf("GET /5/off") >= 0) {
Serial.println("GPIO 5 off");
output5State = "off";
digitalWrite(output5, LOW);
} else if (header.indexOf("GET /4/on") >= 0) {
Serial.println("GPIO 4 on");
output4State = "on";
digitalWrite(output4, HIGH);
} else if (header.indexOf("GET /4/off") >= 0) {
Serial.println("GPIO 4 off");
output4State = "off";
digitalWrite(output4, LOW);
}
// Display the HTML web page
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
// CSS to style the on/off buttons
// Feel free to change the background-color and font-size attributes to fit your preferences
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".button2 {background-color: #77878A;}</style></head>");
// Web Page Heading
client.println("<body><h1>CRYPTO BUDDI</h1>");
// Display current state, and ON/OFF buttons for GPIO 5
client.println("<p>GPIO 5 - State " + output5State + "</p>");
// If the output5State is off, it displays the ON button
if (output5State=="off") {
client.println("<p><a href=\"/5/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/5/off\"><button class=\"button button2\">OFF</button></a></p>");
}
// Display current state, and ON/OFF buttons for GPIO 4
client.println("<p>GPIO 4 - State " + output4State + "</p>");
// If the output4State is off, it displays the ON button
if (output4State=="off") {
client.println("<p><a href=\"/4/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/4/off\"><button class=\"button button2\">OFF</button></a></p>");
}
client.println("</body></html>");
// The HTTP response ends with another blank line
client.println();
// Break out of the while loop
break;
} else { // if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
Draw_Icon();
//
// Display Initializing msg on lcd
//
Cycle = Cycle + 1;
Serial.print("Cycle = ");
Serial.println(Cycle);
Serial.print("Poll = ");
Serial.println(Poll);
if ((Cycle == 1) or (Cycle > 60)) {
Serial.println("");
Serial.println("POLL");
Serial.println("");
Lookup_Star_Atlas();
Lookup_Polis();
Poll = Poll + 1;
Cycle = 2;
}
else{
Serial.println("");
Serial.println("DONT POLL");
Serial.println("");
Display_Star_Atlas();
}
}
void clear_display(void) {
//
// Clears Display
//
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(30,2); // Start at top-left corner
Serial.println("Clearing Display");
display.display();
}
void updating_msg(void) {
//
// ...
//
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(30,2); // Start at top-left corner
display.println(F("UPDATING"));
Serial.println("Updating");
display.display();
}
void graphic(void) {
//
// ...
//
Serial.println("Graphic");
display.display();
display.clearDisplay();
display.drawPixel(10, 10, SSD1306_WHITE);
display.drawPixel(10, 11, SSD1306_WHITE);
display.drawPixel(10, 12, SSD1306_WHITE);
display.display();
}
void Lookup_Star_Atlas() {
// wait for WiFi connection
if ((WiFiMulti.run() == WL_CONNECTED)) {
std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
client->setInsecure();
HTTPClient https;
Serial.print("[HTTPS] begin...\n");
//
// If just turned ON Display msg on lcd
//
Serial.print("Not_First_Pass = ");
Serial.println(Not_First_Pass);
if (Not_First_Pass == 0) {
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(20,2); // Start at top-left corner
display.println(F("WiFi CONNECTED"));
display.setCursor(25,20); // Start at top-left corner
display.println(F("Getting Data"));
display.display();
}
delay(3000);
if (https.begin(*client, "https://api.coingecko.com/api/v3/simple/price?ids=star-atlas&vs_currencies=usd")) { // 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);
// file found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = https.getString();
Serial.println(payload);
Not_First_Pass = 1;
//
// Display Title
//
display.clearDisplay();
Draw_Icon();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(33,2); // Start at top-left corner
display.println(F("STAR ATLAS"));
//
// Display Price Size 3
//
display.setTextSize(3); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(20,25); // Start at top-left corner
int x=1;
a: if (payload.substring(x, x+1) == ".")
{
Serial.println(x);
Serial.println(payload.substring(x-1, x+2));
display.println(payload.substring(x-1, x+4));
//char temp[6] = {(payload.substring(x-1, x+4))};
//char *ptr = payload.substring(x-1, x+4)).c_str();
//char ptr = payload.c_str();
const char *ptr = payload.c_str();
ptr = ptr + x - 1;
strncpy(temp, ptr, 5);
Serial.print("t=");
Serial.println(temp);
strncpy(Atlas_temp, temp, sizeof Atlas_temp);
Serial.print("at=");
Serial.println(Atlas_temp);
delay(10000);
}
else
{
x=x+1;
goto a;
}
//
//
//
display.display();
}
} else {
Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
//
// Display Failed msg on lcd
//
//display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(20,30); // Start at top-left corner
display.println(F("Download Failed"));
display.display();
}
https.end();
} else {
Serial.printf("[HTTPS] Unable to connect\n");
}
}
Serial.println("Wait before next update...");
delay(5000);
display.clearDisplay();
Draw_Icon();
}
void Display_Star_Atlas() {
//
// Display Title
//
display.clearDisplay();
Draw_Icon();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(33,2); // Start at top-left corner
display.println(F("STAR ATLAS"));
display.display();
//
// Display Price Size 3
//
display.setTextSize(3); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(20,25); // Start at top-left corner
Serial.println(Atlas_temp);
//display.println(F(Atlas_temp));
//display.print((__FlashStringHelper*)Atlas_temp);
display.print((Atlas_temp));
display.display();
Serial.println("Wait before next dummy update...");
delay(5000);
display.clearDisplay();
Draw_Icon();
}
void Lookup_Polis() {
// wait for WiFi connection
if ((WiFiMulti.run() == WL_CONNECTED)) {
std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
client->setInsecure();
HTTPClient https;
Serial.print("[HTTPS] begin...\n");
//
// If just turned ON Display msg on lcd
//
if (Not_First_Pass == 0) {
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(20,2); // Start at top-left corner
display.println(F("WiFi CONNECTED"));
display.setCursor(25,20); // Start at top-left corner
display.println(F("Getting Data"));
display.display();
}
if (https.begin(*client, "https://api.coingecko.com/api/v3/simple/price?ids=star-atlas-dao&vs_currencies=usd")) { // 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);
// file found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = https.getString();
Serial.println(payload);
Not_First_Pass = 1;
//
// Display Title
//
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(44,2); // Start at top-left corner
display.println(F("POLIS"));
//
// Display Price Size 3
//
display.setTextSize(3); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(20,25); // Start at top-left corner
//
// Extract 1 char to left of decimal point to 2 to the right of decimal point
//
int x=1;
a: if (payload.substring(x, x+1) == ".")
{
//
// Check if result is 1.8} format, if so trim tailing } char
//
Serial.println("x substring = ");
Serial.print(payload.substring(x+3, x+2));
if (payload.substring(x+3, x+2) == "}")
{
Serial.println("Trimming }");
Serial.println(x);
Serial.println(payload.substring(x-1, x+2));
display.println(payload.substring(x-1, x+2));
}
else
{
Serial.println(x);
Serial.println(payload.substring(x-1, x+3));
display.println(payload.substring(x-1, x+3));
}
}
else
{
x=x+1;
goto a;
}
//
//
//
display.display();
}
} else {
Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
//
// Display Failed msg on lcd
//
//display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(20,30); // Start at top-left corner
display.println(F("Download Failed"));
//display.setTextSize(2); // Normal 1:1 pixel scale
//display.setTextColor(SSD1306_WHITE); // Draw white text
//display.setCursor(20,30); // Start at top-left corner
//display.println(payload);
display.display();
}
https.end();
} else {
Serial.printf("[HTTPS] Unable to connect\n");
}
}
Serial.println("Wait before next update...");
delay(5000);
display.clearDisplay();
Draw_Icon();
}
void Draw_Icon(){
//
// Draw WIFI Icon
//
if ((WiFiMulti.run() == WL_CONNECTED)) {
Serial.println("Connected.");
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.clearDisplay();
display.drawPixel(117, 1, SSD1306_WHITE);
display.drawPixel(118, 1, SSD1306_WHITE);
display.drawPixel(119, 1, SSD1306_WHITE);
display.drawPixel(120, 1, SSD1306_WHITE);
display.drawPixel(121, 1, SSD1306_WHITE);
display.drawPixel(122, 1, SSD1306_WHITE);
display.drawPixel(123, 1, SSD1306_WHITE);
display.drawPixel(116, 2, SSD1306_WHITE);
display.drawPixel(124, 2, SSD1306_WHITE);
display.drawPixel(115, 3, SSD1306_WHITE);
display.drawPixel(125, 3, SSD1306_WHITE);
display.drawPixel(118, 4, SSD1306_WHITE);
display.drawPixel(119, 4, SSD1306_WHITE);
display.drawPixel(120, 4, SSD1306_WHITE);
display.drawPixel(121, 4, SSD1306_WHITE);
display.drawPixel(122, 4, SSD1306_WHITE);
display.drawPixel(117, 5, SSD1306_WHITE);
display.drawPixel(123, 5, SSD1306_WHITE);
display.drawPixel(116, 6, SSD1306_WHITE);
display.drawPixel(124, 6, SSD1306_WHITE);
display.drawPixel(119, 7, SSD1306_WHITE);
display.drawPixel(120, 7, SSD1306_WHITE);
display.drawPixel(121, 7, SSD1306_WHITE);
display.drawPixel(118, 8, SSD1306_WHITE);
display.drawPixel(122, 8, SSD1306_WHITE);
display.drawPixel(120, 9, SSD1306_WHITE);
display.drawPixel(119, 10, SSD1306_WHITE);
display.drawPixel(120, 10, SSD1306_WHITE);
display.drawPixel(121, 10, SSD1306_WHITE);
display.drawPixel(120, 11, SSD1306_WHITE);
display.display();
}
else{
Serial.println("Not Connected.");
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.clearDisplay();
display.display();
}
}