Friday 17 November 2017

Motion detection with raspi and MQTT

I built a simple surveillance system for my hallway to monitor my cat during holiday. Used a raspberry 2 with a cheap china camera, probably the cheapest way to get a good camera with good possibilities.
I followed the guide to install mjpg-streamer at https://github.com/jacksonliam/mjpg-streamer
and installed motion with : apt-get install motion , verify that motion is working with browsing to IP:8080 and 8081


Mqtt Scripts


I made two bash scripts, one for mqtt switch on and one for off. This could probably be done smarter but it works.


/home/user/motion/mqtt/MotionOff.sh
#!/bin/sh
mosquitto_pub -t /dev/hallwaymotion -m 0

/home/user/motion/mqtt/MotionOn.sh
#!/bin/sh
mosquitto_pub -t /dev/hallwaymotion -m 1

In my case I run the mqtt server on the same server as motion. You might change the mosquitto command a bit.

 

Motion config file


The configfile for motion is located at /etc/motion/motion.conf

In my case I changed the added the following lines:

daemon on
netcam_url http://192.168.1.114:8080/?action=stream
netcam_keepalive on
webcontrol_localhost off
stream_localhost off
on_event_start /home/user/motion/mqttMotionOn.sh
on_event_end /home/user/motion/mqttMotionOff.sh



remember to remove any videodevice lines

Homeassistant 


sensor:
  platform: mqtt
    state_topic: "/dev/hallwaymotion"
    name: "kitchenpresence"
camera:
  - platform: generic
    name: kjokken
    still_image_url: http://192.168.1.114:8080/?action=snapshot

Note: I use the remote camera instead on the local motion stream because i want a better resolution in HomeAssistant than the one motion created





 

Sunday 29 October 2017

A money making smart heater!


Intro and TL;DR

I got 3 low energy desktop computers and got them mining Verium. These computer now makes both money in the form of Verium and saves me money on heat. Not much but some. On hot days my office gets too hot with all of them mining so I'll automated them to shutdown and restart based on electricity price, the weather forecast and temperature sensor.

Hardware

Toss out everything not needed like extra graphics cards, DVD rom, remove hdd leds etc. removing all these components will save you some watts. In bios disable everything not needed like serial ports, audio etc. Use 2,5" HDD instead of 3,5". If on board graphics card has a ram selection enabled choose the lowest possible option.
I started with a simple Debian installation with only ssh server.

Miners


First do a : sudo apt-get git

The easy way is to use docker you find a great guide at https://github.com/Roykk/veriumMinerDocker.

If you want to go the "hard" route you will find the miner at https://github.com/effectsToCause/veriumMiner
For profit calculation use this google doc : Profitability Calculator Computer Hash/Watt estimate

Setting up WOL(Wake on Lan)

This was the time consuming part. first check that wake up over lan is enabled. The name varies from bios to bios. The most common term is wake on pci. Enable it.

sudo apt-get install ethtool net-tools
(
net-tools isnt nessecay unless you need to debug or a skilled with other commands)


First check that WOL are enabled
Run the command sudo ethtool eth0

Supports Wake-on: pumbg
Wake-on: g

To make the changes persistent we must change the /etc/network/interfaces file
and "add ethernet-wol g" to the network card. So it should look something like this:

auto em1
iface em1 inet dhcp
        ethernet-wol g


 If you get a g on wake-on is what we should look for. if other value check https://wiki.debian.org/WakeOnLan
Now we need the mac adress on the network card

ifconfig -a|grep ether

We need the mac address and ip for the next steps.
On the homeassistant you you can install wol with apt-get install wakeonlan
to test wol on the miners shut them down a do wakeonlan <<MACADRESS>> if they start up you are good to go.

Homeassistant

sudo apt-get install pm-tools
Follow the guide at https://home-assistant.io/components/switch.wake_on_lan/

The guide will use pm-suspend. The only thing to change if you want to use shutdown is change the command in the shell_command and change ass ALL=NOPASSWD:/usr/sbin/pm-suspend to hass ALL=NOPASSWD:/sbin/shutdown in step 7 in the
Suspending Linux exsample Note: some computer don't work with the command shutdown if that's the case use pm-suspend
I THINK shutdown will save you some power consume so i used it on two of my machines.

Switches:
- platform: wake_on_lan
  name: deb-veriton
  host: <>
  mac_address: '44:87:fc:6a:dd:7b'
  turn_off:
    service: shell_command.turnoffdebveriton
- platform: wake_on_lan
  name: debthink
  host: <>
  mac_address: '8c:89:a5:22:73:c3'
  turn_off:
    service: shell_command.turnoffdebthink
- platform: wake_on_lan
  name: debdesk
  host: <>
  mac_address: '10:78:d2:e5:9e:aa'
  turn_off:
    service: shell_command.turnoffdebdesk

Shell Commands:


shell_command:
  turnoffdebveriton: 'ssh hass@<<IP>> sudo shutdown'
  turnoffdebthink: 'ssh homeassistant@<<IP>> sudo shutdown'
  turnoffdebdesk: 'ssh homeassistant@<<IP>> sudo pm-suspend' 

Automation



Now you should have a wol switch for each computer. so we can start automating.



In my case first I want to stop mining if my electricity cost is above 0.4 NOK/kWh i get the price from the Tibber sensor component.
- alias: 'Disable Mining When High Price'
  trigger:
    - platform: numeric_state
      entity_id: sensor.electricity_price_yri_14
      above: 0.4
  action:
  - service: homeassistant.turn_off
    entity_id: switch.debveriton
  - service: homeassistant.turn_off
    entity_id: switch.debthink
  - service: light.turn_off
    entity_id: homeassistant.turn_off
Use miners to heat the room
- alias: 'Enable all miners '
  trigger:
    - platform: numeric_state
      entity_id: sensor.fibaro_system_fgsd002_smoke_sensor_temperature
      below: 20
  action:
  - service: homeassistant.turn_on
    entity_id: switch.debveriton
  - service: homeassistant.turn_on
    entity_id: switch.debthink
  - service: light.turn_off
    entity_id: homeassistant.turn_on
Turn off all miners due to heat.
- alias: 'disable all miners because of heat'
  trigger:
    - platform: numeric_state
      entity_id: sensor.fibaro_system_fgsd002_smoke_sensor_temperature
      above: 32
  action:
  - service: homeassistant.turn_off
    entity_id: switch.debveriton
  - service: homeassistant.turn_off
    entity_id: switch.debthink
  - service: light.turn_off
    entity_id: homeassistant.turn_off

Note: In my example my smoke sensor with temperature sensor if located behind the computers so temperature is much higher than the rest of the room


Saturday 23 September 2017

Windows 10 Mqtt - New Features and source

After a few good days of working on this project I've made some big changes and released the full source. The source code are available at https://github.com/KjetilSv/Win10As/src and the last installer download at https://github.com/KjetilSv/Win10As/raw/master/installer.zip

The documentation at https://github.com/KjetilSv/Win10As will continue to work on this and add real life example and uses.

Some of the new features since the last blog post:

App/running
If you send a message like mosquitto_pub -t KJETILPC/app/running -m firefox it will reply with "KJETILPC/app/running/firefox -m 1" if running and -m 0 if it's not running.
Starting a application I've desired to add as a custom command instead of making a "open application" listener because of potential security problems.


Disk and battery sensors








Now it will be easy to monitor a Windows Nas :)

Selection of TTS Speaker







Not the most important thing but i would like a woman talking and not a man so I implemented a chooser instead of just using the default. Speaker as part of the MQTT string may be implemented in later versions.

Monitor

Instead of computer power option, I've tested some ways to turn the monitor on and off. But haven't been able to find a solution to work on all computer, some work and some don't.
If you want to test just send a KJETILPC/monitor -m 0 to try to turn it off. I will continue to work on some better code.

I will continue to work on most of the features so don't forget to follow me on github :)

Thursday 6 July 2017

HASS - Vacuum cleaner helper script

I have a "stupid" and cheap vacuum cleaner(Skantic Robot Cleaner 10). It's completely manual and there are no indication when it is fully charged.  The charging thing is really annoying because I wasted to much time starting it without it being fully charged or forgot to start it when i could be used.

I had a Fibaro wall switch i connected to the charger and made a simple script in Hass to make a sensor of the power used by the switch. After some monitoring of the values. This was the result:

- 0.0W = not connected
- 0.4W = fully charged
- > 0.4W = charging( the values was between 12w and 6w)

With this values i made a sensor template in HASS

vacuumcleanerchargestatus:
      value_template: "{%- if states('sensor.fibaro_system_fgwpef_wall_plug_power_3') == '0.0' %} unplugged {% elif states('sensor.fibaro_system_fgwpef_wall_plug_power_3')  == '0.4'  %} fully charged {% else %} charging {%- endif %}"
      friendly_name: "Vacuum cleaner"

Now i have a easy sensor I can do automation/sensor with.

Thursday 1 June 2017

Automating Windows 10 with MQTT

I have developed a application for windows 10(some of the things should work on older versions).
The application is now avalible at https://github.com/KjetilSv/Win10As/


This application does
- Mute/unmute sound
- Set the volume(1-100)
- Power modes - Suspend/Shutdown/Reboot/Hibernate
- Custom commands ( everything that can be scripted should work)
- TTS( default language on default speaker)
- Toast message(link)
- Make screenshot(of primary monitor) to file(local or UNC) or MQTT
- Publishes cpu time, free memory, volume and mute(true/false)


Sample uses:


Girlfrind Nagging

In Home assistant a script I have a script my girl friend calls from Alexa, that triggers a toast and TTS message.

Home Assistant script:
(instead of mqtt switches it was easiest to just make a simple .sh script)
mosquitto_pub -t kjetilsv/toast -m "Home Assistant,kom ned!,,c:\temp\iselin.jpg"
mosquitto_pub -t kjetilsv/tts -m "NAG NAG NAG"




Easy remote monitoring

Often I'm waiting on download or stuff to compile I use the mqtt camera i Home Assistant.
With this I now can follow the process on my tablet relaxing in the couch.


HA implementation:
camera:
  - platform: mqtt
    topic: kjetilsv/mqttcamera
    name: kjetilsv


Alexa trigger work script
Mqtt scene that triggers a new RDP connection on my windows machine(the scene does some radio and light settings as well)



HA implementation:
- platform: mqtt
  name: "OfficeComputerWorkConnection"
  command_topic: "kjetilsv/jobb"
  payload_on: "1"
  optimistic: false
  qos: 0
  retain: false

Monday 23 January 2017

Getting Started With ESP EASY

EasyEsp is a framework for IOT for esp8266 and includes support for among others the nodemcu board.

We first need to setup the Arduino IDE for esp support and load the firmware. There is a great guide at http://www.letscontrolit.com/wiki/index.php/Tutorial_Arduino_Firmware_Upload

Now the firmware is loaded we need to restart the esp to load the defaults. so just unplugg wait a few seconds and reconnect it. Now you should have a new Wifi network avalible named configesp connect to it use the password wpakey



Nodemcu pinout



Add your wifi connection and wait untill get the wifi connection confirmation. You now have the ip adress to.


Choose the esp Access point

Select or enter ssd and password



Wifi ok, now the ESP EASY AP point is down. Windows should now reconnect to your default WIFI. Wait until it's connected then you click the proceed to main config link.

Enter the ip and credentials to your mqtt server and hit enter.



Now you can connect some leds to nodemcu like
I tested with 3 leds(blue, yellow and red) Connect the leds(with 220Ohms resistors) to D0,D1,D2. If you dont know now to connect it see this guide : http://www.instructables.com/id/Basic-Arduino-Tutorials-01-Blinking-LED/

When everything is connected we can start testing with :
mosquitto_pub -t '/<<yournodename>>/gpio/05' -m '1'

If everything is wired correctly the LED on gpio should light up. if you change the '1' to '0' it will stop shining.

You have to remeber to use the gpio pinnumbers not the nodemcu names(D0,D1,D2 etc)

Now the cool part. Integrate it with Home Assistant.






Notes:
Reset wifi on the esp : python esptool.py -p com6 erase_flash or upload a scetch with WiFi.disconnect(); in the setup section.







Saturday 31 December 2016

PIR Raspberry PI sensor

Using a HC-SR505(generic PIR) module to detect motion for your home automastion is pretty straigh forward. For my setup i connected pin 2 to 5v pin 12 to signal pin 14 to GND
check this for pinlayout : https://kjetiliot.blogspot.no/p/raspberry-pi.html

Install dependencies:

  1. sudo apt-get install python-pip
  2. pip install paho-mqtt
Code:

Make the script executable

sudo chmod a+x filename

Add script to crontab

To start the script to automaticly run at at startup run the command : sudo crontab -e and add the following line(edit the path to fit your filename and path) : @reboot sudo python /home/pi/pirsensor.py & You can now reboot and check everything is working.
You can test that the pir is publishing correctly by subscribint to the mqtt topic you used in the pir sensor python script mosquitto_sub -v -t 'home/office/presence'

Parts: