Home Automation with Raspberry Pi, MQTT, and Espruino

Setting up your Pi

network={
       ssid="YourNetworkSSID"
       psk="Your Network's Passphrase"
       key_mgmt=WPA-PSK
    }

Now it's time to make sure the operating system is sized to your SD card.

It could take as long as half an hour to complete that step!

And that's it!

Interfacing with an Espruino WiFi

var WIFI_NAME = "Espruino";
var WIFI_OPTIONS = { password : "helloworld" };
var MQTT_HOST = "raspberrypi";
var PATH = "/mydevice/";
var mqtt;
var wifi;

function mqttMessage(pub) {
  console.log(
     "MQTT=> ",pub.topic,pub.message);
  if (pub.topic==PATH+"1/set") {
    var v = pub.message!=0;
    digitalWrite(B3, !v);
    mqtt.publish(PATH+"1/status", v?1:0);
  }
  if (pub.topic==PATH+"2/set") {
    var v = pub.message!=0;
    digitalWrite(B4, !v);
    mqtt.publish(PATH+"2/status", v?1:0);
  }
}

function mqttConnect() {
  mqtt = require("MQTT").connect({
    host: MQTT_HOST,
  });
  mqtt.on('connected', function() {
    console.log("MQTT connected");
    // subscribe to wildcard for our name
    mqtt.subscribe(PATH+"#");
  });
  mqtt.on('publish', mqttMessage);
  mqtt.on('disconnected', function() {
    console.log("MQTT disconnected... reconnecting.");
    setTimeout(function() {
      mqtt.connect();
    }, 1000);
  });
}

setInterval(function() {
  if (!mqtt) return;
  mqtt.publish(
    PATH+"cputemp",
    E.getTemperature());
}, 2*60*1000);

setWatch(function() {
  if (!mqtt) return;
  mqtt.publish(
    PATH+"buttonpress",
    1);
}, BTN, {edge:"rising",repeat:true,debounce:50});


function onInit() {
  console.log("Connecting to WiFi");
  wifi = require("EspruinoWiFi");
  wifi.connect(WIFI_NAME, WIFI_OPTIONS,
               function(e) { 
    if (e) {
      console.log("Connection Error: "+e);
      return;
    }
    console.log("WiFi Connected");
    wifi.getIP(function(f,ip) {
      console.log("IP: ",ip);
      mqttConnect();
    });
  });
}

Interfacing with a SonOff

But for those eager, the code is:

var WIFI_NAME = "Espruino";
var WIFI_OPTIONS = { password : "helloworld" };
var MQTT_HOST = "raspberrypi";
var PATH = "/mydevice/";
var LED = D13;
var RELAY = D12;
var BTN = D0;
var mqtt;
var wifi;

function setState(v) {
  RELAY.write(v);
  LED.write(!v);
  mqtt.publish(PATH+"status", v?1:0);
}

function mqttMessage(pub) {
  console.log("MQTT=> ",pub.topic,pub.message);

  if (pub.topic == PATH+"set") {
    setState(pub.message!=0);
  }
  if (pub.topic == PATH+"eval") {
    try {
      mqtt.publish(PATH+"response", eval(pub.message));
    } catch(e) {
      mqtt.publish(PATH+"exception", e.toString());
    }
  }
}

function mqttConnect() {
  mqtt = require("MQTT").connect({
    host: MQTT_HOST,
  });
  mqtt.on('connected', function() {
    console.log("MQTT connected");
    setTimeout(function() {      
      mqtt.subscribe(PATH+"#");
    }, 1000);
  });
  mqtt.on('publish', mqttMessage);
}


function onInit() {
  console.log("Connecting WiFi");  
  setInterval(function() {
    if (!mqtt) return;
    if (!mqtt.connected) {
      console.log("MQTT disconnected... reconnecting.");
      mqtt.connect();
    }
  }, 60*1000);

  wifi = require("Wifi");
  wifi.on('connected',function() {
    console.log("Connected to WiFi");  
  });
  wifi.on('disconnected',function() {
    console.log("Disconnected from WiFi");  
  });
  wifi.setHostname("MYDEVICE");
  wifi.stopAP();
  wifi.connect(WIFI_NAME, WIFI_OPTIONS,
               function(ap){ 
    console.log("Successful connect.");
  });
  // wait, and connect MQTT
  setTimeout(function() {
    console.log("MQTT connecting");
    mqttConnect();
  }, 10000);
}

This page is auto-generated from GitHub. If you see any mistakes or have suggestions, please let us know.