I copied the DigitalSensorCode pieces from
Example → Cayenne → Sensors → DigitalMotionSensor sketch,
to
Example → Cayenne-MQTT-ESP8266 → ESP8266
but all i get is a second channel, and still cannot add a DigitalMotionSensor Widget.
Any ideas?
// 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
#include <CayenneMQTTESP8266.h>
//#include <CayenneEthernet.h>
// WiFi network info.
char ssid = “IWILLSTEALYOURMONEY”;
char wifiPassword = “mitmhoneypotattack”;
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “ed1794a0-6bf3-11e7-8c3a-xxxxxx”;
char password = “934a207798ce1080cafb92cxxxxxxxxxxx”;
char clientID = “b9b8b850-7107-11e7-844a-xxxxxxxxx”;
unsigned long lastMillis = 0;
// Virtual Pin of the Digital Motion Sensor widget.
#define VIRTUAL_PIN V1
// 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;
void setup() {
Serial.begin(9600);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}
void loop() {
Cayenne.loop();
//Cayenne.run();
checkSensor();
/*
//Publish data every 10 seconds (10000 milliseconds). Change this value to publish at a different interval.
if (millis() - lastMillis > 10000) {
lastMillis = millis();
//Write data to Cayenne here. This example just sends the current uptime in milliseconds.
Cayenne.virtualWrite(0, lastMillis);
//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);
}
*/
}
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(motionSensorPin);
if (currentState != previousState) {
Cayenne.virtualWrite(VIRTUAL_PIN, currentState);
previousState = currentState;
}
previousMillis = currentMillis;
}
}
//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(“CAYENNE_IN_DEFAULT(%u) - %s, %s”, request.channel, getValue.getId(), getValue.asString());
//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError(“Error message”);
}