ESP8266 + Soil moisture sensor + Relay

Hello,

I am working on my new project but i have a problem. I am trying to control a rely based on soil moisture sensor.

Soil moisture value is shown on dashboard but i cant add a relay. Can you help me to solve this problem?

@shramik_salgaonkar can you help me ?

CODE OF THE PROJECT


#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial

#include <CayenneMQTTESP8266.h>
// WiFi network info.
char ssid[] = "*******";
char wifiPassword[] = "********";


// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "****************";
char password[] = "**************";
char clientID[] = "*****************";

#define VIRTUAL_CHANNEL 1
#define ACTUATOR_PIN 5

int sense_Pin = 0; // sensor input at Analog pin A0

int value = 0;
void setup() {
  Serial.begin(9600);
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);
 pinMode(ACTUATOR_PIN, OUTPUT);
}
void loop() {
  Cayenne.loop();
  Serial.print("MOISTURE LEVEL : ");
  value = analogRead(sense_Pin);
  value = value / 10;
  Serial.println(value);

 if (value <= 40) {
  digitalWrite(ACTUATOR_PIN, HIGH); 
}
else if (value >= 60.0) {
  digitalWrite(ACTUATOR_PIN, LOW); 
}

}
CAYENNE_OUT_DEFAULT()
{
  Cayenne.virtualWrite(1, value, "analog_sensor", "null");
  Cayenne.virtualWrite(0, millis());
}

CAYENNE_IN(VIRTUAL_CHANNEL)
{
  int value = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL, ACTUATOR_PIN, value);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN, value);
}

The code seems ok to me. What is it doing or not doing? Any errors in the terminal?

I have only one problem, i cant control the relay.

Connections
Soil Moisture sensor…ESP8266 ( A0 ), its working.
Relay…ESP8266 (D5), its not working.

Does it turn on/off with your analog pin reading?

Yes, i can control the relay based on sensor data, but i cant control with Cayenne.

Do you get logs in the serial monitor when you press the button in the UI?

In Cayenne always is shown a different value for the relay. You can see the screenshot below.

i meant logs in the Arduino serial monitor

@shramik_salgaonkar

With this code i can control the relay based on sensor data. But i cant control it using Cayenne.

In Arduino serial monitor is shown this.

keep serial monitor open and turn ON/OFF the button on cayenne dashboard. You should see some output in the serial monitor

@shramik_salgaonkar

When i turn ON/OFF the button on Cayenne dashboard, this is shown in the serial monitor.

@shramik_salgaonkar i am waiting for your help.

You’re using channel 0 for the relay, but in your code it’s connected to pin 5. Change the channel to 5 and it should work.

@adam I have changed the channel but it is still not working.

In the code the channel used in 1
So you need to add button with channel 1 on the cayenne dashboard

1 Like

@shramik_salgaonkar
Yes, i have added the button with channel 1 and its working.

Now i can change the relay state from Cayenne dashboard, but t have one more problem.
For example, when i want to turn off the relay, i touch the button on cayenne dashboard and its working.
But the relay turn on again because its programed based on sensor data.

What do i need to do, if i touch the button to change the relay state and to neglect the sensor data.

you can have this code inside an if condition with caynnen in value.