mirror of
https://github.com/supleed2/ELEC50003-P1-CW.git
synced 2024-12-23 05:55:50 +00:00
Updated main to use portable Ticker Lib
This commit is contained in:
parent
99c3af355b
commit
0dee19f28c
|
@ -108,17 +108,11 @@
|
||||||
var BTRY_VOLT = 0;
|
var BTRY_VOLT = 0;
|
||||||
var ODO_DIST = 0;
|
var ODO_DIST = 0;
|
||||||
|
|
||||||
function round(value, precision) {
|
|
||||||
var multiplier = Math.pow(10, precision || 0);
|
|
||||||
return Math.round(value * multiplier) / multiplier;
|
|
||||||
}
|
|
||||||
|
|
||||||
connection.onmessage = function (event) {
|
connection.onmessage = function (event) {
|
||||||
var raw_data = event.data;
|
var raw_data = event.data;
|
||||||
console.log(raw_data);
|
console.log(raw_data);
|
||||||
var data = JSON.parse(raw_data);
|
var data = JSON.parse(raw_data);
|
||||||
digiBTRY_VOLT = data.BTRY_VOLT;
|
BTRY_VOLT = data.BTRY_VOLT;
|
||||||
BTRY_VOLT = round((digiBTRY_VOLT * (4.8e-4) + 4), 1)
|
|
||||||
ODO_DIST = data.ODO_DIST;
|
ODO_DIST = data.ODO_DIST;
|
||||||
document.getElementById("btry_meter").value = BTRY_VOLT;
|
document.getElementById("btry_meter").value = BTRY_VOLT;
|
||||||
document.getElementById("Odometer").innerHTML = ODO_DIST;
|
document.getElementById("Odometer").innerHTML = ODO_DIST;
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
#include <AsyncTCP.h>
|
#include <AsyncTCP.h>
|
||||||
#include <ESPAsyncWebServer.h>
|
#include <ESPAsyncWebServer.h>
|
||||||
#include <ESPmDNS.h>
|
#include <ESPmDNS.h>
|
||||||
#include <Ticker.h>
|
#include "TickerV2.h"
|
||||||
#include <WebSocketsServer.h>
|
#include <WebSocketsServer.h>
|
||||||
#include <credentials.h>
|
#include <credentials.h>
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
|
@ -27,10 +27,10 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t length)
|
||||||
// Global objects
|
// Global objects
|
||||||
AsyncWebServer webserver(80);
|
AsyncWebServer webserver(80);
|
||||||
WebSocketsServer websocketserver(81);
|
WebSocketsServer websocketserver(81);
|
||||||
Ticker ticker(returnSensorData, 500, 0, MILLIS);
|
Ticker ticker;
|
||||||
|
|
||||||
// Global variables
|
// Global variables
|
||||||
int battery_voltage = 0;
|
float battery_voltage = 4.0f;
|
||||||
int distance_travelled = 0;
|
int distance_travelled = 0;
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
|
@ -71,7 +71,7 @@ void setup()
|
||||||
|
|
||||||
websocketserver.begin();
|
websocketserver.begin();
|
||||||
websocketserver.onEvent(webSocketEvent);
|
websocketserver.onEvent(webSocketEvent);
|
||||||
ticker.start();
|
ticker.attach(0.5, returnSensorData);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
|
@ -101,7 +101,17 @@ void printFPGAoutput()
|
||||||
void returnSensorData()
|
void returnSensorData()
|
||||||
{
|
{
|
||||||
// Collect sensor data here?
|
// Collect sensor data here?
|
||||||
|
distance_travelled++;
|
||||||
|
if (battery_voltage < 6)
|
||||||
|
{
|
||||||
|
battery_voltage += 0.2;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
battery_voltage = 4;
|
||||||
|
}
|
||||||
String JSON_Data = String("{\"BTRY_VOLT\":") + battery_voltage + String(",\"ODO_DIST\":") + distance_travelled + "}";
|
String JSON_Data = String("{\"BTRY_VOLT\":") + battery_voltage + String(",\"ODO_DIST\":") + distance_travelled + "}";
|
||||||
|
Serial.println(JSON_Data);
|
||||||
websocketserver.broadcastTXT(JSON_Data);
|
websocketserver.broadcastTXT(JSON_Data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue