Has anyone managed to get the motion sensor working on an Arduino with the new MQTT?
I am wondering if it is my code.
Has anyone managed to get the motion sensor working on an Arduino with the new MQTT?
I am wondering if it is my code.
Yes, you just need a generic digital input widget. Can you post your code?
Adam,
Thank you for the prompt reply. I did get it to work but it is still a bit slow. Currently I have triggers set to turn on and off the white light with motion. This is a huge step since the MQTT upgrade of the system. I just wish the response times were a bit faster.
WW
/*
This example shows how to connect to Cayenne using an Ethernet W5100 shield 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. Set the Cayenne authentication info to match the authentication info from the Dashboard.
2. Compile and upload the sketch.
3. 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 // Uncomment to show debug messages
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <CayenneMQTTEthernet.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "xxxnotsharingthisxxx";
char password[] = "xxxnotsharingthisxxx";
char clientID[] = "xxxnotsharingthisxxx";
#define LUXSENSOR_PIN 0
#define TEMPSENSOR_PIN 7
#define MOTIONSENSOR_PIN 2
#define WHITELIGHT_PIN 3
#define GREENLIGHT_PIN 6
#define YELLOWLIGHT_PIN 5
#define REDLIGHT_PIN 4
#define VIRTUAL_CHANNEL1 1
#define VIRTUAL_CHANNEL2 2
#define VIRTUAL_CHANNEL3 3
#define VIRTUAL_CHANNEL4 4
#define VIRTUAL_CHANNEL5 5
#define VIRTUAL_CHANNEL6 6
#define VIRTUAL_CHANNEL7 7
OneWire oneWire(TEMPSENSOR_PIN);
DallasTemperature sensors(&oneWire);
void setup()
{
Serial.begin(9600);
Cayenne.begin(username, password, clientID);
sensors.begin();
pinMode(WHITELIGHT_PIN, OUTPUT);
pinMode(GREENLIGHT_PIN, OUTPUT);
pinMode(YELLOWLIGHT_PIN, OUTPUT);
pinMode(REDLIGHT_PIN, OUTPUT);
}
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 >= 2000) {
// Check the sensor state and send data when it changes.
currentState = digitalRead(MOTIONSENSOR_PIN);
if (currentState != previousState) {
Serial.println("Sending data to Cayenne");
Cayenne.virtualWrite(VIRTUAL_CHANNEL3, currentState, "digital_sensor", "d");
previousState = currentState;
}
previousMillis = currentMillis;
}
}
// This function is called at intervals to send sensor data to Cayenne.
CAYENNE_OUT(VIRTUAL_CHANNEL1)
{
Cayenne.virtualWrite(VIRTUAL_CHANNEL1, analogRead(LUXSENSOR_PIN));
}
// This function is called at intervals to send sensor data to Cayenne.
CAYENNE_OUT(VIRTUAL_CHANNEL2)
{
// Send the command to get temperatures.
sensors.requestTemperatures();
// This command writes the temperature in Celsius to the Virtual Channel.
// Cayenne.celsiusWrite(VIRTUAL_CHANNEL, sensors.getTempCByIndex(0));
// To send the temperature in Fahrenheit use the corresponding code below.
Cayenne.fahrenheitWrite(VIRTUAL_CHANNEL2, sensors.getTempFByIndex(0));
}
// This function is called when data is sent from Cayenne.
CAYENNE_IN(VIRTUAL_CHANNEL4)
{
int value = getValue.asInt();
CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL4, WHITELIGHT_PIN, value);
// Write the value received to the digital pin.
digitalWrite(WHITELIGHT_PIN, value);
}
// This function is called when data is sent from Cayenne.
CAYENNE_IN(VIRTUAL_CHANNEL5)
{
int value = getValue.asInt();
CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL5, GREENLIGHT_PIN, value);
// Write the value received to the digital pin.
digitalWrite(GREENLIGHT_PIN, value);
}
// This function is called when data is sent from Cayenne.
CAYENNE_IN(VIRTUAL_CHANNEL6)
{
int value = getValue.asInt();
CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL6, YELLOWLIGHT_PIN, value);
// Write the value received to the digital pin.
digitalWrite(YELLOWLIGHT_PIN, value);
}
// This function is called when data is sent from Cayenne.
CAYENNE_IN(VIRTUAL_CHANNEL7)
{
int value = getValue.asInt();
CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL7, REDLIGHT_PIN, value);
// Write the value received to the digital pin.
digitalWrite(REDLIGHT_PIN, value);
}
the delay you get is because of this. change it to 250
Thank you for catching that. I think I changed that when trouble shooting the triggers.