Using digital motion sensor with ESP8266

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 5

void 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!

what do you get in your serial monitor when you add #define CAYENNE_DEBUG

Does the vm314 work for sure on 3 volts?

VM314 (Velleman) has 5V DC power input.

I inserted that line of code, in the code I upload to the ESP8266 trough the arduino IDE, but nothing happens

what do you mean notting happens? your serial monitor is blank?

I’m sorry, I don’t know what the serial monitor is. I will try to learn it

did you add this?

This is the code I upload to the ESP8266, as you can see I add that line:

#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 = “hotspot_raspberry”;
char wifiPassword = “12345678”;

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “af7ff680-f0fa-11e9-ba7c-716e7f5ba423”;
char password = “02659680d3bf6c2c0d9bd92a713f1bd1159ec0db”;
char clientID = “1b886fc0-483e-11ea-a38a-d57172a4b4d4”;

#define SENSOR_PIN 10 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define VIRTUAL_CHANNEL 8

void 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 >= 1000) {
// Check the sensor state and send data when it changes.
currentState = digitalRead(SENSOR_PIN);
if (currentState != previousState) {
Cayenne.virtualWrite(VIRTUAL_CHANNEL, currentState, “digital_sensor”, “d”);
previousState = currentState;
}
previousMillis = currentMillis;
}
}

remove // from the beginning.

I wrote only more that line of code and uploaded the code again:

can you press the reset button on the esp8266 with the serial monitor open.

This is what happened:

do you get any output while following this instruction?

I have my ESP8266 connected to my laptop.
When I press the reset button, this text appears in serial monitor in the arduino ide: `⸮⸮⸮⸮⸮

follow the above tutorial and see if it works.

The tutorial has nothing about the cayenne platform or what code I should use to this situation.
What I’m I supposed to do?

i am not referring to cayenne. just see if you get anything in the serial monitor.