diff --git a/src/config.hpp b/src/config.hpp new file mode 100644 index 0000000..acbc28c --- /dev/null +++ b/src/config.hpp @@ -0,0 +1,48 @@ +#ifndef CONFIG_HPP +#define CONFIG_HPP + +/*===== Platform selection =====*/ +// platform list: +// ESP8266, ESP32, NO_WIFI +#ifndef PLATFORM + #define PLATFORM ESP8266 +#endif + + +#if PLATFORM == ESP32 || PLATFORM == ESP8266 + #define WIRELESS_SUPPORT +#endif + + + +/*===== Pin =====*/ +#define HEAT_GUN_FAN_PIN D2 +#define HEATER1_PIN D0 +#define HEATER2_PIN D5 +#define CONNECTION_LED_PIN D5 +#define HEATER1_LED_PIN D4 +#define HEATER2_LED_PIN D5 +#define HEATER1_THERMISTOR_PIN A0 +#define HEATER2_THERMISTOR_PIN A0 + + + +/*===== Wireless config =====*/ +#ifdef WIRELESS_SUPPORT + /*[AP]*/ + #define AP_SSID "test" + #define AP_PASSWD "sonofussr" + #define AP_CHANNEL 1 + #define AP_MAX_CLIENT 4 +#endif + + + +/*===== Heat Config =====*/ +/*[const]*/ +#define HEAT_OFF -300 + +/*[heat]*/ +#define MAX_TEMP 350 + +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 9dd5a25..1286686 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,45 +1,30 @@ +#include "config.hpp" #include -#include -#include -#include #include -#include -/*===== Define =====*/ -/*[const]*/ -#define HEAT_OFF -300 -/*[pin]*/ -#define HEAT_GUN_FAN_PIN D2 -#define HEATER1_PIN D0 -#define HEATER2_PIN D5 -#define CONNECTION_LED_PIN D5 -#define HEATER1_LED_PIN D4 -#define HEATER2_LED_PIN D5 -#define HEATER1_THERMISTOR_PIN A0 -#define HEATER2_THERMISTOR_PIN A0 +/*===== Platform =====*/ +/*[WIRELESS_SUPPORT]*/ +#ifdef WIRELESS_SUPPORT + #include "platform/esp_platform.hpp" + #include +#endif -/*[wifi]*/ -#define AP_SSID "test" -#define AP_PASSWD "sonofussr" -#define AP_CHANNEL 1 -#define AP_MAX_CLIENT 4 -/*[heat]*/ -#define MAX_TEMP 350 +/*[NO_WIFI]*/ +#if PLATFORM == NO_WIFI + +#endif -/*===== Object =====*/ -AsyncWebServer server(80); -AsyncWebSocket ws("/ws"); +/*===== Object =====*/ -Ticker temp_reporter; Ticker heater; -/*===== Variable =====*/ +/*===== Variable =====*/ //-300: off double temp1_current = 0; double temp1_target = HEAT_OFF; @@ -47,6 +32,7 @@ double temp2_current = 0; double temp2_target = HEAT_OFF; + /*===== Function =====*/ double getTemperature(uint8_t thermistor_pin){ @@ -91,66 +77,6 @@ void heat_drive(uint8_t heater, bool heat_sw){ } -/*===== Handler =====*/ - -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'; - - Serial.println((char*)data); - String str_data = (String)(char*)data; - String head = str_data.substring(0,3); - String body = str_data.substring(3,str_data.length()); - - Serial.println("head: "+ head + " body: " + body); - - if(head == "h1t"){ - temp1_target = body.toDouble(); - }else if(head == "h2t"){ - temp2_target = body.toDouble(); - } - //Serial.println(power); - - //pinMode(LED_PIN, 0); - //LED_handler.attach_ms(300, led_off); - } -} - - -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()); - temp1_target = HEAT_OFF; - temp2_target = HEAT_OFF; - break; - case WS_EVT_DISCONNECT: - Serial.printf("WebSocket client #%u disconnected\n", client->id()); - temp1_target = HEAT_OFF; - temp2_target = HEAT_OFF; - break; - case WS_EVT_DATA: - handleWebSocketMessage(arg, data, len); - break; - case WS_EVT_PONG: - case WS_EVT_ERROR: - temp1_target = HEAT_OFF; - temp2_target = HEAT_OFF; - break; - } -} - - -void temp_report_handler(){ - ws.textAll("h1c" + String(temp1_current)); - ws.textAll("h2c" + String(temp2_current)); - ws.textAll("h1t" + String(temp1_target)); - ws.textAll("h2t" + String(temp2_target)); - Serial.println("report temp" + String(" h1c: ") + String(temp1_current) + " h2c: " + String(temp2_current) + " h1t: " + String(temp1_target) + " h2t: " + String(temp2_target)); -} - - void heat_handler(){ temp1_current = getTemperature(HEATER1_THERMISTOR_PIN); temp2_current = getTemperature(HEATER2_THERMISTOR_PIN); @@ -169,59 +95,34 @@ void heat_handler(){ } + /*===== Main =====*/ void setup() { - Serial.begin(115200); - - //pin init - pinMode(HEAT_GUN_FAN_PIN, OUTPUT); - pinMode(HEATER1_PIN, OUTPUT); - pinMode(HEATER2_PIN, OUTPUT); - pinMode(CONNECTION_LED_PIN, OUTPUT); - pinMode(HEATER1_LED_PIN, OUTPUT); - pinMode(HEATER2_LED_PIN, OUTPUT); - - - //init LittleFS - if(!LittleFS.begin()){ - Serial.println("fail init fs"); - return; - } - - //show all files - Dir root = LittleFS.openDir("/"); - while (root.next()) { - File file = root.openFile("r"); - Serial.print(" FILE: "); - Serial.print(root.fileName()); - Serial.print(" SIZE: "); - Serial.println(file.size()); - file.close(); - } - - //init Wifi - WiFi.softAP(AP_SSID, AP_PASSWD, AP_CHANNEL, 0, AP_MAX_CLIENT); - - //web handler - ws.onEvent(onEvent); - server.addHandler(&ws); - server.onNotFound([](AsyncWebServerRequest *request){ request->redirect("/"); }); //captive portal redirect - server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ request->send(LittleFS, "/main.html", "text/html"); }); //main html - server.on("/main.css", HTTP_GET, [](AsyncWebServerRequest *request){ request->send(LittleFS, "/main.css", "text/css"); }); //main css - - server.begin(); - ArduinoOTA.begin(); - - //ticker - temp_reporter.attach(0.5, temp_report_handler); - heater.attach(0.1, heat_handler); + Serial.begin(115200); + + /*[Pin Init]*/ + pinMode(HEAT_GUN_FAN_PIN, OUTPUT); + pinMode(HEATER1_PIN, OUTPUT); + pinMode(HEATER2_PIN, OUTPUT); + pinMode(CONNECTION_LED_PIN, OUTPUT); + pinMode(HEATER1_LED_PIN, OUTPUT); + pinMode(HEATER2_LED_PIN, OUTPUT); + + #ifdef WIRELESS_SUPPORT + wireless_setup(); + #endif + + /*[Ticker Init]*/ + heater.attach(0.1, heat_handler); } + void loop() { - //temp1_current = rand()%100; - //temp2_current = rand()%100; - //delay(100); - ws.cleanupClients(); - ArduinoOTA.handle(); + //temp1_current = rand()%100; + //temp2_current = rand()%100; + //delay(100); + #ifdef WIRELESS_SUPPORT + wireless_handler(); + #endif } \ No newline at end of file diff --git a/src/platform/esp_platform.cpp b/src/platform/esp_platform.cpp new file mode 100644 index 0000000..d7fe6df --- /dev/null +++ b/src/platform/esp_platform.cpp @@ -0,0 +1,139 @@ +#include "../config.hpp" +#ifdef WIRELESS_SUPPORT + + +#include "esp_platform.hpp" +#include +#include +#include +#include +#include + + + +/*===== Platform =====*/ + /*[ESP32]*/ + #if PLATFORM == ESP32 + #include + #endif + + /*[ESP8266]*/ + #if PLATFORM == ESP8266 + #include + #endif + + + +/*===== Object =====*/ +AsyncWebServer server(80); +AsyncWebSocket ws("/ws"); +Ticker temp_reporter; + + + +/*===== Function =====*/ + +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'; + + Serial.println((char*)data); + String str_data = (String)(char*)data; + String head = str_data.substring(0,3); + String body = str_data.substring(3,str_data.length()); + + Serial.println("head: "+ head + " body: " + body); + + if(head == "h1t"){ + temp1_target = body.toDouble(); + }else if(head == "h2t"){ + temp2_target = body.toDouble(); + } + //Serial.println(power); + + //pinMode(LED_PIN, 0); + //LED_handler.attach_ms(300, led_off); + } +} + + +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()); + temp1_target = HEAT_OFF; + temp2_target = HEAT_OFF; + break; + case WS_EVT_DISCONNECT: + Serial.printf("WebSocket client #%u disconnected\n", client->id()); + temp1_target = HEAT_OFF; + temp2_target = HEAT_OFF; + break; + case WS_EVT_DATA: + handleWebSocketMessage(arg, data, len); + break; + case WS_EVT_PONG: + case WS_EVT_ERROR: + temp1_target = HEAT_OFF; + temp2_target = HEAT_OFF; + break; + } +} + + +void web_report_handler(){ + ws.textAll("h1c" + String(temp1_current)); + ws.textAll("h2c" + String(temp2_current)); + ws.textAll("h1t" + String(temp1_target)); + ws.textAll("h2t" + String(temp2_target)); + Serial.println("report temp" + String(" h1c: ") + String(temp1_current) + " h2c: " + String(temp2_current) + " h1t: " + String(temp1_target) + " h2t: " + String(temp2_target)); +} + + +void wireless_setup(){ + /*[LittleFS Init]*/ + if(!LittleFS.begin()){ + Serial.println("fail init fs"); + return; + } + + /*[Show All Files]*/ + Dir root = LittleFS.openDir("/"); + while (root.next()) { + File file = root.openFile("r"); + Serial.print(" FILE: "); + Serial.print(root.fileName()); + Serial.print(" SIZE: "); + Serial.println(file.size()); + file.close(); + } + + /*[Wifi Init]*/ + WiFi.softAP(AP_SSID, AP_PASSWD, AP_CHANNEL, 0, AP_MAX_CLIENT); + + /*[Web Init]*/ + ws.onEvent(onEvent); + server.addHandler(&ws); + server.onNotFound([](AsyncWebServerRequest *request){ request->redirect("/"); }); //captive portal redirect + server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ request->send(LittleFS, "/main.html", "text/html"); }); //main html + server.on("/main.css", HTTP_GET, [](AsyncWebServerRequest *request){ request->send(LittleFS, "/main.css", "text/css"); }); //main css + + server.begin(); + + /*[OTA Init]*/ + ArduinoOTA.begin(); + + /*[Ticker Init]*/ + temp_reporter.attach(0.5, web_report_handler); +} + + +void wireless_handler(){ + ws.cleanupClients(); + ArduinoOTA.handle(); +} + + + +#endif \ No newline at end of file diff --git a/src/platform/esp_platform.hpp b/src/platform/esp_platform.hpp new file mode 100644 index 0000000..ef1b010 --- /dev/null +++ b/src/platform/esp_platform.hpp @@ -0,0 +1,16 @@ +#ifndef ESP_PLATFORM_HPP +#define ESP_PLATFORM_HPP + + /*===== Variable =====*/ + extern double temp1_current; + extern double temp1_target; + extern double temp2_current; + extern double temp2_target; + + + /*===== Function =====*/ + void wireless_setup(); + + void wireless_handler(); + +#endif \ No newline at end of file