I have a PIR sensor working on this board with Cayenne but for some reason I just can’t get the state of my push button to be read. I would like for someone to have a look at the code below and let me know if this is a mistake in the code. If not I will keep on checking hardware.
Thanks!
#define CAYENNE_DEBUG // Uncomment to show debug messages
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include "CayenneDefines.h"
#include "BlynkSimpleEsp8266.h"
#include "CayenneWiFiClient.h"
// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "*********";
// Your network name and password.
char ssid[] = "******";
char password[] = "********";
// Virtual Pin of the Digital Motion Sensor widget.
#define VIRTUAL_PIN V1
#define VIRTUAL_PIN1 V2
// Digital pin the motion sensor is connected to. Do not use digital pins 0 or 1 since those conflict with the use of Serial.
const int motionSensorPin = 4;
const int frontDoor = 3;
void setup()
{
Serial.begin(9600);
Cayenne.begin(token, ssid, password);
//pinMode(frontDoor, INPUT);
//digitalWrite(frontDoor, LOW);
}
void loop()
{
Cayenne.run();
checkSensor();
}
int previousState = -1;
int currentState = -1;
int fdState = -1;
unsigned long previousMillis = 0;
void checkSensor()
{
unsigned long currentMillis = millis();
// Check sensor data every 250 milliseconds
if (currentMillis - previousMillis >= 250) {
// Check the sensor state and send data when it changes.
currentState = digitalRead(motionSensorPin);
fdState = digitalRead(frontDoor);
//Serial.println("PIR State: ");
//Serial.print(currentState);
if (currentState != previousState) {
Cayenne.virtualWrite(VIRTUAL_PIN1, fdState);
Cayenne.virtualWrite(VIRTUAL_PIN, currentState);
//Serial.println("PIR State: ");
//Serial.print(currentState);
previousState = currentState;
}
previousMillis = currentMillis;
}
}