diff --git a/platformio.ini b/platformio.ini index 766129f..4562647 100644 --- a/platformio.ini +++ b/platformio.ini @@ -10,6 +10,7 @@ [env:esp12e] platform = espressif8266 board = esp12e +board_build.filesystem = littlefs framework = arduino lib_deps = adafruit/Adafruit MAX31855 library @@ -18,3 +19,4 @@ lib_deps = ESPAsyncTCP zeed/ESP Async WebServer Arduino_JSON +monitor_filters = esp8266_exception_decoder, colorize diff --git a/src/main.cpp b/src/main.cpp index 57ca825..0fc3473 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -18,42 +19,25 @@ 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"; +String checkSensor(Adafruit_MAX31855 &sensor) { + if (isnan(sensor.readCelsius())) { + uint8_t err = sensor.readError(); + if (err & MAX31855_FAULT_OPEN) { + return "FAULT: thermocouple 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 (err & MAX31855_FAULT_SHORT_GND) { + return "FAULT: thermocouple short to GND"; } - if (eB & MAX31855_FAULT_OPEN) { - Serial.println("FAULT: thermocouple B open"); - readings["tempB"] = "FAULT: thermocouple B open"; + if (err & MAX31855_FAULT_SHORT_VCC) { + return "FAULT: thermocouple short to VCC"; } - 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"; - readings["time"] = String(lastTime); } + return String(sensor.readCelsius()) + "°C"; +} +String getSensorReadings() { + readings["tempA"] = checkSensor(thermocoupleA); + readings["tempB"] = checkSensor(thermocoupleB); + readings["time"] = String(lastTime); String jsonString = JSON.stringify(readings); return jsonString; } @@ -63,51 +47,49 @@ void notifyClients(String 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); - //} - } + 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; - } + 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); + ws.onEvent(onEvent); + server.addHandler(&ws); } void setup() { Serial.begin(9600); while (!Serial) delay(1); - delay(5000); // prevent garbage on serial output + delay(5000); // prevent garbage on serial output Serial.println("serial init"); - if(!SPIFFS.begin()) { - delay(500); - } - Serial.printf("SPIFFS init\n"); + LittleFS.begin(); + Serial.printf("LittleFS init\n"); SPI.begin(); Serial.println("SPI init"); Serial.println("MAX31855 test"); @@ -132,17 +114,16 @@ void setup() { initWebSocket(); server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) { - request->send(SPIFFS, "/index.html", "text/html"); + request->send(LittleFS, "/index.html", "text/html"); }); server.onNotFound([](AsyncWebServerRequest *request) { request->send(404, "text/plain", "404 Not Found"); }); - server.serveStatic("/", SPIFFS, "/"); + server.serveStatic("/", LittleFS, "/"); server.begin(); Serial.printf("webserver init\n"); - } void loop() {