This post is to comment the code of the project explained on:

you can donwload the code from my web page at the link: https://www.colome.org/esp32demo.zip 

Schema:

Let’s go, the first part oft the code (Setup):

In this part, the program connects to the wifi, create the two queues for communication between process and creates a new thread on core 0. Who is responsible to interact with requests and responses through the network.

void setup()
{
  pinMode(led1, OUTPUT);
   pinMode(led2, OUTPUT);
   pinMode(led3, OUTPUT);
   pinMode(led4, OUTPUT);
   pinMode(boto1, INPUT);
   pinMode(boto2, INPUT);
   pinMode(boto3, INPUT);
   pinMode(boto4, INPUT);
   // task variable to run on core 0 and controls socket communication
   TaskHandle_t Task1;
    Serial.begin(115200);

    // Set WiFi to station mode and disconnect from an AP if it was previously connected
    WiFi.mode(WIFI_STA);
    WiFi.disconnect();
    delay(100);

    Serial.println("Setup done");
    // try to connect to wifi, led 4 blinks
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        digitalWrite(led4, HIGH);
        delay(200);
        digitalWrite(led4, LOW);
        Serial.print(".");
        delay(200);
    }
    // wifi connection done, now try to connect to tcp/server
    delay(500);
    Serial.println("\nConnect done");
    digitalWrite(led4, HIGH);
    // create queues for communication between processes
    

    /* Create a queue capable of containing  10 char [50] */
    //void *message = malloc( sizeof(char) * 50 );
    xQueuesend = xQueueCreate( 10, sizeof(char) * 50 );
    xQueuereceive = xQueueCreate( 10, sizeof(char) * 50);

    if( xQueuesend == NULL || xQueuereceive == NULL)
    {
        Serial.println("\nError creating queues");
    }

    xTaskCreatePinnedToCore(
     clienttcp, /* Function to implement the task */
     "Task1", /* Name of the task */
      10000,  /* Stack size in words */
      NULL,  /* Task input parameter */
      0,  /* Priority of the task */
      &Task1,  /* Task handle. */
      0); /* Core where the task should run */
      
   
}

clienttcp :

This part of the code is for the asynchronous communication between server and client. It is done with the AsyncTCP library avaliable on GitHub at: https://github.com/me-no-dev/AsyncTCP The program is all the time waiting asynchronously for some event coming from the server ( function client.onData) when some are received, this is pushed on the receive queue:  xQueueSend( xQueuereceive… the program checks continuously the xQueuesend, that is the queue used to send events from the other thread to the TCP/IP server, this is the most difficult part of the code to understand 🙂

void clienttcp(void * c)
{
 // asyncronous tcp client 
 AsyncClient client;
  
  client.onError([](void* arg, AsyncClient * c, int8_t error) {
   // Serial.printf("Error: %s\n\n", c->errorToString(error));
    digitalWrite(led4, HIGH);
    c->close();
  });
  client.onTimeout([](void* arg, AsyncClient * c, uint32_t time) {
  //  Serial.printf("Timeout\n\n");
  });
  client.onConnect([](void* arg, AsyncClient * c) {
   // Serial.println("-");
  //  Serial.printf("Connected\n"); // Sending data.\n");
    digitalWrite(led4, LOW);
   // c->write(("?(B1,1)\r" +  String(host)  )
   //          .c_str());
  });
  client.onData([](void* arg, AsyncClient * c, void* data, size_t len) {
  //  Serial.printf("Data received with length: %d\n", len);

    //char subbuff[len];
    //memcpy( subbuff, &((char*)data)[0], len );
    //Serial.printf("%s", (char*)data);
    if( xQueueSend( xQueuereceive,
                ( char * ) data,
                ( TickType_t ) 10 ) != pdPASS )
    {
      /* Failed to post the message, even after 10 ticks. */
    }


    
    //c->close();
  });
  client.connect(host, port);
  //Serial.println(xPortGetCoreID());
  while (true)
  {
    char missatge[50];
    vTaskDelay(10);
    if( xQueueReceive( xQueuesend, missatge, ( TickType_t ) 10 ) )
      {
         //Serial.println(missatge);
         client.add(missatge,sizeof(missatge));
         client.send();

      }
  }
}

the main loop:

The Main Thread only checks for physical interaction of the buttons on the ESP32. When an event is produced ( button change state) the program sends the message to the queue XQueuesend through the function SendToServer. The process also checks the xQueuereceive queue, that is filled with the other process clienttcp ( when a message from the TCP/IP server is received)

void loop()
{
  char* cadena="";
  buttonState = digitalRead(boto1);
  if (buttonState != button1state)
  {
    button1state=buttonState;
    cadena="";
    if (buttonState==LOW) 
      cadena="?(B1,0)\r\n";
    else
      cadena="?(B1,1)\r\n";

    SendToServer(cadena);
  }
  buttonState = digitalRead(boto2);
  if (buttonState != button2state)
  {
    button2state=buttonState;
    cadena="";
    if (buttonState==LOW) 
      cadena="?(B2,0)\r\n";
    else
      cadena="?(B2,1)\r\n";

    SendToServer(cadena);
  }
 
  vTaskDelay(10);
  char missatge[50];
  if( xQueueReceive( xQueuereceive, missatge, ( TickType_t ) 10 ) )
    {
        //Serial.println(missatge);
       ProcessMessage(missatge);
       

    }
  // while (true);//client.connected)
}

The function SendToServer:

This function symply sends the action to the XQueuesend, the other process sends to the TCP/IP server the message to do.

void SendToServer(char* value)
{
   Serial.println(value);
  if( xQueueSend( xQueuesend,
                ( char * ) value,
                ( TickType_t ) 10 ) != pdPASS )
    {
    /* Failed to post the message, even after 10 ticks. */
    }

}

Facebooktwitterredditpinterestlinkedinmail