Skip to content

Instantly share code, notes, and snippets.

@elbruno
Last active October 28, 2021 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elbruno/924817da44aab4c42ee647d2896a41f6 to your computer and use it in GitHub Desktop.
Save elbruno/924817da44aab4c42ee647d2896a41f6 to your computer and use it in GitHub Desktop.
WioTerminalAzFunctionInvoke.ino
/*
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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment