From bf2675fa45803bb96e47e686a0fd7137e48a5410 Mon Sep 17 00:00:00 2001 From: jc4419 <60656643+jc4419@users.noreply.github.com> Date: Sat, 29 May 2021 07:54:53 +0400 Subject: [PATCH] Major Update: Developed all the backend programming for full duplex transmission in real time, using websockets. Added some minor fixes to formatting of text. Tested using a button and pot to simulate odometer and battery values to test real time information transfer from rover to client. Used LED to signifiy movement direction, and tested the same from client to rover. Tests passed! --- Command/index.html | 100 ++++++++-- Command/main/main.ino | 421 ++++++++++++++++++++++++++++++++++++++---- 2 files changed, 467 insertions(+), 54 deletions(-) diff --git a/Command/index.html b/Command/index.html index 8c3c91f..a5421ed 100644 --- a/Command/index.html +++ b/Command/index.html @@ -90,19 +90,90 @@ meter::after { @@ -150,39 +224,25 @@ document.onkeyup = function(e) {

Sensor Data

-
@@ -192,4 +252,4 @@ document.onkeyup = function(e) { - \ No newline at end of file + diff --git a/Command/main/main.ino b/Command/main/main.ino index 3fce95e..c330a25 100644 --- a/Command/main/main.ino +++ b/Command/main/main.ino @@ -1,38 +1,391 @@ -const int potPin = 27; -const int butPin = 16; -const int U_Led = 14; -const int L_Led = 12; -const int R_Led = 15; -const int D_Led = 13; +#include +#include +#include +#include +#include +#include "Ticker.h" + +const int potPin = 34; //used to simulate battery voltage. +const int butPin = 16; //used to increment a variable to simulate distance increasing. +const int U_Led = 14; //LED subsitute for the 'movement forward command'. +const int L_Led = 12; //LED subsitute for the 'movement left command'. +const int R_Led = 15; //LED subsitute for the 'movement right command'. +const int D_Led = 13; //LED subsitute for the 'movement back command'. + int potVal = 0; -bool butState = 0; -void setup() { - // put your setup code here, to run once: - Serial.begin(115200); - pinMode(U_Led, OUTPUT); - pinMode(L_Led, OUTPUT); - pinMode(R_Led, OUTPUT); - pinMode(D_Led, OUTPUT); - pinMode(butPin, INPUT_PULLUP); +bool butState = 1; //Variables only for testing - will be removed in final + +int d = 0; //Initializing variable for odometer distance. + +void send_sensor(); +Ticker timer; + +char index_html[] PROGMEM = R"=====( + + + + +Rover Command Center + + + + + + + +

ROVER COMMAND CENTER

+ +
+ +
+
+

Movement Control

+
+ +
+
+ + + +
+ +
+
+ +
+
+

Sensor Data

+
    + +
  • + +
    +
    + +
    +
  • + + +
  • + +
    +
    + 28mm +
    +
  • + +
+
+ +
+ +
+ + + + + + +)====="; + +AsyncWebServer server(80); // server port 80 for initial HTTP request for the main webpage. +WebSocketsServer websockets(81); // server port 81 for real time data flow through websockets. + +void notFound(AsyncWebServerRequest *request) +{ + request->send(404, "text/plain", "Page Not found. Check URI/IP address."); +} + +void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) { + + switch (type) + { + case WStype_DISCONNECTED: + Serial.printf("Client[%u] Disconnected!\n", num); + break; + case WStype_CONNECTED: { + IPAddress ip = websockets.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) { + Serial.print("deserializeJson() failed: "); + Serial.println(error.c_str()); + return; + } + + 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"]; + + digitalWrite(U_Led,MVM_F_status); + digitalWrite(L_Led,MVM_L_status); + digitalWrite(R_Led,MVM_R_status); + digitalWrite(D_Led,MVM_B_status); + } + } +} + +void setup() +{ + + Serial.begin(115200); + pinMode(U_Led,OUTPUT); + pinMode(L_Led,OUTPUT); + pinMode(R_Led,OUTPUT); + pinMode(D_Led,OUTPUT); + pinMode(butPin, INPUT_PULLUP); + + WiFi.softAP("RoverAP", "SplendidCheeks"); + Serial.println(); + Serial.println("RoverAP running"); + Serial.print("Rover IP address: "); + Serial.println(WiFi.softAPIP()); + + + if (!MDNS.begin("rover")) { + Serial.println("Error setting up MDNS responder!"); + while (1) { + delay(2000); + } + } + Serial.println("mDNS responder started! Rover Command Center can now be accessed at 'rover.local' "); + + + + server.on("/", [](AsyncWebServerRequest * request) + { + request->send_P(200, "text/html", index_html); + }); + + server.onNotFound(notFound); + + server.begin(); // it will start webserver + websockets.begin(); + websockets.onEvent(webSocketEvent); + timer.attach(0.5,send_sensor_data); +} + +void loop() +{ + websockets.loop(); + potVal = analogRead(potPin); +} + +void send_sensor_data() +{ + + butState = digitalRead(butPin); + if (butState == LOW) { + //increment ODO: + d += 10; + } + // JSON_Data = {"BTRY_VOLT":v,"ODO_DIST":d} + String JSON_Data = "{\"BTRY_VOLT\":"; + JSON_Data += potVal; + JSON_Data += ",\"ODO_DIST\":"; + JSON_Data += d; + JSON_Data += "}"; + //Serial.println(JSON_Data); + websockets.broadcastTXT(JSON_Data); +} \ No newline at end of file