Smart Home

Mud Map

Built with cola.js.

OpenHAB

-Back to Mud Map-

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam aliquet accumsan euismod. Cras ac lectus a tellus lobortis pretium. Donec eget dictum tortor. Vivamus et ornare tortor, malesuada commodo enim. In ullamcorper nisl vulputate, dictum magna in, tincidunt velit. Ut non aliquet dui, vel blandit sem. Aliquam vel turpis ac ex bibendum molestie. Praesent pretium turpis quis augue porttitor vehicula quis et tellus. Pellentesque facilisis elit quis dolor faucibus, a placerat leo feugiat.

Proin nec arcu vel ante congue maximus eget eget ex. Curabitur at purus vitae turpis ullamcorper sodales nec non lacus. Maecenas pretium vitae orci eu imperdiet. Maecenas bibendum quis eros nec elementum. Donec condimentum bibendum velit, at tincidunt massa imperdiet id. Mauris nec tristique felis. Donec a eros bibendum, accumsan lacus in, volutpat lectus. Integer vel viverra ante, eget imperdiet purus. Proin risus lorem, bibendum et tellus quis, molestie efficitur nisl. Nulla sodales scelerisque convallis. Sed consectetur malesuada convallis. Mauris urna libero, porttitor eleifend est ut, laoreet vestibulum lorem. Praesent pharetra dictum lacinia.

Sed at nibh eget neque pulvinar consectetur. Duis tincidunt efficitur arcu eget aliquet. Proin efficitur, sem eu porta consectetur, metus leo volutpat est, ut condimentum libero odio sit amet tellus. Morbi mauris mauris, pharetra et auctor eget, sagittis ac justo. Suspendisse sed lacinia quam. Maecenas volutpat, erat vel vulputate fringilla, nibh sapien sollicitudin nibh, at condimentum mi leo in est. Quisque eu lacinia lacus, sit amet dignissim velit. Aenean cursus dolor vitae nunc imperdiet, non blandit massa ullamcorper. Donec metus nisl, auctor vel mattis id, gravida eu lectus. Fusce eget facilisis urna, sit amet dictum arcu.

Mosquitto

-Back to Mud Map-

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam aliquet accumsan euismod. Cras ac lectus a tellus lobortis pretium. Donec eget dictum tortor. Vivamus et ornare tortor, malesuada commodo enim. In ullamcorper nisl vulputate, dictum magna in, tincidunt velit. Ut non aliquet dui, vel blandit sem. Aliquam vel turpis ac ex bibendum molestie. Praesent pretium turpis quis augue porttitor vehicula quis et tellus. Pellentesque facilisis elit quis dolor faucibus, a placerat leo feugiat.

Proin nec arcu vel ante congue maximus eget eget ex. Curabitur at purus vitae turpis ullamcorper sodales nec non lacus. Maecenas pretium vitae orci eu imperdiet. Maecenas bibendum quis eros nec elementum. Donec condimentum bibendum velit, at tincidunt massa imperdiet id. Mauris nec tristique felis. Donec a eros bibendum, accumsan lacus in, volutpat lectus. Integer vel viverra ante, eget imperdiet purus. Proin risus lorem, bibendum et tellus quis, molestie efficitur nisl. Nulla sodales scelerisque convallis. Sed consectetur malesuada convallis. Mauris urna libero, porttitor eleifend est ut, laoreet vestibulum lorem. Praesent pharetra dictum lacinia.

Etiam faucibus scelerisque ante a molestie. Fusce et sapien ligula. Donec eleifend auctor sapien sed pharetra. Duis varius quam et mauris rhoncus, at vestibulum mi iaculis. Nullam gravida, erat et tristique rhoncus, diam arcu facilisis ligula, at congue sem mauris quis ante. Nunc a luctus lacus. Ut dictum nulla eros, non condimentum erat varius sit amet. Aliquam nec odio et dui semper posuere. Donec at tellus vulputate, tincidunt nibh efficitur, ullamcorper augue. Sed varius efficitur fermentum. Duis in molestie lorem, quis viverra dui. Vestibulum erat nunc, laoreet nec aliquet eget, pretium nec nisl. Fusce sit amet sapien eu metus iaculis interdum. Duis ac egestas arcu. Cras sagittis, sem id tincidunt laoreet, urna tortor posuere odio, vel ornare nisl massa sit amet massa.

HomeBridge

-Back to Mud Map-

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam aliquet accumsan euismod. Cras ac lectus a tellus lobortis pretium. Donec eget dictum tortor. Vivamus et ornare tortor, malesuada commodo enim. In ullamcorper nisl vulputate, dictum magna in, tincidunt velit. Ut non aliquet dui, vel blandit sem. Aliquam vel turpis ac ex bibendum molestie. Praesent pretium turpis quis augue porttitor vehicula quis et tellus. Pellentesque facilisis elit quis dolor faucibus, a placerat leo feugiat.

Proin nec arcu vel ante congue maximus eget eget ex. Curabitur at purus vitae turpis ullamcorper sodales nec non lacus. Maecenas pretium vitae orci eu imperdiet. Maecenas bibendum quis eros nec elementum. Donec condimentum bibendum velit, at tincidunt massa imperdiet id. Mauris nec tristique felis. Donec a eros bibendum, accumsan lacus in, volutpat lectus. Integer vel viverra ante, eget imperdiet purus. Proin risus lorem, bibendum et tellus quis, molestie efficitur nisl. Nulla sodales scelerisque convallis. Sed consectetur malesuada convallis. Mauris urna libero, porttitor eleifend est ut, laoreet vestibulum lorem. Praesent pharetra dictum lacinia.

// ESP WIFI, HTTP CLIENT AND UPDATE FOR FIRWARE UPDATES
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h>

// MQTT LIBRARY AND JSON LIBRARY
#include <PubSubClient.h>
#include <ArduinoJson.h>

// I2C WIRE LIBRARY AND BME280 LIBRARY
#include <Wire.h>
#include <Adafruit_BME280.h>

/*-------------------------------*/
/* CONFIGURATION                 */
/*-------------------------------*/

// WIFI CREDENTIALS
const char* ssid = ""; 
const char* password = "";

// MQTT CREDENTIALS
const char* mqtt_server = "";  // eg: "192.168.1.5"
const int mqtt_port =  1883;
const char mqtt_id[] = "";     // eg: "myEspBme"
const char* mqtt_topic = "";   // eg: "MYESPBME/"

// REFRESH RATES
const int BmeRefreshRate = 15; // every x seconds
const int FW_CHECK_RATE = 15;  // every x seconds

// FIRMWARE UPDATE INFO
const int FW_VERSION = 2019041504; // firmware revision number
const char* fwUrlBase = "";        // http path to firmware

/*-------------------------------*/

Adafruit_BME280 bme; // I2C

WiFiClient espClient;
PubSubClient mqttClient(espClient);

uint64_t prev_p = 0;
char msg[300];
char topic[30];

long lastbeat = 0;
long lastMsg = BmeRefreshRate * -950;

long lastFwCheck = FW_CHECK_RATE * -950; // force a check soon after boot

void setup() {
  Serial.begin(115200);
  setup_wifi();

  mqttClient.setServer(mqtt_server, 1883);
  mqttClient.setCallback(mqttCallback);
  
  bool status;
  status = bme.begin();  
  if (!status) {
      Serial.println("Could not find a valid BME280 sensor, check wiring!");
      while (1);
  }
}

void setup_wifi() {
  delay(10);

  Serial.println("");
  Serial.println("Connecting to wifi:");
  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());
  Serial.println("MAC address: ");
  Serial.println(WiFi.macAddress());
}

void mqttCallback(char* topic, byte* payload, unsigned int length) {
  // PRINTING A MESSAGE ARRIVED (MUST BE SUBSCRIBED)
  Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] ");
  for (int i = 0; i < length; i++) { Serial.print((char)payload[i]); } Serial.println();
}

void mqttReconnect() {
  // Loop until we're reconnected
  while (!mqttClient.connected()) {
    Serial.println("Attempting MQTT connection...");
    // Attempt to connect
    if (mqttClient.connect(mqtt_id)) {
      StaticJsonBuffer<300> jsonBuffer;
      String output;
      JsonObject& root = jsonBuffer.createObject();
      root[F("ID")] = String(mqtt_id);
      root[F("FW")] = FW_VERSION;
      root[F("IP")] = WiFi.localIP().toString();
      root[F("status")] = "Online";

      digitalWrite(D3, HIGH);
      digitalWrite(D8, LOW);

      root.printTo(output);
      output.toCharArray(msg, 300);
      (String(mqtt_topic) + String("/sysStatus")).toCharArray(topic, 30);
      mqttClient.publish(topic, msg);
      Serial.print(topic);
      Serial.print(": ");
      Serial.println(msg);

      (String(mqtt_topic) + String("/command")).toCharArray(topic, 30);
      mqttClient.subscribe(topic);

    } else {
      Serial.print("failed, rc="); Serial.print(mqttClient.state()); Serial.println(" try again in 5 seconds");
      delay(5000);
    }
  }
}

void checkForUpdates() {
  String fwURL = String( fwUrlBase );
  fwURL.concat( mqtt_id );
  String fwVersionURL = fwURL;
  fwVersionURL.concat( ".version" );

  HTTPClient httpClient;
  httpClient.begin( fwVersionURL );
  int httpCode = httpClient.GET();
  
  if( httpCode == 200 ) {
    
    String newFWVersion = httpClient.getString();
    int newVersion = newFWVersion.toInt();
   
    if( newVersion > FW_VERSION ) {

      StaticJsonBuffer<300> jsonBuffer;
      JsonObject& root = jsonBuffer.createObject();
      String output;
      root[F("ID")] = String(mqtt_id);
      root[F("status")] = "Updating Firmware";
      root[F("currentFW")] = FW_VERSION;
      root[F("newFW")] = newVersion;
      
      root.printTo(output);
      output.toCharArray(msg, 300);
      char topic[30];
      (String(mqtt_topic) + String("/sysStatus")).toCharArray(topic, 30);
      mqttClient.publish(topic, msg);
      Serial.print(topic);
      Serial.print(" : ");
      Serial.println(msg);
      
      String fwImageURL = fwURL;
      fwImageURL.concat( ".bin" );
      t_httpUpdate_return ret = ESPhttpUpdate.update( fwImageURL );
    }
    else {
      //Serial.println( "Already on latest version" );
    }
  }
  else {
    StaticJsonBuffer<300> jsonBuffer;
    JsonObject& root = jsonBuffer.createObject();
    String output;
    root[F("ID")] = String(mqtt_id);
    root[F("status")] = "Firmware check failed";
        
    root.printTo(output);
    output.toCharArray(msg, 300);
    char topic[30];
    (String(mqtt_topic) + String("/sysStatus")).toCharArray(topic, 30);
    mqttClient.publish(topic, msg);
      Serial.print(topic);
      Serial.print(" : ");
      Serial.println(msg);
    
    Serial.print( "Firmware version check failed, got HTTP response code " );
    Serial.println( httpCode );
  }
  httpClient.end();
}

void heartBeat(){
  uint64_t p = 0;
  StaticJsonBuffer<300> jsonBuffer;
  JsonObject& root = jsonBuffer.createObject();
  String output;
  
  root[F("ID")] = String(mqtt_id);
  root[F("heart")] = String("beat");

  root.printTo(output);
  output.toCharArray(msg, 300);
  (String(mqtt_topic) + String("/sysStatus")).toCharArray(topic, 30);
  mqttClient.publish(topic, msg);
  Serial.print(topic);
  Serial.print(": ");
  Serial.println(msg);
  
}

void loop() {
  
  long now = millis();

  if (!mqttClient.connected()) {
    mqttReconnect();
  }
  mqttClient.loop();
  
  if (now - lastFwCheck > (FW_CHECK_RATE * 1000)){
    checkForUpdates();    
    lastFwCheck = now;
  }

  if (now - lastMsg > BmeRefreshRate*1000) {
    lastMsg = now;

    StaticJsonBuffer<300> jsonBuffer;
    JsonObject& root = jsonBuffer.createObject();
    
    root[F("pressure")] = String(bme.readPressure() / 100.0F);
    root[F("temp")] = String(bme.readTemperature());
    root[F("humidity")] = String(bme.readHumidity());
    
    String output;
    root.printTo(output);
    output.toCharArray(msg, 300);
    char topic[30];
    (String(mqtt_topic) + String("/data")).toCharArray(topic, 30);
    mqttClient.publish(topic, msg);
    Serial.print(topic);
    Serial.print(" : ");
    Serial.println(msg);
   
  }

  if (now - lastbeat > (30000)){
    lastbeat = now;
    heartBeat();    
  }
  
  
}