Digital Motion Sensor Not responding after MQTT Switch


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.

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


OneWire oneWire(TEMPSENSOR_PIN);
DallasTemperature sensors(&oneWire);

void setup()
  Cayenne.begin(username, password, clientID);

void loop()

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.virtualWrite(VIRTUAL_CHANNEL1, analogRead(LUXSENSOR_PIN));

// This function is called at intervals to send sensor data to Cayenne.
  // Send the command to get temperatures.
  // 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));


change this :

Cayenne.virtualWrite(VIRTUAL_CHANNEL3, currentState, "digital_sensor", "d");


Thank you for your reply. I made the suggested changes and the script compiled and uploaded fine.

I still am not getting a response from the motion sensor on Cayenne (no indication of state 0/1 for value).




I have noticed lately that 0/1 state has quite a long lag time. Not sure if that applies but I’ve had trouble with mine not updating past few weeks.



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.


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.


Interesting…I haven’t had that happen yet. Can you take a screen shot of the error?



Here are a couple of screen shots from the MQTT.fx error.




Can you PM me your server settings? I think there may be an error there.


Actually, I think I see it in your error. You should have as the server instead of (That domain name belongs to Porsche :wink: )



Thank you for your input. I was thinking about something like that driving to work this morning with a Porsche Cayenne in front of me.

It connects fine now.




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.



Thank you for your time and input. I will check the wiring and try another motion sensor.