Raspbian Jessie as OpenHAB + MQTT Server: Part 3: Add Websockets

Nowhere near completely usable, BUT, what IS working so far:

  • Websocket javascript with Paho library subscribes succesfully to the Websocket listener on Mosquitto
  • I can subscribe, and recieve all the published messages on the HAB
  • I am using the data received, to dynamically, and automatically, build up a device tree – it looks at the topic path (remember how a year a ago I wrote a post about why the topic name is important (;  – here you see why – from the topic names i can deduce the entire hierarchy automatically)
  • Clicking any item in the tree brings ip a CONTROL interface – to be extended with sensorvalues, etc
  • The basic concept here is working great – by setting my devices to announce themselves, or response to a broadcast requesting them to announce themselves, I can cut a LOT of configuration out – theoretically I can have a new device connect to MQTT, send the page its name, location, connected devices, what those devices are (switch, dimmer, sensor, or actuator) and the javascript can automagically make a UI for it.  This means you can mostly get away without a server, without a database.

Source being shared here: https://github.com/openhardwarecoza/ESP8266-Sketches/tree/master/htt-websocket-esp8266

mqtt

SMW3D OpenCNC

Check out my collection on Google Plus: https://plus.google.com/collection/ERlkgB for more details!

 

16 - 1 (1)

ESP8266 With PIR and DHT11

/*
 Basic ESP8266 MQTT example

 This sketch demonstrates the capabilities of the pubsub library in combination
 with the ESP8266 board/library.
m
 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>
#include "DHT.h"

#define DHTPIN 13     // what digital pin we're connected to
// Uncomment whatever type you're using!
#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);

// 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 network18
  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);
  dht.begin();
}

void loop() {

    if (!client.connected()) {
      reconnect();
    }
    client.loop();

    long now = millis();
    if (now - lastMsg > 5000) {
      lastMsg = now;
      float h = dht.readHumidity();
      // Read temperature as Celsius (the default)
      float t = dht.readTemperature();
         
      // Check if any reads failed and exit early (to try again).
      if (isnan(h) || isnan(t)) {
        Serial.println("Failed to read from DHT sensor!");
        return;
      }
    
      // Compute heat index in Celsius (isFahreheit = false)
      //float hic = dht.computeHeatIndex(t, h, false);
      char t2[50];
      char h2[50];
      dtostrf(t, 5, 2, t2);
      dtostrf(h, 5, 2, h2);
      
      client.publish("/sensor/temp", t2 );  
      client.publish("/sensor/humidity", h2 );
    }

    int reading = digitalRead(12);

    Serial.println(reading);
    if (previousReading == LOW && reading == HIGH) {
    counter++;
    client.publish("/sensor/movement", "Motion!");  
    delay(1000);

    }
    previousReading = reading;

  
}

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

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

 

 

SmoothieBrainz!

index

 

Off to the board house today!

https://github.com/openhardwarecoza/Smoothieboard