Add simple web interface
This commit is contained in:
parent
980c96cb40
commit
f35f52156d
11
data/index.html
Normal file
11
data/index.html
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>therminator</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<p class="reading">A: <span id="tempA"></span></p>
|
||||||
|
<p class="reading">B: <span id="tempB"></span></p>
|
||||||
|
<script src="script.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
44
data/script.js
Normal file
44
data/script.js
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
|
||||||
|
var gateway = `ws://${window.location.hostname}/ws`;
|
||||||
|
var websocket;
|
||||||
|
// Init web socket when the page loads
|
||||||
|
window.addEventListener('load', onload);
|
||||||
|
|
||||||
|
function onload(event) {
|
||||||
|
initWebSocket();
|
||||||
|
}
|
||||||
|
|
||||||
|
function getReadings(){
|
||||||
|
websocket.send("getReadings");
|
||||||
|
}
|
||||||
|
|
||||||
|
function initWebSocket() {
|
||||||
|
console.log('Trying to open a WebSocket connection...');
|
||||||
|
websocket = new WebSocket(gateway);
|
||||||
|
websocket.onopen = onOpen;
|
||||||
|
websocket.onclose = onClose;
|
||||||
|
websocket.onmessage = onMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
// When websocket is established, call the getReadings() function
|
||||||
|
function onOpen(event) {
|
||||||
|
console.log('Connection opened');
|
||||||
|
getReadings();
|
||||||
|
}
|
||||||
|
|
||||||
|
function onClose(event) {
|
||||||
|
console.log('Connection closed');
|
||||||
|
setTimeout(initWebSocket, 2000);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function that receives the message from the ESP32 with the readings
|
||||||
|
function onMessage(event) {
|
||||||
|
console.log(event.data);
|
||||||
|
var myObj = JSON.parse(event.data);
|
||||||
|
var keys = Object.keys(myObj);
|
||||||
|
|
||||||
|
for (var i = 0; i < keys.length; i++){
|
||||||
|
var key = keys[i];
|
||||||
|
document.getElementById(key).innerHTML = myObj[key];
|
||||||
|
}
|
||||||
|
}
|
|
@ -12,6 +12,9 @@ platform = espressif8266
|
||||||
board = esp12e
|
board = esp12e
|
||||||
framework = arduino
|
framework = arduino
|
||||||
lib_deps =
|
lib_deps =
|
||||||
adafruit/Adafruit MAX31855 library@^1.4.2
|
adafruit/Adafruit MAX31855 library
|
||||||
Wire
|
Wire
|
||||||
SPI
|
SPI
|
||||||
|
ESPAsyncTCP
|
||||||
|
zeed/ESP Async WebServer
|
||||||
|
Arduino_JSON
|
||||||
|
|
155
src/main.cpp
155
src/main.cpp
|
@ -1,5 +1,8 @@
|
||||||
#include <Adafruit_MAX31855.h>
|
#include <Adafruit_MAX31855.h>
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
#include <Arduino_JSON.h>
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
#include <ESPAsyncWebServer.h>
|
||||||
#include <SPI.h>
|
#include <SPI.h>
|
||||||
#include <pinout.h>
|
#include <pinout.h>
|
||||||
|
|
||||||
|
@ -7,11 +10,105 @@
|
||||||
#define CS_B D3
|
#define CS_B D3
|
||||||
Adafruit_MAX31855 thermocoupleA(CS_A);
|
Adafruit_MAX31855 thermocoupleA(CS_A);
|
||||||
Adafruit_MAX31855 thermocoupleB(CS_B);
|
Adafruit_MAX31855 thermocoupleB(CS_B);
|
||||||
|
|
||||||
|
AsyncWebServer server(80);
|
||||||
|
AsyncWebSocket ws("/ws");
|
||||||
|
|
||||||
|
JSONVar readings;
|
||||||
|
|
||||||
|
unsigned long int lastTime = 0;
|
||||||
|
unsigned long int timerDelay = 3000;
|
||||||
|
|
||||||
|
String getSensorReadings() {
|
||||||
|
if (isnan(thermocoupleA.readCelsius()) | isnan(thermocoupleB.readCelsius())) {
|
||||||
|
uint8_t eA = thermocoupleA.readError();
|
||||||
|
uint8_t eB = thermocoupleB.readError();
|
||||||
|
if (eA & MAX31855_FAULT_OPEN) {
|
||||||
|
Serial.println("FAULT: thermocouple A open");
|
||||||
|
readings["tempA"] = "FAULT: thermocouple A open";
|
||||||
|
}
|
||||||
|
if (eA & MAX31855_FAULT_SHORT_GND) {
|
||||||
|
Serial.println("FAULT: thermocouple A short to GND");
|
||||||
|
readings["tempA"] = "FAULT: thermocouple A short to GND";
|
||||||
|
}
|
||||||
|
if (eA & MAX31855_FAULT_SHORT_VCC) {
|
||||||
|
Serial.println("FAULT: thermocouple A short to VCC");
|
||||||
|
readings["tempA"] = "FAULT: thermocouple A short to VCC";
|
||||||
|
}
|
||||||
|
if (eB & MAX31855_FAULT_OPEN) {
|
||||||
|
Serial.println("FAULT: thermocouple B open");
|
||||||
|
readings["tempB"] = "FAULT: thermocouple B open";
|
||||||
|
}
|
||||||
|
if (eB & MAX31855_FAULT_SHORT_GND) {
|
||||||
|
Serial.println("FAULT: thermocouple B short to GND");
|
||||||
|
readings["tempB"] = "FAULT: thermocouple B short to GND";
|
||||||
|
}
|
||||||
|
if (eB & MAX31855_FAULT_SHORT_VCC) {
|
||||||
|
Serial.println("FAULT: thermocouple B short to VCC");
|
||||||
|
readings["tempB"] = "FAULT: thermocouple B short to VCC";
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
double tempA = thermocoupleA.readCelsius();
|
||||||
|
double tempB = thermocoupleB.readCelsius();
|
||||||
|
readings["tempA"] = String(tempA) + "°C";
|
||||||
|
readings["tempB"] = String(tempB) + "°C";
|
||||||
|
}
|
||||||
|
String jsonString = JSON.stringify(readings);
|
||||||
|
return jsonString;
|
||||||
|
}
|
||||||
|
|
||||||
|
void notifyClients(String sensorReadings) {
|
||||||
|
ws.textAll(sensorReadings);
|
||||||
|
}
|
||||||
|
|
||||||
|
void handleWebSocketMessage(void *arg, uint8_t *data, size_t len) {
|
||||||
|
AwsFrameInfo *info = (AwsFrameInfo*)arg;
|
||||||
|
if (info->final && info->index == 0 && info->len == len && info->opcode == WS_TEXT) {
|
||||||
|
//data[len] = 0;
|
||||||
|
//String message = (char*)data;
|
||||||
|
// Check if the message is "getReadings"
|
||||||
|
//if (strcmp((char*)data, "getReadings") == 0) {
|
||||||
|
//if it is, send current sensor readings
|
||||||
|
String sensorReadings = getSensorReadings();
|
||||||
|
Serial.print(sensorReadings);
|
||||||
|
notifyClients(sensorReadings);
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void onEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) {
|
||||||
|
switch (type) {
|
||||||
|
case WS_EVT_CONNECT:
|
||||||
|
Serial.printf("WebSocket client #%u connected from %s\n", client->id(), client->remoteIP().toString().c_str());
|
||||||
|
break;
|
||||||
|
case WS_EVT_DISCONNECT:
|
||||||
|
Serial.printf("WebSocket client #%u disconnected\n", client->id());
|
||||||
|
break;
|
||||||
|
case WS_EVT_DATA:
|
||||||
|
handleWebSocketMessage(arg, data, len);
|
||||||
|
break;
|
||||||
|
case WS_EVT_PONG:
|
||||||
|
case WS_EVT_ERROR:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void initWebSocket() {
|
||||||
|
ws.onEvent(onEvent);
|
||||||
|
server.addHandler(&ws);
|
||||||
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
while (!Serial) delay(1);
|
while (!Serial) delay(1);
|
||||||
|
delay(5000); // prevent garbage on serial output
|
||||||
|
Serial.println("serial init");
|
||||||
|
if(!SPIFFS.begin()) {
|
||||||
|
delay(500);
|
||||||
|
}
|
||||||
|
Serial.printf("SPIFFS init\n");
|
||||||
SPI.begin();
|
SPI.begin();
|
||||||
|
Serial.println("SPI init");
|
||||||
Serial.println("MAX31855 test");
|
Serial.println("MAX31855 test");
|
||||||
delay(500);
|
delay(500);
|
||||||
Serial.println("sensor init");
|
Serial.println("sensor init");
|
||||||
|
@ -19,32 +116,42 @@ void setup() {
|
||||||
Serial.println("sensor init error");
|
Serial.println("sensor init error");
|
||||||
while (true) delay(10);
|
while (true) delay(10);
|
||||||
}
|
}
|
||||||
|
WiFi.mode(WIFI_AP_STA);
|
||||||
|
Serial.printf("wifi init\n");
|
||||||
|
WiFi.begin("hackerspace.gr", "hsgrwifi");
|
||||||
|
Serial.printf("connecting");
|
||||||
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
|
Serial.printf(".");
|
||||||
|
delay(500);
|
||||||
|
}
|
||||||
|
Serial.printf(".\n");
|
||||||
|
Serial.printf("connected! ip addr: ");
|
||||||
|
Serial.println(WiFi.localIP());
|
||||||
|
|
||||||
Serial.println("init done");
|
initWebSocket();
|
||||||
|
|
||||||
|
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
|
||||||
|
request->send(SPIFFS, "/index.html", "text/html");
|
||||||
|
});
|
||||||
|
|
||||||
|
server.onNotFound([](AsyncWebServerRequest *request) {
|
||||||
|
request->send(404, "text/plain", "404 Not Found");
|
||||||
|
});
|
||||||
|
|
||||||
|
server.serveStatic("/", SPIFFS, "/");
|
||||||
|
server.begin();
|
||||||
|
Serial.printf("webserver init\n");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
Serial.print(">internalA:");
|
if ((millis() - lastTime) > timerDelay) {
|
||||||
Serial.println(thermocoupleA.readInternal());
|
String sensorReadings = getSensorReadings();
|
||||||
Serial.print(">internalB:");
|
Serial.println(sensorReadings);
|
||||||
Serial.println(thermocoupleB.readInternal());
|
notifyClients(sensorReadings);
|
||||||
|
|
||||||
double cA = thermocoupleA.readCelsius();
|
lastTime = millis();
|
||||||
double cB = thermocoupleB.readCelsius();
|
|
||||||
if (isnan(cA) | isnan(cB)) {
|
|
||||||
uint8_t eA = thermocoupleA.readError();
|
|
||||||
uint8_t eB = thermocoupleB.readError();
|
|
||||||
if (eA & MAX31855_FAULT_OPEN) Serial.println("FAULT: thermocouple A open");
|
|
||||||
if (eA & MAX31855_FAULT_SHORT_GND) Serial.println("FAULT: thermocouple A short to GND");
|
|
||||||
if (eA & MAX31855_FAULT_SHORT_VCC) Serial.println("FAULT: thermocouple A short to VCC");
|
|
||||||
if (eB & MAX31855_FAULT_OPEN) Serial.println("FAULT: thermocouple B open");
|
|
||||||
if (eB & MAX31855_FAULT_SHORT_GND) Serial.println("FAULT: thermocouple B short to GND");
|
|
||||||
if (eB & MAX31855_FAULT_SHORT_VCC) Serial.println("FAULT: thermocouple B short to VCC");
|
|
||||||
} else {
|
|
||||||
Serial.print(">tempcA:");
|
|
||||||
Serial.println(cA);
|
|
||||||
Serial.print(">tempcB:");
|
|
||||||
Serial.println(cB);
|
|
||||||
}
|
}
|
||||||
delay(1000);
|
|
||||||
}
|
ws.cleanupClients();
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue