macca-nz

ESP32 Dual Core Threading Blynk example

Jan 5th, 2022 (edited)
1,405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* ESP32 Blynk multi threading example
  2.  * IE: Blink runs on Core 0, User tasks on core 1
  3.  * Tip: ADC's are split acroos the two cores so be warned =)
  4.  * Functions:
  5.  * 1 x analogRead (10K or larger POT if its an experiment)
  6.  * 2 x digitalRead (Button 1 = Count Up, Button 2 = Zero Counter)
  7.  * Duty cycle for updates = 2 seconds
  8.  * Link to read more about the FreeRTOS API's
  9.  * https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/freertos.html#freertos
  10.  * Video of this sketch running https://www.youtube.com/watch?v=oVztP9qYUAc
  11.  *
  12.  */
  13.  
  14. // Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
  15. // See the Device Info tab, or Template settings
  16. #define BLYNK_TEMPLATE_ID "Your Template ID"
  17. #define BLYNK_DEVICE_NAME "Your Device Name"
  18. #define BLYNK_AUTH_TOKEN "Your Device Token"
  19.  
  20. // Comment this out to disable prints and save space
  21. #define BLYNK_PRINT Serial
  22.  
  23. #include <WiFi.h>
  24. #include <WiFiClient.h>
  25. #include <BlynkSimpleEsp32.h>
  26.  
  27. char auth[] = BLYNK_AUTH_TOKEN;
  28.  
  29. // Your WiFi credentials.
  30. // Set password to "" for open networks.
  31. char ssid[] = "Your WiFi SSID";
  32. char pass[] = "Your WiFi Password";
  33.  
  34. // Constants
  35. const int countPin = 15, zeroPin = 25, voltPin = 34;
  36. #define DUTY 50L
  37. #define REF_VOLTS (float)3.33
  38. #define ADC_MAX (uint16_t)4095
  39. #define TWO_SEC 40
  40. //Variables
  41. byte ledState = LOW, countState, lastCountState = HIGH,
  42.        zeroState, lastZeroState = HIGH;
  43. uint16_t pressCount = 0, dutyCount = 0;
  44. uint32_t prevTime = 0;
  45. bool countPress = false, zeroPress = false;
  46.  
  47. // Just practicing strings
  48. String calculateVolts(uint16_t i){        
  49.     float x = ((float)i*(REF_VOLTS/ADC_MAX));
  50.     return String(x);                             // Returns calculated result as a string
  51. }
  52.  
  53. void loop2(void *pvParameters){    // Core 1 loop - User tasks
  54.   while (1){
  55.     countState = digitalRead(countPin);
  56.     zeroState = digitalRead(zeroPin);
  57.         if(countState != lastCountState){
  58.             prevTime = millis();
  59.             countPress = true;
  60.         }
  61.         if(zeroState != lastZeroState){
  62.             prevTime = millis();
  63.             zeroPress = true;
  64.         }
  65.         if(millis() - prevTime >= DUTY){
  66.             prevTime = millis();
  67.             dutyCount++;
  68.                 if(countPress){
  69.                     if(countState == LOW){
  70.                         pressCount++;
  71.                         dutyCount = 0;
  72.                         countPress = false;
  73.                     }
  74.                 }
  75.                 if(zeroPress){
  76.                     if(zeroState == LOW){
  77.                         pressCount = 0;
  78.                         dutyCount = 0;
  79.                         zeroPress = false;
  80.                     }
  81.                 }
  82.                 if(dutyCount >= TWO_SEC){                    
  83.                     uint16_t reading = analogRead(voltPin);
  84.                     String volts = calculateVolts(reading);
  85.                     Blynk.virtualWrite(V0, reading);
  86.                     Blynk.virtualWrite(V1, volts);
  87.                     Blynk.virtualWrite(V2, pressCount);
  88.                     Serial.printf("Reading: %u", reading);
  89.                     Serial.printf("\tVoltage reading: %s", volts);
  90.                     Serial.printf("\tPress No: %u", pressCount);
  91.                     Serial.println("");
  92.                     dutyCount = 0;
  93.                 }
  94.         }
  95.         lastCountState = countState;
  96.         lastZeroState = zeroState;
  97.   }
  98. }
  99.  
  100. void loop1(void *pvParameters){    // Core 0 - Blink loop
  101.   while (1) {
  102.      Blynk.run();
  103.      delay(1);
  104.   }
  105. }
  106.  
  107. void setup(){
  108.   // Debug console
  109.   Serial.begin(115200);
  110.   Serial.println("Connecting to Blynk Server\n");
  111.   Blynk.begin(auth, ssid, pass);
  112.  
  113.   pinMode(countPin, INPUT_PULLUP);
  114.   pinMode(zeroPin, INPUT_PULLUP);
  115.   pinMode(voltPin, INPUT);
  116.  
  117.   xTaskCreatePinnedToCore(loop2, "loop2", 4096, NULL, 1, NULL, 1);
  118.   xTaskCreatePinnedToCore(loop1, "loop1", 4096, NULL, 1, NULL, 0);
  119.   Serial.println("");
  120.   Serial.println("Connected and Ready - Waiting for Button Press\n");
  121. }
  122.  
  123. void loop(){}
Add Comment
Please, Sign In to add comment