2021-05-29 03:54:53 +00:00
|
|
|
#include <WiFi.h>
|
2021-05-31 19:57:23 +00:00
|
|
|
#include <SPIFFS.h>
|
2021-05-29 03:54:53 +00:00
|
|
|
#include<ESPmDNS.h>
|
|
|
|
#include <WebSocketsServer.h>
|
|
|
|
#include <ESPAsyncWebServer.h>
|
|
|
|
#include <ArduinoJson.h>
|
|
|
|
#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'.
|
|
|
|
|
2021-05-31 17:49:50 +00:00
|
|
|
/* const char* ssid = "ssid";
|
|
|
|
const char* password = "xxxxxxxx"; */
|
|
|
|
|
2021-05-28 22:04:04 +00:00
|
|
|
int potVal = 0;
|
2021-05-29 03:54:53 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2021-05-31 19:57:23 +00:00
|
|
|
|
|
|
|
case WStype_BIN:
|
|
|
|
case WStype_ERROR:
|
|
|
|
case WStype_FRAGMENT_TEXT_START:
|
|
|
|
case WStype_FRAGMENT_BIN_START:
|
|
|
|
case WStype_FRAGMENT:
|
|
|
|
case WStype_FRAGMENT_FIN:
|
|
|
|
default:
|
|
|
|
break;
|
2021-05-29 03:54:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void setup()
|
|
|
|
{
|
|
|
|
|
2021-05-28 22:04:04 +00:00
|
|
|
Serial.begin(115200);
|
2021-05-29 03:54:53 +00:00
|
|
|
pinMode(U_Led,OUTPUT);
|
|
|
|
pinMode(L_Led,OUTPUT);
|
|
|
|
pinMode(R_Led,OUTPUT);
|
|
|
|
pinMode(D_Led,OUTPUT);
|
2021-05-28 22:04:04 +00:00
|
|
|
pinMode(butPin, INPUT_PULLUP);
|
2021-05-29 03:54:53 +00:00
|
|
|
|
2021-05-31 19:57:23 +00:00
|
|
|
if(!SPIFFS.begin(true)){
|
|
|
|
Serial.println("An Error has occurred while mounting SPIFFS");
|
|
|
|
return;
|
|
|
|
}
|
2021-05-31 17:49:50 +00:00
|
|
|
/* Serial.println();
|
|
|
|
Serial.println();
|
|
|
|
Serial.print("Connecting to ");
|
|
|
|
Serial.println(ssid);
|
|
|
|
|
|
|
|
WiFi.begin(ssid, password);
|
|
|
|
|
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
|
|
delay(500);
|
|
|
|
Serial.print(".");
|
|
|
|
}
|
|
|
|
|
|
|
|
Serial.println("");
|
|
|
|
Serial.println("Rover connected to ");
|
|
|
|
Serial.println(ssid);
|
|
|
|
Serial.println();
|
|
|
|
Serial.println("Rover IP address: ");
|
|
|
|
Serial.println(WiFi.localIP()); */
|
|
|
|
|
2021-05-29 03:54:53 +00:00
|
|
|
WiFi.softAP("RoverAP", "SplendidCheeks");
|
|
|
|
Serial.println();
|
|
|
|
Serial.println("RoverAP running");
|
|
|
|
Serial.print("Rover IP address: ");
|
|
|
|
Serial.println(WiFi.softAPIP());
|
|
|
|
|
2021-05-28 22:04:04 +00:00
|
|
|
|
2021-05-29 03:54:53 +00:00
|
|
|
if (!MDNS.begin("rover")) {
|
|
|
|
Serial.println("Error setting up MDNS responder!");
|
|
|
|
while (1) {
|
|
|
|
delay(2000);
|
|
|
|
}
|
2021-05-28 22:04:04 +00:00
|
|
|
}
|
2021-05-29 03:54:53 +00:00
|
|
|
Serial.println("mDNS responder started! Rover Command Center can now be accessed at 'rover.local' ");
|
|
|
|
|
2021-05-31 19:57:23 +00:00
|
|
|
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
request->send(SPIFFS, "/index.html", "text/html");
|
2021-05-29 03:54:53 +00:00
|
|
|
});
|
|
|
|
|
2021-05-31 19:57:23 +00:00
|
|
|
server.on("/favicon.ico", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
request->send(SPIFFS, "/favicon.ico", "image/png");
|
|
|
|
});
|
|
|
|
|
2021-05-29 03:54:53 +00:00
|
|
|
server.onNotFound(notFound);
|
|
|
|
|
2021-05-31 17:49:50 +00:00
|
|
|
server.begin();
|
2021-05-29 03:54:53 +00:00
|
|
|
websockets.begin();
|
|
|
|
websockets.onEvent(webSocketEvent);
|
|
|
|
timer.attach(0.5,send_sensor_data);
|
2021-05-28 22:04:04 +00:00
|
|
|
}
|
2021-05-29 03:54:53 +00:00
|
|
|
|
|
|
|
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;
|
2021-05-31 17:49:50 +00:00
|
|
|
JSON_Data += "}";
|
2021-05-29 03:54:53 +00:00
|
|
|
websockets.broadcastTXT(JSON_Data);
|
2021-05-31 17:49:50 +00:00
|
|
|
}
|