Wednesday 18 May 2016

Running a looping python script automatically

To run a looping python script at startup after a reboot or halt. The best way is to put the script in the root crontab.

Write the following command  : sudo crontab -e 
Enter the follwing line @reboot python3 /usr/python/arduino.py &
My personal choise of editor is nano so you might select nano in the editor chooser window. ctrl + x + x saves and exit.



NB! the & at the end. and then you can reboot your linux boot to test that everything is working fine. Youy can read more about crontab here : http://www.computerhope.com/unix/ucrontab.htm

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.