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

Advertisements

#include

Resurrecting the OpenCNC-ZA with the help of SMW3D.com

A few weeks ago I released the old OpenCNC-ZA files on here – since then renewed interest in the design has exploded in a rather massive way!

SWM3d.com approached me to say they want to commercialise the design in the US and EU, so I have started revamping the design to bring it into 2016!

Follow me on Patreon or Google Plus to stay up to date on this one!

opencnc 2

Laserweb was featured on Hackaday.com!

Go read the full article on http://hackaday.com/2016/01/26/stop-driving-laser-cutters-with-3d-printer-software/

 

hackaday.png

Adding Raster support to LaserWeb: progressing nicely!

 

 

raster view

For more details, see https://github.com/openhardwarecoza/LaserWeb/issues/32

 

Free designs for a folded metal CNC and 3D printer!

In 2014 I designed a folded metal 3D printer, and with collaboration with Waleed Kahn, we also designed a folded metal CNC machine

The designs have been sitting in Github since I closed the shop in Dec 2014, and it’s just sad to see two such really cool machine designs just sitting around.

So if you have (or have access to) a sheetmetal factory anywhere in the world, and you would like to sell these, you have my blessings and support! If you get rich off it, do remember to buy me some beer though!  https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=45DXEXK9LJSWU

RepRap-Petervanderwalt3

 

Sources are on Github:

OpenCNC-ZA: https://github.com/openhardwarecoza/OPHW-HW-OpenCNC-ZA

Babybot 3D Printer: https://github.com/openhardwarecoza/OPHW-HW-BabyBot

OpenHardware.co.za in 2015 – a Review

I have never been a good blogger.  For me, firing up the PC, logging into wordpress and banging out a post is a LOT more work that just quickly posting on one of the social networks.

That said, 2015 was still a pretty good year statistically:

WordPress.com:

Some highlights:

  • 62451 views from 22482 visitors
  • almost 1/6th coming from the USA (with my own country coming in at 4th place – shame on you! lol)
  • I had one of my projects featured on Hackaday.com and that referred me a whopping 5164 visits
  • The OpenHAB projects, being in tutorial format, seems to have brought in the largest readership! Lets see in 2016 where I expect most of my posts will be about lasers, CNCs and OpenBuilds!

Open source hardware: Why not Open Stats too:  Here’s my WordPress stats:

stats1

Google+

On Google Plus I think I did marginally better:

  • Almost doubled my number of followers
  • Became Moderator for the following Google Plus Communities:
    • OpenBuilds: with 795 Members at the time of writing
    • 3D Printing: with 227,220 Members at the time of writing
  • Clocked in over 2.5 Million views on my Google Plus profile!  Quite proud of that..
  • google plus

Facebook Pages

Pretty much gave up on Facebook this year, I did gain around 100 followers during 2015 (now at 846 total) but I nowadays rarely post on there… I just find my other audiences more engaging to work with

facebook.png

Github.com

Only 33 followers – but worth checking out – thats where ALL the open source work I do, ends up! Tonnes of gold in those – from PCBs to a complete open source CNCs (folded metal design), 3D Printers, etc live.  You guys could be selling those and then sending me some beer!

github

 

 

LaserWeb: Preliminary work on Javascript based Raster Support

Over the past two days I have been working on using Paper.JS to generate Raster GCODE so I can later integrate that into LaserWeb:

test raster

TO read about the specifics of this work, I am not going to blog about it here since it can get very technical with links back to Github lines all over, so I am going to use a Github Issue as a worklog / collaboration tool:
Review the thread on https://github.com/openhardwarecoza/LaserWeb/issues/32