Digital Motion Sensor Not responding after MQTT Switch


#1

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

#2

change this :

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


#3

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).

Regards,

WW


#4

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.

William


#5

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.


#6

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

#7

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.


#8

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.

thanks


#9

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. https://mydevices.com/cayenne/docs_stage/cayenne-mqtt-api/#cayenne-mqtt-api-manually-publishing-subscribing


#10

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.


#11

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


#12

Adam,

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

Regards,

WW


#13

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


#14

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 :wink: )


#15

Adam,

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.

Regards,

WW


#16

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.


#17

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)?


#18

Adam,

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.


#19

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.


#20

Adam,

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

Regards,

WW