Hello,
I’m using a:
- ESP8266 - CP2102 NodeMCU V3 Lua
- PIR Motion Sensor VM314 (velleman)
When I connect the motion sensor trough the GPIOs on my raspberry and use the Cayenne dashboard, the motion sensor works perfectly, showing I/0 state of the sensor on cayenne dashboard widget.
Now, I’m trying to use my ESP8266 module GPIOs instead of my raspberry to transfer the data, but for some reason the data is corrupted and the information on cayenne dashboard widget is wrong (I/0 state always changing wrong and “blinking” in the widget).
This is the code I uploaded to my ESP8266:
#include <CayenneMQTTESP8266.h>
// This example shows how to connect to Cayenne using an ESP8266 and send/receive sample data.
// Make sure you install the ESP8266 Board Package via the Arduino IDE Board Manager and select the correct ESP8266 board before compiling.//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial// WiFi network info.
char ssid = “helloworld”;
char wifiPassword = “0000”;// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “verysecret”;
char password = “verydifficulttocrack”;
char clientID = “hackersnotwelcome”;#define SENSOR_PIN 4 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define VIRTUAL_CHANNEL 5void setup()
{
pinMode(SENSOR_PIN, INPUT);
Serial.begin(9600);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}void loop()
{
Cayenne.loop();
checkSensor();
}int previousState = -1;
int currentState = -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(SENSOR_PIN);
if (currentState != previousState) {
Cayenne.virtualWrite(5, currentState, “digital_sensor”, “d”);
previousState = currentState;
}
previousMillis = currentMillis;
}
}
Thank you!