hr50-remote-display/client/hr50_rd_client.ino

140 lines
4.1 KiB
C++

#include <WiFi.h>
#include <HTTPClient.h>
#include "Arduino.h"
#include "heltec.h"
#include <ArduinoJson.h>
#include "hr50_fonts.h"
#include <EasyButton.h>
#include "config.h"
// Pin 0 is the built-in upper button
#define BUTTON_PIN 0
EasyButton button(BUTTON_PIN);
// Contruct API calls
String api_button_long = api_base_url + "/exec_serial";
String api_button_short = api_base_url + "/exec_shell";
String api_status_url = api_base_url + "/get_status";
int first_row = 0;
int second_row = 24;
int third_row = 48;
int left = 0;
int right = 128;
String cmd_status = "";
// milliseconds of button push until count as a long press
int duration = 1000;
// This happens when the button will be pressed long
void long_button_press() {
cmd_status = send_http_request(api_button_long);
}
// This happens when the button will be pressed short
void short_button_press() {
cmd_status = send_http_request(api_button_short);
}
// Sends a HTTP request to the server and grabs the response
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;
}
// Things that need to be done after booting the device
void setup() {
// Initialize Heltec display
Heltec.begin(true /*DisplayEnable Enable*/, false /*LoRa Disable*/, false /*Serial Enable*/);
Heltec.display->flipScreenVertically();
Heltec.display->setFont(DialogInput_plain_16);
// Log into Wifi and wait a bit
WiFi.begin(ssid, password);
delay(4000);
// Initialize the PRG button
button.begin();
// attach action to long button press
button.onPressedFor(duration, long_button_press);
// attach action to short button press
button.onPressed(short_button_press);
}
// Print error message on the display
void printError(String err) {
Heltec.display->clear();
Heltec.display->setTextAlignment(TEXT_ALIGN_LEFT);
Heltec.display->drawString(left, second_row, err );
Heltec.display->display();
}
// Extracts data from the server response, formats it
// and writes it to the OLED
void print_status_screen(JsonObject status_json) {
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 vlt = status_json["VLT"].as<String>();
String power = pep + "W/" + avg + "W";
String ptt = status_json["PTT"].as<String>();
String temp = status_json["TMP"].as<String>();
String return_msg = status_json["RET"].as<String>();
// clear the display
Heltec.display->clear();
// print left column
Heltec.display->setTextAlignment(TEXT_ALIGN_LEFT);
Heltec.display->drawString(left, first_row, band ); // top row
Heltec.display->drawString(left, second_row, power ); // mid row
Heltec.display->drawString(left, third_row, return_msg ); // bottom row
// print right column
Heltec.display->setTextAlignment(TEXT_ALIGN_RIGHT);
Heltec.display->drawString(right, first_row, swr ); // top row
Heltec.display->drawString(right, second_row, ptt ); // mid row
Heltec.display->drawString(right, third_row, temp ); // bottom row
Heltec.display->display();
}
}
void loop() {
//Check WiFi connection status
if (WiFi.status() == WL_CONNECTED) {
// read out button state
button.read();
// get status data from server
String response = send_http_request(api_status_url);
// The server sends the status data as json string
// This converts the string to a json object
StaticJsonDocument<200> doc;
deserializeJson(doc, response);
JsonObject obj = doc.as<JsonObject>();
// print data to OLED screen
print_status_screen(obj);
// wait a bit
delay(500);
}
else {
printError("No WiFi!");
}
}