Showing posts with label MQTT. Show all posts
Showing posts with label MQTT. Show all posts

Saturday, 4 December 2021

Door/Window Sensor with an ESP8266

It's been a long time since my last post but I thought I would write up about my latest project - a door/window sensor using an ESP 8266.

I've bought a few door sensors in the past from Tuya and a Hive one (load of rubbish) but I came across a brilliant YouTube video from a gent called MakerMeik who used a logic toggle circuit to wake up the ESP from sleep mode when a reed switch is opened or closed (https://youtu.be/vxbuO1zWo3w).

Using the two XOR logic gates alowed both the door open and door closed to wake up the ESP by toggling the Enable pin whilst also passing the reed switch state to the ESP to publish.

Mike used the ESP to connect to the router via DHCP (or fixed IP) which took a few seconds to establish before then logging onto your MQTT server and publishing the door status.

So I downloaded Kicad and gave it a try, I had tried Kicad a few years ago and was a bit disappointed but the latest version was streets ahead of what I remembered and I soon had a PCB ready to send off for fabrication.

Bare PCB

Populated PCB

The PCBs were delivered and I assembled one which worked as expected (nice surprise) and while I was waiting for the PCBs to arrive back from PCBWay I exported the PCB design as a 3D model (STEP file) which I was able to import into Fusion360 and use as the basis for an enclosure.
Initially my idea was to use an 18650 Lithium cell as I had plenty that I'd reclaimed from old laptop batteries, which still had reasonable capacities, my idea was to use two halves that could be screwed together holding the PCB and battery, this could be screwed to a surface or stuck on with double sided adhesive tape.
 
 


The design is quite tall at 33mm but this is OK for my back door which is double glazed and the door is quite deep so the deep back places the reed switch nicely inline with the magnet on the door.
Printed enclosure

Having finished this version of the sensor I've been investigating ways to increase the battery life and reducing the overall size.

I found some smaller batteries that I could use with this design in conjunction with ESP-NOW code that uses alot less transmission time so I designed another rear half that is alot shallower for the new battery.



Smaller rear half for smaller battery

One tip I can give is to buy the plastic encapsulated reed switches instead of the normal glass ones as they are prone to breaking, even though I used surface mount pads to solder the switch rather than having to bend the wires to go through holes.


With my next version I will be trying out the PCB assembly service from JLCPCB so that I can utilise smaller SMD components and not have to solder them together myself!!

Some things I want to improve: -
  • Smaller size
  • Longer battery life and/or smaller battery
  • Add voltage divider to monitor actual battery voltage
  • Have a dual supply solution to use 1.5V AA cell with booster or single 3.6V Lipo cell without booster
  • Learn about ESP-NOW
There are plenty of clever people out there on YouTube with some great ideas around home automation and using the ESP8266 and ESP32 so time to watch some videos.

Gerbers, STL files etc. available from here https://1drv.ms/u/s!AkqVSEM4Y7kDhuZ0Lg1CdJrBs745Ug?e=ACkTkJ

The code for the device is available from MakerMeik's GIThub page, watch his video.

Version 2 coming soon .......




Saturday, 11 March 2017

Hot water Remote Control using a Wemos D1 .... then a Raspberry Pi Zero W

For quite a while now I've been meaning to put together a project to control the Hot water heating remotely from my phone, I all ready have a 2nd generation Nest to control the heating but don't fancy spending the money for the 3rd generation just to add the water heating function.
I started with the plan to use a Wemos D1 Mini with a couple of the plug-in modules like a relay module, processor module and a button module all placed on the dual base PCB.


Button Module

Processor Module
Relay Module


Dual footprint Motherboard
For those of you not familiar with the Wemos D1 you have to be aware of which pins the different modules use because the relay module uses D1 which also happens to be used for one the I2C pins so if you wanted to use the I2C OLED display module it would clash.  I decided to re-wire my relay pin to use D6 (GPIO12) as this is the default pin that the Sonoff / arendst software uses for the relay anyway.

Once the parts were assembled and the software flashed (the button module happens to be the flash button as well)

The idea was to load some software written by a chap called arendst on Github this software allows you to control a relay via MQTT, usually used in WiFi controlled power sockets found at ITEAD but there's no reason why it can't be used on any ESP8266 to drive a relay and more importantly read a connected temperature sensor like a DHT22 or DS18B20.

I planned to control this from my Raspberry Pi 3 running Node Red, I would design a control layout using the Dashboard module, which serves up a webpage I can view on my phone/PC/tablet etc. inside my WiFi network.
Work in progress Dashboard UI in Node Red

 but could also be controlled outside my network using an MQTT app on my phone via the Cloudmqtt service using 4G data.

I also got hold of an Amazon DASH Button which I've configured using a node "node-dash-button", I've configured it so that pressing this button gives a 30 min boost to the Hot Water.

I tried several MQTT apps and found the one I liked the most was MQTT dash it let me create quite a nice interface.
Sample layout for MQTT Dash
Another one I liked was MQTT Dashboard because this has widgets that you can have on you phone desktop to switch something on or off  without opening the app first (Android only, I think).

So all was going well and starting to work when I saw that the lovely people that make the Raspberry Pis had just launched a new version of the Pi Zero, this time with WiFi and Bluetooth included and it was just under £10 .....Whoa! .... hold on a minute! under a tenner! .... I'm quite prepared to use a Pi Zero W instead of a Wemos D1 mini and shut it in the airing cupboard controlling the hot water.

The Pi Zero isn't very fast but I was certain it was plenty fast enough to control one relay and read the temperature using a DS18B20.

So I changed plans at this point a turned toward the Pi Zero W, if you want to use an ESP8266 then go over to Github and use arendst's code (I use it on 3 Sonoffs) and make it happen.

I will put together a blog entry for the Pi Zero W in a few days, I'm just sorting out the housing and wiring to put it in the airing cupboard.

As usual if there are any questions just ask them in the comments and I'll do my best to answer them.



Monday, 21 March 2016

View Live data on your phone with Node-Red and Thingstud.io

Some time ago I came across a blog run by Pete Scargill (tech.scargill.net) which dealt with many tech gadgets to do with IOT and home automation.

There were a couple of subjects I found very interesting, Node Red and MQTT, these seemed to very useful tools to help develop home automation solutions.

Node Red turned out to be pivotal in my ability to glue everything together, there are so many different nodes that can be used to link into different services.

I'm running Node Red on my Raspberry Pi but it can also be installed on your PC and there is also a hosted installation of Node Red found here that allows to to try out an instance of node red without installing it on any platform at all, it runs in your browser and there is free account option.

The FRED Node Red runs even when your PC is switched off, there are some limitations around hardware inputs that would normally come from your PC like serial data and data from your OS, also you cannot install any of your own nodes.

Another important aspect of home automation was remote access to the data, whilst you are in your home on your own network this is not too difficult but when you are out and about and want to know what you hot water temperature is or if your central heating is on then it's a bit trickier, unless you expose your network to the outside world that is.

This is where cloud hosted MQTT servers came in for me, they allow me to send and receive data to and from the outside world easily and securely (at least I hope it's secure).

So to learn how to use the tools I set a goal to measure the hot water temperature in my storage tank and transmit an MQTT topic via WiFi and using Node Red to receive it and process it, if required before displaying it on my phone using Thingstud.io, the next step will be to have a button in the app to switch the hot water heating on/off manually and/or automatically switch it on from Node Red.

Step1 - Measure the hot water temp and send via an MQTT topic

For this I used and ESP-12 mounted on a development board I bought from AliExpress
There are also other boards that can be used
WeMos D1 Mini
Witty Cloud
The Fritzing circuit looks like this

In the Fritzing diagram above I've shown a battery for the supply but I actually use a 5V mains USB supply plugged into the USB connector and the DS18B20 Temperature sensor I used is the probe version so that the sensing part can be fed through the tank's insulation to touch the copper tank near to the top.

Before writing the sketch for the ESP-12 I tried out a couple of different MQTT providers: -
  • Mosquitto installed on my raspberry Pi
  • CloudMQTT.com private broker with password access.
  • HIVEMQ - free public access MQTT broker (no password)
I settled on Cloudmqtt using a free account but found that the app I created in Thingstud.io wouldn't work on my Android phone, I got in touch with the thingstud.io support and they were very helpful and said it worked on their Android phone and offered to help debug my phone to find out why but I didn't really want to spend the time trying to fix it so I tried the public server version of HIVEMQ and that worked great.

If you are new to MQTT then HIVEMQ have some really good introduction documents here that explain everything.

Before using the Arduino IDE to program your ESP12 you will need to add the details to your boards manager, details can be found here.

Then you can program your ESP with this sketch, don't forget to edit the SSID and passwords sections in the code to suit your network and mqtt broker.

This sketch uses the Cloudmqtt broker.

/*
 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 minutes
  - 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
- Read the temperature of a single DS18B20 device and publish it to the MQTT broker
- Read the battery supply voltage and publish it to the MQTT broker
 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.
 After publishing the data it will then go to sleep for nnn seconds  (set nnn in the int interval)
 For the ESP-12 to wake up after the nnn interval you will need to connect D0 to the reset pin
 or if supplied from a mains adapter you can remove the sleep command altogether.
 I have also commented out the code that switches on the LED.
 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 "onewire.h"
#include "dallastemperature.h"
#include "pubsubclient.h"

// Update these with values suitable for your network.
const char* ssid = "#######";
const char* password = "#######";
const char* mqtt_server = "m20.cloudmqtt.com";
const char* mqttUser = "#######";
const char* mqttPass = "#######";
float temp; //store temperature value
float BattLevel; //Store Battery voltage
int interval = 120;//Interval between messages in seconds
char msg[50];

ADC_MODE(ADC_VCC);//Set the ADCC to read supply voltage.

WiFiClient espClient;
PubSubClient client(espClient);
#define ONE_WIRE_BUS 2  // DS18B20 pin (D4)
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);

void setup() {
  Serial.begin(115200);
  setup_wifi();
  delay(5000);
  client.setServer(mqtt_server, 10388);
  client.setCallback(callback);
}

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(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(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",mqttUser, mqttPass)) {
      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 loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  //Read Temperature from DS18B20
  DS18B20.requestTemperatures();
  temp = DS18B20.getTempCByIndex(0);
  //Read supply voltage
   BattLevel= ESP.getVcc();
  /*Print Temp and Battery level [optional]
  Serial.print("Temperature: ");
  Serial.println(temp);
  Serial.print("Supply Voltage: ");
  Serial.println(BattLevel);  
  delay(200);
  */
  //Publish Data to MQTT
  dtostrf(temp, 4, 2, msg); //Convert float to string
  client.publish("Temperatures/HotWater/Temp", msg, true);
  Serial.print("Published message Temp: ");
  Serial.println(msg);
  dtostrf(BattLevel, 3, 1, msg); //Convert float to string
  client.publish("Temperatures/HotWater/Battery", msg, true);
  Serial.print("Published message Batt Level: ");
  Serial.println(msg);

  //Disconnect from WiFi before sleep
  client.disconnect();
  WiFi.disconnect();
  delay(100);

  Serial.print("Entering deep sleep for ");
  Serial.print(interval/60);
  Serial.println(" Minutes");
  ESP.deepSleep(interval * 1000000, WAKE_RF_DEFAULT);
  //ESP.deepSleep(10 * 1000, WAKE_NO_RFCAL);
  delay(500);   // wait for deep sleep to happen
}
To test the sketch you can use the Websocket UI in the Control panel
In the next part I'll go through setting up Thingstud.io to display the data on your phone.

New Accessories for the AirChrony S3

With the increasing abilities of AI it has become so much easier to take an idea to a finished, working item in a really short time. I was t...

Popular Posts