Updated main to use portable Ticker Lib

This commit is contained in:
Aadi Desai 2021-06-05 13:11:09 +01:00
parent 99c3af355b
commit 0dee19f28c
2 changed files with 15 additions and 11 deletions

View file

@ -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;

View file

@ -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);
} }