Skip to content

Commit

Permalink
wdt improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
a-pokidin committed Oct 22, 2022
1 parent cb3fa21 commit 2e01a7c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 25 deletions.
17 changes: 10 additions & 7 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
]
}
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
],
"unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
]
}
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"files.associations": {
"functional": "cpp"
}
}
43 changes: 25 additions & 18 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ AutoConnect portal(server);
#define PING_TIMEOUT_MS 6000 //set from 500 to 1000 to see if its better for discons/hr
#define SUBACK_TIMEOUT_MS 6000 //set from 500 to 1000 to see if its better for discons/hr
void msgReceived(char* topic, byte* payload, unsigned int len);
char* mqtt_server = "192.168.3.1";
char* mqtt_server = "192.168.8.1";

#define CREDENTIAL_OFFSET 64
uint8_t opcode; // register
int I_HIGH = 19;
int I_LOW = 9;
int I_REPEATS = 5;
int I_PIN = 25;
int msgCount = 0;


EnergyMonitor emon1; // Create an instance
Expand Down Expand Up @@ -74,7 +75,7 @@ void setup(){
Serial.begin(115200);
Serial.println("Starting...");

wdtTicker.attach(60, wdtTickerCheck);
wdtTicker.attach(300, wdtTickerCheck);


pinMode(I_PIN, OUTPUT);
Expand All @@ -98,6 +99,19 @@ void setup(){

portal.config(config);

server.on("/clear-wifi", []() {
AutoConnectCredential credential;
station_config_t cfg;
uint8_t ent = credential.entries();

while (ent--) {
credential.load(ent, &cfg);
credential.del((const char*)&cfg.ssid[0]);
}
server.sendHeader("Location", String("/_ac/config"), true);
server.send(302, "text/plane","");
});

Serial.println("portal.begin...");
portal.begin();

Expand Down Expand Up @@ -221,7 +235,8 @@ boolean reconnect() {
// Once connected, publish an announcement...
mqtt.publish("POWER-Sensor/connected", "hello world");
// ... and resubscribe
mqtt.subscribe("POWER-Sensor/input");
// mqtt.subscribe("POWER-Sensor/input");
mqtt.subscribe("POWER-Sensor/output");
} else {
Serial.print("failed, rc=");
Serial.print(mqtt.state());
Expand All @@ -233,6 +248,7 @@ boolean reconnect() {


void callback(char* topic, byte* message, unsigned int length) {
msgCount++;
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
Expand All @@ -243,29 +259,20 @@ void callback(char* topic, byte* message, unsigned int length) {
}
myNewArray[length] = NULL;
Serial.println();

// Feel free to add more if statements to control more GPIOs with MQTT

// If a message is received on the topic esp32/output, you check if the message is either "on" or "off".
// Changes the output state according to the message
// if (String(topic) == "POWER-Sensor/input") {
// myObject = JSON.parse(myNewArray);
// Serial.print("Changing output to ");
// mainText = String(myNewArray);
// Serial.print((const char*) myObject["pass"]);
// Serial.println((const char*) myObject["next"]);
// }

}


int wdtTickerCount = 0;
void wdtTickerCheck(){

if( (wdtTickerCount > 5) || (msgCount < 5) ){
ESP.restart();
}
msgCount = 0;

if (WiFi.status() != WL_CONNECTED || !mqtt.connected()) {
wdtTickerCount++;
}

if(wdtTickerCount > 5){
ESP.restart();
}
}

0 comments on commit 2e01a7c

Please sign in to comment.