0

I am a beginner in ESP32-S2 programming and I find it hard to link all the data and produce one output in the form of queue on serial monitor. I am sorry for the imperfect coding skills as I am still learning and doing my best but can someone please help me out and identify how can I correct this code. Thank You!

#include <WiFi.h>
#include "time.h"
#include "sntp.h"
#include <esp_task_wdt.h>
#include <esp_sleep.h>
#include <queue.h>
#include <Arduino_HTS221.h>
#define WDT_TIMEOUT 200
#define Freertos/queue.h

int i = 0;
int last = millis();

int h;
int temp;
int chk = 0;

u_int32_t Freq = 0;

hw_timer_t *timer0 = NULL;
hw_timer_t *timer1 = NULL;
portMUX_TYPE timerMux0 = portMUX_INITIALIZER_UNLOCKED;
portMUX_TYPE timerMux1 = portMUX_INITIALIZER_UNLOCKED;

QueueHandle_t queue;

const char* ssid = "V2027"; 
const char* password = "oblivious101"; 
const char* ntpServer1 = "th.pool.ntp.org";
const long gmtOffset_sec = 3600*7;
const int daylightOffset_sec = 0;
const int wakeup_time_sec = 10;
const int ledTimeout = 10000;
const int SCLpin = 40;
const int SDApin = 41;
const byte button=0;
int counter = 1;

static QueueHandle_t xQueue1 = NULL, xQueue2 = NULL, xQueue3 = NULL, xQueue4 = NULL;
static QueueSetHandle_t xQueueSet = NULL;

TaskHandle_t TaskHandle_1;
TaskHandle_t TaskHandle_2;
TaskHandle_t TaskHandle_3;
TaskHandle_t TaskHandle_4;

void first_task (void *parameter)
{
   while(1) 
   {
    printLocalTime();
    vTaskDelay(10000/portTICK_PERIOD_MS);
    //Serial.println("Real Time Clock (RTC) : ");
    const char * const pcMessage = "Real Time Clock (RTC) : ";
    xQueueSend( xQueue1, &pcMessage, 0 );
  }
}

void second_task (void *parameter)
{
  for(;;)
  {
     configTime(gmtOffset_sec , daylightOffset_sec, ntpServer1);
     printLocalTime();
     vTaskDelay(20000/ portTICK_PERIOD_MS);
     //Serial.println("Network Time Protocol (NTP) : ");
     const char * const pcMessage = "Network Time Protocol (NTP) : ";
     xQueueSend( xQueue2, &pcMessage, 0 );
  }
}

void third_task (void *parameter)
{
  for(;;)
  {
      if (millis() - last >= 5000 && i < 10 ) {
      Serial.println("Watchdog Timer Reset Initiated");
      esp_task_wdt_reset();
      last = millis();
      i++;
      if (i == 10) {
      esp_err_t task_wdt_delete(NULL);
      //Serial.println("Watchdog Timer Disabled");
      const char * const pcMessage = "Watchdog Timer Disabled";
      xQueueSend( xQueue3, &pcMessage, 0 );
      delay (40000);
      }
    }
  }
}

void printLocalTime()
{
  struct tm timeinfo;
  if(!getLocalTime(&timeinfo)){
    Serial.println("Failed to Configure Time");
    return;
  }
  Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
  h = timeinfo.tm_min;
  if (chk == 0){
   temp = h;
    chk = 1;
  }
  if (h > (temp+1))
  {
    Serial.println("Deep Sleep Mode Initiated");
    esp_sleep_enable_timer_wakeup(wakeup_time_sec * 1000000);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
  }
      
    esp_deep_sleep_start();
    chk = 0;
  
    }
  delay(1000); 
}

void fourth_task (void *parameter)
{
  for(;;)
  {
    float temperature = HTS.readTemperature();
    //Serial.println("Temperature: "+String(temperature)+" °C");
    String pcMessage = "Temperature: "+String(temperature)+" °C";
    xQueueSend( xQueue4, &pcMessage, 0 );
    delay(3000);
    }
  }

void IRAM_ATTR turnOffLed() {
  portENTER_CRITICAL_ISR(&timerMux1);
  Serial.println("Interrupt Routin Switch LED Turned Off");
  digitalWrite(LED_BUILTIN, LOW); 
  
  portEXIT_CRITICAL_ISR(&timerMux1);
}

void IRAM_ATTR turnOnLed(){
  if (digitalRead(button)==0){
    digitalWrite(LED_BUILTIN, HIGH);
    Serial.println("Interrupt Routine Switch Activated");
    timerWrite(timer1, 0);
  }
}

void vReceiverTask( void *pvParameters )
{
    QueueHandle_t xQueueThatContainsData;
    char *pcReceivedString;
   for( ;; )
{
    xQueueThatContainsData = ( QueueHandle_t ) 
    xQueueSelectFromSet( xQueueSet,portMAX_DELAY );
    xQueueReceive( xQueueThatContainsData, &pcReceivedString, 0 );
    Serial.println(pcReceivedString);
}
  delay(1000);
}

void setup()
{
  Serial.begin(115200);

  Serial.printf("Synching Connection with %s ", ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
  }
  Serial.println(" Connected");

  Freq = getCpuFrequencyMhz();
  Serial.print("CPU Frequency = ");
  Serial.print(Freq);
  Serial.println("MHz");
  setCpuFrequencyMhz(80);
  Freq = getCpuFrequencyMhz();
  Serial.print("CPU Frequency = ");
  Serial.print(Freq);
  Serial.println("MHz");
  Serial.println("CPU Frequency reduced from 240 MHz to 80 MHz");

  Serial.println("Watchdog Timer Configuration Initiated....");
  esp_task_wdt_init(WDT_TIMEOUT, false);
  esp_task_wdt_add(NULL);

  queue = xQueueCreate(3, sizeof(float));
  
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
  pinMode(button, INPUT);
  attachInterrupt(digitalPinToInterrupt(button), turnOnLed, CHANGE); 
     
  xTaskCreate(first_task,"sec",10000,NULL,1,NULL);
  xTaskCreate(second_task,"min",10000,NULL,2,NULL);
  xTaskCreate(third_task,"WDT",10000,NULL,1,NULL);
  xTaskCreate(fourth_task,"readTemp",10000,NULL,2,NULL);

  xQueue1 = xQueueCreate( 1, sizeof( char * ) );
  xQueue2 = xQueueCreate( 1, sizeof( char * ) );
  xQueue3 = xQueueCreate( 1, sizeof( char * ) );
  xQueue4 = xQueueCreate( 1, sizeof( char * ) );
  xQueueSet = xQueueCreateSet( 1 * 4 );
  xQueueAddToSet( xQueue1, xQueueSet );
  xQueueAddToSet( xQueue2, xQueueSet );
  xQueueAddToSet( xQueue3, xQueueSet );
  xQueueAddToSet( xQueue4, xQueueSet );

  xTaskCreate(vReceiverTask, "Receiver", 100, NULL, 4, NULL );
  vTaskStartScheduler();
 
 delay(500);
}

void loop()
{
  Serial.println("Inserting Data in Queue");
  delay(7000);
}
  • 2
    Hi explorer, welcome to SO. Please define "one output" that you're looking to see in serial monitor. – Tarmo Oct 25 '22 at 19:00
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Oct 26 '22 at 02:47

0 Answers0