Marco
Published © GPL3+

Wireless controlled home

The project uses an app (developed by me) to control the lights of a house and to receive data from it. The house is powered by solar panel.

AdvancedShowcase (no instructions)10 hours1,442
Wireless controlled home

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1
Arduino UNO
Arduino UNO
×1
Solar panel
×1
Solar panel charge controller - CMTD
×1
12V/2.3Ah battery
×1
Wood Screws
×1
Jumper wires (generic)
Jumper wires (generic)
×1
White LED strip 12 volts
Waterproof isn't needed. You only need a 11cm 12volt led tape
×1
BMP280
This module give you the temperature and the pressure
×1
Photo resistor
Photo resistor
×1
BS170 transistor or equivalent
×1
Resistor 330 ohm
Resistor 330 ohm
×1
Resistor 82KΩ
×1
USB Arduino Cable
Used to connect Arduino to Raspberry Pi
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
Used to power the Raspberry Pi from the solar panel charger
×1
Box or a little plastic house
Used to contain the sensors and the LEDs. I used an Harry Potter plastic house for a better aesthetics
×1

Software apps and online services

Arduino IDE
Arduino IDE
Raspbian
Raspberry Pi Raspbian
Emoncms

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Electric drill

Story

Read more

Custom parts and enclosures

Project_scheme.png

This is the original build scheme

Schematics

Project circuit

Code

Arduino Code

Arduino
This code is used to connect the Arduino to the Raspberry Pi through the Serial Port.
I'll add the comment to the sketch
// RTX_controller
// Read Tempeature and Pressure from BMP280 (I2C), Lux from analog Photoresistor and tramits to Raspberry WiFi Gateway.
// Read the Light switch status from Raspberry that reads from the Cloud server (emoncms)

#include "BMP280.h"

BMP280 bmp;     //bmp280 uses pins A4(SDA) and A5(SCL)

double T,P;
char result;

int FotoPin = A3;
int L;

String sread;//used for serial data reading


void setup() 
{
  pinMode(13, OUTPUT);
  Serial.begin(9600);
  //Serial.setTimeout(50);
  bmp.begin();
  bmp.setOversampling(4);
  pinMode(FotoPin, INPUT); 
  digitalWrite(13, LOW);
  delay(500);
  digitalWrite(13, HIGH);
  delay(500);
  digitalWrite(13, LOW);
  delay(500);
  digitalWrite(13, HIGH);
  delay(500);
  digitalWrite(13, LOW);
}
  

void loop()
{  
    if (Serial.available() > 0) 
    {
     // legge la stringa inviata
      sread = Serial.readString();
      Serial.flushRX();

   if(sread == "v1")
      {
//      Serial.println("I RECEIVED LIGHTS ON");
      digitalWrite(13, HIGH);
      }
      
   if(sread == "v0")
      {
//      Serial.println("I RECEIVED LIGHTS OFF");
      digitalWrite(13, LOW);
      }
      sread="";
      
    }
   
   result = bmp.startMeasurment();   
   result = bmp.getTemperatureAndPressure(T,P);
   /*Serial.print("Temperature : ");
   Serial.print(T);
   Serial.print("  Pressure : ");
   Serial.print(P);
   Serial.print("  Light : ");
   Serial.println(L);
  delay(500);
  */
  Serial.print("OK 1 ");
  Serial.println(T);    //BMP280 Temp
  delay(1000);
  Serial.print("OK 2 ");
  Serial.println(P);    //BMP280 Press
  delay(1000);
  Serial.print("OK 3 ");
  L = map(analogRead(FotoPin), 1023, 0, 0, 1050);
  Serial.println(L);    // Photoresistor Lux
  delay(1000);         
}

Raspberry Pi Code

Python
This code is used to connect the Raspberry Pi to the Arduino through the Serial Port and send the data on the server. I'll add the comment to the program later
# Read from serial with data coming from ARDUINO (Temperature, Pressure and LIght)

#Change Log
# Version 1
# fixed serial synch

import serial, sys, string
import time, datetime, httplib, urllib
from socket import gaierror, timeout, error
from httplib import BadStatusLine

# Domain you want to post to: localhost would be an emoncms installation on your own laptop
# this could be changed to emoncms.org to post to emoncms.org
domain = "emoncms.org"

# Location of emoncms in your server, the standard setup is to place it in a folder called emoncms
# To post to emoncms.org change this to blank: ""
emoncmspath = ""

# Write apikey of emoncms account
apikey = "49884feb7e4e807d1e3e208d09926bc4"

# Set this to the serial port of your emontx and baud rate, 9600 is standard emontx baud rate
ser = serial.Serial('/dev/ttyACM0', 9600)

# ser.write("4b\n")
# ser.write("210g\n")

print "Arduino to Python to Eemoncms cloud serial link"

conn = httplib.HTTPConnection(domain, timeout=60)
print "Conn HTTP created"

def feedval(feedid):
    data_url="http://emoncms.org/feed/value.json?apikey=" + apikey + "&id=" + str(feedid)
    # read in the data from emoncms
    try:
        sock = urllib.urlopen(data_url)
        data_str = sock.read()
        sock.close
        onoff = data_str.replace('"', '')
        return onoff
    except Exception, detail:
        print "Error ", detail
light = feedval(123960)

# TELLS ARDUINO SENSORS PROGRAM TO STRART SENDING DATA IN THE FORMAT OK 1 23,8 123.8 456,9
ser.write("START")
# WAIT 3 Seconds
time.sleep(3)
ser.flushInput()
synch = 0        
# Read in line of readings from Arduino serial
linestr = ser.readline()
#print "READ from serial"
while synch == 0:
# Remove the new line at the end
    linestr = linestr.rstrip()
    print "DATA RX:"+linestr
    # Split the line at the whitespaces
    values = linestr.split(' ')

    if values[0]=="OK":
            
        try:
            nodeid = int(values[1])
            nameid = 1
        except ValueError:
            linestr = ser.readline()
            print "OK BUT FALSE:"+linestr
        pass # it was an int.
        synch = 1
    else:
        synch = 0
        print "NOT OK RECEIVED!:"+linestr
print "SYNCH OK  "+linestr

ser.write("v1")
time.sleep(5)
ser.write("v0")
print "HO ACCESO E SPENTO"
while 1:
    
    try:
        
        #print "TRY to read from serial"
        # Read in line of readings from Arduino serial
        linestr = ser.readline()
        #print "READ from serial"

        # Remove the new line at the end
        linestr = linestr.rstrip()

        #print "DATA RX:"+linestr

        # Split the line at the whitespaces
        values = linestr.split(' ')
                   
        if values[0]=="OK":
            nodeid = int(values[1])
            nameid = 1


            #datacsv = []
            #for i in range(2,(len(values)-1),1):
            
                ## Get 16-bit integer
                ## value = int(values[i]) + int(values[i+1])*256
                ## if value>32768: value -= 65536
                
                #value = float(values[i])
                #datacsv.append(str(value))

            #req = "node="+str(nodeid)+"&csv="+','.join(datacsv)
            #req = "node="+str(nodeid)+"&csv="+(datacsv)
            #req = "node="+str(nodeid)+"&csv="+ str(values[2])
            req = "node="+str(nodeid)+"&csv="+ str(float(values[2]))
            print time.ctime(), "        ", req # 'Mon Oct 18 13:35:29 2010'
            #print req

            # Send to emoncms

            conn.request("GET", "/"+emoncmspath+"/input/post.json?apikey="+apikey+"&"+req)
            response = conn.getresponse()
            print response.read()
            print "Luce : ", feedval(123960)
            
            
    except KeyError:
        conn.close()
        sys.exit()
    except gaierror:
    #except socket.gaierror:
        print "Error with request - re-opening connection"
        conn.close()
        print "gai Error with request - close connector"
        conn = httplib.HTTPConnection(domain, timeout=60)
        print "gai Error with request - recycle"
        #conn.request("GET", "/"+emoncmspath+"/input/post.json?apikey="+apikey+"&"+req)
        #response = conn.getresponse()
        #print response.read()
    except timeout:
    #except socket.timeout:
        print "Timeout Error with request - re-opening connection"
        conn.close()
        print "Timeout Error with request - close connector"
        conn = httplib.HTTPConnection(domain, timeout=60)
        print "Timeout Error with request - recycle"
        #conn.request("GET", "/"+emoncmspath+"/input/post.json?apikey="+apikey+"&"+req)
        #response = conn.getresponse()
        #print response.read()
    except error:
    #except socket.timeout:
        print "Err 113 Host not found with request - re-opening connection"
        conn.close()
        print "Err 113 Host not found with request - close connector"
        conn = httplib.HTTPConnection(domain, timeout=60)
        print "Err 113 Host not found with request - recycle"
        #conn.request("GET", "/"+emoncmspath+"/input/post.json?apikey="+apikey+"&"+req)
        #response = conn.getresponse()
        #print response.read()
    except BadStatusLine:
	#except httplib.BadStatusLine:
        print "Err httplib.BadStatusLine with response - re-opening connection and retry"
        conn.close()
        print "Err httplib.BadStatusLine with response - closed connector"
        conn = httplib.HTTPConnection(domain, timeout=60)
        print "Err httplib.BadStatusLine with response - recycle"
        #conn.request("GET", "/"+emoncmspath+"/input/post.json?apikey="+apikey+"&"+req)
        #response = conn.getresponse()
        #print response.read()

    if light == feedval(123960):
        print feedval(123960)
    else:
        if feedval(123960) == "0":
               ser.flushOutput()
               ser.write("v0")
               print feedval(123960)
               light = feedval(123960)
               time.sleep(3)
        if feedval(123960) == "1":
               ser.flushOutput()
               ser.write("v1")
               print feedval(123960)
               light = feedval(123960)
               time.sleep(3)

Remote temp.apk

Java
This file is an APK (Android Package). Before using this you need to configure the server and create an account.
No preview (download only).

Credits

Marco
4 projects • 0 followers

Comments