Hi all, I've successfully grabbed some json data from a project i am working on and being able to print the data and display it on an OLED screen but i am having trouble using the data to do other things.
I want to be able to light an LED if the colour sent back in the const char* field is yellow or green etc. I have no idea how to do this as of yet, any help would be appreciated.
see attachment or code below.
thanks
const size_t capacity = JSON_OBJECT_SIZE(7) + 100;
DynamicJsonDocument doc(capacity);
String payload = http.getString();
const char* json = payload.c_str();
deserializeJson(doc, json);
const char* colour = doc["colour"]; // "yellow"
const char* side = doc["side"]; // "buy"
const char* sizeBtc = doc["sizeBtc"]; // "0.05"
const char* price = doc["price"]; // "8267.0"
long time = doc["time"]; // 1595590955
const char* readableTime = doc["readableTime"]; // "24/07/2020 11:42:35"
float gecko = doc["gecko"]; // 8191.32
pcbbc
July 26, 2020, 12:05am
2
Your sketch doesn’t compile. Please provide a complete sketch that compiles and posted in [code]code tags[/code]. You can insert them with the </> button on the posting toolbar, or use the “copy code for forum” option on the edit menu in the Arduino IDE.
//Includes-------------------------------------------|
#include "WiFi.h"
#include <ArduinoJson.h>
#include <HTTPClient.h>
#include "U8g2lib.h"
//Definitions-----------------------------------------|
#define FONT_ONE_HEIGHT 8 // font one height in pixels
#define FONT_TWO_HEIGHT 20 // font two height in pixels
#define ONBOARD_LED 25
//Constants and Variables----------------------------|
char chPassword[] = "oldman 1952";
char chSSID[] = "Winterfell";
char chBuffer[128];
//String ("colour") = yellow;
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, 16, 15, 4);
//Setup----------------------------------------------|
void setup() {
Serial.begin (115200);
pinMode(ONBOARD_LED, OUTPUT);
Serial.print("Connecting to Wifi");
WiFi.begin (chSSID, chPassword);
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay (1250);
}
// Oled Screen Startup
u8g2.begin();
u8g2.setFont(u8g2_font_6x10_tr);
u8g2.setFontRefHeightExtendedText();
u8g2.setDrawColor(1);
u8g2.setFontPosTop();
u8g2.setFontDirection(0);
// Display Wifi Connecting to....
u8g2.clearBuffer();
sprintf(chBuffer, "%s", "Connecting to:");
u8g2.drawStr(64 - (u8g2.getStrWidth(chBuffer) / 2), 0, chBuffer);
sprintf(chBuffer, "%s", chSSID);
u8g2.drawStr(64 - (u8g2.getStrWidth(chBuffer) / 2), 31 - (FONT_ONE_HEIGHT / 2), chBuffer);
u8g2.sendBuffer();
delay(1000);
u8g2.clearBuffer();
sprintf(chBuffer, "%s", "WiFi Stats:");
u8g2.drawStr(64 - (u8g2.getStrWidth(chBuffer) / 2), 0, chBuffer);
// Display the ssid of the wifi router.
sprintf(chBuffer, "SSID: %s", chSSID);
u8g2.drawStr(0, FONT_ONE_HEIGHT * 3, chBuffer);
// Display the rssi.
sprintf(chBuffer, "RSSI: %d", WiFi.RSSI());
u8g2.drawStr(0, FONT_ONE_HEIGHT, chBuffer);
u8g2.sendBuffer();
delay(3000);
}
//Loop-----------------------------------------------|
void loop() {
if ((WiFi.status() == WL_CONNECTED))
Serial.println ("Connected: ");
Serial.print (WiFi.localIP());
{
HTTPClient http;
http.begin ("http://www.dogstarweb.co.uk/coinfalcon/botty/led.php");
int httpCode = http.GET();
if (httpCode > 0) {
}
sprintf(chBuffer, "WiFi connected to %s.", chSSID);
u8g2.clearBuffer();
Serial.println ("\nHTTP statuscode: " + String(httpCode));
delay (1000);
const size_t capacity = JSON_OBJECT_SIZE(7) + 100;
DynamicJsonDocument doc(capacity);
String payload = http.getString();
const char* json = payload.c_str();
deserializeJson(doc, json);
const char* colour = doc["colour"]; // "yellow"
const char* side = doc["side"]; // "buy"
const char* sizeBtc = doc["sizeBtc"]; // "0.05"
const char* price = doc["price"]; // "8267.0"
long time = doc["time"]; // 1595590955
const char* readableTime = doc["readableTime"]; // "24/07/2020 11:42:35"
float gecko = doc["gecko"]; // 8191.32
//Serial Outputs----------------------------|
Serial.println("-------------------------------");
Serial.print("Side: ");
Serial.println(side);
Serial.print("colour: ");
Serial.println(colour);
Serial.print("Size: ");
Serial.println(sizeBtc);
Serial.print("Price: ");
Serial.println(price);
Serial.print("Time: ");
Serial.println(readableTime);
Serial.print("BTC Price: ");
Serial.println(gecko);
Serial.println("-------------------------------");
//Screen Outputs----------------------------|
u8g2.clearBuffer();
u8g2.setCursor(0, 0);
u8g2.print("Side: ");
u8g2.print(side);
u8g2.setCursor(0, 10);
u8g2.print("Size: ");
u8g2.print(sizeBtc);
u8g2.setCursor(0, 20);
u8g2.print("Colour: ");
u8g2.print(colour);
u8g2.setCursor(0, 30);
u8g2.print("Price: ");
u8g2.print(price);
u8g2.setCursor(0, 40);
u8g2.print("Time: ");
u8g2.print(readableTime);
u8g2.setCursor(0, 50);
u8g2.print("Gecko Price: ");
u8g2.print(gecko, 2);
u8g2.sendBuffer();
http.end();
}
delay (60000);
}