Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // mapping of Waveshare ESP32 Driver Board
- // BUSY -> 25, RST -> 26, DC -> 27, CS-> 15, CLK -> 13, DIN -> 14
- #include <ArduinoJson.h>
- #include <GxEPD2_BW.h>
- #include <Adafruit_GFX.h>
- #include <Fonts/FreeMonoBold9pt7b.h>
- #include <Fonts/FreeSansBold24pt7b.h>
- #include <Fonts/Tahomabd9pt8b.h>
- #include <Fonts/Tahomabd45pt8b.h>
- #include <Fonts/FreeSans9pt7b.h>
- #include <Fonts/FreeSerif9pt7b.h>
- //#if defined(ESP32)
- GxEPD2_BW<GxEPD2_270, GxEPD2_270::HEIGHT> display(GxEPD2_270(/*CS=*/ 15, /*DC=*/ 27, /*RST=*/ 26, /*BUSY=*/ 25));
- //#endif
- #if defined(ESP8266) || defined(ESP32)
- #include <StreamString.h>
- #define PrintString StreamString
- #else
- class PrintString : public Print, public String
- {
- public:
- size_t write(uint8_t data) override
- {
- return concat(char(data));
- };
- };
- #endif
- #define EPD_BLACK 0x0000
- #define EPD_WHITE 0xFFFF
- // Wifi
- #include <WiFi.h>
- #include <WiFiClient.h>
- #include <WebServer.h>
- #include <ESPmDNS.h>
- #include <Update.h>
- //Veriables
- const char* ssid = "Test";
- const char* password = "Test123456";
- const char* host = "monitor-web";
- int16_t tbx, tby; uint16_t tbw, tbh , x , y;
- float tempC = 5;
- int timer_set = 7200;
- int timer_left = 0;
- int retry = 0;
- unsigned long previousMillis = 0;
- String timedis;
- // constants won't change :
- const long interval = 5000; // interval at which to blink (milliseconds)
- WiFiClient espClient;
- WebServer server(80);
- void handleRoot() {
- Serial.println("start Handle root");
- //String s = MAIN_page; //Read HTML contents
- String html = "<html> <head> <style> html { font-family: Arial; display: inline-block; margin: 0px auto; text-align: center; } h1 { color: #0F3376; padding: 2vh; } p { font-size: 1.5rem; } .button { display: inline-block; background-color: #008CBA; border: none; border-radius: 4px; color: white; padding: 16px 40px; text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer; } .button2 { background-color: #f44336; } .form { background-color: #f44336; color: white; } .units { font-size: 1.2rem; } .sensor-labels { font-size: 1.5rem; vertical-align:middle; padding-bottom: 15px; } </style> <title>E-Paper Temperature Monitor</title> <meta name='viewport' content='width=device-width, initial-scale=1'> </head> <body> <h1>E-Paper Temperature Monitor</h1><p> Firmware Upgrade</strong></p> <p> <form method='POST' action='/update' enctype='multipart/form-data'> <input class='button' type='file' name='update'> <a href='/update'><button class='button button2'>Upgrade</button></a> </form> <p></p> <a href='/restart'><button class='button button2'>Restart</button></a> </p> </body></html>";
- server.send(200, "text/html", html); //Send web page
- Serial.println("Handle root");
- }
- void printSensors();
- void initDisplay();
- String getData(String url) {
- String line,payload;
- if (espClient.connect("192.168.0.50", 80)) {
- Serial.print("requesting URL: ");
- Serial.println(url);
- espClient.print(String("GET ") + url + " HTTP/1.1\r\n" +
- "Host: " + host + "\r\n" +
- "Connection: close\r\n\r\n");
- Serial.println("request sent");
- int count = 10;
- while (count > 0) {
- line = espClient.readStringUntil('\n');
- if (line == "\r") {
- Serial.println("headers received");
- break;
- }
- count--;
- }
- payload = espClient.readStringUntil('\n');
- Serial.println("reply was:");
- Serial.println("==========");
- Serial.println(payload);
- Serial.println("==========");
- Serial.println("closing connection");
- }
- else{
- Serial.println("error");
- }
- return payload;
- }
- void getSensorData() {
- DynamicJsonBuffer jsonBuffer;
- JsonObject& json_temp = jsonBuffer.parseObject(getData("/cm?cmnd=status%208"));
- JsonObject& json_timer = jsonBuffer.parseObject(getData("/cm?cmnd=pulsetime1"));
- float test = tempC;
- tempC = (float)json_temp["StatusSNS"]["DS18B20"]["Temperature"];
- if ( test > 0 && tempC == 0) {
- tempC = test;
- }
- timer_set = (int)(json_timer["PulseTime1"]["Set"]);
- timer_left = (int)(json_timer["PulseTime1"]["Remaining"]);
- Serial.print("Temp: ");
- Serial.println(tempC);
- }
- void setup_wifi()
- {
- WiFi.begin(ssid, password);
- if (WiFi.waitForConnectResult() == WL_CONNECTED) {
- MDNS.begin(host);
- Serial.printf("Ready! Open http://%s.local in your browser\n", host);
- Serial.println(WiFi.localIP());
- } else {
- Serial.println("WiFi Failed");
- }
- }
- void setup()
- {
- display.init(115200); // uses standard SPI pins, e.g. SCK(18), MISO(19), MOSI(23), SS(5)
- // *** special handling for Waveshare ESP32 Driver board *** //
- // ********************************************************* //
- SPI.end(); // release standard SPI pins, e.g. SCK(18), MISO(19), MOSI(23), SS(5)
- //SPI: void begin(int8_t sck=-1, int8_t miso=-1, int8_t mosi=-1, int8_t ss=-1);
- SPI.begin(13, 12, 14, 15); // map and init SPI pins SCK(13), MISO(12), MOSI(14), SS(15)
- // *** end of special handling for Waveshare ESP32 Driver board *** //
- // **************************************************************** //
- Serial.begin(115200);
- Serial.println();
- Serial.println("Starting");
- WiFi.mode(WIFI_STA);
- setup_wifi();
- Serial.println();
- Serial.println("WiFi connected");
- // Print the IP address
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- delay(100);
- if (WiFi.waitForConnectResult() == WL_CONNECTED) {
- MDNS.begin(host);
- server.on("/", handleRoot);
- server.on("/restart", HTTP_GET, []() {
- ESP.restart();
- });
- server.on("/update", HTTP_POST, []() {
- server.sendHeader("Connection", "close");
- server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
- ESP.restart();
- }, []() {
- HTTPUpload& upload = server.upload();
- if (upload.status == UPLOAD_FILE_START) {
- Serial.setDebugOutput(true);
- Serial.printf("Update: %s\n", upload.filename.c_str());
- if (!Update.begin()) { //start with max available size
- Update.printError(Serial);
- }
- } else if (upload.status == UPLOAD_FILE_WRITE) {
- if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
- Update.printError(Serial);
- }
- } else if (upload.status == UPLOAD_FILE_END) {
- if (Update.end(true)) { //true to set the size to the current progress
- Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
- } else {
- Update.printError(Serial);
- }
- Serial.setDebugOutput(false);
- } else {
- Serial.printf("Update Failed Unexpectedly (likely broken connection): status=%d\n", upload.status);
- }
- });
- server.begin();
- MDNS.addService("http", "tcp", 80);
- }
- else{
- Serial.println("error");
- }
- }
- void loop() {
- unsigned long currentMillis = millis();
- server.handleClient();
- if (currentMillis - previousMillis >= interval) {
- previousMillis = currentMillis;
- getSensorData();
- printSensors();
- }
- }
- //initialise e-paper display
- void initDisplay() {
- display.init(115200);
- display.setFullWindow();
- display.setPartialWindow(0, 0, display.width(), display.height());
- }
- //print to e-paper display
- void printSensors() {
- delay(100);
- display.setRotation(1);
- display.setTextColor(display.epd2.hasColor ? GxEPD_RED : GxEPD_BLACK);
- display.fillScreen(GxEPD_WHITE);
- PrintString valueTemp;
- PrintString valueStatus;
- PrintString valueWater;
- display.firstPage();
- do
- {
- display.setFont(&tahomabd45pt8b);
- String tempo = String((int)tempC) + "°";
- valueTemp.print((int)tempC);
- display.getTextBounds(valueTemp, 0, 0, &tbx, &tby, &tbw, &tbh);
- x = ((display.width() - tbw) / 2) - tbx;
- y = (display.height() * 2 / 6) + tbh / 2; // y is base line!
- display.setCursor(x, y);
- display.print(tempo);
- display.setFont(&Tahomabd9pt8b);
- valueStatus.print(timer_set);
- display.getTextBounds(valueStatus, 0, 0, &tbx, &tby, &tbw, &tbh);
- x = ((display.width() - tbw) / 2) - tbx;
- y = (display.height() * 7 / 10) + tbh / 2; // y is base line!
- display.setCursor(x, y);
- display.print((String)valueStatus);
- valueWater.print("");
- display.getTextBounds(valueWater, 0, 0, &tbx, &tby, &tbw, &tbh);
- x = ((display.width() - tbw) / 2) - tbx;
- y = (display.height() * 8 / 10) + tbh / 2; // y is base line!
- display.setCursor(x, y);
- display.print(valueWater);
- display.setFont(&FreeSerif9pt7b);
- display.setCursor(1, 173);
- String wifi_status = "";
- if ((WiFi.status() == WL_CONNECTED)) {
- wifi_status = "Wifi: OK, IP: 192.168.0.24";
- }
- else {
- wifi_status = "Wifi: Error";
- }
- display.print(wifi_status);
- }
- while (display.nextPage());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement