ELEC50003-P1-CW/Control/src/main.cpp

180 lines
4.7 KiB
C++
Raw Normal View History

#pragma region Includes
2021-05-18 11:18:36 +00:00
#include <Arduino.h>
#include <string>
2021-06-03 20:29:14 +00:00
// #include <SoftwareSerial.h> Software Serial not currently needed
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <ESPmDNS.h>
#include "TickerV2.h"
2021-06-03 20:29:14 +00:00
#include <WebSocketsServer.h>
#include <credentials.h>
#include <ArduinoJson.h>
2021-06-04 00:02:32 +00:00
#include <SPIFFS.h>
#pragma endregion
2021-06-04 00:02:32 +00:00
#pragma region Enable extra debugging info for ESP32
2021-06-04 00:02:32 +00:00
#undef LOG_LOCAL_LEVEL
2021-06-03 20:29:14 +00:00
#define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE
#include "esp_log.h"
#pragma endregion
2021-06-03 20:29:14 +00:00
#pragma region Definitions eg pins
2021-06-03 20:29:14 +00:00
#define RX1pin 14 // Pin 10 on expansion board, UART1
#define TX1pin 4 // Pin 11 on expansion board, UART1
#pragma endregion
2021-06-03 20:29:14 +00:00
#pragma region Function Declarations
2021-06-03 20:29:14 +00:00
void printFPGAoutput();
void returnSensorData();
void notFound(AsyncWebServerRequest *request);
void webSocketEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t length);
#pragma endregion
2021-06-03 20:29:14 +00:00
#pragma region Global objects
2021-06-03 20:29:14 +00:00
AsyncWebServer webserver(80);
WebSocketsServer websocketserver(81);
Ticker ticker;
#pragma endregion
2021-06-03 20:29:14 +00:00
#pragma region Global variables
float battery_voltage = 4.0f;
2021-06-03 20:29:14 +00:00
int distance_travelled = 0;
#pragma endregion
2021-06-03 20:29:14 +00:00
void setup()
{
2021-06-04 00:02:32 +00:00
esp_log_level_set("*", ESP_LOG_ERROR); // set all components to ERROR level
esp_log_level_set("wifi", ESP_LOG_WARN); // enable WARN logs from WiFi stack
esp_log_level_set("dhcpc", ESP_LOG_INFO); // enable INFO logs from DHCP client
2021-06-03 20:29:14 +00:00
Serial.begin(115200); // Set up hardware UART0 (Connected to USB port)
Serial1.begin(9600, SERIAL_8N1, RX1pin, TX1pin); // Set up hardware UART1
// Set up remaining communication ports here (Energy, Drive, Vision)
2021-06-04 16:12:40 +00:00
if (!SPIFFS.begin(true)) // Mount SPIFFS
2021-06-04 00:02:32 +00:00
{
Serial.println("SPIFFS failed to mount");
return;
}
2021-06-04 16:12:40 +00:00
Serial.println("SPIFFS mounted");
2021-06-04 00:02:32 +00:00
2021-06-03 20:29:14 +00:00
WiFi.begin(WIFI_SSID, WIFI_PW);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
}
while (!MDNS.begin("rover"))
{
Serial.println("Error setting up mDNS, retrying in 5s");
delay(5000);
}
2021-06-03 20:29:14 +00:00
Serial.println("mDNS set up, access Control Panel at 'rover.local/'");
2021-06-04 00:02:32 +00:00
webserver.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
{ request->send(SPIFFS, "/index.html", "text/html"); });
webserver.on("/favicon.ico", HTTP_GET, [](AsyncWebServerRequest *request)
{ request->send(SPIFFS, "/favicon.ico", "image/png"); });
2021-06-03 20:29:14 +00:00
webserver.onNotFound(notFound);
webserver.begin();
websocketserver.begin();
websocketserver.onEvent(webSocketEvent);
ticker.attach(0.5, returnSensorData);
}
2021-06-03 20:29:14 +00:00
void loop()
{
printFPGAoutput();
String FPGAinput; // Forward serial monitor input to FPGA
if (Serial.available())
{
FPGAinput = String(Serial.readStringUntil('\n'));
Serial1.println(FPGAinput);
}
2021-06-03 20:29:14 +00:00
websocketserver.loop(); // Handle incoming client connections
}
2021-06-03 20:29:14 +00:00
void printFPGAoutput()
{ // Print serial communication from FPGA to serial monitor
String FPGAoutput;
if (Serial1.available())
{
FPGAoutput = String(Serial1.readStringUntil('\n'));
Serial.println(FPGAoutput);
}
}
2021-06-03 20:29:14 +00:00
void returnSensorData()
{
// Collect sensor data here?
distance_travelled++;
if (battery_voltage < 6)
{
battery_voltage += 0.2;
}
else
{
battery_voltage = 4;
}
2021-06-03 20:29:14 +00:00
String JSON_Data = String("{\"BTRY_VOLT\":") + battery_voltage + String(",\"ODO_DIST\":") + distance_travelled + "}";
Serial.println(JSON_Data);
2021-06-03 20:29:14 +00:00
websocketserver.broadcastTXT(JSON_Data);
}
2021-05-18 11:18:36 +00:00
2021-06-03 20:29:14 +00:00
void notFound(AsyncWebServerRequest *request)
{
request->send(404, "text/plain", "Page Not found. Check URI/IP address.");
2021-05-18 11:18:36 +00:00
}
2021-06-03 20:29:14 +00:00
void webSocketEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t length)
{
switch (type)
{
2021-06-04 00:02:32 +00:00
case WStype_DISCONNECTED:
{
Serial.printf("Client[%u] Disconnected!\n", num);
}
break;
case WStype_CONNECTED:
{
IPAddress ip = websocketserver.remoteIP(num);
Serial.printf("Client[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
}
break;
case WStype_TEXT:
{
Serial.printf("Client[%u] sent Text: %s\n", num, payload);
String command = String((char *)(payload));
DynamicJsonDocument doc(200); //creating an instance of a DynamicJsonDocument allocating 200bytes on the heap.
DeserializationError error = deserializeJson(doc, command); // deserialize 'doc' and parse for parameters we expect to receive.
if (error)
2021-06-03 20:29:14 +00:00
{
2021-06-04 00:02:32 +00:00
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
return;
2021-06-03 20:29:14 +00:00
}
2021-06-04 00:02:32 +00:00
int MVM_F_status = doc["MVM_F"];
int MVM_L_status = doc["MVM_L"];
int MVM_R_status = doc["MVM_R"];
int MVM_B_status = doc["MVM_B"];
2021-06-03 20:29:14 +00:00
2021-06-04 00:02:32 +00:00
Serial.println('<' + MVM_F_status + ',' + MVM_B_status + ',' + MVM_L_status + ',' + MVM_R_status + '>');
}
break;
case WStype_PONG:
{
Serial.println("Websocket keep-alive PONG");
}
2021-06-04 16:12:40 +00:00
break;
2021-06-04 00:02:32 +00:00
default:
{
Serial.println(String("Websocket received invalid event type: ") + type + String(", exiting"));
exit(1);
}
}
2021-06-03 20:29:14 +00:00
}