PIR sensor, ESP8266 and MQTT

MQTT

Install MQTT and start a test subscribe:

 

sudo apt-get install mosquitto mosquitto-clients
mosquitto_sub -t outTopic

Arduino/ESP8266 Sketch

 

On the ESP8266:  Install the following sketch using Arduino ( Note I won’t go into detail on setting up Arduino for ESP8266, lots of articles on the Internet already explain that)

NB: Do make sue you install the MQTT library: https://github.com/knolleary/pubsubclient

 

/*
 Basic ESP8266 MQTT example

 This sketch demonstrates the capabilities of the pubsub library in combination
 with the ESP8266 board/library.

 It connects to an MQTT server then:
  - publishes "hello world" to the topic "outTopic" every two seconds
  - subscribes to the topic "inTopic", printing out any messages
    it receives. NB - it assumes the received payloads are strings not binary
  - If the first character of the topic "inTopic" is an 1, switch ON the ESP Led,
    else switch it off

 It will reconnect to the server if the connection is lost using a blocking
 reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
 achieve the same result without blocking the main loop.

 To install the ESP8266 board, (using Arduino 1.6.4+):
  - Add the following 3rd party board manager under "File -> Preferences -> Additional Boards Manager URLs":
       http://arduino.esp8266.com/stable/package_esp8266com_index.json
  - Open the "Tools -> Board -> Board Manager" and click install for the ESP8266"
  - Select your ESP8266 in "Tools -> Board"

*/

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.

const char* ssid = "openhardwarecoza";
const char* password = "novell1234";
const char* mqtt_server = "192.168.1.13";
int counter = 0;
int previousReading = LOW;



WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

void setup_wifi() {

  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();

  // Switch on the LED if an 1 was received as first character
  if ((char)payload[0] == '1') {
    digitalWrite(BUILTIN_LED, LOW);   // Turn the LED on (Note that LOW is the voltage level
    // but actually the LED is on; this is because
    // it is acive low on the ESP-01)
  } else {
    digitalWrite(BUILTIN_LED, HIGH);  // Turn the LED off by making the voltage HIGH
  }

}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("ESP8266Client")) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish("outTopic", "hello world");
      // ... and resubscribe
      client.subscribe("inTopic");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {
  pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
  pinMode(12, INPUT);
}

void loop() {

  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  int reading = digitalRead(12);
  Serial.println(reading);
  if (previousReading == LOW && reading == HIGH) {
    counter++;
    client.publish("outTopic", "Motion Detected");  
    Serial.print("Triggered ");
    Serial.print(counter);
    Serial.print("x Times! ");
    delay(1000);

  }
  previousReading = reading;

}

Wiring

Connect VCC on the IR Sensor to 5v (NB it has an onboard 3.3v regulator, so the output voltage is 3.3v (safe for ESP) but it needs to be powered off 5v!)
Connect Signal to pin 12 on the ESP8266 (or change the Arduino code to the pin where you wired it to)
Connect GND to GND

Order PIR Sensors?

AliExpress.com Product – 1pcs High Quality HC-SR501 Infrared PIR Motion Sensor Module For Arduino Raspberry pi

AliExpress.com Product – New HC-SR501 Adjust IR Pyroelectric Infrared PIR Motion Sensor Detector Module SG039-SZ

#include

OpenHAB, MQTT, Arduino and ESP8266: Part 3: Hardware: Arduino with Ethernet Shield

The following series of posts will document my journey to full Home Automation using the MQTT protocol, all custom hardware (Arduino and ESP8266 based), and all tied together using OpenHAB

  • Part 1:  Setting up the server / environment
  • Part 2:  Publish, Subscribe, Command, State, and WTFs
  • Part 3:  Hardware:  Arduino with Ethernet Shield
  • Part 4:  Hardware:  ESP8266 with NodeMCU firmware
  • Part 5:  Hardware:  Sensors
    • Part 5.1:  Graphing Sensor Data
  • Part 6:  OpenHAB Automation Rules

Lets dive right in:
Take a Arduino Uno, and install a standard Ethernet shield on top.   Wire pin D4 and D5 to a two channel relay breakout or just LEDs (for now – just testing phase)

Wire a I2C LCD to SDA/SCL and VCC/GND  – I used this as a supplementary check of what my code is doing but this may also make it into any final hardware version

#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>


// Update these with values suitable for your network.
byte mac[]    = {  0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
byte server[] = { 192, 168, 0, 100 }; // IP Address of your MQTT Server
byte ip[]     = { 192, 168, 0, 120 }; // IP for this device


EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);


LiquidCrystal_I2C    lcd(0x27,2,1,0,4,5,6,7); // 0x27 is the I2C bus address for an unmodified backpack


byte bulb[8] = {
    0b01110,
    0b10001,
    0b10001,
    0b10001,
    0b01110,
    0b01010,
    0b01110,
    0b00100
};

byte plug[8] = {
    0b01010,
    0b01010,
    0b11111,
    0b10001,
    0b10001,
    0b01110,
    0b00100,
    0b00100
};

void callback(char* topic, byte* payload, unsigned int length) {
Serial.println("Callback");
Serial.print("Topic:");
Serial.println(topic);
Serial.print("Length:");
Serial.println(length);
Serial.print("Payload:");
Serial.write(payload,length);
Serial.println();

  if (strcmp(topic,"/home/1/ard1/p1/com")==0) { 
   if (payload[0] == '0') 
    {
    digitalWrite(4, LOW);
    lcd.home();
    lcd.clear();
    lcd.write((uint8_t)1);
    lcd.print ("Turning Fan OFF");
    delay(100);
    client.publish("/home/1/ard1/p1/state","0");
    } 
    else if (payload[0] == '1') 
    {
    digitalWrite(4, HIGH);
    lcd.home();
    lcd.clear();
    lcd.write((uint8_t)1);
    lcd.print ("Turning Fan ON");
    delay(100);
    client.publish("/home/1/ard1/p1/state","1");
    }
   } 

  if (strcmp(topic,"/home/1/ard1/p2/com")==0) { 
   if (payload[0] == '0') 
    {
    digitalWrite(5, LOW);
    lcd.home();
    lcd.clear();
    lcd.write((uint8_t)1);
    lcd.print ("Turning Light OFF");
    delay(100);
    client.publish("/home/1/ard1/p2/state","0");
    } 
    else if (payload[0] == '1') 
    {
    digitalWrite(5, HIGH);
    lcd.home();
    lcd.clear();
    lcd.write((uint8_t)1);
    lcd.print ("Turning Light ON");
    delay(100);
    client.publish("/home/1/ard1/p2/state","1");
    }
   } 


}

void setup()
{
  Serial.begin(9600);
  lcd.begin (20,4); // for 16 x 2 LCD module
  lcd.createChar(0, bulb);
  lcd.createChar(1, plug);
  lcd.home(); // set cursor to 0,0
  lcd.print(" Starting  "); 
  delay(1000);
  Ethernet.begin(mac, ip);
    lcd.home();
    lcd.clear();
    lcd.print("Ethernet Connected"); 
    
  
  if (client.connect("arduinoClient")) {
    lcd.home();
    lcd.clear();
    lcd.print("MTQQ Connected");
    client.publish("outTopic","hello world");
    client.subscribe("/home/1/ard1/#");  // Subscribe to all messages for this device
  }
  pinMode(4, OUTPUT);
  digitalWrite(4, LOW);
  pinMode(5, OUTPUT);
  digitalWrite(5, LOW);

}

void loop()
{
  client.loop();


}

Now comes the interesting time: Time to start customizing OpenHAB to handle this newly built hardware (:

1.   Create a new Sitemap – sitemaps in OpenHAB define which items to display

sudo vi /etc/openhab/configurations/sitemaps/dolphin.sitemap

Inside this file, paste the following content

sitemap dolphin label="Main Menu"
{
   Frame label="MQTT" {
   Switch item=mqttsw1 label="MQTT Switch 1"
   Switch item=mqttsw2 label="MQTT Switch 2"
   Switch item=lamp1 label="Office Lamp"
  }  
}

Lets configure these three new items

sudo vi /etc/openhab/configurations/items/dolphin.items

Add the following to the new file

Group All
Switch mqttsw1 "Switch 1" (all) {mqtt=">[broker:/home/1/ard1/p1/com:command:on:1],>[broker:/home/1/ard1/p1/com:command:off:0]"}
Switch mqttsw2 "Switch 2" (all) {mqtt=">[broker:/home/1/ard1/p2/com:command:off:0],>[broker:/home/1/ard1/p2/com:command:on:1]"}Restart OpenHAB
sudo /etc/init.d/openhab restart

Once it has started, access your new sitemap:

http://127.0.0.1:8080/openhab.app?sitemap=dolphin

mqtt

Lets recap the configuration:

  1. We have a device(s) on /home/1/ard1/p1/com and /home/1/ard1/p2/com
  2. These devices subscribe to our MQTT broker on 192.168.0.100:1883 (See the arduino Sketch)
  3. Our MQTT Broker, Mosquitto, runs on 192.168.0.100:1883
  4. OpenHAB has a Broker configuration in /etc/openhab/configurations/openhab.cfg (Where OpenHAB can Publish commands, for example turn ON or OFF /home/1/ard1/p1)
  5. We configured a sitemap with two items (sorry screenshot above shows 3 devices – ahead of myself there) under /etc/openhab/configurations/sitemaps/dolphin.sitemap (we’ll add groups and other cool bits later – for now the basics)
  6. The items in our sitemap, has relevant configurations under /etc/openhab/configurations/items/dolphin.items
  7. These items have a MQTT Binding to send 1 = ON and 2 = OFF
    {mqtt=">[broker:/home/1/ard1/p1/com:command:on:1],>[broker:/home/1/ard1/p1/com:command:off:0]"}

#include