Hello Dear Master community of Arduino.
I need your help to create on cayenne a system based on Arduino one +ethernet W5100 which is turning a led on when there is no light or another settings could be turning different LEDs on depending on the light or a third setting might be changing colour of rgb led. Those 3 options are just ideas, my concept is when lux is down do something with a led.
I found a guide how to this remotely but not on cayenne. Do you have any idea?
Thank you for your support in advance
Best regards
Gesuelemi
I think I am clarifying my project at the moment. Basically what I want to reach is:
turn a led on if photoresistance value is > of a constant value.
this is the code which is loading the photo resistance data but I think I made something wrong with the if else due to the fact that I have still to click on the led switcher
here is the code:
/*
Cayenne Photoresistor Example
This sketch shows how to send Photoresistor Sensor data to the Cayenne Dashboard.
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. Attach a photoresistor to an analog pin on your Arduino.
Make sure to use an analog pin, not a digital pin.
Schematic:
[Ground] -- [10k-resistor] -- | -- [Photoresistor] -- [5V]
|
Analog Pin
2. Set the SENSOR_PIN value below to the pin number you used when connecting the sensor.
3. Set the VIRTUAL_CHANNEL value below to a free virtual channel (or the virtual channel of a Photoresistor Sensor widget you have added) in the Dashboard.
4. Set the Cayenne authentication info to match the authentication info from the Dashboard.
5. Compile and upload this sketch.
6. Once the Arduino connects to the Dashboard it should automatically create a temporary display widget (or update the Photoresistor Sensor widget you have added) with data.
To make a temporary widget permanent click the plus sign on the widget.
*/
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <CayenneMQTTEthernet.h>
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "*****************************************";
char password[] = "*****************************************";
char clientID[] = "*****************************************";
#define SENSOR_PIN A3
#define VIRTUAL_CHANNEL_4 8
#define VIRTUAL_CHANNEL_3 3
#define ACTUATOR_PIN 8 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
int SENSOR_VALUE;
void setup()
{
Serial.begin(9600);
pinMode(ACTUATOR_PIN, OUTPUT);
Cayenne.begin(username, password, clientID);
}
void loop()
{
Cayenne.loop();
}
// This function is called at intervals to send sensor data to Cayenne.
CAYENNE_OUT(VIRTUAL_CHANNEL_4)
{
Cayenne.virtualWrite(VIRTUAL_CHANNEL_4, analogRead(SENSOR_PIN), "analog_sensor", "null");
}
// This function is called when data is sent from Cayenne.
CAYENNE_IN(VIRTUAL_CHANNEL_3)
{
int value = getValue.asInt();
CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_3, ACTUATOR_PIN, value);
// Write the value received to the digital pin.
digitalWrite(ACTUATOR_PIN, value);
SENSOR_VALUE = analogRead(SENSOR_PIN);
if (SENSOR_VALUE >599)
{
digitalWrite(8,HIGH);
}
else
{
digitalWrite(8,LOW);
delay (100);
}
}
Any help is really appreciated
CAYENNE_IN(VIRTUAL_CHANNEL_3)
....
}
This function only runs when data is received from cayenne like a button to turn ON led
you can move the if check into the main loop.
Hello shramik_salgaonkar,
thank you very much for finding the time to reply me.
I tried to move to the main loop which I think is void loop() but I think I made mistakes because I got messages like:
expected initializer before ‘int’
‘getValue’ was not declared in this scope
Could you please tell how to do it? I can easily turn on the led but when I want to set an if which uses the photoresistor like if luminosity is < 500 turn led on, I am lost
Looking at my dashboard I have an issue with the photoresistor as well, it is not automatically recognised but if I use a general analogical input, I get some data.
Should the channel be the same as the pin in the arduino board? Like write A3 instead of 17 for the analogical input?
Sorry for bothering you
Hi guys , finally it works the photoresistor turns the led on when there is no luminosity.
/*
Cayenne Generic Analog Input Example
This sketch shows how to send Generic Analog Input Sensor data to the Cayenne Dashboard.
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. Attach an analog input device (e.g. a potentiometer) to an analog pin on your Arduino. Make sure to use an analog pin, not a digital pin.
2. Set the SENSOR_PIN value below to the pin number you used when connecting the sensor.
3. Set the VIRTUAL_CHANNEL value below to a free virtual channel (or the virtual channel of an Generic Analog Input Sensor widget you have added) in the Dashboard.
4. Set the Cayenne authentication info to match the authentication info from the Dashboard.
5. Compile and upload this sketch.
6. Once the Arduino connects to the Dashboard it should automatically create a temporary display widget (or update the Generic Analog Input Sensor widget you have added) with data.
To make a temporary widget permanent click the plus sign on the widget.
*/
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <CayenneMQTTEthernet.h>
#define PhotoResistor A5
#define REDLED 9
#define YELLOWLED 11
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "";
char password[] = "";
char clientID[] = "";
#define VIRTUAL_CHANNEL 30
#define VIRTUAL_CHANNEL 20
void setup()
{
Serial.begin(9600);
Cayenne.begin(username, password, clientID);
pinMode(PhotoResistor, INPUT);
pinMode(REDLED, OUTPUT);
pinMode(YELLOWLED, OUTPUT);
}
void loop()
{
Cayenne.loop();
Serial.println(analogRead(PhotoResistor));
if (analogRead(PhotoResistor)<250)
digitalWrite(REDLED,HIGH);
else
digitalWrite(REDLED,LOW);
}
// This function is called at intervals to send sensor data to Cayenne.
CAYENNE_OUT(VIRTUAL_CHANNEL)
{
Cayenne.virtualWrite(VIRTUAL_CHANNEL, analogRead(PhotoResistor), "analog_sensor", "null");
CAYENNE_LOG("Send data for Virtual Channel 0");
// This command writes the device's uptime in seconds to the Virtual Channel.
Cayenne.virtualWrite(0, millis() / 1000);
}
CAYENNE_IN(VIRTUAL_CHANNEL)
{
int value = getValue.asInt();
CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL, YELLOWLED, value);
// Write the value received to the digital pin.
digitalWrite(YELLOWLED, value);
}
// This function is called at intervals to send data to Cayenne and keep the device online.
// Will create a temporary green widget on Channel 0, make it permanent by clicking on '+'.
I am struggling now on other two topics:
I wanted to add an additional led to be switched on/off but is always turned on. I think since there are two virtual channels now I should do something, maybe virtual channel_01 and virtual channel_2
I wanted to trigger when the led of the photoresistor is on but seems the trigger is not available (see pic below), why?