Skip to content

Commit

Permalink
network.h, network.cpp, main.cpp: redirect serial device for Provisio…
Browse files Browse the repository at this point in the history
…nCli
  • Loading branch information
dingo35 committed Oct 17, 2024
1 parent 9f99728 commit fe395a3
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Sensorbox2_ESP/include/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ extern MQTTclient_t MQTTclient;
#endif

extern void WiFiSetup(void);
extern void handleWIFImode(void);
extern void handleWIFImode(HardwareSerial *s = &Serial);

#if DBG == 0
//used to steer RemoteDebug
Expand Down
26 changes: 19 additions & 7 deletions Sensorbox2_ESP/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,8 @@ void P1Receive() {
}
}

bool blockP1 = false;
bool blockCT = false;

// ----------------------------------------------------------------------------------------------------------------
// Task that handles incoming P1 and CT data
Expand All @@ -499,29 +501,39 @@ void P1Task(void * parameter) {
while(1) {

// Check if there is new P1 data.
P1Receive();
if (!blockP1) P1Receive();
if (!heap_caps_check_integrity_all(true)) {
_LOG_A("\nheap error after P1 receive\n");
}

// Check if there is a new measurement from the PIC (CT measurements)
if (WIFImode != 2) CTReceive();
if (!blockCT) CTReceive();
if (!heap_caps_check_integrity_all(true)) {
_LOG_A("\nheap error after CT receive\n");
}

// remember state of dataready, as it will be cleared after sending the data to the modbus Master.
if (dataready > datamemory) datamemory = dataready;

if (!(datamemory & 0x80 ) && WiFi.status() != WL_CONNECTED && esp_timer_get_time() / 1000000 > 5 && WIFImode != 2 && esp_timer_get_time() / 1000000 < 180) {
// if P1 is not connected,
// and we have no wifi
// and we are not in the first 5 seconds of startup (to give the existing wifi time to connect)
if (WiFi.status() != WL_CONNECTED && esp_timer_get_time() / 1000000 > 5 && WIFImode != 2 && esp_timer_get_time() / 1000000 < 180) {
// if we have no wifi
// and we are not in the first 5 seconds of startup (to give the existing wifi time to connect and P1 data to be entered)
// and we are not already in wifimode 2
// and we are not later then the first 180s after startup; perhaps we are not interested in having a wifi connection?
// we go to wifimode 2 smartconfig
WIFImode = 2;
handleWIFImode();
if (datamemory & 0x80 ) {
handleWIFImode(&Serial); // P1 data comes in so Serial0 is available
blockCT = true;
} else {
handleWIFImode(&Serial2);
blockP1 = true;
}
}

if (WIFImode != 2) {
blockCT = false;
blockP1 = false;
}

// keep track of available stack ram
Expand Down
52 changes: 31 additions & 21 deletions Sensorbox2_ESP/src/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ bool isValidInput(String input) {


static uint8_t CliState = 0;
void ProvisionCli() {

void ProvisionCli(HardwareSerial &s) {
// SSID and PW for your Router
static String Router_SSID, Router_Pass;
static char CliBuffer[64];
Expand All @@ -91,50 +90,50 @@ void ProvisionCli() {
char ch;

if (CliState == 0) {
Serial.println("Enter WiFi access point name:");
s.println("Enter WiFi access point name:");
CliState++;

} else if (CliState == 1 && entered) {
Router_SSID = String(CliBuffer);
Router_SSID.trim();
if (!isValidInput(Router_SSID)) {
Serial.println("Invalid characters in SSID.");
s.println("Invalid characters in SSID.");
Router_SSID = "";
CliState = 0;
} else CliState++; // All OK, now request password.
idx = 0;
entered = false;

} else if (CliState == 2) {
Serial.println("Enter WiFi password:");
s.println("Enter WiFi password:");
CliState++;

} else if (CliState == 3 && entered) {
Router_Pass = String(CliBuffer);
Router_Pass.trim();
if (idx < 8) {
Serial.println("Password should be min 8 characters.");
s.println("Password should be min 8 characters.");
Router_Pass = "";
CliState = 2;
} else CliState++; // All OK
idx = 0;
entered = false;

} else if (CliState == 4) {
Serial.println("WiFi credentials stored.");
s.println("WiFi credentials stored.");
WiFi.mode(WIFI_STA); // Set Station Mode
WiFi.begin(Router_SSID, Router_Pass); // Configure Wifi with credentials
CliState++;
}


// read input, and store in buffer until we read a \n
while (Serial.available()) {
ch = Serial.read();
while (s.available()) {
ch = s.read();

// When entering a password, replace last character with a *
if (CliState == 3 && idx) Serial.printf("\b*");
Serial.print(ch);
if (CliState == 3 && idx) s.printf("\b*");
s.print(ch);

// check for CR/LF, and make sure the contents of the buffer is atleast 1 character
if (ch == '\n' || ch == '\r') {
Expand All @@ -145,7 +144,7 @@ void ProvisionCli() {
} else if (idx < 63) { // Store in buffer
if (ch == '\b' && idx) {
idx--;
Serial.print(" \b"); // erase character from terminal
s.print(" \b"); // erase character from terminal
} else {
CliBuffer[idx++] = ch;
}
Expand Down Expand Up @@ -1386,6 +1385,10 @@ void timeSyncCallback(struct timeval *tv)


void SetupPortalTask(void * parameter) {
#ifdef SENSORBOX_VERSION
HardwareSerial *s1 = *((HardwareSerial **)parameter);
HardwareSerial &s = *s1;
#endif
_LOG_A("Start Portal...\n");
WiFi.disconnect(true);

Expand All @@ -1408,12 +1411,14 @@ void SetupPortalTask(void * parameter) {

//Wait for SmartConfig packet from mobile.
_LOG_V("Waiting for SmartConfig.\n");
Serial.end();
Serial.begin(115200, SERIAL_8N1, PIN_RXD, PIN_TXD, false); // Input from TX of PIC, and debug output to USB
#ifdef SENSORBOX_VERSION
s.end();
s.begin(115200, SERIAL_8N1, PIN_RXD, PIN_TXD, false); // Input from TX of PIC, and debug output to USB
#endif
unsigned long configTimer = millis();
while (!WiFi.smartConfigDone() && (WIFImode == 2) && (WiFi.status() != WL_CONNECTED) && millis() - configTimer < 180000) {
// Also start Serial CLI for entering AP and password.
ProvisionCli();
ProvisionCli(s);
delay(100);
} // loop until connected or Wifi setup menu is exited.
delay(2000); // give smartConfig time to send provision status back to the users phone.
Expand All @@ -1427,26 +1432,31 @@ void SetupPortalTask(void * parameter) {
handleWIFImode();
}
write_settings();
CliState= 0;
#ifndef SENSORBOX_VERSION //so we are not on a sensorbox but on a smartevse
LCDNav = 0;
#else
s.end();
if (s == Serial2) {
Serial2.setRxBufferSize(2048); // Important! first increase buffer, then setup Uart2
Serial2.begin(115200, SERIAL_8N1, PIN_RX, -1, true);
Serial.begin(115200, SERIAL_8N1, PIN_PGD, PIN_TXD, false);
} else
s.begin(115200, SERIAL_8N1, PIN_PGD, PIN_TXD, false); // Input from TX of PIC, and debug output to USB
#endif

CliState= 0;
Serial.end();
Serial.begin(115200, SERIAL_8N1, PIN_PGD, PIN_TXD, false); // Input from TX of PIC, and debug output to USB
WiFi.stopSmartConfig(); // this makes sure repeated SmartConfig calls are succesfull
vTaskDelete(NULL); //end this task so it will not take up resources
}


void handleWIFImode() {
void handleWIFImode(HardwareSerial *s) {
if (WIFImode == 2 && WiFi.getMode() != WIFI_AP_STA) {
//now start the portal in the background, so other tasks keep running
xTaskCreate(
SetupPortalTask, // Function that should be called
"SetupPortalTask", // Name of the task (for debugging)
10000, // Stack size (bytes) // printf needs atleast 1kb
NULL, // Parameter to pass
(void *) &s, // Parameter: which serial interface to use for ProvisionCli
3, // Task priority - medium
NULL // Task handleCTReceive
);
Expand Down

0 comments on commit fe395a3

Please sign in to comment.