Both on the Pi 3 Model B and Arduino Uno with W5100 ethernet shield after switching over to the MQTT are having issues. The Pi seems to work with more delay but will not trigger.
The Arduino is not responding to motion at all. I first setup the lux and temp sensors with the new MQTT scripts which are now both working. Then added the Digital Motion Sensor code as shown below:
/*
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[] = "not shown here";
char password[] = "not shown here";
char clientID[] = "not shown here";
#define LUXSENSOR_PIN 0
#define TEMPSENSOR_PIN 7
#define MOTIONSENSOR_PIN 2
#define VIRTUAL_CHANNEL1 1
#define VIRTUAL_CHANNEL2 2
#define VIRTUAL_CHANNEL3 3
OneWire oneWire(TEMPSENSOR_PIN);
DallasTemperature sensors(&oneWire);
void setup()
{
Serial.begin(9600);
Cayenne.begin(username, password, clientID);
sensors.begin();
}
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(MOTIONSENSOR_PIN);
if (currentState != previousState) {
Cayenne.virtualWrite(VIRTUAL_CHANNEL3, currentState);
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));
}
When I go to live data I can only see the Temp and Lux data. The motion data is not being received. At one point experimenting with the code I placed :
int previousState = -1;
int currentState = -1;
unsigned long previousMillis = 0;
Inside of the checkSensor function and I was receiving data just not the on / off state changes. I am sure this is an issue with the script. The hardware setup is working and was on several of the same devices before MQTT. I just need to get the motion sensor script right.
Try adding in a print statement to make sure it’s getting in the if statement. If it is then you might be getting rate limited. Try checking the sensor every one second (or longer) instead of 4 times every second.
if (currentState != previousState) {
Serial.println("Sending data to Cayenne");
Cayenne.virtualWrite(VIRTUAL_CHANNEL3, currentState);
previousState = currentState;
}
Thank you for the suggestion. I added the print statement and changed the rate to 1000 then 2000 milliseconds. When i look at live data I see one time stamp for channel 3 and a print out right after I update the script. Then no further action from the sensor.
Let me know if you have any more suggestions to get the digital motion sensor working with the Arduino Uno.
The dashboard does not seem to be receiving any data from it when you click on the “data” icon. I can see the data coming in from the temp and lux but not the motion sensor.
The next thing I would suggest is disconnect your device and then connect to the Cayenne mqtt server with MQTTfx using your device credentials. Send the same info with that an see if it shows up on the dashboard. That will tell you if you have a code problem or a Cayenne problem. Cayenne Features - Developer | myDevices.com
Thank you for the suggestion. I downloaded and installed the MQTTfx and entered my device credentials. I get a mqttexception and a red light on the left side of the software screen when clicking the connect button.
Actually, I think I see it in your error. You should have mqtt.mydevices.com as the server instead of mqtt.cayenne.com (That domain name belongs to Porsche )
The MQTTfx connected and the sensors show on the dashboard. I am guessing there is something wrong with my code for the motion sensor but I am not seeing it. It seems to not be looping the sensor. It shows data once for motion when started as a value of “0” and then only data for the lux and temp thereafter.
Ok, so definitely sounds like a code or hardware problem. Did you put the line Serial.println("Sending data to Cayenne"); in your code? Do you see that printed when you expect it to be (ex when it detects motion and then again when the motion timeout sets the pin back to 0)?
I did setup the Serial.println and it prints sending data to Cayenne just once when I upload the script. It does not seem to be passing the data for the motion sensor to Cayenne. As far as hardware, I have had two of these units working monitoring my server rooms for a long time before the switch over to MQTT so I am sure the hardware is fine.
If you’re only seeing “Sending data to Cayenne” once and this code has worked in the past then that leads me to believe you have a faulty motion sensor or wiring problem. No matter what is happening with Cayenne you should see that line being printed every time the state of the motion sensor changes.