MQTT Sensor State Publishing

Connect security sensors such as PIR motion detectors, microwave motion detectors, and glass breakage detectors to your Arduino. Provides full end-of-line monitoring for tamper detection, and power passthrough to sensors. [Product page]
Post Reply
Nivin37
Posts: 3
Joined: Wed Nov 23, 2011 10:33 am

MQTT Sensor State Publishing

Post by Nivin37 » Sun Mar 29, 2015 8:04 am

Hi All,

I have the Security Sensor Shield on the bench working as to the Practical Arduino book.

Next I would like it to report the state of each sensor to my MQTT server, however I'm lost & unable to move forward from here.

Anyone care to help?

Thanks.

Steve...

p.s. Here is where I'm up to...

Code: Select all

/**
 * SecuritySensors
 *
 * Reads the state of security sensors such as PIR motion detectors
 * connected to analog inputs using EOL (end-of-line) circuitry to
 * prevent tampering. The state of each input is reported back to a
 * connected host via USB.
 *
 * Note that the "checkSensor" function returns a state value that is
 * not used in this example. This is to allow the sketch to be simply
 * modified to perform logic on the return value, such as asserting
 * an output for a determined period if an input is triggered.
 *
 * Copyright 2009 Jonathan Oxer <jon@oxer.com.au>
 * Copyright 2009 Hugh Blemings <hugh@blemings.org>
 * http://www.practicalarduino.com/projects/security-sensors
 */
 
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include <DHT.h>

//Ethernet and pubsub setup BEGIN
byte mac[]    = {  0x00, 0x19, 0x21, 0x68, 0x12, 0x23 };
//byte ip[] = { 192, 168, 1, 211 }; //Comment out if you want DHCP also you need to modify Ethernet.begin(mac, ip)

// IP Address of MQTT Server
byte mqttserver[] = {192, 168, 1, 200};

void callback(char* topic, byte* payload, unsigned int length) {
  // handle message arrived
}

EthernetClient ethClient;
PubSubClient client("192.168.1.200", 1883, callback, ethClient);
//Ethernet and pubsub setup END

// Use analog inputs 0 through 3 for sensor connections A through D
byte channelAInput = 0;
byte channelBInput = 1;
byte channelCInput = 2;
byte channelDInput = 3;

// Use digital outputs 4 through 7 for status indicator LEDs A through D
byte channelALed = 4;
byte channelBLed = 5;
byte channelCLed = 6;
byte channelDLed = 7;

/**
 * Initial configuration
 */
void setup() 
{
  Serial.begin(9600);  // Use the serial port to report back reading

  if (Ethernet.begin(mac) == 0)
  {
    Serial.println("Failed to configure Ethernet using DHCP");
    return;
  }

  Serial.print("My IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print(".");
  }
  Serial.println();
  Serial.println();
  Serial.println("Sketch = Security_mqtt_223_2");
  Serial.println();
  delay(3000);

  pinMode(channelALed, OUTPUT);   // Set up channel A
  digitalWrite(channelALed, LOW);
  pinMode(channelBLed, OUTPUT);   // Set up channel B
  digitalWrite(channelBLed, LOW);
  pinMode(channelCLed, OUTPUT);   // Set up channel C
  digitalWrite(channelCLed, LOW);
  pinMode(channelDLed, OUTPUT);   // Set up channel D
  digitalWrite(channelDLed, LOW);

//Connect to MQTT
  if (!client.connected())
  {
    Serial.println("Connecting to MQTT server");
    client.connect("arduino_223");
    client.publish("campaspe/hardware/arduino_223", "ONLINE");
    //client.subscribe("/status/read");
    Serial.println("Connected to MQTT server"); 
  }

}
void loop() {

  byte sensorStatus;
  sensorStatus = checkSensor(channelAInput, channelALed);
  sensorStatus = checkSensor(channelBInput, channelBLed);
  sensorStatus = checkSensor(channelCInput, channelCLed);
  sensorStatus = checkSensor(channelDInput, channelDLed);

  Serial.println("");
  delay(500);   // Wait half a second before reading all channels again
  } 

/**
 * Checks the state of a sensor and reports it to the connected host
 */
boolean checkSensor( byte sensorInput, byte statusOutput )
{
  byte state;
  int sensorReading = analogRead(sensorInput);
  if( sensorReading < 400 ) {
    state = 0;                        // Wire shorted. Possible tampering.
    digitalWrite(statusOutput, HIGH); // Turn the associated status LED on
  } else if ( sensorReading >= 400 && sensorReading < 590 ) {
    state = 1;                        // Normal state, sensor not triggered
    digitalWrite(statusOutput, LOW); // Turn the associated status LED off
  } else if ( sensorReading >= 590 && sensorReading < 800 ) {
    state = 2;                        // Sensor triggered.
    digitalWrite(statusOutput, HIGH); // turn the associated status LED on
  } else {
    state = 3;                        // Open circuit. Cut or tamper triggered.
    digitalWrite(statusOutput, HIGH); // Turn the associated status LED on
  }
  // Output the current reading to the host via the serial connection
  Serial.print(sensorInput, DEC);
  Serial.print(": ");
  Serial.print(sensorReading, DEC);
  Serial.print(" (");
  Serial.print(state, DEC);
  Serial.print(") ");
  // Pass the current state back to the calling function
  return state;
  }

Rorly
Posts: 1
Joined: Fri Aug 19, 2016 8:29 am

Re: MQTT Sensor State Publishing

Post by Rorly » Sat Aug 20, 2016 7:03 am

Nivin37 wrote:Hi All,

I have the Security Sensor Shield on the bench working as to the Practical Arduino book.

Next I would like it to report the state of each sensor to my MQTT server, however I'm lost & unable to move https://www.mixedink.com/phen375-review-results forward from Phen375 here.

Anyone care to help?

Thanks.

Steve...
Hi Steve, how did you get on with this?
Last edited by Rorly on Thu Apr 13, 2017 9:26 am, edited 2 times in total.

keiranwyllie
Posts: 40
Joined: Tue Aug 16, 2016 9:17 am

Re: MQTT Sensor State Publishing

Post by keiranwyllie » Fri Oct 21, 2016 9:55 pm

You can try adding the following in your loop (adjust your MQTT topic/s as required):

Code: Select all

  if (digitalRead(4) == HIGH) {
    client.publish("sensor1", "1");
  }
  if (digitalRead(5) == HIGH) {
    client.publish("sensor2", "1");
  }
  if (digitalRead(6) == HIGH) {
    client.publish("sensor3", "1");
  }
  if (digitalRead(7) == HIGH) {
    client.publish("sensor4", "1");
  }
  
This works fine for me. Since the code uses the digital pins 4-7 for LED indication, you can use that status to publish to MQTT.

I'm doing the same thing and now trying to get my sketch right in order to control the relay 8 driver shield by subscribing to the MQTT server.


Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest