logo elektroda
logo elektroda
X
logo elektroda

[Solved] ESP32 and Arduino MEGA: no communication with relays 6, 7, 8 using BT Serial Terminal

powerT 786 10
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 19286243
    powerT
    Level 9  
    Question for 100 points :-) .

    I have the ESP32 connected to the Arduino MEGA (as it should be, via the 3v - 5v logic, because without this bidolka the ESP started to heat up) and I send from the cell (BT Serial Terminal) a command (such my macro) under the button

    ? Relay, 109, 1 !

    The command is parsed, all great, means - turn on all relays. On ESP32 (serial.monitor) and Xiaomi (BT monitor) I get the feedback: enabled:

    Relay no. 1 ON
    Relay no. 2 ON
    Relay 3 ON
    Relay 4 ON
    Relay 5 ON
    Relay 6 ON
    Relay 7 ON
    Relay 8 ON

    The mega as instructed turns on all relays, only there is one but .... why does it show me on its SERIAL MONITOR:

    a command from the ESP has arrived: I am switching on relay no. 1
    a command from the ESP has arrived: I am switching on relay 2
    ESP Incoming command: switching on relay 3
    ESP Incoming ESP command: switching on relay 4
    ESP Incoming command: switching on relay 5

    6,7,8 does not exist --- but turns on. Macros from Xiaomi turn on 6,turn off 6, 7 or 8 is OK. But when I get 8 strings (in a loop with ESP) to display after 5 it stops showing it. It doesn't bother me but I like to speculate, buffer overflow ? or what ?

    what is going on ? The function in MEGA Relay_Report is executed for all the relays (1-8) just why does the Relay_Report function after 5 calls not show for 6, 7, 8 ?

    
    // ---------------------------------------------------------------------------------------------- //
    // --- R E L A Y   S W I T C H ------------------------------------------------------------------ //   <- Przekaznik (SWITCH)
    // ---------------------------------------------------------------------------------------------- //
                                                                                                      //     
    void Przekaznik_Przelacz( int p, bool stan)                                                       //
    {                                                                                                 //   
        switch ( stan )                                                                               //
        {                                                                                             //
            case  0 : P[p].turnOff(); PrzekaznikStan[p] = P[p].getState(); break;                     //   
            case  1 : P[p].turnOn();  PrzekaznikStan[p] = P[p].getState(); break;                     //   
        }                                                                                             //    
        Przekaznik_Raport(p);                                                                         //
    }                                                                                                 //
                                                                                                      //
    // ---------------------------------------------------------------------------------------------- //
    // --- R E L A Y   S W I T C H ------------------------------------------------------------------ //   <- Przekaznik (RAPORT)
    // ---------------------------------------------------------------------------------------------- //
                                                                                                      //     
    void Przekaznik_Raport(int  p)                                                                   //
    {                                                                                                 //   
        #if Terminal                                                                                  //
            Serial.print(F("Stan przekaźnika nr : "));                                                //
            Serial.print(p);                                                                          //        
            if ( PrzekaznikStan[p] ) Serial.println(F(" ON "));                                       //
            else                                                                                      //
            if (!PrzekaznikStan[p] ) Serial.println(F(" OFF "));                                      //
        #endif                                                                                        //  
    }
    
    Do you have a problem with Arduino? Ask question. Visit our forum Arduino.
  • ADVERTISEMENT
  • #2 19286262
    khoam
    Level 42  
    powerT wrote:
    Code I will not paste, it is not relevant
    .
    This is exactly what is most relevant, i.e. the code that sends on the ESP side and receives on the Mega side. Without the code, it is indeed a matter of guesswork.
  • ADVERTISEMENT
  • #3 19286269
    Anonymous
    Level 1  
  • #4 19286278
    powerT
    Level 9  
    Erbit wrote:
    These pieces of code contribute nothing to solving your problem. Show the entire code.
    .

    What for.
    if you execute 8 times the code Relay_Report why only Relay_Report is executed 5 times ????

    that is the question ... the rest of the code is meaningless ! what do you want to look for in 12000 lines of code in ESP ? where most of it is support for 2 x 8 MAX72XX display, 3 inch TTF with SD card, data logger, etc etc etc - you will only get confused and lose time. Here's where things fudge, 8 times it does its thing, 5 times it displays....
  • ADVERTISEMENT
  • #5 19286302
    khoam
    Level 42  
    In line 3000 of the code on either the Mega or ESP, data is incorrectly received or sent - the state of the input or output buffer is not examined correctly. I am unable to write anything more - the golden bullet is screwing me up.
  • #6 19286306
    powerT
    Level 9  
    Erbit wrote:
    These pieces of code contribute nothing to solving your problem. Show the whole code.
    .
    Okay, I've cracked it. Arduino is too slow for ESP I think. I hate DELAY I'll convert it to something else - delay(5) in the ESP32 code and it's already as it should be.

    
    // -------------------------------------------------------------------------------------------------- //
    // --- WYSYŁA dane o zmianie przekaźnika z BT do Arduino -------------------------------------------- //
    // -------------------------------------------------------------------------------------------------- //
    void Send2ArduinoRelayStatus(int numerPrzekaznika)                                                    //   <--- wyślij nowy status przekaźnika do Arduino  
    {                                                                                                     //
          String sendmsg = "";                                                                            //
          int code = 0;                                                                                   //
                                                                                                          //
          // przygotuj wiadomosc do wysłania ------------------------------------------------------------ //
                                                                                                          //
          switch ( numerPrzekaznika )                            // ustaw który przekaźnik -------------- //
          {                                                                                               //
              case   1  :  code = 301; break;                    // przekaźnik nr. 1 - kod 301 ---------- //
              case   2  :  code = 302; break;                    // przekaźnik nr. 2 - kod 302 ---------- //
              case   3  :  code = 303; break;                    // przekaźnik nr. 3 - kod 303 ---------- //
              case   4  :  code = 304; break;                    // przekaźnik nr. 4 - kod 304 ---------- //
              case   5  :  code = 305; break;                    // przekaźnik nr. 5 - kod 305 ---------- //
              case   6  :  code = 306; break;                    // przekaźnik nr. 6 - kod 306 ---------- //
              case   7  :  code = 307; break;                    // przekaźnik nr. 7 - kod 307 ---------- //
              case   8  :  code = 308; break;                    // przekaźnik nr. 8 - kod 308 ---------- //
              default   :  code = 0;   break;                    // error ! nie rób nic ----------------- //
          };                                                                                              //
                                                                                                          //
          sendmsg = "<Przekaznik, " + String(code) + ","+ String(RelayStatus[numerPrzekaznika]) + ">";    //         
          SerialDUE.println(sendmsg);                                                                     //
          delay(5);      
                                                                                                          //
          // --- gotowe i wysłane !!! ------------------------------------------------------------------- //      
                                                                                                          //
          #if DEBUG                                                                                       //
                #if TERMINAL                                                                              //
                    #if POLISH                                                                            //
                        Serial.printf("\nWysłano info o przekaźniku %d do Arduino", numerPrzekaznika);    //
                    #else                                                                                 //
                        Serial.printf("\nSend info on state Relay %d to Arduino", numerPrzekaznika);      //
                    #endif                                                                                //
                #endif                                                                                    //
           #endif                                                                                         //   
                                                                                                          //
          // --- BT info -------------------------------------------------------------------------------- //
                                                                                                          //
           #if POLISH                                                                                     //
              SerialBT.printf("Wysłano info o przekaźniku %d do Arduino\n", numerPrzekaznika);            //
          #else                                                                                           //
              SerialBT.printf("Send info on state Relay %d to Arduino\n", numerPrzekaznika);              //                             
          #endif                                                                                          //          
                                                                                                          //
          // --- koniec Debug --------------------------------------------------------------------------- //
    }                                                                                                     //
    // -------------------------------------------------------------------------------------------------- //
    
    .

    Added after 4 [minutes]:

    khoam wrote:
    In line 3000 of the code on either the Mega or the ESP incorrectly receives or sends data - the state of the input or output buffer is not examined correctly. I am unable to write anything more - the golden bullet is messing me up.
    .
    No, it's not. Too slow MEGA. With Due it was working for me. All clear now. 16Mhz
    Moderated By tmf:

    I have removed unnecessary stuff.

    .
  • #7 19286326
    khoam
    Level 42  
    powerT wrote:
    For a slow Arduino.
    .
    I don't think so. Usually in situations like this, the problems arise from a lack of verification of data receipt and/or a lack of verification that the output buffer has been emptied. Well, but that already requires a bit more effort. The delay() solution is a seal, not a solution.
  • ADVERTISEMENT
  • #8 19286337
    powerT
    Level 9  
    khoam wrote:
    powerT wrote:
    For slow Arduino.
    .
    I don't think so. Usually in situations like this, the problems arise from a lack of verification of data receipt and/or a lack of verification that the output buffer has been emptied. Well, but that already requires a bit more effort. The delay() solution is a seal, not a solution.


    As you say, I will rework it. I have a backlog of NANO's and MEGA's in my drawer I'm trying to use them for something, as they're only good for blinking LED's, switching relays, nothing else. I don't need these monitors after Serial for anything if it works, but I like to know why it is so, to look for an optimal solution.
    There will definitely be no DELAY, I will send with a delay but not by this method. It's a project for the house, I don't need 16 (for now I'm doing under 8) relays to suddenly turn something on or off. It's exterior and interior lighting: 4x icicles of 30 each, illumination of the outline of the house, crib lighting, in the garage which will work on a motion and dusk sensor.

    I don't use Blynk because I prefer the clunky Serial BT Terminal app for my smartphone, 35 buttons under the macro + the ability to send its commands under various parsers.
  • #9 19286344
    Anonymous
    Level 1  
  • #10 19286353
    powerT
    Level 9  
    I'm already thinking about it and working on it so that it doesn't send something else until the receiver says it can. This is the first time I've encountered this problem and I'm tackling it. I'm not one to look for a code and paste a ready-made one.

    You know how it is. My mum used to ask what do you do? I said, I code. Today, some people say: I'm looking for a code. I belong to the first group. I can do it.

    Thank you for your advice, I admit, I haven't checked it out. You have directed me to the optimal solution that I will use.
  • #11 19286365
    Anonymous
    Level 1  

Topic summary

The discussion revolves around an issue with an ESP32 and Arduino MEGA setup where relays 6, 7, and 8 are not responding correctly despite receiving commands via a Bluetooth Serial Terminal. The user reports that while the ESP32 sends commands to activate all relays, the Arduino MEGA only acknowledges the first five relays. Responses suggest that the problem may stem from incorrect data handling or timing issues, with recommendations to implement acknowledgment of receipt and verification of command understanding. The user plans to eliminate delays in the code and improve communication reliability between the devices.
Summary generated by the language model.
ADVERTISEMENT