Browse Source

serial mode support

master
p8p671 2 years ago
parent
commit
abb68a272a
  1. 4
      platformio.ini
  2. 4
      src/config.hpp
  3. 68
      src/main.cpp
  4. 122
      src/platform/esp_platform.cpp

4
platformio.ini

@ -15,7 +15,7 @@ framework = arduino
lib_deps = ottowinter/ESPAsyncWebServer-esphome@^3.0.0
board_build.filesystem = littlefs
upload_speed = 921600
monitor_speed = 115200
monitor_speed = 9600
[env:nodemcuv2_OTA]
platform = espressif8266
@ -25,4 +25,4 @@ lib_deps = ottowinter/ESPAsyncWebServer-esphome@^3.0.0
board_build.filesystem = littlefs
upload_protocol = espota
upload_port = 192.168.4.1
monitor_speed = 115200
monitor_speed = 9600

4
src/config.hpp

@ -9,11 +9,15 @@
#endif
// do not edit this
#if PLATFORM == ESP32 || PLATFORM == ESP8266
#define WIRELESS_SUPPORT
#endif
/*===== Serial Debug =====*/
//#define SERIAL_DEBUG
/*===== Pin =====*/
#define HEAT_GUN_FAN_PIN D2

68
src/main.cpp

@ -21,6 +21,7 @@
/*===== Object =====*/
Ticker heater;
Ticker serial_reporter;
@ -31,6 +32,10 @@ double temp1_target = HEAT_OFF;
double temp2_current = 0;
double temp2_target = HEAT_OFF;
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
/*===== Function =====*/
@ -95,11 +100,62 @@ void heat_handler(){
}
#ifndef SERIAL_DEBUG
void getCommand(){
String commandString = "";
if(inputString.length() > 0){
commandString = inputString.substring(1,6);
}
if(commandString.equals("TARTP")){ //absolutely set temperature
String abs_temp_str = inputString.substring(6,inputString.length());
temp1_target = abs_temp_str.toInt();
}
if(commandString.equals("DRCTP")){ //relatively set temperature
String rel_temp_str = inputString.substring(6,inputString.length());
temp1_target += rel_temp_str.toInt();
}
inputString="";
}
void serialEvent(){
while(Serial.available() && !stringComplete){
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if(inChar == '\n'){
stringComplete = true;
}
}
}
void serial_report_handler(){
Serial.println(temp1_target);
Serial.print(",");
Serial.print(temp1_current);
Serial.print(",");
Serial.print(temp2_target);
Serial.print(",");
Serial.print(temp2_current);
Serial.print(",");
}
#endif
/*===== Main =====*/
void setup() {
Serial.begin(115200);
Serial.begin(9600);
/*[Pin Init]*/
pinMode(HEAT_GUN_FAN_PIN, OUTPUT);
@ -115,6 +171,9 @@ void setup() {
/*[Ticker Init]*/
heater.attach(0.1, heat_handler);
#ifndef SERIAL_DEBUG
serial_reporter.attach(0.5, serial_report_handler);
#endif
}
@ -125,4 +184,11 @@ void loop() {
#ifdef WIRELESS_SUPPORT
wireless_handler();
#endif
#ifndef SERIAL_DEBUG
if(stringComplete){
getCommand();
stringComplete = false;
}
#endif
}

122
src/platform/esp_platform.cpp

@ -34,84 +34,94 @@ 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();
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());
#ifdef SERIAL_DEBUG
Serial.println("head: "+ head + " body: " + body);
#endif
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;
}
switch (type) {
case WS_EVT_CONNECT:
#ifdef SERIAL_DEBUG
Serial.printf("WebSocket client #%u connected from %s\n", client->id(), client->remoteIP().toString().c_str());
#endif
temp1_target = HEAT_OFF;
temp2_target = HEAT_OFF;
break;
case WS_EVT_DISCONNECT:
#ifdef SERIAL_DEBUG
Serial.printf("WebSocket client #%u disconnected\n", client->id());
#endif
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));
ws.textAll("h1c" + String(temp1_current));
ws.textAll("h2c" + String(temp2_current));
ws.textAll("h1t" + String(temp1_target));
ws.textAll("h2t" + String(temp2_target));
#ifdef SERIAL_DEBUG
Serial.println("report temp" + String(" h1c: ") + String(temp1_current) + " h2c: " + String(temp2_current) + " h1t: " + String(temp1_target) + " h2t: " + String(temp2_target));
#endif
}
void wireless_setup(){
/*[LittleFS Init]*/
if(!LittleFS.begin()){
Serial.println("fail init fs");
#ifdef SERIAL_DEBUG
Serial.println("fail init fs");
#endif
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();
}
#ifdef SERIAL_DEBUG
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();
}
#endif
/*[Wifi Init]*/
WiFi.softAP(AP_SSID, AP_PASSWD, AP_CHANNEL, 0, AP_MAX_CLIENT);
/*[Web Init]*/
ws.onEvent(onEvent);
server.addHandler(&ws);
@ -121,9 +131,11 @@ void wireless_setup(){
server.begin();
/*[OTA Init]*/
ArduinoOTA.begin();
/*[Ticker Init]*/
temp_reporter.attach(0.5, web_report_handler);
}

Loading…
Cancel
Save