35
6th March 2017| Raspberry Pi Meetup # 3 DIY IoT

6th March 2017| Raspberry Pi Meetup # 3blog.bennymichielsen.be/.../2017/03/RaspberryPiMeetup.pdf · 2017-03-13 · • Ways to capture usage: • Eye on the dials? • Mechanical

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 6th March 2017| Raspberry Pi Meetup # 3blog.bennymichielsen.be/.../2017/03/RaspberryPiMeetup.pdf · 2017-03-13 · • Ways to capture usage: • Eye on the dials? • Mechanical

6th March 2017| Raspberry Pi Meetup # 3

DIY IoT

Page 2: 6th March 2017| Raspberry Pi Meetup # 3blog.bennymichielsen.be/.../2017/03/RaspberryPiMeetup.pdf · 2017-03-13 · • Ways to capture usage: • Eye on the dials? • Mechanical

IT ConsultantMCT

Geek Aficionado

Less talk more codeProgramming, Motherfucker

Benny [email protected]

@bennymichielsen

Page 3: 6th March 2017| Raspberry Pi Meetup # 3blog.bennymichielsen.be/.../2017/03/RaspberryPiMeetup.pdf · 2017-03-13 · • Ways to capture usage: • Eye on the dials? • Mechanical

• Intro

• Monitoring

• Water

• Environment

• Gas

• Things to come

Info Support | Solid Innovator

Agenda

Page 4: 6th March 2017| Raspberry Pi Meetup # 3blog.bennymichielsen.be/.../2017/03/RaspberryPiMeetup.pdf · 2017-03-13 · • Ways to capture usage: • Eye on the dials? • Mechanical

Info Support | Solid Innovator

Tick Tack

Page 5: 6th March 2017| Raspberry Pi Meetup # 3blog.bennymichielsen.be/.../2017/03/RaspberryPiMeetup.pdf · 2017-03-13 · • Ways to capture usage: • Eye on the dials? • Mechanical

Info Support | Solid Innovator

5 Tick Tack

Page 6: 6th March 2017| Raspberry Pi Meetup # 3blog.bennymichielsen.be/.../2017/03/RaspberryPiMeetup.pdf · 2017-03-13 · • Ways to capture usage: • Eye on the dials? • Mechanical

Info Support | Solid Innovator

6

Page 7: 6th March 2017| Raspberry Pi Meetup # 3blog.bennymichielsen.be/.../2017/03/RaspberryPiMeetup.pdf · 2017-03-13 · • Ways to capture usage: • Eye on the dials? • Mechanical

Info Support | Solid Innovator

7

Page 8: 6th March 2017| Raspberry Pi Meetup # 3blog.bennymichielsen.be/.../2017/03/RaspberryPiMeetup.pdf · 2017-03-13 · • Ways to capture usage: • Eye on the dials? • Mechanical

Info Support | Solid Innovator

8

Page 9: 6th March 2017| Raspberry Pi Meetup # 3blog.bennymichielsen.be/.../2017/03/RaspberryPiMeetup.pdf · 2017-03-13 · • Ways to capture usage: • Eye on the dials? • Mechanical

Info Support | Solid Innovator

9 Tick

Page 10: 6th March 2017| Raspberry Pi Meetup # 3blog.bennymichielsen.be/.../2017/03/RaspberryPiMeetup.pdf · 2017-03-13 · • Ways to capture usage: • Eye on the dials? • Mechanical

Info Support | Solid Innovator

10 Tick Tack

Page 11: 6th March 2017| Raspberry Pi Meetup # 3blog.bennymichielsen.be/.../2017/03/RaspberryPiMeetup.pdf · 2017-03-13 · • Ways to capture usage: • Eye on the dials? • Mechanical

• Through official instances:

• What’s needed:

• Monthly fee data logger: € 38,68

• Might also need:

• Water monitoring yearly fee: € 123,7

• Pulsecontact yearly fee: € 54,65

• Add my own watermeter behind the official one

• Prices vary between € 20 - € 100

• Change in infrastructure

Info Support | Solid Innovator

11

Replace water meter with something smart

Tick Tack

Page 12: 6th March 2017| Raspberry Pi Meetup # 3blog.bennymichielsen.be/.../2017/03/RaspberryPiMeetup.pdf · 2017-03-13 · • Ways to capture usage: • Eye on the dials? • Mechanical

• Ways to capture usage:

• Eye on the dials?

• Mechanical Turk?

• How do the dials move ?

• Water flows through chambers which cause a disk to turn

• As the disk moves around, a magnet connects to the register

• The magnetic pulse moves the dials

• How can magnetic pulses be captured?

• Hall effect sensor

• Reed switch (€ 2,5)

Info Support | Solid Innovator

12

Capture water usage

Tick Tack

Page 13: 6th March 2017| Raspberry Pi Meetup # 3blog.bennymichielsen.be/.../2017/03/RaspberryPiMeetup.pdf · 2017-03-13 · • Ways to capture usage: • Eye on the dials? • Mechanical

• Simple Schema

• 5V => Reed Switch

• Magnetic field closes the switch (NO)

• GPIO17 turns HIGH

• The resistor was quite a mystery

• Digital pin very sensitive

• Noise from:

• Nearby circuits

• Cables

• Finger

Info Support | Solid Innovator

13

Schematics

Tick Tack

Page 14: 6th March 2017| Raspberry Pi Meetup # 3blog.bennymichielsen.be/.../2017/03/RaspberryPiMeetup.pdf · 2017-03-13 · • Ways to capture usage: • Eye on the dials? • Mechanical

• Mono

• http://www.mono-project.com/

• .NET

• Open Source

• Cross Platform

• Raspberry Sharp IO

• https://github.com/raspberry-sharp/raspberry-sharp-io

• Interact with GPIO

Info Support | Solid Innovator

14

Programming!

Page 15: 6th March 2017| Raspberry Pi Meetup # 3blog.bennymichielsen.be/.../2017/03/RaspberryPiMeetup.pdf · 2017-03-13 · • Ways to capture usage: • Eye on the dials? • Mechanical

Info Support | Solid Innovator

15

Page 16: 6th March 2017| Raspberry Pi Meetup # 3blog.bennymichielsen.be/.../2017/03/RaspberryPiMeetup.pdf · 2017-03-13 · • Ways to capture usage: • Eye on the dials? • Mechanical

Info Support | Solid Innovator

16

var pin2Sensor = ConnectorPin.P1Pin11.Input();

GpioConnection connection = new GpioConnection(pin2Sensor);

connection.PinStatusChanged +=

(sender, statusArgs) =>

Console.WriteLine("Pin changed", statusArgs.Configuration.Name);

Page 17: 6th March 2017| Raspberry Pi Meetup # 3blog.bennymichielsen.be/.../2017/03/RaspberryPiMeetup.pdf · 2017-03-13 · • Ways to capture usage: • Eye on the dials? • Mechanical

Info Support | Solid Innovator

17

pi@raspberrypi:~/ticktack $ sudo mono TickTackConsole.exe

Missing method .ctor in assembly /home/pi/ticktack/Raspberry.IO.GeneralPurpose.dll, type

System.Runtime.CompilerServices.ExtensionAttribute Can't find custom attr constructor image:

/home/pi/ticktack/Raspberry.IO.GeneralPurpose.dll mtoken: 0x0a000014 * Assertion at class.c:5597, condition

`!mono_loader_get_last_error ()' not met

Stacktrace:

Native stacktrace:

Debug info from gdb:

[New LWP 1965]

[Thread debugging using libthread_db enabled]

Using host libthread_db library "/lib/arm-linux-gnueabihf/libthread_db.so.1".

0x76e67ee8 in __libc_waitpid (Cannot access memory at address 0x1 pid=1966, stat_loc=0x7e904960, options=0) at

../sysdeps/unix/sysv/linux/waitpid.c:40 40 ../sysdeps/unix/sysv/linux/waitpid.c: No such file or directory. Id Target Id

Frame 2 Thread 0x769f3430 (LWP 1965) "mono" 0x76e65a40 in do_futex_wait (isem=isem@entry=0x3181a4) at

../nptl/sysdeps/unix/sysv/linux/sem_wait.c:48 * 1 Thread 0x76f5e000 (LWP 1961) "mono" 0x76e67ee8 in __libc_waitpid (Cannot

access memory at address 0x1 pid=1966, stat_loc=0x7e904960, options=0) at

../sysdeps/unix/sysv/linux/waitpid.c:40 Thread 2 (Thread 0x769f3430 (LWP 1965)): #0 0x76e65a40 in do_futex_wait

(isem=isem@entry=0x3181a4) at ../nptl/sysdeps/unix/sysv/linux/sem_wait.c:48 #1 0x76e65af4 in __new_sem_wait (sem=0x3181a4) at

../nptl/sysdeps/unix/sysv/linux/sem_wait.c:69 #2 0x00219f98 in mono_sem_wait () #3 0x0019091c in ?? () Backtrace stopped:

previous frame identical to this frame (corrupt stack?) Thread 1 (Thread 0x76f5e000 (LWP 1961)): Cannot access memory at

address 0x1 #0 0x76e67ee8 in __libc_waitpid (pid=1966, stat_loc=0x7e904960, options=0) at

../sysdeps/unix/sysv/linux/waitpid.c:40 #1 0x000c0ba4 in ?? ()

Cannot access memory at address 0x1 Backtrace stopped: previous frame identical to this frame (corrupt stack?)

=================================================================

Got a SIGABRT while executing native code. This usually indicates a fatal error in the mono runtime or one of the native

libraries used by your application.

=================================================================

Page 18: 6th March 2017| Raspberry Pi Meetup # 3blog.bennymichielsen.be/.../2017/03/RaspberryPiMeetup.pdf · 2017-03-13 · • Ways to capture usage: • Eye on the dials? • Mechanical

Info Support | Solid Innovator

18

Page 19: 6th March 2017| Raspberry Pi Meetup # 3blog.bennymichielsen.be/.../2017/03/RaspberryPiMeetup.pdf · 2017-03-13 · • Ways to capture usage: • Eye on the dials? • Mechanical

Info Support | Solid Innovator

19

sudo apt-get install libmono-system-core4.0-cil

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80

--recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF

echo "deb http://download.mono-project.com/repo/debian wheezy main" |

sudo tee /etc/apt/sources.list.d/mono-xamarin.list

sudo apt-get update

Page 20: 6th March 2017| Raspberry Pi Meetup # 3blog.bennymichielsen.be/.../2017/03/RaspberryPiMeetup.pdf · 2017-03-13 · • Ways to capture usage: • Eye on the dials? • Mechanical

Info Support | Solid Innovator

20

Page 21: 6th March 2017| Raspberry Pi Meetup # 3blog.bennymichielsen.be/.../2017/03/RaspberryPiMeetup.pdf · 2017-03-13 · • Ways to capture usage: • Eye on the dials? • Mechanical

Info Support | Solid Innovator

21

Oh look free database

Page 22: 6th March 2017| Raspberry Pi Meetup # 3blog.bennymichielsen.be/.../2017/03/RaspberryPiMeetup.pdf · 2017-03-13 · • Ways to capture usage: • Eye on the dials? • Mechanical

Info Support | Solid Innovator

22

public void Upload(Tick tick){

using (HttpClient client = new HttpClient()){

client.PostAsync(_apiEndpoint, new StringContent(JsonConvert.SerializeObject(tick), Encoding.UTF8,

"application/json")).Wait();}

}

System.TypeInitializationException:

The type initializer for 'Newtonsoft.Json.JsonWriter' threw an exception.

---> System.BadImageFormatException: Could not resolve field token 0x04000493

File name: 'Newtonsoft.Json'

at Newtonsoft.Json.JsonWriter.BuildStateArray ()

<0x76860c90 + 0x0009f> in <filename unknown>:0

Page 23: 6th March 2017| Raspberry Pi Meetup # 3blog.bennymichielsen.be/.../2017/03/RaspberryPiMeetup.pdf · 2017-03-13 · • Ways to capture usage: • Eye on the dials? • Mechanical

Info Support | Solid Innovator

23

Shower: 70L

Dishwasher: 30L

Page 24: 6th March 2017| Raspberry Pi Meetup # 3blog.bennymichielsen.be/.../2017/03/RaspberryPiMeetup.pdf · 2017-03-13 · • Ways to capture usage: • Eye on the dials? • Mechanical

• DHT22

Info Support | Solid Innovator

24

Tracking temperature and humidity

Page 25: 6th March 2017| Raspberry Pi Meetup # 3blog.bennymichielsen.be/.../2017/03/RaspberryPiMeetup.pdf · 2017-03-13 · • Ways to capture usage: • Eye on the dials? • Mechanical

Info Support | Solid Innovator

25

// Measure required by host : pull down then pull uppin.Write(false);HighResolutionTimer.Sleep(WakeupInterval);pin.Write(true);// Read acknowledgement from DHTpin.Wait(true, timeout);pin.Wait(false, timeout);// Read 40 bits output, or time-outvar cnt = 7;var idx = 0;for (var i = 0; i < 40; i++){

pin.Wait(true, timeout);var start = DateTime.UtcNow;pin.Wait(false, timeout);

// Determine whether bit is "1" or "0"if (DateTime.UtcNow - start > bitSetUptime)

data[idx] |= (byte)(1 << cnt);

if (cnt == 0){

idx++; // next bytecnt = 7; // restart at MSB

}else

cnt--;}

Temperature: 15°C / -28°C

Humidity: ~ 3000%

Page 26: 6th March 2017| Raspberry Pi Meetup # 3blog.bennymichielsen.be/.../2017/03/RaspberryPiMeetup.pdf · 2017-03-13 · • Ways to capture usage: • Eye on the dials? • Mechanical

• https://github.com/technion/lol_dht22

• C Library implementing the DHT22 interface

• Shell executable

• WiringPi

• GPIO Interface Library for the Raspberry Pi

• Wrappers for other languages available

• Mono & Pinvoke

• http://www.mono-project.com/docs/advanced/pinvoke/

Info Support | Solid Innovator

26

C

Page 27: 6th March 2017| Raspberry Pi Meetup # 3blog.bennymichielsen.be/.../2017/03/RaspberryPiMeetup.pdf · 2017-03-13 · • Ways to capture usage: • Eye on the dials? • Mechanical

for ( i=0; i< MAXTIMINGS; i++) {

counter = 0;

while (sizecvt(digitalRead(DHTPIN)) == laststate) {

counter++;

delayMicroseconds(1);

if (counter == 255) {

break;

}

}

laststate = sizecvt(digitalRead(DHTPIN));

if (counter == 255) break;

// ignore first 3 transitions

if ((i >= 4) && (i%2 == 0)) {

// shove each bit into the storage bytes

dht22_dat[j/8] <<= 1;

if (counter > 16)

dht22_dat[j/8] |= 1;

j++;

}

}

Info Support | Solid Innovator

27

Technion lol_dht22

Page 28: 6th March 2017| Raspberry Pi Meetup # 3blog.bennymichielsen.be/.../2017/03/RaspberryPiMeetup.pdf · 2017-03-13 · • Ways to capture usage: • Eye on the dials? • Mechanical

extern "C"

{

extern bool setup(int pin);

extern float getLastHumidity();

extern float getLastTemperature();

extern bool readValues();

}

Info Support | Solid Innovator

28

Page 29: 6th March 2017| Raspberry Pi Meetup # 3blog.bennymichielsen.be/.../2017/03/RaspberryPiMeetup.pdf · 2017-03-13 · • Ways to capture usage: • Eye on the dials? • Mechanical

class Dht22Wrapper

{

[DllImport("dht22.so", EntryPoint = "setup")]

public static extern bool Setup(int pin);

[DllImport("dht22.so", EntryPoint = "getLastHumidity")]

public static extern float GetLastHumidity();

[DllImport("dht22.so", EntryPoint = "getLastTemperature")]

public static extern float GetLastTemperature();

[DllImport("dht22.so", EntryPoint = "read_dht22_dat")]

public static extern bool ReadValues();

}

Info Support | Solid Innovator

29

Page 30: 6th March 2017| Raspberry Pi Meetup # 3blog.bennymichielsen.be/.../2017/03/RaspberryPiMeetup.pdf · 2017-03-13 · • Ways to capture usage: • Eye on the dials? • Mechanical

var dhtTimer = new System.Threading.Timer(state => {

int tryCount = 0;

bool readCompleted = false;

while (!(readCompleted = Dht22Wrapper.ReadValues()) && tryCount< 10)

{

tryCount++;

Thread.Sleep(2000);

}

if (readCompleted)

{

var temp = Dht22Wrapper.GetLastTemperature();

Console.WriteLine("Measured temperature: " + temp);

var humidity = Dht22Wrapper.GetLastHumidity();

Console.WriteLine("Measured humidity: " + humidity);

_monitor.SendTick(3, temp);

_monitor.SendTick(4, humidity);

}

_dhtTimer.Change(900000, Timeout.Infinite);

}, null, 900000, Timeout.Infinite);

Info Support | Solid Innovator

30

Page 31: 6th March 2017| Raspberry Pi Meetup # 3blog.bennymichielsen.be/.../2017/03/RaspberryPiMeetup.pdf · 2017-03-13 · • Ways to capture usage: • Eye on the dials? • Mechanical

Info Support | Solid Innovator

31

Page 32: 6th March 2017| Raspberry Pi Meetup # 3blog.bennymichielsen.be/.../2017/03/RaspberryPiMeetup.pdf · 2017-03-13 · • Ways to capture usage: • Eye on the dials? • Mechanical

• Same principle as the water meter

• Signal is harder to capture

• Arduino worked, Raspberry Pi not so much

• TODO: Capture with Wiring Pi

Info Support | Solid Innovator

32

Gas Meter Monitoring

Page 33: 6th March 2017| Raspberry Pi Meetup # 3blog.bennymichielsen.be/.../2017/03/RaspberryPiMeetup.pdf · 2017-03-13 · • Ways to capture usage: • Eye on the dials? • Mechanical

• Electricity generates a magnetic field

• Existing solutions

• Clamp around single current carrying wire

• Clamp measures the current

• OpenEnergyMonitor

• Needs to be installed inside your fuse box

• Which wires to connect?

• For more accurate measurements an active connection to a power outlet is needed.

• TODO: using an eye on the electricity meter

Info Support | Solid Innovator

33

Capture electricity usage

Tick Tack

Page 34: 6th March 2017| Raspberry Pi Meetup # 3blog.bennymichielsen.be/.../2017/03/RaspberryPiMeetup.pdf · 2017-03-13 · • Ways to capture usage: • Eye on the dials? • Mechanical

• Dashboard

• Permanent housing for the Pi

• Pi as hub for smaller boards

• Smart doorbell

• Light switching

Info Support | Solid Innovator

34

TODO list

Page 35: 6th March 2017| Raspberry Pi Meetup # 3blog.bennymichielsen.be/.../2017/03/RaspberryPiMeetup.pdf · 2017-03-13 · • Ways to capture usage: • Eye on the dials? • Mechanical

Questions?