Understanding Events

I have been programing Arduino's for a while (not an expert by any meads) and now to the point where I want to understand events. The specific task I am working on has an ESP-32 talking to a MEGA over I2C. I want the ESP-32 to be able and request different information from the MEGA. Right now, I am able to send a request for data but don't know how to ask for specific data.

I am finding several lib's that make using events easer, but I want to understand how Arduino handles events.

Thanks.

I can't answer the question about events.

You first write a message to the Mega telling it which data you want. An extremely simple example can be that if the value that you send equals 1, it tells the Mega that you want the status of pin 1, if you send a 2 it tells the Mega that you want the status of pin 2. After successful send, you can request the data; the Mega now knows what you're interested in and will send that back.

Note:
It might be easier to just make the Mega the master and let it send the data to the ESP-32. In that case the Mega needs to send an identifier so the ESP-32 knows which data it is; e.g. 10 could mean pin 1 LOW and 21 could mean pin 2 HIGH.

Likely not the best choice of interface for this application.

Let's say one is using serial and one is using the proper level shifters.

One could send sentences.

A sentence could be structured with a beginning of sentence marker, data, data separetors, and end of sentence marker.

Something like

< !,2,4,5> Where < is the beginning of sentence marker, the characters !@#$%^&* are function designators, the ", " is a data separator, and the > is an end of sentence marker.

Here is ESP32 code to receive the thing

void fReceiveSerial_LIDAR( void * parameters  )
{
  bool BeginSentence = false;
  sSerial.reserve ( StringBufferSize300 );
  char OneChar;
  for ( ;; )
  {
    EventBits_t xbit = xEventGroupWaitBits (eg, evtReceiveSerial_LIDAR, pdTRUE, pdTRUE, portMAX_DELAY);
    if ( LIDARSerial.available() >= 1 )
    {
      while ( LIDARSerial.available() )
      {
        OneChar = LIDARSerial.read();
        if ( BeginSentence )
        {
          if ( OneChar == ‘>’)
          {
            if ( xSemaphoreTake( sema_ParseLIDAR_ReceivedSerial, xSemaphoreTicksToWait10 ) == pdTRUE )
            {
              xQueueOverwrite( xQ_LIDAR_Display_INFO, ( void * ) &sSerial );
              xEventGroupSetBits( eg, evtParseLIDAR_ReceivedSerial );
              //
            }
            BeginSentence = false;
            break;
          }
          sSerial.concat ( OneChar );
        }
        else
        {
          if ( OneChar == ‘<’ )
          {
            sSerial = “”; // clear string buffer
            BeginSentence = true; // found begining of sentence
          }
        }
      } //  while ( LIDARSerial.available() )
    } //if ( LIDARSerial.available() >= 1 )
    xSemaphoreGive( sema_ReceiveSerial_LIDAR );
  }
  vTaskDelete( NULL );
} //void fReceiveSerial_LIDAR( void * parameters  )

here is esp32 code to parse the data

void fParseLIDAR_ReceivedSerial ( void * parameters )
{
  // distribute received LIDAR info
  String sTmp = "";
  sTmp.reserve ( 20 );
  String sMessage = "";
  sMessage.reserve ( StringBufferSize300 );
  for ( ;; )
  {
    EventBits_t xbit = xEventGroupWaitBits (eg, evtParseLIDAR_ReceivedSerial, pdTRUE, pdTRUE, portMAX_DELAY) ;
    xQueueReceive ( xQ_LIDAR_Display_INFO, &sMessage, QueueReceiveDelayTime );
    //        Serial.println ( sMessage );
    int commaIndex = sMessage.indexOf(',');
    sTmp.concat ( sMessage.substring(0, commaIndex) );
    sMessage.remove( 0, (commaIndex + 1) ); // chop off begining of message
    //    Serial.println ( sTmp );
    if ( sTmp == "!" )
    {
      xSemaphoreGive ( sema_LIDAR_OK );
      //  Display info from LIDAR
      sLIDAR_Display_Info = sMessage;
    }
    if ( sTmp == "$" )
    {
      xEventGroupSetBits( eg1, evtResetWatchDogVariables );
    }
    if ( sTmp == "#")
    {
      // Serial.println ( "#" );
      xSemaphoreTake( sema_LIDAR_Alarm, xSemaphoreTicksToWait );
      sLIDAR_Alarm_info = sMessage;
      xSemaphoreGive( sema_LIDAR_Alarm );
      xEventGroupSetBits( eg, evtfLIDAR_Alarm );
    }
    //    Serial.println ( "parse serial ok" );
    sTmp = "";
    sMessage = "";
    xSemaphoreGive( sema_ParseLIDAR_ReceivedSerial );
    //    Serial.print( "fParseReceivedSerial " );
    //    Serial.print(uxTaskGetStackHighWaterMark( NULL ));
    //    Serial.println();
    //    Serial.flush();
  }
  vTaskDelete( NULL );
} // void fParseReceivedSerial ( void * parameters )

You seem to be talking about two separate things:

  1. I2C polling
  2. Events

What have they got to do with each other, in your application?

Or are you just confused about terminology?

A UART connection would be much easier to deal with than I2C.

You must use a logic level converter for data transfer connections between the 3.3V ESP32 and the 5V Mega.

I suggest editing the thread title to correct the typo so it will be searchable.

Not confused. I2C is the hardware side events are the SW side. Did not want to be told I did not provide enough information ( may be anyway). From what I have read the MEGA side (recipient of the request) can only accept one Interrupt from the I2C bus and that the device sending the request has to send information for the receiving device to pars and figure out what to send. From one of the responses I see how to send the data.

The two devices are in the same enclosure. the ESP is the master because it will be requesting data from other devices. It will than make the data avaliable via a web interface to start. Eventually the data will be sent to a pi.

What would you recommend?

yep. have one. Already have two of the devices talking.

UART with appropriate level shifting.

Help me understand, why is a UART with level shifting better than I2C with level shifting?

If all the data is going to be sent to a RPi just load up MQTT Broker on the RPI and use PubSubClient on the ESP32's to exchange the data over WiFi.

I have 13 ESP32's using PubSubClient to communicate with a RPi, wireless.