Change implementation to

- Use a switch instead of a button
- Replace interrupt with loop read because of crashes
(retro analysis: crashes might be due to wrong cabling)
This commit is contained in:
Joe Tretter
2023-12-12 18:35:43 -06:00
parent 008cd645cc
commit 5509160d03

View File

@@ -12,71 +12,67 @@
#define GPIO_SYNC_LED 19 #define GPIO_SYNC_LED 19
#define GPIO_REQUESTED_STATE_LED 2 #define GPIO_REQUESTED_STATE_LED 2
#define GPIO_EMERGENCY_BUTTON 16 #define GPIO_EMERGENCY_SWITCH 16
// Software Parameters // Software Parameters
#define LOOP_DELAY 1 #define LOOP_DELAY 1
#define PING_TIMEOUT_NUM_LOOPS 3 #define PING_TIMEOUT_NUM_LOOPS 3
#define EMERGENCY_BUTTON_SCAN_RATE_SECONDS 1
String macList[2] = {"24:6F:28:B1:EE:74", "24:6F:28:B2:40:8C"}; String macList[2] = {"24:6F:28:B2:40:8C", "24:6F:28:B1:EE:74"};
String macLocal, macRemote; String macLocal, macRemote;
uint8_t macRemoteIntArr[6]; uint8_t macRemoteIntArr[6];
esp_now_peer_info_t peerInfo = {}; esp_now_peer_info_t peerInfo = {};
int64_t lastPingUptimeInSeconds; int64_t lastPingUptimeInSeconds;
volatile int64_t lastButtonPushUptimeInSeconds;
volatile boolean isReceiverEmergencyLedOn; volatile boolean isReceiverEmergencyLedOn;
volatile boolean isSenderEmergency; volatile boolean isSenderEmergency;
TaskHandle_t BigLEDTask; TaskHandle_t BigLEDTask;
typedef struct struct_info { typedef struct struct_info
{
boolean isEmergency; boolean isEmergency;
boolean isPing; boolean isPing;
String macOfSender; String macOfSender;
} struct_info; } struct_info;
struct_info myData; struct_info myData;
int64_t getUptimeInSeconds(){ int64_t getUptimeInSeconds()
{
return esp_timer_get_time() / 1000000; return esp_timer_get_time() / 1000000;
} }
// Callback when data is sent // Callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) { void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status)
{
Serial.print("\r\nLast Packet Send Status:\t"); Serial.print("\r\nLast Packet Send Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail"); Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
} }
// Callback when data is received // Callback when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) { void OnDataRecv(const uint8_t *mac, const uint8_t *incomingData, int len)
{
struct_info myData; struct_info myData;
memcpy(&myData, incomingData, sizeof(incomingData)); memcpy(&myData, incomingData, sizeof(incomingData));
Serial.print("Bytes received: "); Serial.print("Bytes received: ");
Serial.println(len); Serial.println(len);
Serial.printf("d:_%d_|", myData.isPing); Serial.printf("d:_%d_|", myData.isPing);
if (myData.isPing) { if (myData.isPing)
{
lastPingUptimeInSeconds = getUptimeInSeconds(); lastPingUptimeInSeconds = getUptimeInSeconds();
} }
if(myData.isEmergency) { if (myData.isEmergency)
{
digitalWrite(GPIO_EMERGENCY_LED_SMALL, HIGH); digitalWrite(GPIO_EMERGENCY_LED_SMALL, HIGH);
isReceiverEmergencyLedOn = true; isReceiverEmergencyLedOn = true;
} else { }
else
{
digitalWrite(GPIO_EMERGENCY_LED_SMALL, LOW); digitalWrite(GPIO_EMERGENCY_LED_SMALL, LOW);
isReceiverEmergencyLedOn = false; isReceiverEmergencyLedOn = false;
} }
} }
void OnButtonPushed(){
Serial.println("BUTTON PUSH CALLBACK.");
if((getUptimeInSeconds()-lastButtonPushUptimeInSeconds)>EMERGENCY_BUTTON_SCAN_RATE_SECONDS){
isSenderEmergency=!isSenderEmergency;
digitalWrite(GPIO_REQUESTED_STATE_LED,isSenderEmergency?HIGH:LOW);
lastButtonPushUptimeInSeconds=getUptimeInSeconds();
Serial.println("BUTTON PUSH EXEC.");
}
}
// Convert single hex char to integer // Convert single hex char to integer
int char2int(char input) int char2int(char input)
{ {
@@ -101,7 +97,8 @@ void hex2bin(const char* src, uint8_t* target)
} }
// the big LED should flash when we are in emergency state. // the big LED should flash when we are in emergency state.
void BigLEDTaskCode(void * parameter){ void BigLEDTaskCode(void *parameter)
{
// Test the channels of the big LED // Test the channels of the big LED
digitalWrite(GPIO_EMERGENCY_LED_GREEN, LOW); digitalWrite(GPIO_EMERGENCY_LED_GREEN, LOW);
digitalWrite(GPIO_EMERGENCY_LED_BLUE, LOW); digitalWrite(GPIO_EMERGENCY_LED_BLUE, LOW);
@@ -116,8 +113,10 @@ void BigLEDTaskCode(void * parameter){
digitalWrite(GPIO_EMERGENCY_LED_BLUE, LOW); digitalWrite(GPIO_EMERGENCY_LED_BLUE, LOW);
// Blink in emergecy mode. // Blink in emergecy mode.
for(;;){ for (;;)
if (isReceiverEmergencyLedOn){ {
if (isReceiverEmergencyLedOn)
{
digitalWrite(GPIO_EMERGENCY_LED_BLUE, HIGH); digitalWrite(GPIO_EMERGENCY_LED_BLUE, HIGH);
delay(100); delay(100);
digitalWrite(GPIO_EMERGENCY_LED_BLUE, LOW); digitalWrite(GPIO_EMERGENCY_LED_BLUE, LOW);
@@ -132,7 +131,8 @@ void BigLEDTaskCode(void * parameter){
} }
} }
void setPowerLEDBrightness(int brightnessValue){ void setPowerLEDBrightness(int brightnessValue)
{
// setting PWM properties // setting PWM properties
const int freq = 5000; const int freq = 5000;
const int ledChannel = 0; const int ledChannel = 0;
@@ -143,7 +143,8 @@ void setPowerLEDBrightness(int brightnessValue){
ledcWrite(ledChannel, brightnessValue); ledcWrite(ledChannel, brightnessValue);
} }
void setup(){ void setup()
{
// GPIO Setup // GPIO Setup
pinMode(GPIO_POWER_LED, OUTPUT); pinMode(GPIO_POWER_LED, OUTPUT);
pinMode(GPIO_EMERGENCY_LED_RED, OUTPUT); pinMode(GPIO_EMERGENCY_LED_RED, OUTPUT);
@@ -152,7 +153,7 @@ void setup(){
pinMode(GPIO_EMERGENCY_LED_SMALL, OUTPUT); pinMode(GPIO_EMERGENCY_LED_SMALL, OUTPUT);
pinMode(GPIO_REQUESTED_STATE_LED, OUTPUT); pinMode(GPIO_REQUESTED_STATE_LED, OUTPUT);
pinMode(GPIO_SYNC_LED, OUTPUT); pinMode(GPIO_SYNC_LED, OUTPUT);
pinMode(GPIO_EMERGENCY_BUTTON,INPUT_PULLDOWN); pinMode(GPIO_EMERGENCY_SWITCH, INPUT_PULLDOWN);
// Initialize state // Initialize state
digitalWrite(GPIO_POWER_LED, HIGH); // Power LED ON digitalWrite(GPIO_POWER_LED, HIGH); // Power LED ON
@@ -165,10 +166,6 @@ void setup(){
isReceiverEmergencyLedOn = false; isReceiverEmergencyLedOn = false;
myData.isEmergency = false; myData.isEmergency = false;
isSenderEmergency = false; isSenderEmergency = false;
lastButtonPushUptimeInSeconds=getUptimeInSeconds();
// The button triggers an interrupt, attach callback function
attachInterrupt(GPIO_EMERGENCY_BUTTON, OnButtonPushed, RISING);
sleep(LOOP_DELAY); // give the serial monitor a moment to connect. sleep(LOOP_DELAY); // give the serial monitor a moment to connect.
Serial.begin(115200); Serial.begin(115200);
@@ -179,7 +176,8 @@ void setup(){
Serial.println("Local [" + macLocal + "] -> Remote: [" + macRemote + "]"); Serial.println("Local [" + macLocal + "] -> Remote: [" + macRemote + "]");
// Init ESP-NOW // Init ESP-NOW
if (esp_now_init() != ESP_OK) { if (esp_now_init() != ESP_OK)
{
Serial.println("Error initializing ESP-NOW"); Serial.println("Error initializing ESP-NOW");
return; return;
} }
@@ -200,7 +198,8 @@ void setup(){
peerInfo.ifidx = WIFI_IF_STA; peerInfo.ifidx = WIFI_IF_STA;
// Add peer // Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK){ if (esp_now_add_peer(&peerInfo) != ESP_OK)
{
Serial.println("Failed to add peer"); Serial.println("Failed to add peer");
return; return;
} }
@@ -212,40 +211,54 @@ void setup(){
xTaskCreatePinnedToCore(BigLEDTaskCode, "BigLEDHandler", 10000, NULL, 0, &BigLEDTask, 0); xTaskCreatePinnedToCore(BigLEDTaskCode, "BigLEDHandler", 10000, NULL, 0, &BigLEDTask, 0);
} }
void loop(){ void loop()
{
// Send message via ESP-NOW // Send message via ESP-NOW
sleep(LOOP_DELAY); sleep(LOOP_DELAY);
boolean isButtonPushed = (HIGH== digitalRead(GPIO_EMERGENCY_BUTTON)); Serial.println((HIGH == digitalRead(GPIO_EMERGENCY_SWITCH)) ? "EM-SWITCH ON" : "EM-SWITCH OFF");
Serial.println(isButtonPushed?"PUSHED":"RELEASED");
// only one device has a button, the origin will tell the recipient to revert back the status. // only the sender device has a switch, when the switch is operated (meaning I receive a high state) I know that I am the sender.
if (isSenderEmergency) { isSenderEmergency = (digitalRead(GPIO_EMERGENCY_SWITCH) == HIGH);
if (isSenderEmergency)
{
myData.macOfSender = macLocal; myData.macOfSender = macLocal;
} }
if (myData.macOfSender!=macLocal) { if (myData.macOfSender != macLocal)
{
Serial.println("I am the receiver");
myData.isEmergency = isReceiverEmergencyLedOn; myData.isEmergency = isReceiverEmergencyLedOn;
} else { }
else
{
Serial.println("I am the sender");
myData.isEmergency = isSenderEmergency; myData.isEmergency = isSenderEmergency;
} }
esp_err_t result = esp_now_send(macRemoteIntArr, (uint8_t *)&myData, sizeof(myData)); esp_err_t result = esp_now_send(macRemoteIntArr, (uint8_t *)&myData, sizeof(myData));
Serial.println(myData.isEmergency ? "EMERGENGY ON" : "EMERGENCY OFF"); Serial.println(myData.isEmergency ? "EMERGENGY ON" : "EMERGENCY OFF");
Serial.println((isSenderEmergency==1)?"SEN EMERG ON":"SEN EMERG OFF"); Serial.println((isSenderEmergency == true) ? "SEN EMERG ON" : "SEN EMERG OFF");
// Control the SYNC-LED, if we didn't receive a ping for too long, the LED is switched off // Control the SYNC-LED, if we didn't receive a ping for too long, the LED is switched off
if ((getUptimeInSeconds()-lastPingUptimeInSeconds) < (LOOP_DELAY*PING_TIMEOUT_NUM_LOOPS)) { if ((getUptimeInSeconds() - lastPingUptimeInSeconds) < (LOOP_DELAY * PING_TIMEOUT_NUM_LOOPS))
{
digitalWrite(GPIO_SYNC_LED, HIGH); digitalWrite(GPIO_SYNC_LED, HIGH);
} else { }
else
{
digitalWrite(GPIO_SYNC_LED, LOW); digitalWrite(GPIO_SYNC_LED, LOW);
} }
// To indicate the device hasn't crashed we vary the brightess of the power LED // To indicate the device hasn't crashed we vary the brightess of the power LED
setPowerLEDBrightness((getUptimeInSeconds()%5*20)+50); const int numberOfBrightnessLevels = 5;
const int brightnessDifferencePerLevel = 20;
const int minimumBrightness = 50;
setPowerLEDBrightness((getUptimeInSeconds() % numberOfBrightnessLevels * brightnessDifferencePerLevel) + minimumBrightness);
switch (result) { switch (result)
{
case ESP_OK: case ESP_OK:
Serial.println("[" + macLocal + "] Sent with success"); Serial.println("[" + macLocal + "] Sent with success");
break; break;
@@ -255,5 +268,4 @@ void loop(){
default: default:
Serial.printf("[%s] Error sending the data [%X]\n", macLocal, result); Serial.printf("[%s] Error sending the data [%X]\n", macLocal, result);
} }
} }