Showing posts with label raspberry pi. Show all posts
Showing posts with label raspberry pi. Show all posts

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





 

Thursday, 13 October 2016

Easy steam scraping with BeautifulSoup


I wanted to make a sensor for my steam account to use for future projects. After some googling i found BeautifulSoup. I framework to search with in text/html and xml. After some fiddling i got it working. To get steam working you need a working public profile page.

Install BeautifulSoup

sudo apg-get update -y && sudo atp-get upgrade -y && sudo apt-get install python-bs4 -y

Steam scraper

Will return blank if Im not playing anything or the name of the game if im playing.

Referance
https://www.crummy.com/software/BeautifulSoup/bs4/doc/

Raspian commandline network speedtest

To check internet speed on your raspberry do the following steps.


  • sudo apg-get update
  • sudo apt-get upgrade
  • sudo apt-get install python-pip
  • sudo easy_install speedtest-cli


Then simply type speedtest-cli and you will get something like this



Monday, 10 October 2016

Smartify a simple heater


I needed to make a controller for my cheap and simple heater. It don't have any termostat and the setting are really strange and I could not find the correct settings, either too hot or too cold. I connected a 433 sender to a raspberry pi and made a flask web API. I then added a switch in Home Assistant that run a curl command to the other pi.

Hardware

Parts

Wiring

  • VCC -> pin 1 3,3Volt
  • Gnd -> pin 6 Gnd
  • Date/Data/signdal -> GPIO 17 (pin11)

Software


Flask code

 

Homeassistant


Referances

Sunday, 28 August 2016

Save shutdown of raspberry pi

It's tempting to just pull the power when we move the pi but that will 
stress the SD card and eventually corrupt it.
so instead run the command.

sudo shutdown -h now

Monday, 16 May 2016

Talking and reporting flower





I started this to learn to program arduino but it escaled pretty quicly.when i was finished with the project i had a flowersensor that reported to home assistant and i was able to chat with the data thru telegram.





Hardware I used:


Sensors:
  • Photo Resistor
  • LM35 Temp Sensor 
  • PIR Motion Sensor
  • DHT11 Temperature and Humidity Sensor
  • Soil humidity sensor
Just connect them as described in code and in the datasheets for the sensors.


Getting started with the electronics and the arduino:

#include "DHT.h"
#define DHTPIN 7    
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
int humidSensorPIN = 0;
int lightSensorPIN = 1;
int tempSensorPIN = 2;

void setup()
{
  Serial.begin(9600);
  dht.begin();
}

void loop()
{
  Serial.print("Soilhumidity:");
  Serial.println(analogRead(humidSensorPIN));
  Serial.print("Light:");
  Serial.println(analogRead(lightSensorPIN));
  int tempratureVal;
  int tempratureDat;
  tempratureVal = analogRead(tempSensorPIN);
  tempratureDat = (125 * tempratureVal) >> 8;
  Serial.print("TempratureRes:");
  Serial.println(tempratureDat);
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float f = dht.readTemperature(true);
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  Serial.print("AirHumidity:");
  Serial.println(h);
  Serial.print("Temperature:");
  Serial.println(t);
  byte sensorPin = 6;
  byte state = digitalRead(sensorPin);
  Serial.print("Presence:");
  Serial.println(state);
  delay(5000);
}


This makes the aruino pump the following data out to the usb connector to the raspberry.

Light:137
TempratureRes:20
AirHumidity:23.00
Temperature:21.00
Presence:0
Soilhumidity:0


Now I connected it to the raspberry pi and wrote this python script. Notice you might change the usb serial port, if may vary from raspberry to raspberry. Mine is as you can see ttyACM0 The script reads the data and parse and send it to my mqtt server. this code use a bit of CPI so you might play around with the time.sleep(1) command to optimize for you system.

---- start arduino.py ----
#!/usr/bin/python
import time
import serial
import paho.mqtt.client as mqtt
connected = False
# Establish the connection on a specific port
ser = serial.Serial('/dev/ttyACM0', 9600)
mqttc = mqtt.Client()
mqttc.username_pw_set("mqtt", "mqtt")
mqttc.connect("localhost", 1883,60)
## loop until the arduino tells us it is ready
while not connected:
    serin = ser.read()
    connected = True

while 1:
    if ser.inWaiting():
        x=ser.readline().decode("utf-8")
        if str(x[:6])=="Light:":
            mqttc.publish("home/office/light",str(x[6:8]));
        elif x[:14]=="TempratureRes:":
            mqttc.publish("home/office/tempratureres",str(x[14:16]));
        elif x[:12]=="AirHumidity:":
            mqttc.publish("home/office/airhumidity",str(x[12:14]));
        elif x[:12]=="Temperature:":
            mqttc.publish("home/office/temperature",str(x[12:14]));
        elif x[:9]=="Presence:":
            mqttc.publish("home/office/presence",str(x[9:10]));
        elif x[:13]=="Soilhumidity:":
            mqttc.publish("home/office/soilhumidity",str(x[13:16]));
        else:
            print (x)

time.sleep(1)           
ser.close()

---- end arduino.py ----

Now I have the data in mqtt I can connect Home Assistant(HASS) to the read the data.  HASS is a really good framework to have for you iot projects as it's very flexible as you can see later. add the following lines in the configuration.yaml file to read the sensor data from mqtt.

mqtt:
  broker: 127.0.0.1
  port: 1883
  client_id: home-assistant-1
  keepalive: 60
  username: mqtt
  password: mqtt

sensor 3:
  platform: mqtt
  state_topic: "home/office/light"
  qos: 0
  name: "home/office/light"

sensor 4:
  platform: mqtt
  state_topic: "home/office/temperature"
  qos: 0
  name: "home/office/temperature"
  unit_of_measurement: "c"
 
sensor 5:
  platform: mqtt
  state_topic: "home/office/presence"
  qos: 0
  name: "home/office/presence"
 
sensor 6:
  platform: mqtt
  state_topic: "home/office/soilhumidity"
  qos: 0
  name: "home/office/soilhumidity"
 
sensor 7:
  platform: mqtt
  unit_of_measurement: "%"
  state_topic: "home/office/airhumidity"
  qos: 0
  name: "home/office/airhumidity"
 
sensor 8:
  unit_of_measurement: "c"
  platform: mqtt
  state_topic: "home/office/tempratureres"
  qos: 0
  name: "home/office/temperatureres"



Now you get something like this in HASS.
HASS now provides you a easy way to read JSON data from it. So i wrote a python script to read the data and talk to telegram for input and output.

--- start telegram.py ----
import time
import datetime
import telepot
import json
from requests import get
headers = {'x-ha-access': 'raspberry','content-type': 'application/json'}
def handle(msg):
    chat_id = msg['chat']['id']
    command = msg['text']    elif command == '/time':
        bot.sendMessage(chat_id, str(datetime.datetime.now()))
    elif command == '/how you doing?':
        url = 'http://localhost:8123/api/states/sensor.mqtt_sensor'
        response = get(url, headers=headers)
        tmps = str(response.text)
        d = json.loads(tmps)
        temp= d['state']
        bot.sendMessage(chat_id, 'I feel great! my temp is ' + temp + 'c')
bot = telepot.Bot('--- BOT API ---')
bot.message_loop(handle)
print ("I am listening ...")
while 1:
    time.sleep(10)
--- end telegram.py ----

Insert the BOT API code you get from bot father. Now you can start chatting with your new bot. It's easy to add new commands to the bot if you want to build more logic around it.



Disclamer: I'm not a python/c programmer so this code could/should be optimized.