These are chat archives for esp8266/Arduino

16th
Aug 2018
diraniyoussef
@diraniyoussef
Aug 16 2018 20:26
Good day. I'm having hard times with onData method of AsyncClient and I wonder if someone can help me
Me No Dev
@me-no-dev
Aug 16 2018 20:26
what is the problem?
diraniyoussef
@diraniyoussef
Aug 16 2018 20:26
The problem was probably memory, so to escape it I made things on the stack a bit more, instead of the heap
now it crashes like this when I receive something

ets Jan 8 2013,rst cause:4, boot mode:(3,6)

wdt reset
load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
v614f7c32
~ld

And no exception is thrown
Me No Dev
@me-no-dev
Aug 16 2018 20:27
show some code
diraniyoussef
@diraniyoussef
Aug 16 2018 20:31

'''
IntermediateMAndOp messageAndOperation_1_1( &async_client_1 );
IntermediateMAndOp messageAndOperation_1_2( &async_client_1 );

//defining locks
boolean while_connecting_lock = false; //volatile
boolean message_received_and_still_in_processing_lock = false; //volatile

//defining counters
int max_counter_to_manipulate_sockets; //volatile
int counter_to_manipulate_sockets = 0; //volatile
int max_counter_to_retry_socket_connection; //volatile
int counter_to_retry_socket_connection = 0; //volatile
int max_counter_to_destroy_old_socket; ///volatile
int counter_to_destroy_old_socket; //volatile

class IntermediateOperation {

void createSocket1() {
while_connecting_lock = true;
counter_to_retry_socket_connection = max_counter_to_retry_socket_connection;
//Serial.println("Before trying to connect to intermediate");
setEvents1( );
async_client_1.connect( IP, port1 );
Serial.printf("Now connecting to intermediate on port %d\n\n\n", port1);
}

void setEvents1() {
async_client_1.onConnect( {
//NodeMCU::setConnectFailureNotifierPinLow(); //Comment for debugging

  Serial.printf("onConnect triggered for port %d. Success.\n\n\n", port1);
  async_client_1.onError(NULL, NULL);    //not sure if it's good to uncomment it
  //Serial.println("onConnect - right after setting onError to an ignore state");

  //resetting counters

// Serial.println("onConnect - setting counter_to_manipulate_sockets.");
counter_to_manipulate_sockets = max_counter_to_manipulate_sockets;
// Serial.println("onConnect - setting counter_to_destroy_old_socket.");
counter_to_destroy_old_socket = max_counter_to_destroy_old_socket;
// Serial.println("onConnect - setting counter_to_retry_socket_connection.");
counter_to_retry_socket_connection = 0;

  //sending a message to intermediate server in order to let it register a printer of this mod panel
  messageAndOperation_1_1.sendHiIntermediate(); //the idea is that the intermediate server knows the id of this NodeMCU and registers a dedicated Printer there.

  async_client_1.onData([]( void * arg, AsyncClient * c, void * data, size_t len ) { //I would rather avoid setting data to anything
      //Serial.println("inside onData, before the lock test");
      if( !message_received_and_still_in_processing_lock ) { /*I don't want it to be flood with incoming messages and restarts.
                      * Another useful reason is that messageAndOperation_1_2 is not created within onData so it has to finish its work steadily.
                      */
        message_received_and_still_in_processing_lock = true;
        //Serial.println("onData triggered. Message received!");
        if( is_PCF_connected ) {
          char* received_buff = (char*) data; /*received_buff is full of weird garbage concatenated at the end of it, 
                                               * but it is well handled in IntermediateMAndOp particularily getLastCharOccurenceOfMessage
                                               */
          //Serial.printf("Intermediate - Data length is: %d \n And incoming message (as is) is: %s\n", len, String( received_buff ).c_str() );             
          /*
           * for( size_t i = 0; i < len; i++ ) {
           *   Serial.write( d[i] );
           * }
           */
          messageAndOperation_1_2.getMessage( received_buff, len ); 
         //and some more messages in class IntermediateMAndOp 

}
//delete[] received_buff;
}
message_received_and_still_in_processing_lock = false;
}
//delete arg;
//delete c;
}, NULL);

  while_connecting_lock = false;
  //delete arg;
  //delete client;       
}, NULL);         

}
'''

Me No Dev
@me-no-dev
Aug 16 2018 20:32
use ```cpp
diraniyoussef
@diraniyoussef
Aug 16 2018 20:32
like then code here then
ok
will repeat
Me No Dev
@me-no-dev
Aug 16 2018 20:33
on new lines though
diraniyoussef
@diraniyoussef
Aug 16 2018 20:35
async_client_2.onData([]( void * arg, AsyncClient * c, void * data, size_t len ) { 
          //Serial.println("inside onData, before the lock test");
          if( !message_received_and_still_in_processing_lock ) { 
            message_received_and_still_in_processing_lock = true;
            //Serial.println("onData triggered. Message received!");
            if( is_PCF_connected ) {
              char* received_buff = (char*) data; 

              messageAndOperation_2_2.getMessage( received_buff, len ); 
              if( messageAndOperation_2_2.getIdOfClient() != "" ) {//this is for authentication
                messageAndOperation_2_2.sendAck(); 
                if( messageAndOperation_2_2.analyze() ) { //this is to check if message context is intelligible
                  if( !messageAndOperation_2_2.isJustReport() ) {
                    messageAndOperation_2_2.updatePinAndEEPROM();
                  }
                  messageAndOperation_2_2.sendReport();
                }            
              } 
            }
            message_received_and_still_in_processing_lock = false;                   
          }        
      }, NULL);
I only copied the onData to make life easier
I made async_client_2, messageAndOperation_2_1, and messageAndOperation_2_2 on the stack instead on the heap
Now lucky me, it is working, but for some reason, sporadically, it crashes
mostly it happened with onData
I used to have a lot of Serial.println(...), then to limit the factors I removed them.
Me No Dev
@me-no-dev
Aug 16 2018 20:38
what are you actually trying to do?
maybe you are doing too much and wdt is rebooting
diraniyoussef
@diraniyoussef
Aug 16 2018 20:41
I connect to a server (on which I wrote the Java code there) that has a public IP on a specified port, then my NodeMCU (which is the client) expects from time to time to receive some messages (incoming from the server)
these messages are analyzed and then NodeMCU writes something to the EEPROM (if the incoming message wants it to) and sends back a report to the server (which I named "intermediate")
All this is done in onData
Right now I'm testing it and it's working fine
but you never know when it hits!
Me No Dev
@me-no-dev
Aug 16 2018 20:43
writing to eeprom is a bad idea from there
diraniyoussef
@diraniyoussef
Aug 16 2018 20:44
I thought that too...
As an amount of job, is that a lot?
Me No Dev
@me-no-dev
Aug 16 2018 20:44
some flash operations do take time
diraniyoussef
@diraniyoussef
Aug 16 2018 20:45
So since I want the NodeMCU to remember its state, then I will flag to the normal flow
Me No Dev
@me-no-dev
Aug 16 2018 20:45
yes
diraniyoussef
@diraniyoussef
Aug 16 2018 20:45
ok
Then I will try and come back later
Any other note?
Me No Dev
@me-no-dev
Aug 16 2018 20:46
you might not get the whole message in one packet
diraniyoussef
@diraniyoussef
Aug 16 2018 20:46
it's small
Me No Dev
@me-no-dev
Aug 16 2018 20:46
depending on how large is the message
diraniyoussef
@diraniyoussef
Aug 16 2018 20:46
like 30 chars
Me No Dev
@me-no-dev
Aug 16 2018 20:46
you might get more than one in a single packet too
diraniyoussef
@diraniyoussef
Aug 16 2018 20:47
Actually, I noticed that... And it's interesting.
Me No Dev
@me-no-dev
Aug 16 2018 20:47
it's normal ;)
diraniyoussef
@diraniyoussef
Aug 16 2018 20:48
I got garbage at the end of incoming messages, but luckily
onData([]( void * arg, AsyncClient * c, void * data, size_t len )
helped me out
so I analyze the useful (true) part

BTW, is the NULL in
```onData({

}, NULL); ```
useful as a callback to run somehow??

Me No Dev
@me-no-dev
Aug 16 2018 20:53
it's an argument that you can pass to every onData callback
do not bother
diraniyoussef
@diraniyoussef
Aug 16 2018 20:53
ok
Do I have to call something to take care of "data" pointer in onData callback?
or is it all automatic?
Me No Dev
@me-no-dev
Aug 16 2018 20:55
no, it's freed on exit of the function
and ack is sent
diraniyoussef
@diraniyoussef
Aug 16 2018 20:56
sure .
Sending two messages in onData is ok?
async_client->write( (const char*)usefulOutMessage_buff );
like two buffers
Me No Dev
@me-no-dev
Aug 16 2018 20:57
depends on the size of the messages, how much space is available in the connection window and such
diraniyoussef
@diraniyoussef
Aug 16 2018 20:58
I seem to have to be a real programmer, not just code hunter :)
so two messages of 30 chars is not too much I guess for say like 3 onData per minute
Me No Dev
@me-no-dev
Aug 16 2018 20:59
nope
diraniyoussef
@diraniyoussef
Aug 16 2018 20:59
ok, will try the EEPROM and come back then
thanks a lot