#WioTerminal – Getting JSON data from an #Azure ☁️ Function !

Hi !

In today’s post I’ll share an scenario about:

Invoke an Azure Function to get JSON data

In my Digital Twin scenario, I got an Azure Function that returns the current state of a Digital Twin door. The response from the function is similar to this one:

{
    "state": 0,
    "desc": "closed",
    "source": "blog"
}

So, once the Wio Terminal has internet access, I can invoke the Azure Function and read the value with the door state. In example, for a door open state:

Wio Terminal Azure Function Response door closed

And for the door open:

Wio Terminal Azure Function Response door open

Important: The sample code is based on the work from Rui Santos, in the article [ESP32 HTTP GET and HTTP POST with Arduino IDE (JSON, URL Encoded, Text)].

I updated the code to work with the specific WiFi and HTTPClient libraries for the Wio Terminal.

Source Code

As usual, the full code is below and some notes

  • As mentioned before, this sample uses the Wio Terminal specific libraries for Wifi and HTTPClient libraries .
  • The request to the Azure Function for the IoT Door state will be performed every 10 seconds.
  • I added a couple of functions at the end of the ino file to display the information on the TFT screen.
  • In line 98 the HTTP GET process start. After reading the code for the HTTP Library, I realized that I need to define a timeout (3 seconds) and also invoke the end() function before init the http variable.
/*
Bruno Capuano, https://www.elbruno.com
Based on Rui Santos post, https://RandomNerdTutorials.com/esp32-http-get-post-arduino/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
#include "rpcWiFi.h"
#include <HTTPClient.h>
#include"TFT_eSPI.h"
TFT_eSPI tft;
// WiFi information
const char* ssid = "WiFi Name";
const char* password = "WiFi password";
int connectingSeconds = 0;
// Set timer to 10 seconds
unsigned long lastTime = 0;
unsigned long timerDelay = 10000;
// Azure Function
String acFncResponse;
// TFT Display
const int col0 = 20;
const int row0 = 30;
const int row1 = 55;
const int row2 = 80;
const int row3 = 105;
const int row4 = 130;
void setup() {
Serial.begin(115200);
tft.begin();
tft.setRotation(3);
tft.fillScreen(TFT_BLACK);
tft.setTextSize(2);
tftPrintLine(row0, "El Bruno – @elbruno", "");
tftPrintLine(row1, "Connecting to WiFi…", "");
// Set WiFi to station mode and disconnect from an AP if it was previously connected
Serial.println("Connecting");
WiFi.mode(WIFI_STA);
WiFi.disconnect();
WiFi.begin(ssid, password);
// attempt to connect to Wifi network:
while (WiFi.status() != WL_CONNECTED) {
connectingSeconds++;
tftPrintLine(row2, "Seconds waiting: ", String(connectingSeconds));
// wait 1 second for re-trying
delay(1000);
WiFi.begin(ssid, password);
}
tftPrintLine(row3, "Connected!", "");
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
Serial.println("Timer set to 10 seconds (timerDelay variable), it will take 10 seconds before publishing the first reading.");
delay(2000);
// Clean screen to show results
tftInit();
}
void loop() {
if ((millis() – lastTime) > timerDelay) {
//Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
acFncResponse = httpGETRequest();
Serial.println(acFncResponse);
tftPrintLineCol(0, row3, String(acFncResponse), "");
}
else {
Serial.println("WiFi Disconnected");
tftPrintLine(row3, WiFi Disconnected, "");
}
lastTime = millis();
}
}
String httpGETRequest() {
Serial.println("Start http GET request");
HTTPClient http;
http.end();
http.setTimeout(3000);
http.begin(" Azure Function Url ");
int httpResponseCode = http.GET();
Serial.println("http GET done");
String payload = ""; //"{}";
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
payload = http.getString();
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
return payload;
}
void tftInit(){
tft.fillScreen(TFT_BLACK);
tftPrintLine(row0, "El Bruno – @elbruno", "");
tftPrintLine(row1, "Azure IoT Door State", "");
}
void tftPrintLineCol(int col, int row, String messagePrefix, String message){
tft.setCursor(col, row);
tft.print(messagePrefix);
tft.setCursor((col0 + tft.textWidth(messagePrefix)), row);
tft.print(message);
}
void tftPrintLine(int row, String messagePrefix, String message){
tftPrintLineCol(col0, row, messagePrefix, message);
}

Now we are connected, so we can start to interact with Azure resources !

Happy coding!

Greetings

El Bruno

More posts in my blog ElBruno.com.

More info in https://beacons.ai/elbruno


References


¿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:

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: