Set Iot system with Arduino uno, W5100, Cayenne, 4LEDs and photoresistor

Hello everyone,
I am new in the community, I know just a bit about coding and I would like to get help from you.
My mood was awesome when I managed to reproduce a code I found on internet which is using a photoresistor to turn on upto 4 LEDs.
Based on this code, my next step was to try to use this on cayenne so to to turn it on or off but I do not know how to.
I have tried to add a new sensor (photoresistor) and a new actuator (LED) but without success.

PS. All credits and the connecrion scheme of code goes to siddh-1

Can some give me some help?

The code which is using the photoresistor to turn on leds is the following:

#include <EEPROM.h>
const int ldrsen = A1;
int BlueLed = 13;
int value = 0;
int RedLed = 12;
int YellowLed = 11;
int WhiteLed = 10;
//
void setup() {
  delay(2000);
  Serial.begin(9600);
  pinMode(RedLed, OUTPUT);
  pinMode(YellowLed, OUTPUT);
  pinMode(WhiteLed, OUTPUT);
  pinMode(BlueLed, OUTPUT); 
  pinMode(ldrsen, INPUT);
  
  // Starting the TEST
  
  Serial.println("======Starting the Test======");
  Serial.println("You should see the Led blinking every 2 sec");
  digitalWrite(BlueLed, HIGH);
  delay(200);
  digitalWrite(BlueLed, LOW);
  delay(200);
  digitalWrite(RedLed, HIGH);
  delay(200);
  digitalWrite(RedLed, LOW);
  delay(200);
  digitalWrite(YellowLed, HIGH);
  delay(200);
  digitalWrite(YellowLed, LOW);
  delay(200);
  digitalWrite(WhiteLed, HIGH);
  delay(200);
  digitalWrite(WhiteLed, LOW);
  delay(200);
  Serial.println("Test 1 is completed");
  Serial.println("Now testing the Photoresistor, Make sure it is connected to the Pin A1");
  delay(2000);
  Serial.println("Starting the test now!");   
   
   rerun:
  value = analogRead(ldrsen) /4;
  
  if(value >1){
    Serial.print("The Photoresistor is connected and here are some value: ");
    delay(200);
    Serial.println(value); 
    delay(200);
    Serial.println(value); 
    delay(200); 
    Serial.println(value);    
  }
    else{
      value = analogRead(ldrsen)/4 ;
        if(value<0)
          Serial.println("Hmm the Photoresistor is not connected");
          Serial.println("Retrying in 5s");
          delay(5000);
          goto rerun;
        }
          Serial.println("======END======");
}


 //*************************LOOP*******************


 
void loop() {
  value = analogRead(ldrsen)/4;
  delay(100);
    if(value < 130){
    digitalWrite(BlueLed, HIGH);
  }
  else
  {
    digitalWrite(BlueLed, LOW);
  }
  //Led2
  if(value < 150){
    digitalWrite(RedLed, HIGH);
  }
  else
  {
    digitalWrite(RedLed, LOW);
  }
  if(value < 250){
    digitalWrite(YellowLed, HIGH);
  }
  else
  {
    digitalWrite(YellowLed, LOW);
  }
  if(value < 500){
    digitalWrite(WhiteLed, HIGH);
  }
  else
  {
    digitalWrite(WhiteLed, LOW);
  }
}

While the cayenne standard code for a photoresistor is

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[] = "6xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
char password[] = "78xxxxxxxxxxxxxxxxxxxxxxxx2xxx0";
char clientID[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxafxxxx7";

#define SENSOR_PIN 0
#define VIRTUAL_CHANNEL undefined

void setup()
{
	Serial.begin(9600);
	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)
{
	Cayenne.virtualWrite(VIRTUAL_CHANNEL, analogRead(SENSOR_PIN), "analog_sensor", "null");
}```

My idea was to turn the system on and off via internet (cayenne) and get a message about luminosity (the message can be easily done via trigger in cayenne. The issue is I have no idea how to "turn on" the system via cayenne
PS. I apologize for the link shared in that way, It was my first message in the forum

here is an example code for turning ON/OFF digital output Cayenne-MQTT-Arduino/GenericDigitalOutput.ino at master · myDevicesIoT/Cayenne-MQTT-Arduino · GitHub

I managed to make it work!!!
this is the code

//#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 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 '+'. `

This is the scheme. When I cover the photoresistor the red light turns on. What I wanted to do more is :open_mouth:

  1. Have a trigger when the redled is on → the issue is that trigger is not find the analogic element
  2. I would like to add a swithc on/off for the yellow led but it is always on. Could you please tell me if I made trouble with the virtual channels? I am confused, how can the system know which virtal channel read if both have the same name ( see #define elements from my coe above )

Thank you for your help in advance and have a great weekend

  • List item

you need to use different variable for each virtual channel.

from connection it looks like you have connected it directly to a power supply