Wednesday 4 November 2015

NodeMCU + DHT22 (ESP8266) wifi thermometer/humidity sensors pt1

Wifi temperature / humidity sensor for under £20

Overview

So I travel a lot, and want to know what's happening in the house whilst I am abroad.
I've researched and there are many solutions out there (like here or here), but mostly they are way, way over priced at over £150 EACH per unit.

I need at least 5 units, for the bathroom, kitchen, boiler room etc. so even doing it with a raspberry pi would have been pricey at around £40-50 per 'sensor' (using the DHT22 for readings), so an outlay of about £250.

What I have found is something even better. The ESP8266. This is a self contained wifi module with built in TCP/IP stack for around £3 and under the size of a 50pence piece. Whoa - cheap eh? Ok, so the issue here is that it can't do a 5v-3v logic shift nor can I be bothered to figure out the electronics to step it down and connect it to USB TTL etc.

So.... I researched a bit more and there is a board that combines the ESP8266 and the step down and the USB TTL and allows me to either publish data via a message queue or host a webserver! This is the NodeMCU

Fantastic little thing that does everything you need it to + can be powered from a micro USB connection.

Parts list

ok so the parts list (you may not need to purchase everything, as you may have some parts already)
1 x NodeMCU (Amazon or ebay) - Beware of the version you get tho! Check the PCB colour and size.
1 x DHT22 temperature/humidity sensor (Amazon or ebay)
A breadboard /protoboard to prototype stuff (pimoroni) - you'll solder it onto this.
A solderless breadboard to test things work (pimoroni)
Soldering iron
Micro USB cable for connecting to a PC
some cheap micro usb charger
Wires for testing (Jumper jerky makes it easier)
Wires for permanent installation (pimoroni)

Total cost of the above is about £30 for the first unit. Cost of the 2nd unit will be about £13, 3rd unit about £13 too, so for 5 units, it's about £16 each. Not bad

Circuit diagram

 

Dev software

ESP software - for loading up LUAs and code

Firmware

Download the firmware

Flashing firmware

config tab
select first slot
flash on tab one
ensure the MAC addresses show. If not, then something went wron.
A green tick at the end shows success.

Modules

DHT22 from here

Upload it with esplorer from above using the 'upload' button. Then compile by clicking reload on the middle right side, then a list of modules will appear. Right click and select compile.

Code

Set the wifi access point and set NIC to DHCP

print(wifi.sta.getip())
--nil
wifi.setmode(wifi.STATION)
wifi.sta.config("SSID","password")
print(wifi.sta.getip())
--192.168.18.110
 
View wifi SSIDs
function listap(t)
  for k,v in pairs(t) do 
    print(k.." : "..v) 
  end 
end 
wifi.sta.getap(listap)

onboard LED

Flash the blue LED near the USB connector
gpio.write(0,gpio.LOW)
gpio.write(0,gpio.HIGH)

GPIO LEDs

Flash an LED on GPIO pin 2
You can change the duty cycles by checking the docs http://www.nodemcu.com/docs/
 pwm.setup(2,1,10)
pwm.start(2)

Reading temperature/humidity

PIN=4
dht22 = require("dht22")
dht22.read(PIN)
t = dht22.getTemperature()
h = dht22.getHumidity()
humi=(h/10).."."..(h%10)
temp=(t/10).."."..(t%10)
print("Humidity:    "..humi.." %")
print("Temperature: "..temp.." deg C")
dht22 = nil
package.loaded["dht22"]=nil

 
All together:
print(wifi.sta.getip())
gpio.mode(2,gpio.OUTPUT)
--gpio.write(2,gpio.HIGH)
--gpio.write(2,gpio.LOW)
--gpio.write(1,gpio.LOW)
gpio.write(0,gpio.LOW)
BLUE=1 -- blue LED is on GPIO 1
pwm.stop(2)
pwm.stop(1)

pwm.setup(2,1,10)
pwm.start(2)

gpio.write(0,gpio.HIGH)


  sv=net.createServer(net.TCP, 2) 
  sv:listen(80,function(c)
      c:on("receive", function(c, pl)
         print(pl) 
      end)
        gpio.mode(BLUE,gpio.OUTPUT)
        gpio.write(BLUE,gpio.HIGH)
        PIN=4
        dht22 = require("dht22")
        dht22.read(PIN)
        t = dht22.getTemperature()
        h = dht22.getHumidity()
        humi=(h/10).."."..(h%10)
        temp=(t/10).."."..(t%10)
        print("Humidity:    "..humi.." %")
        print("Temperature: "..temp.." deg C")
        dht22 = nil
        package.loaded["dht22"]=nil
        c:send("H:"..humi.." ; T:"..temp.."\r\n")
        c:close()
        gpio.write(BLUE,gpio.LOW)
       end)
Note the above server doesn't respond to normal HTTP requests.  it responds with a simple:
H:xx ; T: xx
and disconnects after the initial connect.
You can use either curl or netcat to get the data.
It also flashes the blue LED whilst a network operation is in effect.
The white LED I've used blinks every second, to show it is working.

Dashboard

RRDtool - Create the RRD

This creates a RRD for 5 sensors, values 0 to 100 and 524160 data points (60*24*7*52) so 1 data point a minute
<?
    echo "Creates blank temp graphs";
    $options = array (
     "--step", "60",            // Use a step-size of 1 minute
     "--start", "-1 day",         "DS:hum1:GAUGE:100:0:100",
     "DS:hum2:GAUGE:100:0:100",
     "DS:hum3:GAUGE:100:0:100",
     "DS:hum4:GAUGE:100:0:100",
     "DS:hum5:GAUGE:100:0:100",
     "RRA:AVERAGE:0.3:1:524160",
    );


    $ret = rrd_create("./hum.rrd", $options);
    if (! $ret) {
     echo "<b>Creation error: </b>".rrd_error()."\n";
    }
?>

In part two, I'll show how to render the graphs

Part 2 here



Monday 21 September 2015

Windows 10 search cannot find any applications

So since installing Windows 10, the search has been working fine.
Now today, When using search to find *any* application it doesn't find shit... seriously, it doesn't registere there are any applications.

Even though indexing is turned on for all the relevant places like c:\program files, c:\users, start menu it doesn't work.

The only thing that does work is a re-installation of Cortana

Firstly, open an elevated powershell prompt (see here if you don't know how) and run the following
Get-AppXPackage -Name Microsoft.Windows.Cortana | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register "$($_.InstallLocation)\AppXManifest.xml"}

After that it instantly works.... very annoying or what?
 

Sunday 23 August 2015

Linux dropping to shell on initramfs on ubunu 12.04 with mdadm

So my NAS HDD packed up and I haven't puppet-fied or chefy-fied any of the config so a rebuild from scratch is required....

During this rebuild I got an interesting error which basically dumps me to a root shell as initramfs can't build my software raid properly. I have a SSD as /dev/sda and the raid disks on /dev/sd[b-f]

I know this is BS cos I can mount the drives after typing exit on the prompt.
Even after dumping to a prompt (initramfs) typing:
cat /proc/mdstat
Personalities : [raid6] [raid5] [raid4] [linear] [multipath] [raid0] [raid1] [raid10]
md127 : active raid5 sdf1[5] sde1[3] sdc1[1] sdd1[2] sdb1[0]
      15627540480 blocks super 1.2 level 5, 512k chunk, algorithm 2 [5/5] [UUUUU]
     
unused devices: <none>
 
Initially I got this message:
 Which seemed like the drives didn't spin up in time for mdadm to assemble the RAID
I made many changes (grub changes, initramfs changes etc),but the only thing that worked was to change the scripts on initramfs.

When initramfs starts up it pulls in the mdadm.conf from your system and tried to run it. Due to a bug in mdadm, it doesn't assemble the RAID when using the UUID (blkid). This seems to be a bug and there is an issue raised in bugzilla.

Removing mdadm from the system wasn't an option, cos well, you need to assemble the RAID, right?
Other options about changing the mdadm options in initramfs didn't work for me either, nor setting the /etc/initramfs-tools/scripts/local-top/ for a sleep and stopping the array.

The thing that worked for me was  deleting the mdadm file in 
/usr/share/initramfs-tools/scripts/local-premount

then update-initramfs -u -v
 
BAM!
done








Saturday 1 August 2015

Using fibre optics within your house pt1

Installing fibre optics in your house pt1

Current setup

So my setup is a small, a 24U 19" rack (which in hindsight, I should have bought bigger), 3 x 5GHz APs, 4 x 8 port gig switches, 2 x HP Procurve 1810-24G (with 2 SFP ports). 2 x firewalls (own build), 1 UPS, 3 x HP microservers and 2 x Thecus 5200Ns

I am planning to convert the loft and also build out a office in the garden. Plans for the office garden are below for a 8m x 4m with a 1.5m x 4m comms room on the left side, and shed storage on the right side.

To cater for this, expansion, I have recently bought another HP Procurve 1810-24G with 2 SFP and also a  MikroTik Cloud Router CRS112-8G-4S-I. This will enable me to
  • Trunk and bond (Trunk and port channel in Cisco parlance) the 2 x HP Procurves
  • Give me dual fibre to the loft
  • Give me dual fibre to the office in the garden

HP Procurve setup

First of all, I am running VLANs on my switch, so I will need to trunk the bonded channels.
I have an external, DMZ and internal VLAN amongst others.
 
First of all under the HP web i/f go to Trunks -> Trunk Configuration
Create a new Trunk named 'switch'
Trunks -> Trunking membership and add the ports you need to trunk. I will be trunking at least 4 x 1GB ports.
 Then under VLANS -> Participation/Tagging,  and tag the other VLANs (external/DMZ/local) in there.

I have also colour coded this all as well so it makes sense say a year down the line when I try to troubleshoot it.
Red - External
Orange - DMZ
Blue - Servers (trunked)
Green - General ports
Yellow- APs
Pink - switch trunking.
The inter-switch trunk also has all the VLANs tagged on it so that VLAN traffic can be passed in between each switch


 

 

 

Mikrotik Cloud Router setup

So with this, there are 8x1GB RJ45s and 4 x SFPs!! I will configure this so that the garden office will have 2x SFP incoming and 2 x SFP to a server. The 8 x 1GB will be bridged, and that will enable the switch to act as a switch across all the bonding interfaces and standard interfaces too.
To set this up: 
  • Under Interfaces -> sfp9-slave-local to sfp12-slave-local, set master port = none
  • Under Interfaces -> Bonding -> Add new interface, name = bonding.SFP1. Slaves = SFP9 and SFP10
  • Under Bridge -> Add new bridge, name = Bridge gig2SFP, protocol RSTP
  • Under Interfaces -> VLAN 123 , interface = Bridge gig2SFP
  • Under Interfaces -> VLAN 321, interface = Bridge gig2SFP

Bridge ports

  • Set mode = 802.3ad, link monitoring =ARP, ARP IP target = 192.168.254 (your gateway). The reason for setting ARP rather than mii, is because if one of the link goes down, it may not actually realise this and still try to send packets down the failed link e.g. SFP9. with ARP, it sends out packets every 100ms to determine whether a specific link is down in the bond.
  • Create  new Bond name = bonding.SFP2. Sales = SFP11, and SFP12
  • Set the same settings as bonding.SFP1
Eventually you should have something like this:


This probably isn't correct, but I haven't bothered to read the manual and the terminology is different, but it works.

Fibre to use

I am using LC interface GBICS, and also fibre which is multimode, OM2, 50/125 (good explanation here on the numbers). The numbers basically indicate the size of the fibre itself. The 50/125 gives a higher bandwidth than 62.5/125 fibre and is more common (i.e. cheaper) so that's why I went with that.
OM1 to OM3 classifications give you basically the bandwidth rating. So OM1 is 10Mb -> 1Gb, to OM3 from 10Mb -> 10Gb. See the wikipedia article for more detail
Singlemode fibre gives you longer distances in the range of kilometers, but is way more expensive than multimode, which has an approx max run of about 550m.

For the Mikrotik, I am using 1.25G GBICs, and for the HP I am using 4G and both are 850nm (wavelength)

Conduit

You obviously need to protect the fibre running around, as it's quite fragile. Normally fibre is run in a orange conduit, and that can be a little pricey, but you can get some from ebay. I've used the non-split flexible conduit such as this previously to run fibre (TOSlink cable) for audio inside a floating wall installation.
 
Bear in mind, you can't run fibre at 90 degree angles! There is a minimum distance they can bend.


Thursday 18 June 2015

Accounting for small businesses - Freeagent discount/referral code 44lzibg1

Part 1
So I've decided to start contracting, as it's more lucrative.

The best package I've found for accounting is a cloud based one called Freeagent. If you use this referral code (
  link:



Sunday 10 May 2015

Bonding NICs on linux

You've got a managed switch and want two gig NICs hooked up to give you twice the bandwidth. How do you do this on ubuntu?


Easy...

    apt-get install ifenslave-2.6

vi /etc/network/interfaces

auto eth1
iface eth1 inet manual
bond-master bond0
auto eth2
iface eth2 inet manual
bond-master bond0
# The primary network interface
auto bond0
iface bond0 inet static
address 192.168.2.247
gateway 192.168.2.254
netmask 255.255.253.0
  bond-mode 4
bond-miimon 100
bond-lacp-rate 1
bond-primary eth1 eth2
dns-nameservers 8.8.8.8
dns-search corp.philspencer.org



check with ip link

cat /proc/net/bonding/bond0


bond mode type 4 requires a managed switch (I have a HP Procure 1810) and have enabled trunking on both ports of the switch where my NAS is connected to

finally ifenslave bond0 eth1 eth2