Switch sensor on and off using switch present in dashboard


#1

I want to set up a toggle switch in dashboard in order to switch my PIR sensor ON and OFF. I mean when the PIR sensor is switched ON data should be sent to the dashboard in real time while as when the PIR sensor is switched OFF via toggle button in dashboard, it shouldn’t send any data because i dont need it on regular basis but i can’t find a way out.


#2

add a button and use a if condition for the PIR sensor.

if(x == 1)
{
//PIR sensor reading.
}
CAYENNE_IN(1)
{
	int  x = getValue.asInt() ;

}

#3

"expected unqualified-id before ‘if’ “.While compiling code this appears in the arduino IDE. I tried to keep it in void loop() which gives " ‘x’ was not declared in this scope” error.


#4

int x; before void setup() and change

CAYENNE_IN(1)
{
	x = getValue.asInt() ;
}

place below code in void loop()

if(x == 1)
{
//PIR sensor reading.
}

#7

I tried, but unfortunately not working… Here is my code… please check it and make some necessary alterations…

//#define CAYENNE_DEBUG       // Uncomment to show debug messages
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneMQTTESP8266Shield.h>
#include <DHT.h>
#include <SimpleTimer.h>
#include <Servo.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[] = "";

// Set ESP8266 Serial object. In this example we use the Serial1 hardware serial which is available on boards like the Arduino Mega.
#define EspSerial Serial1

ESP8266 wifi(&EspSerial);

#define PIR_SENSOR_PIN 5 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define LED_PIN 11
#define BUZZER_PIN 12
#define DHT_PIN  7
#define SMOKE_SENSOR_PIN A1
#define SOIL_SENSOR_PIN A3
#define RELAY_ACTUATOR_PIN 34
#define SERVO_ACTUATOR_PIN 4


#define PIR_VIRTUAL_CHANNEL V1
#define HUMIDITY_VIRTUAL_CHANNEL V2
#define TEMPERATURE_VIRTUAL_CHANNEL V3
#define SMOKE_VIRTUAL_CHANNEL V4
#define SOIL_VIRTUAL_CHANNEL V5
#define RELAY_VIRTUAL_CHANNEL V6
#define SERVO_VIRTUAL_CHANNEL 7

#define DHTTYPE DHT11
DHT dht(DHT_PIN, DHTTYPE);
Servo s1;


SimpleTimer timer;
int pos = 0;
int val;



void setup()
{
  pinMode(LED_PIN, OUTPUT);      // declare LED as output
  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(SMOKE_SENSOR_PIN, INPUT);
  pinMode(RELAY_ACTUATOR_PIN, OUTPUT);
  s1.attach(SERVO_ACTUATOR_PIN);
 
servoGo();
  Serial.begin(9600);
  delay(10);
  EspSerial.begin(115200);
  delay(10);

  Cayenne.begin(username, password, clientID, wifi, ssid, wifiPassword);
  timer.setInterval(5000L, transmitData);

  dht.begin();
}

void loop()
{

  if (digitalRead(PIR_SENSOR_PIN) == HIGH) {            // check if the input is HIGH
    digitalWrite(LED_PIN, HIGH);  // turn LED ON
    tone(BUZZER_PIN, 4000, 60000 );
    delay(10);
      }
      else
      {
     digitalWrite(LED_PIN, LOW);  // turn LED ON
     tone(BUZZER_PIN, 0, 4000);
     delay(10);
    }



    val = analogRead(SMOKE_SENSOR_PIN);// reads the value of the potentiometer (value between 0 and 1023)
    if (val >= 100){
  val = map(val, 100, 1023, 170, 0);     // scale it to use it with the servo (value between 0 and 180)
  s1.write(val);                  // sets the servo position according to the scaled value
  delay(15);
   tone(BUZZER_PIN, 4000, 30000 );
  
    }    

 Cayenne.loop();
  pirSensor();
  timer.run();

}

void servoGo(){
  val = analogRead(SMOKE_SENSOR_PIN);
   val = map(val, 100, 1023, 0, 170);     // scale it to use it with the servo (value between 0 and 180)
  s1.write(val);                  // sets the servo position according to the scaled value
  delay(15);
  }
CAYENNE_OUT_DEFAULT()
{
  Cayenne.virtualWrite(0, millis());
}

CAYENNE_IN_DEFAULT()
{
  CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asString());
}



int previousState = -1;
int currentState = -1;
unsigned long previousMillis = 0;
void pirSensor()
{
  unsigned long currentMillis = millis();
  // Check sensor data every 250 milliseconds
  

  if (currentMillis - previousMillis >= 10) {
    // Check the sensor state and send data when it changes.
    currentState = digitalRead(PIR_SENSOR_PIN);
    if (currentState != previousState) {
      Cayenne.virtualWrite(V1, currentState, "motion", "d");
      previousState = currentState;
        previousMillis = currentMillis;
  }
}
}

void transmitData(){
  
  float t = dht.readTemperature();
  Cayenne.celsiusWrite(V2, t); //virtual pin

  float h = dht.readHumidity();
  Cayenne.virtualWrite(V3, h, "rel_hum", "p"); //virtual pin

  Cayenne.virtualWrite(V4, analogRead(SMOKE_SENSOR_PIN), "analog_sensor");
  
  Cayenne.virtualWrite(V5, analogRead(SOIL_SENSOR_PIN), "analog_sensor");
}

CAYENNE_IN(V6)
{
  Cayenne.syncAll();
  // Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
  if (getValue.asInt() == 0) {
    digitalWrite(RELAY_ACTUATOR_PIN, HIGH);
    delay(1);
  }
  else {
    digitalWrite(RELAY_ACTUATOR_PIN, LOW);
    delay(1);
  }
}

CAYENNE_IN(V7)
{
  val = analogRead(SMOKE_SENSOR_PIN);// reads the value of the potentiometer (value between 0 and 1023)
  if(getValue.asInt() == 1){
  
    
  val = map(val, 0, 1023, 0, 170);     // scale it to use it with the servo (value between 0 and 180)
  s1.write(val);                  // sets the servo position according to the scaled value
  delay(15);
  }
  else{
    val = map(val, 0, 1023, 170, 170);     // scale it to use it with the servo (value between 0 and 180)
  s1.write(val);                  // sets the servo position according to the scaled value
  delay(15);

    
    }
  }

#8

i cant find above code in your code.


#10

Thank you for replying quickly… I apologize as i posted previous code…For making sure that the toggle switch on the cayenne dashboard is working , instead of sensor I tried the code on LED but LED isn’t working which means that the variable x isnt taking the value that is assigned to it in

CAYENNE_IN(1)
{
x = getValue.asInt() ;
}

Here is my code:

//#define CAYENNE_DEBUG // Uncomment to show debug messages
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <CayenneMQTTESP8266Shield.h>
#include <DHT.h>
#include <SimpleTimer.h>
#include <Servo.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[] = “”;

// Set ESP8266 Serial object. In this example we use the Serial1 hardware serial which is available on boards like the Arduino Mega.
#define EspSerial Serial1

ESP8266 wifi(&EspSerial);

#define PIR_SENSOR_PIN 5 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define LED_PIN 11
#define BUZZER_PIN 12
#define DHT_PIN 7
#define SMOKE_SENSOR_PIN A1
#define SOIL_SENSOR_PIN A3
#define RELAY_ACTUATOR_PIN 34
#define SERVO_ACTUATOR_PIN 4

#define PIR_VIRTUAL_CHANNEL V1
#define HUMIDITY_VIRTUAL_CHANNEL V2
#define TEMPERATURE_VIRTUAL_CHANNEL V3
#define SMOKE_VIRTUAL_CHANNEL V4
#define SOIL_VIRTUAL_CHANNEL V5
#define RELAY_VIRTUAL_CHANNEL V6
#define SERVO_VIRTUAL_CHANNEL 7

#define DHTTYPE DHT11
DHT dht(DHT_PIN, DHTTYPE);
Servo s1;

SimpleTimer timer;
int pos = 0;
int val;
int x;
CAYENNE_IN(V8)
{
x = getValue.asInt() ;
}

void setup()
{
pinMode(LED_PIN, OUTPUT); // declare LED as output
pinMode(BUZZER_PIN, OUTPUT);
pinMode(SMOKE_SENSOR_PIN, INPUT);
pinMode(RELAY_ACTUATOR_PIN, OUTPUT);
s1.attach(SERVO_ACTUATOR_PIN);

servoGo();
Serial.begin(9600);
delay(10);
EspSerial.begin(115200);
delay(10);

Cayenne.begin(username, password, clientID, wifi, ssid, wifiPassword);
timer.setInterval(5000L, transmitData);

dht.begin();
}

void loop()
{

if(x ==1 ){
digitalWrite(LED_PIN, HIGH);
}

if (digitalRead(PIR_SENSOR_PIN) == HIGH) { // check if the input is HIGH
digitalWrite(LED_PIN, HIGH); // turn LED ON
tone(BUZZER_PIN, 4000, 60000 );
delay(10);
}
else
{
digitalWrite(LED_PIN, LOW); // turn LED ON
tone(BUZZER_PIN, 0, 4000);
delay(10);
}

val = analogRead(SMOKE_SENSOR_PIN);// reads the value of the potentiometer (value between 0 and 1023)
if (val >= 100){
val = map(val, 100, 1023, 170, 0); // scale it to use it with the servo (value between 0 and 180)
s1.write(val); // sets the servo position according to the scaled value
delay(15);
tone(BUZZER_PIN, 4000, 30000 );

}    
Cayenne.loop();
pirSensor();
timer.run();

}

void servoGo(){
val = analogRead(SMOKE_SENSOR_PIN);
val = map(val, 100, 1023, 0, 170); // scale it to use it with the servo (value between 0 and 180)
s1.write(val); // sets the servo position according to the scaled value
delay(15);
}
CAYENNE_OUT_DEFAULT()
{
Cayenne.virtualWrite(0, millis());
}

CAYENNE_IN_DEFAULT()
{
CAYENNE_LOG(“Channel %u, value %s”, request.channel, getValue.asString());
}

int previousState = -1;
int currentState = -1;
unsigned long previousMillis = 0;
void pirSensor()
{
unsigned long currentMillis = millis();
// Check sensor data every 250 milliseconds

if (currentMillis - previousMillis >= 10) {
// Check the sensor state and send data when it changes.
currentState = digitalRead(PIR_SENSOR_PIN);
if (currentState != previousState) {
Cayenne.virtualWrite(V1, currentState, “motion”, “d”);
previousState = currentState;
previousMillis = currentMillis;
}
}
}

void transmitData(){

float t = dht.readTemperature();
Cayenne.celsiusWrite(V2, t); //virtual pin

float h = dht.readHumidity();
Cayenne.virtualWrite(V3, h, “rel_hum”, “p”); //virtual pin

Cayenne.virtualWrite(V4, analogRead(SMOKE_SENSOR_PIN), “analog_sensor”);

Cayenne.virtualWrite(V5, analogRead(SOIL_SENSOR_PIN), “analog_sensor”);
}

CAYENNE_IN(V6)
{
Cayenne.syncAll();
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(RELAY_ACTUATOR_PIN, HIGH);
delay(1);
}
else {
digitalWrite(RELAY_ACTUATOR_PIN, LOW);
delay(1);
}
}

CAYENNE_IN(V7)
{
val = analogRead(SMOKE_SENSOR_PIN);// reads the value of the potentiometer (value between 0 and 1023)
if(getValue.asInt() == 1){

val = map(val, 0, 1023, 0, 170); // scale it to use it with the servo (value between 0 and 180)
s1.write(val); // sets the servo position according to the scaled value
delay(15);
}
else{
val = map(val, 0, 1023, 170, 170); // scale it to use it with the servo (value between 0 and 180)
s1.write(val); // sets the servo position according to the scaled value
delay(15);

}
}

#11

your code is bit confusing.

does this work for you??


#12

yes, flawlessly…


#13

I really need to sort this problem out but I cant do it on my own… I have to submit my project next week.


#14

so your entire code is working? your entire code is confusing .