hr50-remote-display/hr50-remote-display.ino

94 lines
2.7 KiB
Arduino
Raw Normal View History

#include <WiFi.h>
#include <HTTPClient.h>
#include "Arduino.h"
#include "heltec.h"
#include <ArduinoJson.h>
2021-11-12 02:23:34 -05:00
#include "hr50_fonts.h"
const char* ssid = "<WIFI-SSID>";
const char* password = "<WIFI-PASSWORD>";
2021-11-12 04:12:38 -05:00
String api_url = "http://<IP_OF_HR50_API>:5000/status";
2021-11-12 02:23:34 -05:00
int first_row = 0;
int second_row = 24;
int third_row = 48;
2021-11-12 03:15:16 -05:00
int left = 0;
int right = 128;
void setup() {
Heltec.begin(true /*DisplayEnable Enable*/, false /*LoRa Disable*/, false /*Serial Enable*/);
Heltec.display->flipScreenVertically();
2021-11-12 02:23:34 -05:00
Heltec.display->setFont(DialogInput_plain_16);
WiFi.begin(ssid, password);
delay(4000);
}
2021-11-12 03:15:16 -05:00
void printError(String err) {
Heltec.display->clear();
Heltec.display->setTextAlignment(TEXT_ALIGN_LEFT);
Heltec.display->drawString(left, second_row, err );
Heltec.display->display();
}
void loop() {
//Check WiFi connection status
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String payload = "";
http.begin(api_url.c_str());
// Send HTTP GET request
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
payload = http.getString();
}
else {
printError("HTTPError: " + (String)httpResponseCode);
}
// Free resources
http.end();
StaticJsonDocument<200> doc;
deserializeJson(doc, payload);
JsonObject obj = doc.as<JsonObject>();
2021-11-11 18:39:00 -05:00
if ( obj != NULL and obj["PTT"].as<String>() != "ERR") {
String band = obj["BND"].as<String>();
String pep = obj["PEP"].as<String>();
String avg = obj["AVG"].as<String>();
String swr = obj["SWR"].as<String>();
String voltage = obj["VLT"].as<String>();
String power = pep + "W/" + avg + "W";
String ptt = obj["PTT"].as<String>();
String temp = obj["TMP"].as<String>();
// clear the display
Heltec.display->clear();
// print left column
Heltec.display->setTextAlignment(TEXT_ALIGN_LEFT);
2021-11-12 03:15:16 -05:00
Heltec.display->drawString(left, first_row, band );
Heltec.display->drawString(left, second_row, power );
Heltec.display->drawString(left, third_row, voltage );
// print right column
Heltec.display->setTextAlignment(TEXT_ALIGN_RIGHT);
2021-11-12 03:15:16 -05:00
Heltec.display->drawString(right, first_row, swr );
Heltec.display->drawString(right, second_row, ptt );
Heltec.display->drawString(right, third_row, temp );
Heltec.display->display();
}
else
{
// print a "(!)" centered in the third row
// this happens when the TRX/PA is transmitting
// or otherwise an empty response was send by the API
Heltec.display->setTextAlignment(TEXT_ALIGN_LEFT);
Heltec.display->drawString(58, third_row, "(!)" );
Heltec.display->display();
}
}
else {
printError("No WiFi!");
}
2021-11-12 02:23:34 -05:00
}