Guest User

Untitled

a guest
May 5th, 2021
42
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Adafruit_SSD1306.h>
  2. #include <splash.h>
  3. #include <Wire.h>
  4. #include <Adafruit_GFX.h>
  5. #include <dht11.h>
  6. #include <Arduino_FreeRTOS.h>
  7. #include <queue.h>
  8. #define DHTTYPE DHT11
  9. #define DHTPIN 2
  10. #define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
  11. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  12. #define SCREEN_HEIGHT 32 // OLED display height, in pixels
  13. #define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
  14. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  15.  
  16. TaskHandle_t task1Handler;
  17. TaskHandle_t task2Handler;
  18. QueueHandle_t xQueue;
  19. dht11 DHT11;
  20.  
  21. void task1(void *parameter) {
  22.   Serial.println("Task 1 started");
  23.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C, true, true);
  24.   for (;;) {
  25.     int lReceivedValue = -100;
  26.     if (xQueueReceive( xQueue, &lReceivedValue, portMAX_DELAY ) == pdPASS) {
  27.       Serial.print( "Received = ");
  28.       Serial.println(lReceivedValue);
  29.       display.clearDisplay();
  30.       display.setTextSize(2);
  31.       display.setTextColor(SSD1306_WHITE);
  32.       display.setCursor(0, 0);
  33.       display.println(F("Hello, world!"));
  34.       display.setTextSize(2);
  35.       display.setTextColor(WHITE);
  36.       display.setCursor(10, 8);
  37.       display.println(lReceivedValue);
  38.       display.display();
  39.     } else {
  40.       Serial.println("Queue empty");
  41.     }
  42.     vTaskDelay(25 / portTICK_PERIOD_MS);
  43.     taskYIELD();
  44.   }
  45. }
  46.  
  47. void task2(void *parameter) {
  48.   Serial.println("Task 2 started");
  49.   int counter = 0;
  50.   int chk = DHT11.read(DHTPIN);
  51.   counter = DHT11.temperature;
  52.  
  53.   for (;;) {
  54.     Serial.println("READING TEMP");
  55.     DHT11.read(DHTPIN);
  56.     counter = DHT11.temperature;
  57.     Serial.print("Task 2 sending: ");
  58.     Serial.println(counter);
  59.     vTaskDelay(100 / portTICK_PERIOD_MS);
  60.     xQueueSend( xQueue, &counter, portMAX_DELAY == pdPASS);
  61.   }
  62. }
  63.  
  64. void setup() {
  65.   Serial.begin(9600);
  66.  
  67.   xQueue = xQueueCreate( 5, sizeof( int ) );
  68.   if ( xQueue != NULL )
  69.   { Serial.println("ALL GOOD");
  70.     Serial.println(xQueue != NULL);
  71.     xTaskCreate(
  72.       task2,
  73.       "SENDING QUEUE TASK",
  74.       128,
  75.       NULL,
  76.       1,
  77.       &task2Handler);
  78.     xTaskCreate(
  79.       task1,
  80.       "READING QUEUE TASK",
  81.       128,
  82.       NULL,
  83.       1,
  84.       &task1Handler);
  85.     vTaskStartScheduler();
  86.   } else {
  87.     Serial.print("QUEUE CANNOT BE CREATED :");
  88.     Serial.println(xQueue != NULL);
  89.   }
  90. }
  91.  
  92. void loop()
  93. {
  94.   // put your main code here, to run repeatedly:
  95. }
RAW Paste Data