Hi !
In today’s post I’ll share an scenario about:
Invoke an Azure Function to get JSON data, and Parse the JSON data
Yesterday I wrote on how to invoke an Azure Function and get JSON data for my Digital Twin scenario. The response from the function is similar to this one:
{
"state": 0,
"desc": "closed",
"source": "blog"
}
There are several libraries to work with JSON in the Arduino ecosystem, I’m using this one and so far, so good.
The code is super easy to read
- Include Arduino_JSON.h in the include section
- Parse the string with the Azure Function response, this creates a JSONVar object
- Validate for a valid JSON object
- Validate JSON properties to get state, description and source
// Include section
#include <Arduino_JSON.h>
// Azure Function and Door Information
int state = -1;
String state_desc = "undefined";
String state_source = "undefined";
void getDoorStateInformation(String response){
// init default values
state = -1;
state_desc = "undefined";
state_source = "undefined";
// process JSon Response
JSONVar myObject = JSON.parse(response);
if (JSON.typeof(myObject) == "undefined") {
Serial.println("Parsing input failed!");
return;
}
if (myObject.hasOwnProperty("state")) {
state = (int)myObject["state"];
}
if (myObject.hasOwnProperty("desc")) {
state_desc = myObject["desc"];
}
if (myObject.hasOwnProperty("source")) {
state_source = myObject["source"];
}
Serial.print("state: ");
Serial.print(state);
Serial.print(" - desc: ");
Serial.print(state_desc);
Serial.print(" - source: ");
Serial.println(state_source);
}
I’m close to finish the Wio Terminal application. Next step, let’s interact with the Digital Twin using the Wio Terminal buttons.
Happy coding!
Greetings
El Bruno
More posts in my blog ElBruno.com.
More info in https://beacons.ai/elbruno
References
- Download Arduino IDE
- Wiki Seeed – Historgram
- Wiki Seeed – loading Images
- Wiki Seeed – Installing the File System Library
- Wiki Seed – Wifi Connectivity
- GitHub – Arduino_JSON
WioTerminal – Posts to interact with a Digital Twin Door 🚪 with Azure IoT ☁️ and Azure Functions
- Convert and use images on the device
- 1st steps 👣, developer steps
- Buttons and Charts time 📊📉📊
- Connecting to Wifi 📶, display local IP and get ready for Azure ☁️ scenarios
- Getting JSON data from an Azure ☁️ Function
- Parsing JSON data from an Azure ☁️ Function
- Display a Digital Twin Door 🚪 state using XBitmap with Azure IoT ☁️
- Display a countdown progress bar 🚥 for the next Azure IoT ☁️ refresh data call
- Open and close the Digital Twin Door 🚪 using the Wio Terminal Buttons
- Training an 🗣️ audio recognition module. Record 🎙️ audio samples for training
- Training an 🗣️ audio recognition module. Edge Impulse for Arduino step-by-step and optimizations
- Training an 🗣️ audio recognition module. Running the model on the device
- Playing sound on the open and close events from the Digital Twin Door 🚪 (coming soon)
¿Con ganas de ponerte al día?
En Lemoncode te ofrecemos formación online impartida por profesionales que se baten el cobre en consultoría:
- Si tienes ganas de ponerte al día con Front End (ES6, Typescript, React, Angular, Vuejs…) te recomendamos nuestros Máster Front End: https://lemoncode.net/master-frontend#inicio-banner
- Si te quieres poner al día en Backend (stacks .net y nodejs), te aconsejamos nuestro Bootcamp Backend: https://lemoncode.net/bootcamp-backend#bootcamp-backend/banner
- Y si tienes ganas de meterte con Docker, Kubernetes, CI/CD…, tenemos nuestro Bootcamp Devops: https://lemoncode.net/bootcamp-devops#bootcamp-devops/inicio