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

94 lines
2.7 KiB
C++

#include <WiFi.h>
#include <HTTPClient.h>
#include "Arduino.h"
#include "heltec.h"
#include <ArduinoJson.h>
#include "hr50_fonts.h"
const char* ssid = "<WIFI-SSID>";
const char* password = "<WIFI-PASSWORD>";
String api_url = "http://<IP_OF_HR50_API>:5000/status";
int first_row = 0;
int second_row = 24;
int third_row = 48;
int left = 0;
int right = 128;
void setup() {
Heltec.begin(true /*DisplayEnable Enable*/, false /*LoRa Disable*/, false /*Serial Enable*/);
Heltec.display->flipScreenVertically();
Heltec.display->setFont(DialogInput_plain_16);
WiFi.begin(ssid, password);
delay(4000);
}
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>();
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);
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);
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!");
}
}