Is there anyone that could help me with a problem I am having with an ESP32 / ESP-Now project I am working on?
I have stitched together a couple different sketches, and although they both work separately, when I put them in the same sketch it hangs up. I have been beating my head against the wall trying to figure out what is wrong, but still can’t figure it out. I am new to Arduino IDE and coding.
The first thing to do with a program that hangs is to add plenty of console debug statements to the code in order to identify exactly which part of the code is executing and the point at which it is stopping.
Then you need to identify what is causing the halt.
A typical procedure for debugging this type of problem would be to make small changes, testing at each step. You could start with one of the sketches that is working, and add the configuration for the extra sketches (in turn), confirming that the original still works as expected. Then add the setups and reconfirm. If the setups are complex, add them in parts. Then identify parts of the remaining code that make sense on their own, and add them. Or, if you get as far as including the complete setups, it might be worth creating new test or demonstration routines for the components used in the additional sketches, and adding them and testing, before replacing them with the actual code. The point at which it stops working gives you the clue to the location of the problem.
I have created tons of debug statements, and have an idea where the code is hanging up, but I’m not sure how to fix the hang-up or why it is hanging up. If I remove the line that directs it to the other tab in IDE the original code runs fine. The introduction of the notification code causes a hang-up I don’t understand.
I think that you are actually adding some new routines (in the tabs) to an existing program, rather than trying to stitch a couple of programs together. You don’t have enough debug statements to isolate the problem. I think that the area it is failing is the sequence send_message_start() which ends by calling message_start_to_signal() which ends by calling postData_2(). What is the last debug statement you see in that sequence?
Note that you are opening the Serial device more than once, which is not recommended, although I don’t think it is connected to the problem.
The serial port is opened in setup(), as normal, and then again at the beginning of send_message_start() and send_message_open().
Do you mean that “void Flash Red LED complete” is the last console message you see? That does not indicate an issue at line 43, but at line 112, so that’s where the debug statements should be. As the previous call to ledcWriteTone() must have worked in order to get to that point, and the only difference between the two calls is the frequency argument, is it possible that using a frequency of 0 causes the hang?
I just tried changing while to if (Gate Open) but that didn’t help either. Just won’t recognize that the gate/door closed. If I comment out the line: send_message_open(); the board resets when the gate/door closes. . . That’s why I think it must be something in that messageopen tab
Are you referring to the variable ‘GateOpen’? If the code is not detecting that the value of that variable is changing, then you need to look at the code that changes it: OnDataRecv(). That’s where the next set of debug statement needs to be. You need to confirm the function is being called, and you need to look at the values in the data structure.
I assume that the problem of looping and not changing when the new message is received has gone away, and the problem is now that it simply hangs after that last debug statement.
That listing reveals a lot, but not perhaps what you expect. Why does the text “void Flash Red LED complete” occur twice, one second apart? There is only one statement that does that print: the second line should not be there. That suggests either that there is a memory fault, or that memory is getting corrupted. If I had to guess (and it’s only a guess) I would say that the send_message_open() call is triggering a response that is getting processed at OnDataRecv() (that takes one second) and something is going stupid at that point. You need debug statements in OnDataRecv() to see what was received and how it updates myData. You might need to write a little routine to print out all of incomingData in hex.
Yes, but the number of times it occurs depends on duration (which you should be printing in the debug message) and in any case not with a 1 second delay between messages. Something is happening between that first and second message that is causing a long delay and which does not have any debug messages. Probably the state of GateOpen is changing, and causing the delay loop to pause for longer than the delay specified, and that means that OnDataRecv must have fired, so that’s where you need to be looking. If that’s working OK then I suspect the problem is that second delay function is never returning because of the value of GateOpen when it is called. However that doesn’t explain how that last debug message can print, which is why I suspect corruption. You need many more debug messages in that area, and you need to be printing the values of relevant variables along with each message.
FWIW I never trust compound conditional statements without explicit bracketing. But in any case you need to know the values being used at each occurrence.