Why is text from JSON overlapped on OLED?

  • Thread starter Deleted member 750607
  • Start date

Thread Starter

Deleted member 750607

Joined Dec 31, 1969
0
Its not because of the position of the cursor, its because I don't know how to separate Longitude from Latitude in my code.

http://api.open-notify.org/iss-now.json

...you can see in the pic that the numbers aren't clear because both Longitude and latitude are on top of each other

How do I separate them? my code is under this pic

1C23AA15-E727-4093-956D-3513B72CC5AD.png

Main file:
#include <Adafruit_SSD1306.h>
#include <splash.h>
#include <Adafruit_GrayOLED.h>
#include <gfxfont.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SPITFT.h>
#include <Adafruit_SPITFT_Macros.h>
#include <SPI.h>
#include <Wire.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#include "USE_ESP8266.h"
#include <TimeLib.h>
char server [] = "api.open-notify.org";
void digitalClockDisplay();

void setup ()
{
display.begin(SSD1306_SWITCHCAPVCC, 0X3C);
display.clearDisplay ();

  Serial.begin(115200);
  if (!configureNetwork()) //start the network
  {
    Serial.println("Failed to configure the network");
    while(1)
    {
      delay(0); //halt; ESP8266 does not like infinity loop without a delay
    }
   }
int ret = client.connect(server, 80);
if (ret == 1)
{
  Serial.println("Connected");
  client.println("GET /iss-now.json HTTP/1.0"); //the http request
  client.print("Host: "); client.println(server);
  client.println("Connection: close");
  client.println();
}
else
{
  Serial.println("Connection failed, error was: ");
  Serial.print(ret, DEC);
  while(1)
  {
    delay(0); //halt; esp8266 does not like infinite loop without a delay
  }
 }
}

char timestampMarker[] = "\"timestamp\":";
char posMarker[] = "\"iss_position\":";

void loop ()
{
  if (client.available())
  {
    String id = client.readStringUntil('"');
    if (id.equals("timestamp")) //start of timestamp
    {
      if (client.find(':')) //a ":" follows each identifier
      {
      unsigned long timestamp = client.parseInt();
      setTime(timestamp);
      digitalClockDisplay();
      
      }
      else
      {
        Serial.println("failed to parse timestamp.");
      }
    }
    if (id.equals("iss_position")) //start of position data
    {
      if (client.find(':')) 
      {
       while(client.peek () != '}' && client.find('"'))
       {
        String id = client.readStringUntil ('"');
        float val = client.parseFloat ();
        client.find ('"');
        Serial.print(id + ":"); Serial.println(val, 4);

display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("longitude:");
display.display();

display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,10);
display.println(val);
display.display();

display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,40);
display.println("latitude:");
display.display();


display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,50);
display.println(val);
display.display();


       }
      }
      else
      {
        Serial.print("Failed to parse position data.");
      }
    }
  }
if (!client.connected())
  {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    while(1)
  {
    delay (0); //halt, esp8266 does not like loop w/o delay
  }
 }
}
String padDigits(int digit)
{
  String str = String("0") + digit; //put a zero in front of digit
  return str.substring(str.length() - 2);
}

void digitalClockDisplay ()
{
  String datestr = String(year()) + "-" + padDigits(month()) +
                   "-" + padDigits(day());
  String timestr = String(hour()) + ":" + padDigits(minute()) +
                   ":" + padDigits(second());
  Serial.println(datestr + " " + timestr);
 
}
Header file:
#include <SPI.h>
#include <ESP8266WiFi.h>
const char ssid[] = "Home25";
const char password[] = "6033216098";
WiFiClient client;

bool configureNetwork()
{
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay (1000);
    Serial.print("Waiting for connection to "); Serial.println(ssid);
  }
    return true;
  }
 

Irving

Joined Jan 30, 2016
3,897
The structure of your while loop is wrong. You're displaying the same information each time through the loop. You should also be using the JSON library to access the content rather than parsing it by hand.

You need a couple of If statements or a switch()/case block to display the values at the right cursor location e.g. Something like...
C-like:
  display.setTextSize(1);
  display.setTextColor(WHITE);
 
  while(client.peek () != '}' && client.find('"')) {
      String id = client.readStringUntil ('"');
      float val = client.parseFloat ();
      client.find ('"');
      Serial.print(id + ":"); Serial.println(val, 4);

      If(id == "longitude"){
         display.setCursor(0,0);
         display.print("longitude:");
         display.setCursor(0,10);
         display.print(val);
      }
      if(id == "latitude"){
         display.setCursor(0,40);
         display.print("latitude:");
         display.setCursor(0,50);
         display.print(val);
      }
  }//end while
  display.display();

other coding improvements:
Don't use println() use print() if you're setting cursor yourself. Only call display.display() once after you've printed everything. You only need to call setTextSize() and setTextColor() once before the start of the loop
 
Last edited:

Thread Starter

Deleted member 750607

Joined Dec 31, 1969
0
The structure of your while loop is wrong. You're displaying the same information each time through the loop. You should also be using the JSON library to access the content rather than parsing it by hand.

You need a couple of If statements or a switch()/case block to display the values at the right cursor location e.g. Something like...
C-like:
  display.setTextSize(1);
  display.setTextColor(WHITE);

  while(client.peek () != '}' && client.find('"')) {
      String id = client.readStringUntil ('"');
      float val = client.parseFloat ();
      client.find ('"');
      Serial.print(id + ":"); Serial.println(val, 4);

      If(id == "longitude"){
         display.setCursor(0,0);
         display.print("longitude:");
         display.setCursor(0,10);
         display.print(val);
      }
      if(id == "latitude"){
         display.setCursor(0,40);
         display.print("latitude:");
         display.setCursor(0,50);
         display.print(val);
      }
  }//end while
  display.display();

other coding improvements:
Don't use println() use print() if you're setting cursor yourself. Only call display.display() once after you've printed everything. You only need to call setTextSize() and setTextColor() once before the start of the loop

I am trying this out, but im not Sure im doing it right. Latitude not showing up...but a good thing happened, longitude is isolated now.
 

Thread Starter

Deleted member 750607

Joined Dec 31, 1969
0
The structure of your while loop is wrong. You're displaying the same information each time through the loop. You should also be using the JSON library to access the content rather than parsing it by hand.

You need a couple of If statements or a switch()/case block to display the values at the right cursor location e.g. Something like...
C-like:
  display.setTextSize(1);
  display.setTextColor(WHITE);

  while(client.peek () != '}' && client.find('"')) {
      String id = client.readStringUntil ('"');
      float val = client.parseFloat ();
      client.find ('"');
      Serial.print(id + ":"); Serial.println(val, 4);

      If(id == "longitude"){
         display.setCursor(0,0);
         display.print("longitude:");
         display.setCursor(0,10);
         display.print(val);
      }
      if(id == "latitude"){
         display.setCursor(0,40);
         display.print("latitude:");
         display.setCursor(0,50);
         display.print(val);
      }
  }//end while
  display.display();

other coding improvements:
Don't use println() use print() if you're setting cursor yourself. Only call display.display() once after you've printed everything. You only need to call setTextSize() and setTextColor() once before the start of the loop

is this meant to go in with the rest of my code? im assuming its not meant to stand by itself...?
 

Irving

Joined Jan 30, 2016
3,897
yes, its a snippet of code around the while loop... it replaces lines 83 to 116 approx in your original code. If you're still missing 'longitude', check that the 'id' retrieved from the JSON is the same spelling & length as the string its being compared with in line 10 of my code snippet...
 

Thread Starter

Deleted member 750607

Joined Dec 31, 1969
0
yes, its a snippet of code around the while loop... it replaces lines 83 to 116 approx in your original code. If you're still missing 'longitude', check that the 'id' retrieved from the JSON is the same spelling & length as the string its being compared with in line 10 of my code snippet...
Works perfect now, THANKS!!!! also starting to learn a few things from what you told me.
 
Top