Hi
I wanna ask one thing if someone could help me. How to send and receive data? I want two widgets. Slider and Gauge. If I move the slider, the change should be reflected in the gauge. How can I do that without hardware just through code? I am using esp8266 and mqtt to connect with Cayenne.Thanks
use CAYENNE_IN()
to read value of the slider and store in “x” then use Cayenne.virtualWrite(0, x); to send the value to cayenne. change the setting to gauge of the widget.
code look good beside including the cayenne library. add #define CAYENNE_DEBUG and check the serial monitor when you change the slider.
i think the gauge on your dashboard, is added by add new > devices/widget > custom widget > gauge.
first delete both the widget pressure gauge and channel 2.
next when you turn upload the above code, a temporary channel 2 widget will populate the dashboard. add to the dashboard “+”.
click on setting (gear icon) of that widget and change choose widget to gauge.
replace
to
virtualWrite(VIRTUAL_CHANNEL, d, "analog_sensor", "null")
It is working now. Thank you so much, dear.
Can you please tell why you added “analog_sensor and null”? What’s the logic?
have a look at this Data types for Cayenne MQTT API
yes i can help but i dint get what you are trying do.
Like I want a pressure or gauge control model. Are you getting my point?
add another button with channel 3.
int x;
CAYENNE_IN(3)
{
x = getValue.asInt();
}
and change this:
CAYENNE_OUT(VIRTUAL_CHANNEL)
{
if ( x == 1)
{
virtualWrite(VIRTUAL_CHANNEL, d, "analog_sensor", "null")
Serial.println(d);
}
}
why do you want the above?
And there is too much delay in sending and receiving data, like if move the slider, gauge moves after 15-20 sec. Why there is a delay? How can I speed up that?
I want this because I should look like real model. As in real a gauge looks like. As it is not stable, keeps on moving.
read this Sending MQTT messages within rate limits
Where to add these two parts of the code?
this is some dirty thing you can give a try but not sure whether it will work as you want and it will change after every 15 sec.
int y;
CAYENNE_OUT(VIRTUAL_CHANNEL)
{
if ( x == 1)
{
virtualWrite(VIRTUAL_CHANNEL, d, "analog_sensor", "null")
Serial.println(d);
}
else
{
y += 10;
virtualWrite(VIRTUAL_CHANNEL, y, "analog_sensor", "null");
if ( y == 30)
{
virtualWrite(VIRTUAL_CHANNEL, y, "analog_sensor", "null");
y = 0;
}
}
}
add after void loop.
change:
to
CAYENNE_OUT(VIRTUAL_CHANNEL)
{
if ( x == 1)
{
virtualWrite(VIRTUAL_CHANNEL, d, "analog_sensor", "null")
Serial.println(d);
}
}
And how can we control a slider, like if want to send a value to slider everytime the esp turns on, say a preset value 50, how can I do that. Coz whenever I restart Cayenne Slider goes to 0, I want to set it to some value.
#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
int x;
int y;
// WiFi network info.
char ssid[] = “WiFi Name”;
char wifiPassword[] = “Password”;
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = “username”;
char password[] = “password”;
char clientID[] = “ID”;
int i;
double d;
#define VIRTUAL_CHANNEL 2
void setup() {
Serial.begin(9600);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
Serial.println(“Connected”);
}
void loop() {
Cayenne.loop();
}
// This function will be called every time a Dashboard widget writes a value to Virtual Channel 1.
CAYENNE_IN(1)
{
CAYENNE_LOG(“Got a value: %s”, getValue.asStr());
// You can also use:
// i = getValue.asInt();
d = getValue.asDouble();
Serial.println(d);
}
CAYENNE_IN(3)
{
x = getValue.asInt();
}
CAYENNE_OUT(VIRTUAL_CHANNEL)
{
if ( x == 1)
{
virtualWrite(VIRTUAL_CHANNEL, d, "analog_sensor", "null")
Serial.println(d);
}
else
{
y += 10;
virtualWrite(VIRTUAL_CHANNEL, y, "analog_sensor", "null");
if ( y == 30)
{
virtualWrite(VIRTUAL_CHANNEL, y, "analog_sensor", "null");
y = 0;
}
}
}
not possible.
when it restarts after 10 sec it should change it the above code i gave.