PRG button now sends command to API

This commit is contained in:
Michael Clemens 2021-11-27 01:27:19 +01:00
parent f2a002a83f
commit ecf516dd4b
1 changed files with 83 additions and 33 deletions

View File

@ -5,23 +5,77 @@
#include <ArduinoJson.h>
#include "hr50_fonts.h"
#define ESP_INTR_FLAG_DEFAULT 0
#define PRG_BUTTON 0
const char* ssid = "<WIFI-SSID>";
const char* password = "<WIFI-PASSWORD>";
String api_url = "http://<IP_OF_HR50_API>:5000/status";
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;
int left = 0;
int right = 128;
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();
Heltec.display->setFont(DialogInput_plain_16);
WiFi.begin(ssid, password);
delay(4000);
// 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);
}
void printError(String err) {
@ -31,38 +85,18 @@ void printError(String 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();
void print_status_screen(JsonObject status_json) {
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>();
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";
String ptt = obj["PTT"].as<String>();
String temp = obj["TMP"].as<String>();
String ptt = status_json["PTT"].as<String>();
String temp = status_json["TMP"].as<String>();
// clear the display
Heltec.display->clear();
@ -70,7 +104,11 @@ void loop() {
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 );
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);
Heltec.display->drawString(right, first_row, swr );
@ -87,6 +125,18 @@ void loop() {
Heltec.display->drawString(58, third_row, "(!)" );
Heltec.display->display();
}
}
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!");