Browse Source

multi platform support

master
p8p671 2 years ago
parent
commit
dcd22e93df
  1. 48
      src/config.hpp
  2. 175
      src/main.cpp
  3. 139
      src/platform/esp_platform.cpp
  4. 16
      src/platform/esp_platform.hpp

48
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

175
src/main.cpp

@ -1,45 +1,30 @@
#include "config.hpp"
#include <Arduino.h> #include <Arduino.h>
#include <ESPAsyncWebServer.h>
#include <ESP8266WiFi.h>
#include <LITTLEFS.h>
#include <Ticker.h> #include <Ticker.h>
#include <ArduinoOTA.h>
/*===== Define =====*/
/*[const]*/
#define HEAT_OFF -300
/*[pin]*/ /*===== Platform =====*/
#define HEAT_GUN_FAN_PIN D2 /*[WIRELESS_SUPPORT]*/
#define HEATER1_PIN D0 #ifdef WIRELESS_SUPPORT
#define HEATER2_PIN D5 #include "platform/esp_platform.hpp"
#define CONNECTION_LED_PIN D5 #include <ArduinoOTA.h>
#define HEATER1_LED_PIN D4 #endif
#define HEATER2_LED_PIN D5
#define HEATER1_THERMISTOR_PIN A0
#define HEATER2_THERMISTOR_PIN A0
/*[wifi]*/
#define AP_SSID "test"
#define AP_PASSWD "sonofussr"
#define AP_CHANNEL 1
#define AP_MAX_CLIENT 4
/*[heat]*/ /*[NO_WIFI]*/
#define MAX_TEMP 350 #if PLATFORM == NO_WIFI
#endif
/*===== Object =====*/
AsyncWebServer server(80); /*===== Object =====*/
AsyncWebSocket ws("/ws");
Ticker temp_reporter;
Ticker heater; Ticker heater;
/*===== Variable =====*/
/*===== Variable =====*/
//-300: off //-300: off
double temp1_current = 0; double temp1_current = 0;
double temp1_target = HEAT_OFF; double temp1_target = HEAT_OFF;
@ -47,6 +32,7 @@ double temp2_current = 0;
double temp2_target = HEAT_OFF; double temp2_target = HEAT_OFF;
/*===== Function =====*/ /*===== Function =====*/
double getTemperature(uint8_t thermistor_pin){ 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(){ void heat_handler(){
temp1_current = getTemperature(HEATER1_THERMISTOR_PIN); temp1_current = getTemperature(HEATER1_THERMISTOR_PIN);
temp2_current = getTemperature(HEATER2_THERMISTOR_PIN); temp2_current = getTemperature(HEATER2_THERMISTOR_PIN);
@ -169,59 +95,34 @@ void heat_handler(){
} }
/*===== Main =====*/ /*===== Main =====*/
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
//pin init /*[Pin Init]*/
pinMode(HEAT_GUN_FAN_PIN, OUTPUT); pinMode(HEAT_GUN_FAN_PIN, OUTPUT);
pinMode(HEATER1_PIN, OUTPUT); pinMode(HEATER1_PIN, OUTPUT);
pinMode(HEATER2_PIN, OUTPUT); pinMode(HEATER2_PIN, OUTPUT);
pinMode(CONNECTION_LED_PIN, OUTPUT); pinMode(CONNECTION_LED_PIN, OUTPUT);
pinMode(HEATER1_LED_PIN, OUTPUT); pinMode(HEATER1_LED_PIN, OUTPUT);
pinMode(HEATER2_LED_PIN, OUTPUT); pinMode(HEATER2_LED_PIN, OUTPUT);
#ifdef WIRELESS_SUPPORT
//init LittleFS wireless_setup();
if(!LittleFS.begin()){ #endif
Serial.println("fail init fs");
return; /*[Ticker Init]*/
} heater.attach(0.1, heat_handler);
//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);
} }
void loop() { void loop() {
//temp1_current = rand()%100; //temp1_current = rand()%100;
//temp2_current = rand()%100; //temp2_current = rand()%100;
//delay(100); //delay(100);
ws.cleanupClients(); #ifdef WIRELESS_SUPPORT
ArduinoOTA.handle(); wireless_handler();
#endif
} }

139
src/platform/esp_platform.cpp

@ -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

16
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
Loading…
Cancel
Save