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

144 lines
4.2 KiB
Arduino
Raw Normal View History

#include <WiFi.h>
#include <HTTPClient.h>
#include "Arduino.h"
#include "heltec.h"
#include <ArduinoJson.h>
2021-11-12 07:23:34 +00:00
#include "hr50_fonts.h"
2021-11-27 00:27:19 +00:00
#define ESP_INTR_FLAG_DEFAULT 0
#define PRG_BUTTON 0
2021-11-12 07:23:34 +00:00
2021-11-27 00:27:19 +00:00
const char* ssid = "<WIFI-SSID>";
const char* password = "<WIFI-PASSWORD>";
String api_base_url = "http://<IP_OF_HR50_API>:5000";
//String api_button_url = api_base_url + "/?cmd=hrtu1";
String api_button_url = api_base_url + "/exec";
String api_status_url = api_base_url + "/status";
int first_row = 0;
int second_row = 24;
int third_row = 48;
2021-11-12 08:15:16 +00:00
int left = 0;
int right = 128;
2021-11-27 00:27:19 +00:00
String cmd_status = "";
SemaphoreHandle_t semaphore = nullptr;
void IRAM_ATTR handler(void* arg) {
xSemaphoreGiveFromISR(semaphore, NULL);
}
void button_task(void* arg) {
for (;;) {
if (xSemaphoreTake(semaphore, portMAX_DELAY) == pdTRUE) {
cmd_status = send_http_request(api_button_url);
}
}
}
String send_http_request(String url) {
HTTPClient http;
String response = "";
http.begin(url.c_str());
// Send HTTP GET request
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
response = http.getString();
}
else {
printError("HTTPError: " + (String)httpResponseCode);
}
http.end();
return response;
}
void setup() {
Heltec.begin(true /*DisplayEnable Enable*/, false /*LoRa Disable*/, false /*Serial Enable*/);
Heltec.display->flipScreenVertically();
2021-11-12 07:23:34 +00:00
Heltec.display->setFont(DialogInput_plain_16);
2021-11-27 00:27:19 +00:00
WiFi.begin(ssid, password);
delay(4000);
2021-11-27 00:27:19 +00:00
// Configure PRG button of the Heltec
semaphore = xSemaphoreCreateBinary();
// Setup the button GPIO pin
gpio_pad_select_gpio(PRG_BUTTON );
// Quite obvious, a button is a input
gpio_set_direction(GPIO_NUM_0, GPIO_MODE_INPUT);
// Trigger the interrupt when going from HIGH -> LOW ( == pushing button)
gpio_set_intr_type(GPIO_NUM_0, GPIO_INTR_NEGEDGE);
// Associate button_task method as a callback
xTaskCreate(button_task, "prg_button_task", 4096, NULL, 10, NULL);
// Install ISR service
gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
// Add button handler to the ISR
gpio_isr_handler_add(GPIO_NUM_0, handler, NULL);
}
2021-11-12 08:15:16 +00:00
void printError(String err) {
Heltec.display->clear();
Heltec.display->setTextAlignment(TEXT_ALIGN_LEFT);
Heltec.display->drawString(left, second_row, err );
Heltec.display->display();
}
2021-11-27 00:27:19 +00:00
void print_status_screen(JsonObject status_json) {
2021-11-27 00:27:19 +00:00
if ( status_json != NULL and status_json["PTT"].as<String>() != "ERR") {
String band = status_json["BND"].as<String>();
String pep = status_json["PEP"].as<String>();
String avg = status_json["AVG"].as<String>();
String swr = status_json["SWR"].as<String>();
String voltage = status_json["VLT"].as<String>();
String power = pep + "W/" + avg + "W";
2021-11-27 00:27:19 +00:00
String ptt = status_json["PTT"].as<String>();
String temp = status_json["TMP"].as<String>();
// clear the display
Heltec.display->clear();
// print left column
Heltec.display->setTextAlignment(TEXT_ALIGN_LEFT);
2021-11-12 08:15:16 +00:00
Heltec.display->drawString(left, first_row, band );
Heltec.display->drawString(left, second_row, power );
2021-11-27 00:27:19 +00:00
if ( cmd_status == "" ) {
Heltec.display->drawString(left, third_row, voltage );
} else {
Heltec.display->drawString(left, third_row, cmd_status );
}
// print right column
Heltec.display->setTextAlignment(TEXT_ALIGN_RIGHT);
2021-11-12 08:15:16 +00: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();
}
2021-11-27 00:27:19 +00:00
}
void loop() {
//Check WiFi connection status
if (WiFi.status() == WL_CONNECTED) {
String response = send_http_request(api_status_url);
StaticJsonDocument<200> doc;
deserializeJson(doc, response);
JsonObject obj = doc.as<JsonObject>();
print_status_screen(obj);
}
else {
printError("No WiFi!");
}
2021-11-12 07:23:34 +00:00
}