Skip to content

Instantly share code, notes, and snippets.

@futureshocked
Created September 4, 2019 03:05
Show Gist options
  • Save futureshocked/ed9f60a249e8f78d14ecb627be95ac7b to your computer and use it in GitHub Desktop.
Save futureshocked/ed9f60a249e8f78d14ecb627be95ac7b to your computer and use it in GitHub Desktop.
One of Peter's sketches in which he uses JSON to send data to Adafruit IO and receive, parse the response using ArduinoJSON.
void refresh_readings() {
float f_temperature;
float f_humidity;
float f_pressure;
float f_altitude;
digitalWrite(LED_PIN, HIGH);
postCounter++; // New post, increase the post counter
f_temperature = bme.readTemperature();
f_humidity = bme.readHumidity();
f_pressure = bme.readPressure() / 100.0F;
f_altitude = bme.readAltitude(SEALEVELPRESSURE_HPA);
tft.setTextColor(fg, bg);
tft.loadFont("SansSerif-36"); // Create TTF fonts using instructions at https://pages.uoregon.edu/park/Processing/process5.html
tft.setRotation(1);
tft.setCursor(5, 5);
tft.println("Right now...");
tft.setTextColor(TFT_YELLOW, bg);
// Temperature
tft.fillRect(5, 50, 140, 30, bg);
tft.setCursor(5, 50);
tft.print(f_temperature);
tft.println(" °C");
// Humidity
tft.fillRect(5, 90, 130, 30, bg);
tft.setCursor(5, 90);
tft.print(f_humidity);
tft.println(" %");
// Pressure
tft.fillRect(5, 130, 200, 30, bg);
tft.setCursor(5, 130);
tft.print(f_pressure);
tft.println(" hPa");
// Appx altitude
tft.fillRect(5, 170, 200, 30, bg);
tft.setCursor(5, 170);
tft.print(f_altitude);
tft.println(" m");
// // Wifi status
// tft.loadFont("NotoSansBold15");
// tft.setTextColor(TFT_LIGHTGREY, bg);
// tft.fillRect(5, 220, 100, 20, bg);
// tft.setCursor(5, 220);
// tft.print(wl_status_to_string(WiFi.status()));
client.setCACert(test_root_ca);
DEBUG_PRINTLN("\nStarting connection to server...");
if (!client.connect(server, 443))
DEBUG_PRINTLN("Connection failed!");
else {
DEBUG_PRINTLN("Connected to server!");
// Construct the JSON request:
// An example of JSON that updates multiple feeds
String postData = "{ \"feeds\": [ " \
"{ \"key\": \"altitude\", " \
" \"value\": \"";
postData += f_altitude;
postData += "\" },{ \"key\": \"barpressure\", \"value\": \"";
postData += f_pressure;
postData += "\" }, { \"key\": \"humidity\",\"value\": \"";
postData += f_humidity;
postData += "\"}, { \"key\": \"temperature\",\"value\": \"";
postData += f_temperature;
postData += "\" }]}";
// Make a HTTP request:
// client.println("POST https://io.adafruit.com/api/v2/futureshocked/feeds/lab-environment.testrest/data HTTP/1.0"); // This is the simple case where we post to a single feed.
client.println("POST https://io.adafruit.com/api/v2/futureshocked/groups/lab-environment/data HTTP/1.0"); // With this, we can post to multiple feeds in a group.
client.println("Host: io.adafruit.com");
client.println("Content-Type: application/json");
client.println("X-AIO-Key: c10ce8eff2ad45c499dd864629ae40e2");
client.print("Content-Length: ");
client.println(postData.length());
// client.println("Connection: close");
client.println();
client.println(postData);
DEBUG_PRINT("data posting: ");
DEBUG_PRINTLN(postData);
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
DEBUG_PRINTLN("headers received");
break;
}
}
// if there are incoming bytes available
// from the server, read them and print them:
String serverResponse = "";
while (client.available()) {
char c = client.read();
// Serial.write(c);
serverResponse += c;
}
DEBUG_PRINT("Server response: ");
DEBUG_PRINTLN(serverResponse);
client.stop();
// // Deserialize the JSON document
// IT IS NOT NECESSARY TO PARSE THE RESPONCE.
// IT IS GOOD TO DO SO IF YOU WANT TO CONFIRM THAT WHAT WAS RECORDED BY ADAFRUIT IO IS
// WHAT YOU SENT.
const size_t capacity = 2000;
DynamicJsonDocument doc(capacity);
//const char* json = "[{\"id\":\"0E3CMHA132FTMAMJ02H6QSYVF4\",\"value\":\"15\",\"feed_id\":1001608,\"feed_key\":\"rest-example.feed1\",\"created_at\":\"2019-02-24T04:13:34Z\",\"location\":null,\"lat\":null,\"lon\":null,\"ele\":null,\"created_epoch\":1550981614,\"expiration\":\"2019-03-26T04:13:34Z\"},{\"id\":\"0E3CMHA132CHVKTDS130N2AP41\",\"value\":\"20\",\"feed_id\":1001609,\"feed_key\":\"rest-example.feed2\",\"created_at\":\"2019-02-24T04:13:34Z\",\"location\":null,\"lat\":null,\"lon\":null,\"ele\":null,\"created_epoch\":1550981614,\"expiration\":\"2019-03-26T04:13:34Z\"}]";
char json[capacity];
serverResponse.toCharArray(json, capacity);
deserializeJson(doc, json);
JsonObject root_0 = doc[0];
const char* root_0_id = root_0["id"]; // "0E3J35C42W7Z85D8K8QT00TSDY"
const char* root_0_value = root_0["value"]; // "112.73"
long root_0_feed_id = root_0["feed_id"]; // 997698
const char* root_0_feed_key = root_0["feed_key"]; // "lab-environment.altitude"
const char* root_0_created_at = root_0["created_at"]; // "2019-03-02T22:59:26Z"
long root_0_created_epoch = root_0["created_epoch"]; // 1551567566
const char* root_0_expiration = root_0["expiration"]; // "2019-04-01T22:59:26Z"
JsonObject root_1 = doc[1];
const char* root_1_id = root_1["id"]; // "0E3J35C42WQ9J4RZASXD5CDX8E"
const char* root_1_value = root_1["value"]; // "999.78"
long root_1_feed_id = root_1["feed_id"]; // 997697
const char* root_1_feed_key = root_1["feed_key"]; // "lab-environment.barpressure"
const char* root_1_created_at = root_1["created_at"]; // "2019-03-02T22:59:26Z"
long root_1_created_epoch = root_1["created_epoch"]; // 1551567566
const char* root_1_expiration = root_1["expiration"]; // "2019-04-01T22:59:26Z"
JsonObject root_2 = doc[2];
const char* root_2_id = root_2["id"]; // "0E3J35C42WHA3G3BNMMGS45J85"
const char* root_2_value = root_2["value"]; // "52.39"
long root_2_feed_id = root_2["feed_id"]; // 997696
const char* root_2_feed_key = root_2["feed_key"]; // "lab-environment.humidity"
const char* root_2_created_at = root_2["created_at"]; // "2019-03-02T22:59:26Z"
long root_2_created_epoch = root_2["created_epoch"]; // 1551567566
const char* root_2_expiration = root_2["expiration"]; // "2019-04-01T22:59:26Z"
JsonObject root_3 = doc[3];
const char* root_3_id = root_3["id"]; // "0E3J35C42WS88N6AGBSFMCTQ53"
const char* root_3_value = root_3["value"]; // "26.93"
long root_3_feed_id = root_3["feed_id"]; // 997695
const char* root_3_feed_key = root_3["feed_key"]; // "lab-environment.temperature"
const char* root_3_created_at = root_3["created_at"]; // "2019-03-02T22:59:26Z"
long root_3_created_epoch = root_3["created_epoch"]; // 1551567566
const char* root_3_expiration = root_3["expiration"]; // "2019-04-01T22:59:26Z"
DEBUG_PRINT("Altitude: ");
DEBUG_PRINTLN(root_0_value);
DEBUG_PRINT("barPressure: ");
DEBUG_PRINTLN(root_1_value);
DEBUG_PRINT("temperature: ");
DEBUG_PRINTLN(root_3_value);
DEBUG_PRINT("Humidity: ");
DEBUG_PRINTLN(root_2_value);
}
heapSize();
// // Print memory in an attemp to figure out why wifi is unreliable
// tft.fillRect(220, 170, 55, 20, bg);
// tft.setCursor(220, 170);
// tft.print(esp_get_free_heap_size());
//
// DEBUG_PRINT("Free heap size: ");
// DEBUG_PRINTLN(esp_get_free_heap_size());
postsCounter();
// int maxPosts= EEPROM.readInt(0);
// if (postCounter > maxPosts)
// {
// maxPosts = postCounter;
// EEPROM.writeInt(0, maxPosts); // Write the new maxPosts in the EEPROM
// EEPROM.commit();
// }
//
// tft.fillRect(250, 0, 55, 20, bg);
// tft.setCursor(250, 0);
// tft.print(postCounter);
// tft.print("/");
// tft.print(EEPROM.readInt(0));
digitalWrite(LED_PIN, LOW);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment