Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new example #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
207 changes: 207 additions & 0 deletions examples/Space_Station/ISS_API_Class.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
// Space Station class functions which supports the main sketch

#include "ISS_API_Class.h" // Include the header with the function prototypes etc

/***************************************************************************************
** Connect to website and get the International Space Station pass times
***************************************************************************************/
void SpaceStation::getPasses(String latitude, String longitude, ISS_pass* passData)
{
this->passData = passData; // Make a copy of the pointer for this class

JsonStreamingParser json; // Create an instance of the parser
json.setListener(this); // Pass pointer to "this" SpaceStation class to the listener
// so it can call the support functions in this class

// Use WiFiClient class to create TCP connections
WiFiClient client;

// URL and port of the server
const char* host = "api.open-notify.org";
const int httpPort = 80;

// Connect as a client to the server
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}

// Built up the GET request
String url = "http://api.open-notify.org/iss-pass.json?lat=" + latitude + "&lon=" + longitude + "&n=" + PASSES;

// Send GET request
Serial.println("\nSending GET request to api.open-notify.org...\n");
client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");

// Local variables for time-out etc
uint32_t timeout = millis();
char c = 0;
uint16_t ccount = 0;

Serial.println("=====> Header start <=====");

// Read the header that precedes the JSON, ends with \r\n
while ( client.available() > 0 || client.connected())
{
String line = client.readStringUntil('\n');
if (line == "\r") {
Serial.println("=====> Header end <=====");
break;
}

Serial.println(line); // Print the header to serial monitor for viewing

// Check for timeout
if ((millis() - timeout) > 5000UL)
{
Serial.println ("HTTP header timeout");
client.stop();
return;
}
}

// The JSON message should now be in the buffer for reading

// Read the JSON character by character and pass it to the JSON decoder
// The decoder will call the SpaceStation class during decoding, so we can
// save the decoded values

// Use OR since data may still be in the buffer when the client has disconnected!
while ( client.available() > 0 || client.connected())
{
while(client.available() > 0)
{
c = client.read(); // Read a received character

#ifdef SHOW_JSON // Optionally show the message with a simple formatter
Serial.print(c);
#endif

json.parse(c); // Pass to the parser, parser will call listener support functions as needed

// Check for timeout
if ((millis() - timeout) > 8000UL)
{
Serial.println ("JSON parse client timeout");
json.reset();
client.stop();
return;
}
yield();
}
}

json.reset();

client.stop();

}

/***************************************************************************************
** JSON Decoder library calls this when a key has been read
***************************************************************************************/
void SpaceStation::key(String key) {
currentKey = key;

//Serial.print("key: ");
//Serial.println(key);
}

/***************************************************************************************
** JSON Decoder library calls this when a value has been read
***************************************************************************************/
void SpaceStation::value(String val) {

// Test only:
//Serial.print("\nvaluePath :"); Serial.println(valuePath);
//Serial.print("currentParent :"); Serial.println(currentParent);
//Serial.print("currentKey :"); Serial.println(currentKey);
//Serial.print("arrayIndex :"); Serial.println(arrayIndex);
//Serial.print("Value :"); Serial.println(val);

if (currentParent == "")
{
if (currentKey == "message") passData->message = val;
return;
}

else
if (currentParent == "request")
{
if (currentKey == "datetime") passData->datetime = (uint32_t)val.toInt();
}

else
if (valuePath == "/response")
{
if (currentKey == "duration") passData->passDuration[arrayIndex] = (uint16_t)val.toInt();
else
if (currentKey == "risetime") passData->passRiseTime[arrayIndex] = (uint32_t)val.toInt();
}
}

/***************************************************************************************
** JSON Decoder library calls this when a start of document decoded
***************************************************************************************/
void SpaceStation::startDocument() {
currentParent = currentKey = "";
arrayIndex = 0;
ended = false;

//Serial.println("\nstart document");
}

/***************************************************************************************
** JSON Decoder library calls this when a end of document decoded
***************************************************************************************/
void SpaceStation::endDocument() {
ended = true;

//Serial.println("end document. ");
}

/***************************************************************************************
** JSON Decoder library calls this when a start of object decoded
***************************************************************************************/
void SpaceStation::startObject() {
currentParent = currentKey;

//Serial.println("start object. ");
}

/***************************************************************************************
** JSON Decoder library calls this when a end of object decoded
***************************************************************************************/
void SpaceStation::endObject() {
currentParent = "";
arrayIndex++;

//Serial.println("end object. ");
}

/***************************************************************************************
** JSON Decoder library calls this when an array of values has started
***************************************************************************************/
void SpaceStation::startArray() {
arrayIndex = 0;
valuePath = currentParent + "/" + currentKey;

//Serial.println("start array. ");
}

/***************************************************************************************
** JSON Decoder library calls this when an array of values has ended
***************************************************************************************/
void SpaceStation::endArray() {
valuePath = "";

//Serial.println("end array. ");
}

/***************************************************************************************
** JSON Decoder library calls this when a character is whitespace (not used here)
***************************************************************************************/
void SpaceStation::whitespace(char c) {

//Serial.println("whitespace");
}
71 changes: 71 additions & 0 deletions examples/Space_Station/ISS_API_Class.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Header for Space Station class which supports the main sketch
#pragma once

#define SHOW_JSON // Print the JSON to the serial monitor
#define PASSES 7 // Number of ISS passes to fetch

// Choose the WiFi library to load
#ifdef ESP8266
#include <ESP8266WiFi.h> // Built in library for ESP8266
#else // ESP32
#include <WiFi.h> // Built in library for ESP32
#endif

#include <WiFiUdp.h> // Built in library

#include "JsonStreamingParser.h"
#include "JsonListener.h"

// Structure to hold the parsed values
typedef struct ISS_pass {
String message; // Message, usually "sucess"
uint32_t datetime = 0; // Time of request, UTC unix time in seconds since Jan 01 1970
uint16_t passDuration[PASSES] = { 0 }; // Pass duration in seconds
uint32_t passRiseTime[PASSES] = { 0 }; // Time of pass, UTC unix time
} ISS_pass;

class SpaceStation: public JsonListener {

public:

void getPasses(String latitude, String longitude, ISS_pass* passData);

// Start of Listener support functions

void key(String key);

void value(String value);

void startDocument();

void endDocument();

void startObject();

void endObject();

void startArray();

void endArray();

void whitespace(char c);

// End of Listener support functions

private:

bool ended = true; // Flag to indicate document has ended

String currentParent; // Current object e.g. "request"

String currentKey; // Name key of the name:value pair e.g "temperature"

uint16_t arrayIndex; // Array index 0-N e.g. 4 for 5th pass, qualify with valuePath

String valuePath; // object (i.e. sequential key) path (like a "file path")
// taken to the name:value pair in the form "/response"
// so values can be pulled from the correct array.
// Needed since different objects contain "data" arrays.

ISS_pass *passData; // pointer provided by sketch
};
Loading