Using digital motion sensor with ESP8266

This is what appears:

Is my code wrong?

#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_PRINT Serial
#define CAYENNE_DEBUG

// WiFi network info.
char ssid = “–”;
char wifiPassword = “–”;

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “–”;
char password = “verystrong”;
char clientID = “hackersnotwelcome”;

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

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;
}
}

everything is correct, not sure what is the issue here. @adam can you find anything here?

Looks fine to me. That garbage is from the ESP booting. If you set the baud to 115200 you should see some actual info. No clue why it’s not printing debug messages though. Does it show up as online on the dashboard?

With the change of baud:

The temporary widget appears in the cayenne dashboard, and it gives out 0/1 values, but not correct ones. When I put the widget with a icon, it literally “blinks”, but it should be 1 when the motion sensor detects movement and 0 when it does not detect movement.
The motion sensor is working, because when I connect it directly to raspberry GPIOs instead of the ESP8266, it works perfectly in the dashboard.

Very odd. Try the digital read example in the Arduino IDE. File>Examples>Basic>DigitalReadSerial. See if you get any output from that.

This is the result:

try this code:

/*
This example shows how to connect to Cayenne using an ESP8266 and send/receive sample data.
The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
Steps:
1. If you have not already installed the ESP8266 Board Package install it using the instructions here: https://github.com/esp8266/Arduino#installing-with-boards-manager.
2. Select your ESP8266 board from the Tools menu.
3. Set the Cayenne authentication info to match the authentication info from the Dashboard.
4. Set the network name and password.
5. Compile and upload the sketch.
6. A temporary widget will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.
*/

#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>

// WiFi network info.
char ssid[] = "ssid";
char wifiPassword[] = "wifiPassword";

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


void setup() {
	Serial.begin(9600);
	Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}

void loop() {
	Cayenne.loop();
}

// Default function for sending sensor data at intervals to Cayenne.
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
CAYENNE_OUT_DEFAULT()
{
	// Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
	Cayenne.virtualWrite(0, millis());
	// Some examples of other functions you can use to send data.
	//Cayenne.celsiusWrite(1, 22.0);
	//Cayenne.luxWrite(2, 700);
	//Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
}

// Default function for processing actuator commands from the Cayenne Dashboard.
// You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
CAYENNE_IN_DEFAULT()
{
	CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asString());
	//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
}

I got this information from serial monitor when I press reset button of ESP8266 and use 74880 baud:

ets Jan 8 2013,rst cause:2, boot mode:(3,6)

load 0x4010f000, len 1392, room 16
tail 0
chksum 0xd0
csum 0xd0
v3d128e5c
~ld

did you try the above code?

Yes, it uploaded sucessfuly.
The below appears in serial monitor when I press reset of ESP8266:

20:27:49.912 →
20:27:49.912 → ets Jan 8 2013,rst cause:2, boot mode:(3,6)
20:27:49.912 →
20:27:49.912 → load 0x4010f000, len 1392, room 16
20:27:49.948 → tail 0
20:27:49.948 → chksum 0xd0
20:27:49.948 → csum 0xd0
20:27:49.948 → v3d128e5c
20:27:49.948 → ~ld
20:27:50.524 → ⸮⸮⸮⸮⸮⸮⸮

can you disconnect the motion sensor if it is connected and try again.

I solved the problem.

The problem was that I did not connect the ground pin of my ESP8266 to the ground pin of the raspberry pi (that was feeding/powering the motion sensor module).
I had to shunt the ground pin of the raspberry pi with the ground pin of the ESP8266, now I’m receiving correct data on the cayenne dashboard and correspondent widget.

Thank you

2 Likes