4 changed files with 241 additions and 137 deletions
@ -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 |
@ -0,0 +1,139 @@
|
||||
#include "../config.hpp" |
||||
#ifdef WIRELESS_SUPPORT |
||||
|
||||
|
||||
#include "esp_platform.hpp" |
||||
#include <Arduino.h> |
||||
#include <ArduinoOTA.h> |
||||
#include <Ticker.h> |
||||
#include <LITTLEFS.h> |
||||
#include <ESPAsyncWebServer.h> |
||||
|
||||
|
||||
|
||||
/*===== Platform =====*/ |
||||
/*[ESP32]*/ |
||||
#if PLATFORM == ESP32 |
||||
#include <WiFi.h> |
||||
#endif |
||||
|
||||
/*[ESP8266]*/ |
||||
#if PLATFORM == ESP8266 |
||||
#include <ESP8266WiFi.h> |
||||
#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 |
@ -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 |
Loading…
Reference in new issue