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