Plz help! Sensor cannot update its value (EVEN with a Timer setup)

CUHenri
Posts: 4
Joined: Sun Mar 19, 2023 5:08 am

Plz help! Sensor cannot update its value (EVEN with a Timer setup)

Postby CUHenri » Sun Mar 19, 2023 5:25 am

Hello, I have been setting up an ultrasonic sensor for calculating object distances on the Rainmaker recently. However, I have encountered an error when using the timer library. Originally, I used the SimpleTimer (by Kiryanenko Alexander) to set up an interval to update the parameters every 5s, but it doesn't work. :|
So I created my own timer for update, but the same error message kept showing: "E (6309) esp_rmaker_param: Current time not yet available. Cannot report time series data."
The board I am using is ESP32-C3 and here is the code. I have done several debugging and found out that the program cannot execute this statement: if (interruptCounter > 0 && wifi_connected) {}. Can anyone help me to see what is the problem? So many thanks :D :D

Code: Select all

#include "RMaker.h"
#include "WiFi.h"
#include "WiFiProv.h"
#include <wifi_provisioning/manager.h>

// Set Defalt Values
#define DEFAULT_RELAY_MODE true

// BLE Credentils
const char *service_name = "AB_1";
const char *pop = "1234567";

// GPIO
static uint8_t gpio_reset = 0;
static uint8_t relay = 10;
bool relay_state = true;

bool wifi_connected = 0;

//Ultrasonic Sensor
#define echoPin 18 
#define trigPin 19

//Timer
volatile int interruptCounter;
int totalInterruptCounter;
hw_timer_t * timer = NULL;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;

void IRAM_ATTR onTimer() {
 portENTER_CRITICAL_ISR(&timerMux);
 interruptCounter++;
 portEXIT_CRITICAL_ISR(&timerMux);
  }

//------------------------------------------- Declaring Devices -----------------------------------------------------//

//The framework provides some standard device types like switch, lightbulb, fan, temperature sensor.
static TemperatureSensor ultrasonic("Water Level");
static Switch my_switch("Heater", &relay);

void sysProvEvent(arduino_event_t *sys_event)
{
  switch (sys_event->event_id) {
    case ARDUINO_EVENT_PROV_START:
#if CONFIG_IDF_TARGET_ESP32
      Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n", service_name, pop);
      printQR(service_name, pop, "ble");
#else
      Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on SoftAP\n", service_name, pop);
      printQR(service_name, pop, "softap");
#endif
      break;
    case ARDUINO_EVENT_WIFI_STA_CONNECTED:
      Serial.printf("\nConnected to Wi-Fi!\n");
      wifi_connected = 1;
      delay(500);
      break;
    case ARDUINO_EVENT_PROV_CRED_RECV: {
        Serial.println("\nReceived Wi-Fi credentials");
        Serial.print("\tSSID : ");
        Serial.println((const char *) sys_event->event_info.prov_cred_recv.ssid);
        Serial.print("\tPassword : ");
        Serial.println((char const *) sys_event->event_info.prov_cred_recv.password);
        break;
      }
    case ARDUINO_EVENT_PROV_INIT:
      wifi_prov_mgr_disable_auto_stop(10000);
      break;
    case ARDUINO_EVENT_PROV_CRED_SUCCESS:
      Serial.println("Stopping Provisioning!!!");
      wifi_prov_mgr_stop_provisioning();
      break;
  }
}

void write_callback(Device *device, Param *param, const param_val_t val, void *priv_data, write_ctx_t *ctx)
{
  const char *device_name = device->getDeviceName();
  Serial.println(device_name);
  const char *param_name = param->getParamName();

  if (strcmp(device_name, "Heater") == 0)
  {
    if (strcmp(param_name, "Power") == 0)
    {
      Serial.printf("Received value = %s for %s - %s\n", val.val.b ? "true" : "false", device_name, param_name);
      relay_state = val.val.b;
      (relay_state == false) ? digitalWrite(relay, LOW) : digitalWrite(relay, HIGH);
      param->updateAndReport(val);
    }
  }
}


void setup()
{
  Serial.begin(115200);
  
 //Timer
  timer = timerBegin(0, 80, true);
  timerAttachInterrupt(timer, &onTimer, true);
  timerAlarmWrite(timer, 5000000, true);
  timerAlarmEnable(timer);

  // Configure the input GPIOs
  pinMode(gpio_reset, INPUT);
  pinMode(relay, OUTPUT);
  digitalWrite(relay, DEFAULT_RELAY_MODE);

  //Ultrasonic Sensor
  pinMode(trigPin, OUTPUT); 
  pinMode(echoPin, INPUT); 

  //------------------------------------------- Declaring Node -----------------------------------------------------//
  Node my_node;
  my_node = RMaker.initNode("Aquaponics_System");

  //Standard switch device
  my_switch.addCb(write_callback);

  //------------------------------------------- Adding Devices in Node -----------------------------------------------------//
  my_node.addDevice(my_switch);
  my_node.addDevice(ultrasonic);
 
  //This is optional
  RMaker.enableOTA(OTA_USING_PARAMS);
  //If you want to enable scheduling, set time zone for your region using setTimeZone().
  //The list of available values are provided here https://rainmaker.espressif.com/docs/time-service.html
  //RMaker.setTimeZone("Asia/Hong_Kong");
  // Alternatively, enable the Timezone service and let the phone apps set the appropriate timezone
  RMaker.enableTZService();
  RMaker.enableSchedule();

  Serial.printf("\nStarting ESP-RainMaker\n");
  RMaker.start();

  WiFi.onEvent(sysProvEvent);

#if CONFIG_IDF_TARGET_ESP32
  WiFiProv.beginProvision(WIFI_PROV_SCHEME_BLE, WIFI_PROV_SCHEME_HANDLER_FREE_BTDM, WIFI_PROV_SECURITY_1, pop, service_name);
#else
  WiFiProv.beginProvision(WIFI_PROV_SCHEME_SOFTAP, WIFI_PROV_SCHEME_HANDLER_NONE, WIFI_PROV_SECURITY_1, pop, service_name);
#endif

}


void loop()
{
  if (interruptCounter > 0 && wifi_connected) {                    
    Serial.println("Sending Sensor's Data");
    portENTER_CRITICAL(&timerMux);
    interruptCounter--;
    portEXIT_CRITICAL(&timerMux);
    totalInterruptCounter++;
    Send_UltraSensor();
  }

  //-----------------------------------------------------------  Logic to Reset RainMaker

  // Read GPIO0 (external button to reset device
  if (digitalRead(gpio_reset) == LOW) { //Push button pressed
    Serial.printf("Reset Button Pressed!\n");
    // Key debounce handling
    delay(100);
    int startTime = millis();
    while (digitalRead(gpio_reset) == LOW) delay(50);
    int endTime = millis();

    if ((endTime - startTime) > 10000) {
      // If key pressed for more than 10secs, reset all
      Serial.printf("Reset to factory.\n");
      wifi_connected = 0;
      RMakerFactoryReset(2);
    } else if ((endTime - startTime) > 3000) {
      Serial.printf("Reset Wi-Fi.\n");
      wifi_connected = 0;
      // If key pressed for more than 3secs, but less than 10, reset Wi-Fi
      RMakerWiFiReset(2);
    }
  }
  
  delay(100);
}


void Send_UltraSensor()
{
  int TankDepth = 31;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  long duration = pulseIn(echoPin, HIGH);
  float distance = duration * 0.034 / 2; 
  float WaterLevel = TankDepth - distance;
  Serial.print(WaterLevel);
  ultrasonic.updateAndReportParam("Temperature", distance);
  delay(1000);
}


sanketwadekar
Posts: 32
Joined: Tue Jul 26, 2022 10:08 am

Re: Plz help! Sensor cannot update its value (EVEN with a Timer setup)

Postby sanketwadekar » Tue Mar 21, 2023 10:19 am

1. Do you repeatedly get this error "E (6309) esp_rmaker_param: Current time not yet available. Cannot report time series data." ?
2. Please check the value of `gpio_reset` in the sketch. On my esp32c3 devkit board, the boot button is used through pin 9. You might run in an infinite loop if the value set is wrong.
3. Sending messages too frequently will exhaust your MQTT messaging limit and your device cannot report data to the cloud.
4. You can also use `isConnected()` API from the WiFi library to check your WiFi connection.

CUHenri
Posts: 4
Joined: Sun Mar 19, 2023 5:08 am

Re: Plz help! Sensor cannot update its value (EVEN with a Timer setup)

Postby CUHenri » Sat Mar 25, 2023 8:27 am

Sorry for my late reply. Yes, I found out that if I change the gpio to another value, everything works fine. The problem has been solved. Thank you so much!!!

Who is online

Users browsing this forum: No registered users and 10 guests