Arduino Mega+Esp8266 WI-FI +Alexa

Hi guys,
I'm new here and I need help to understand and solve a little problem that maybe someone has already faced.
I'm creating a project with Arduino Mega + Esp8266 + Alexa and always when I turn on the board the Arduino Mega doesn't run while the wi-fi connection isn't connected. It's a problem because I found the Arduino MEGA to work regardless of the ESP8266 being connected to my wifi network.
YES I am using the serialprint to communicate the Esp8266 with MEGA below my code, if anyone can help me I will be grateful.
Just additional information, this code is using EspAlexa to do some commands.
Esp8266

#include <ESP8266WiFi.h> 
#include <Espalexa.h>   
// 
Espalexa Alexa;    
//                    
const char* ssid = "wifi";        
const char* password = "123456"; 
//
IPAddress ip(10,1,1,155);
IPAddress gateway(10,1,1,1);
IPAddress subnet(255,255,255,0);
IPAddress dns(8,8,8,8);
//
void setup() {
Serial.begin(115200);                                
ConexaoWifi();                                    
Alexa.addDevice("CAR", CAR);                  
Alexa.begin();                                      
}
//
void loop(){
ConexaoWifi();
Alexa.loop();
delay(1);
}    
//
void ConexaoWifi() {                   
if(WiFi.status()!= WL_CONNECTED){      
WiFi.mode(WIFI_STA);                   
WiFi.begin(ssid, password);            
WiFi.config(ip, gateway, subnet, dns); 
while(WiFi.status()!= WL_CONNECTED){  
delay(500);                            
Serial.print(".");}
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());}}
//
void CAR(uint8_t status){
if (status) {                
Serial.print("b");}           
else{                         
Serial.print("a");}  
}
//

Arduino Mega

#include <Bounce2.h>

Bounce INT_CAR = Bounce();         
int ST_INT_CAR = HIGH;             
//
void setup() {
Serial.begin(115200); 
INT_CAR.attach(2, INPUT_PULLUP);
INT_CAR.interval(5);  
pinMode(52, OUTPUT);
digitalWrite(52, EST_INT_CAR); 
Serial.begin(115200); 
}
//
void loop() {
INT_CAR.update();                 
if (INT_CAR.changed()){ 
int VER_INT_CAR = INT_CAR.read(); 
if (VER_INT_CAR == LOW){           
ST_INT_CAR = !ST_INT_CAR;    
}}   
 while (Serial.available()){
 char inChar = (char)Serial.read();
if (inChar == 'a' && digitalRead(52) == LOW){
 ST_INT_CAR =!ST_INT_CAR;
  }
  else if(inChar == 'b' && digitalRead(52) == HIGH){
 ST_INT_CAR =!ST_INT_CAR;
  }
  }
  digitalWrite(52, ST_INT_CAR);
}

If the wifi connection is lost the Arduino Mega isn't run the rest of the code.

image

1 Like

I don't see any connection between the Mega and the ESP.
Check out this project for an example of talking between arduino boards
Arduino to Arduino via Serial

Hi drmpf thanks for your reply, the connection between Arduino and ESP is done through dipswitches like this:


whenever Esp8266 writes something to the serial Arduino reads and executes. The problem is that whenever the connection is lost, the button's function doesn't work until the wifi connection comes back, and the intention is that if the wifi connection fails, the button works normally.
The serialprint from side Esp only will print "a" or "b" if I request to Alexa.

Ok in that case I would suggest you send a 'connected' 'disconnected' message to indicate to Mega the state of the WiFi.
You could shorten those to 'c' and 'd' and just extend the one char parser you have at the moment.

Hello again, just justifying that the problem does not occur because of the character that is available in Esp8266, but in waiting for the Arduino to execute the rest of the code since if that part of the code is not available, first Mega wait the serial is avaliable and after run.

while (Serial.available()){
 char inChar = (char)Serial.read();
if (inChar == 'a' && digitalRead(52) == LOW){
 ST_INT_CAR =!ST_INT_CAR;
  }
  else if(inChar == 'b' && digitalRead(52) == HIGH){
 ST_INT_CAR =!ST_INT_CAR;
  }
  }

Imagine if you had an LED connected and suddenly the wifi connection dropped, right away you would need to use the button to turn the LED on or off but the LED turns off and does not allow control by the button until the wi-fi connection returns. The question is why does Arduino Mega need to wait the wi-fi conexion returno to run again?
I guess it should be ignored and go back so the button can take over while the serial isn't available yet. Or am I wrong? Because if I just run the Mega without ESP8266 connection this code starts fast. I would just like to keep my button running the rest of the code in MEGA in case my Wi-Fi connection got lost.

Try this which does not depend on Serial available at all.

 int c = Serial.read();
 if (c != -1) {
   char inChar = (char)c;
  if (inChar == 'a' && digitalRead(52) == LOW){
   ST_INT_CAR =!ST_INT_CAR;
   }
   else if(inChar == 'b' && digitalRead(52) == HIGH){
   ST_INT_CAR =!ST_INT_CAR;
    }
   else if(inChar == 'c' ){
      // wifi connected do something
    }
   else if(inChar == 'd'){
      // wifi disconnected go to manual
    }
 }

Hello drmpf,
Thanks again for your reply. I made some test with your suggestion and the problem continue, I tested with the follow code on Arduino without read the serial monitor and when the pin from Mega is seted to connect to ESP exist a time that Mega needs to wait after run, probably Mega is waiting some conection from ESP or receive some confirmation that ESP system is Ok, but I don't know why.
I did it:

Arduino Mega.

void setup() {
pinMode(52, OUTPUT);
}

void loop() {
 digitalWrite(52,HIGH);
 delay(500);
 digitalWrite(52, LOW);
 delay(500);
}

image

ESP

#include <ESP8266WiFi.h> 
//                    
const char* ssid = "wifi";        
const char* password = "123456"; 
//
IPAddress ip(10,1,1,155);
IPAddress gateway(10,1,1,1);
IPAddress subnet(255,255,255,0);
IPAddress dns(8,8,8,8);
//
void setup() {
Serial.begin(115200);                                
ConexaoWifi();                                                                        
}
//
void loop(){
ConexaoWifi();
delay(1);
}    
//
void ConexaoWifi() {                   
if(WiFi.status()!= WL_CONNECTED){ 
Serial.println("d");     
WiFi.mode(WIFI_STA);                   
WiFi.begin(ssid, password);            
WiFi.config(ip, gateway, subnet, dns); 
while(WiFi.status()!= WL_CONNECTED){  
delay(500);                            
}
Serial.println("c");}}
//

Thanks.

Tricky to debug. Try sending debug output to Mega Serial1 and link that to the IDE either via a TTL to USB cable or via another Arduino (UNO/Mega etc)

You need a Serial.begin() on the Mega side

void setup() {
  Serial.begin(115200);  
  Serial1.begin(9600);  // to another Arduino 
pinMode(52, OUTPUT);
}

On the ESP side you should send the current state 'd' or 'c' once a sec so it is upto date if the connection is lost.
See How to write Timers and Delays in Arduino and
Multi-tasking in Arduino for how to structure you program to used the non-delay timers.

There is an Uno/Uno serial connection BUT use a hardware serial on the Mega instead

Try these sketches
ESP8266_connect_disconnect.ino (2.3 KB)
Mega_Serial1_Monitor.ino (476 Bytes)
Uno_SoftwareSerial_Monitor.ino (589 Bytes)
and the circuit below.
The ESP8266 code ouputs + for connect and - for disconnect and has a timer that tests the disconnect every now and again.
The Mega Sketch just loops through to the UNO and the UNO loops through to the Arduino IDE monitor so you can do some debugging.