Resetting the value of the sensors in the dashboard


#1

Hi Cayenne. Is there a way for me to reset the value of the sensor (to 0) in the dashboard for an analog sensor . I tried resetting the programmer and refreshing the browser but the value does not change. Thanks in advance.


#2

you can send a 0 value from your code using Cayenne.virtualWrite(VIRTUAL_CHANNEL, 0, "analog_sensor", "null");, so that the analog sensor widget shows a 0on the dashboard.


#3

But this value of 0" is only during startup right? That means once the analog sensors get to work, the dashboard will start to show non-zero values? Thanks again


#4

if you want it to show 0 during the startup then you need to add it in the void setup().
i did not understand what are you to do, can you provide more details about your project?


#5

Whenever I turn on my programmer, there are already values, in the water level sensor and the moisture level sensor (probably the value taken from the last time the device was on). I want this value to be zero every time I first turn it on. This should be 0 since my sensors haven’t started to measure values yet (it’s not yet dipped in the water).


#6

okay. add Cayenne.virtualWrite(VIRTUAL_CHANNEL, 0, "analog_sensor", "null"); in void setup() and it will make the widget value 0.


#7

ok thanks shramik. i will try


#8

shramik just one more thing- the “0” in the code you gave corresponds to the pin number where the analog sensor is connected right? Or is it really a constant “0”. Thanks


#9

0 is the value which you want to send to the analog sensor widget.


#10

ok thanks


#11

Hi Shramik. In the code that you gave me

Cayenne.virtualWrite(VIRTUAL_CHANNEL, 0, “analog_sensor”, “null”)

the “analog sensor”, what does this corresspond to? is it the name of the sensor pin i declared in the #define function? How about “null”? do i need to replace this with something? or is it simply just null?

Here is my code:

/*
Cayenne Button Widget Example

This sketch shows how to set up a Button Widget with Cayenne.

The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.

Steps:

  1. In the Cayenne Dashboard add a new Button Custom Widget.
  2. Select a virtual channel number for the widget.
  3. Set the VIRTUAL_CHANNEL value below to virtual channel you selected.
  4. Attach an output actuator (e.g. an LED) to a digital pin on your Arduino.
  5. Set the ACTUATOR_PIN value below to the pin number you used when connecting your output actuator.
  6. Set the Cayenne authentication info to match the authentication info from the Dashboard.
  7. Compile and upload this sketch.
  8. Once the Arduino connects to the Dashboard you can use the widget button to send digital values.
    */

#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <CayenneTemperature.h>
#include <CayenneMQTTEthernet.h>
#include <Servo.h>
Servo servo;

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “0677f7e0-aa84-11e7-bd7e-3193fab997a8”;
char password = “d02305d9f8700780fd283e65881d59163f4f2529”;
char clientID = “16dd72c0-e1b2-11e8-a254-e163efaadfe8”;

unsigned long lastMillis = 0;

// Do not use digital pins 0 or 1 since those conflict with the use of Serial.

#define VIRTUAL_CHANNEL_TEMP 1
#define SENSOR_PIN_TEMP 1
#define VIRTUAL_CHANNEL_MOISTURE 0
#define SENSOR_PIN_MOISTURE 0
#define VIRTUAL_CHANNEL_WATERLEVEL 2
#define SENSOR_PIN_WATERLEVEL 2

#define VIRTUAL_CHANNEL_SERVO 6
#define ACTUATOR_PIN_SERVO 6
#define VIRTUAL_CHANNEL_PUMP 4
#define ACTUATOR_PIN_PUMP 4
#define VIRTUAL_CHANNEL_MOTOR 8
#define ACTUATOR_PIN_MOTOR 8
#define VIRTUAL_CHANNEL_ALARM 5
#define ACTUATOR_PIN_ALARM 5

// Analog pin the TMP36 is connected to.
const int tmpPin = 1;

// Voltage to the TMP36. For 3v3 Arduinos use 3.3.
const float voltage = 5.0;

TMP36 tmpSensor(tmpPin, voltage);

Servo myservo;

void setup()
{
Serial.begin(9600);
pinMode(SENSOR_PIN_TEMP, INPUT);
pinMode(SENSOR_PIN_MOISTURE, INPUT);
pinMode(SENSOR_PIN_WATERLEVEL, INPUT);
pinMode(ACTUATOR_PIN_SERVO, OUTPUT);
pinMode(ACTUATOR_PIN_PUMP, OUTPUT);
pinMode(ACTUATOR_PIN_MOTOR, OUTPUT);
pinMode(ACTUATOR_PIN_ALARM, OUTPUT);
Cayenne.virtualWrite(VIRTUAL_CHANNEL_TEMP, 0, “SENSOR_PIN_TEMP”, “null”);
Cayenne.virtualWrite(VIRTUAL_CHANNEL_MOISTURE, 0, “SENSOR_PIN_MOISTURE”, “null”);
Cayenne.virtualWrite(VIRTUAL_CHANNEL_WATERLEVEL, 0, “SENSOR_PIN_WATERLEVEL”, “null”);

Cayenne.begin(username, password, clientID);

myservo.attach(6);
myservo.write(0);
}

void loop()
{
Cayenne.loop();

//Publish data every 10 seconds (10000 milliseconds). Change this value to publish at a different interval.
if(millis() - lastMillis > 1000) {
lastMillis = millis();
//Write data to Cayenne here. This example just sends the current uptime in milliseconds.
Cayenne.virtualWrite(10, lastMillis);
}
}

// This function is for temperature sensor.
CAYENNE_OUT(1)
{
// This command writes the temperature in Celsius to the Virtual Channel.
Cayenne.celsiusWrite(VIRTUAL_CHANNEL_TEMP, tmpSensor.getCelsius());
// To send the temperature in Fahrenheit or Kelvin use the corresponding code below.
//Cayenne.fahrenheitWrite(VIRTUAL_CHANNEL_TEMP, tmpSensor.getFahrenheit());
//Cayenne.kelvinWrite(VIRTUAL_CHANNEL_TEMP, tmpSensor.getKelvin());
}

// This function is for moisture sensor.
CAYENNE_OUT(0)
{
Cayenne.virtualWrite(VIRTUAL_CHANNEL_MOISTURE, analogRead(SENSOR_PIN_MOISTURE));
}

// This function is for water level sensor.
CAYENNE_OUT(2)
{
Cayenne.virtualWrite(VIRTUAL_CHANNEL_WATERLEVEL, analogRead(SENSOR_PIN_WATERLEVEL));
}

// This function is called when data is sent from Cayenne(Servo).
CAYENNE_IN(6)
{
int x= getValue.asInt();
myservo.write(x);
}

// This function is for pump.
CAYENNE_IN(4)
{
int value = getValue.asInt();
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL_PUMP, ACTUATOR_PIN_PUMP, value);
// Write the value received to the digital pin.
digitalWrite(ACTUATOR_PIN_PUMP, value);
}
// This function is for fan.
CAYENNE_IN(8)
{
int value = getValue.asInt();
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL_MOTOR, ACTUATOR_PIN_MOTOR, value);
// Write the value received to the digital pin.
digitalWrite(ACTUATOR_PIN_MOTOR, value);
}
// This function is for alarm.
CAYENNE_IN(5)
{
int value = getValue.asInt();
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL_ALARM, ACTUATOR_PIN_ALARM, value);
// Write the value received to the digital pin.
digitalWrite(ACTUATOR_PIN_ALARM, value);
}

I inserted three codes in void setup (), one for moisture sensor, for water level sensor and tem sensor. The value in the dashboard still has not reset to 0. I am sure there is something wrong with what I did. Can you help me find it. Thanks


#12

have a look at this topic Data types for Cayenne MQTT API