diff --git a/data/index.html b/data/index.html new file mode 100644 index 0000000..984fbe7 --- /dev/null +++ b/data/index.html @@ -0,0 +1,11 @@ + + + + therminator + + +

A:

+

B:

+ + + \ No newline at end of file diff --git a/data/script.js b/data/script.js new file mode 100644 index 0000000..49f6f77 --- /dev/null +++ b/data/script.js @@ -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]; + } +} \ No newline at end of file diff --git a/src/pinout.h b/include/pinout.h similarity index 100% rename from src/pinout.h rename to include/pinout.h diff --git a/platformio.ini b/platformio.ini index 54d68f3..766129f 100644 --- a/platformio.ini +++ b/platformio.ini @@ -12,6 +12,9 @@ platform = espressif8266 board = esp12e framework = arduino lib_deps = - adafruit/Adafruit MAX31855 library@^1.4.2 + adafruit/Adafruit MAX31855 library Wire SPI + ESPAsyncTCP + zeed/ESP Async WebServer + Arduino_JSON diff --git a/src/main.cpp b/src/main.cpp index fcc3cbf..6c91c7c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,8 @@ #include #include +#include +#include +#include #include #include @@ -7,11 +10,105 @@ #define CS_B D3 Adafruit_MAX31855 thermocoupleA(CS_A); 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() { Serial.begin(9600); 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(); - + Serial.println("SPI init"); Serial.println("MAX31855 test"); delay(500); Serial.println("sensor init"); @@ -19,32 +116,42 @@ void setup() { Serial.println("sensor init error"); 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() { - Serial.print(">internalA:"); - Serial.println(thermocoupleA.readInternal()); - Serial.print(">internalB:"); - Serial.println(thermocoupleB.readInternal()); + if ((millis() - lastTime) > timerDelay) { + String sensorReadings = getSensorReadings(); + Serial.println(sensorReadings); + notifyClients(sensorReadings); - double cA = thermocoupleA.readCelsius(); - 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); + lastTime = millis(); } - delay(1000); -} \ No newline at end of file + + ws.cleanupClients(); +}