Showing posts tagged with arduino

in arduino, project, zigbee, smartthings, electronics

Since I purchased my SmartThings hub a few months ago I've been all about adding new sensors, lights and other smart things to my apartment.

Most recently I have been experimenting with the GE Link Light Bulbs. Overall I love them, but the one thing I (and my fiancée) have found a bit annoying is having to use a smartphone to control them. For the most part, this isn't necessary as I have a number of rules to automatically turn on the lights when need be, but there is always the odd situation where I want to turn the lights on/off manually.

Thinking of a better way to turn on these lights the old fashioned way (without having to install Zigbee light switches, we rent) turned me on to this latest electronics project.

SmartThings has a pretty robust API and I've tinkered around with controlling the lights from a web browser. The process basically involves a simple GET request to a predefined URL with a token, the ID of the switch I want to manipulate and what I want to do.

This lead me to develop physical buttons that hook into an Arduino to send the GET request when I press a button:

SmartThings Button

The circuit its self is pretty simple, each button is wired into a 150 Ohm resistor to ground, the 5V rail, and a separate line to the Arduino inputs. The Arduino has an ethernet shield to communicate with the greater internet.

SmartThings Button 2

Each button controls a different light (I have three bulbs total currently), the red and green switches will eventually be used to turn all the lights on or off but this hasn't been implemented yet.

Code-wise I've made use of the Arduino ethernet library to handle all of the networking and the HTTP request directly:

#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>
#include <util.h>
#include <SPI.h>

int BUTTON_DESK_LOWER= 7;  
int BUTTON_DESK_UPPER = 8;  
int BUTTON_SLIDING = 9;

char server[] = "graph.api.smartthings.com";

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress ip(192,168,0,174);

EthernetClient client;

void setup()  
{
  Serial.begin(9600);
   while (!Serial) {
    ;
  }

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    Ethernet.begin(mac, ip);
  }

  delay(1000);

  pinMode(BUTTON_DESK_LOWER,INPUT);
  pinMode(BUTTON_DESK_UPPER,INPUT);
  pinMode(BUTTON_SLIDING,INPUT);
}

void loop()  
{
  if(digitalRead(BUTTON_DESK_LOWER) == HIGH)
  {
    if (client.connect(server, 80)) 
    {
      Serial.println("connected");
      client.println("GET /api/smartapps/installations/REPLACE HTTP/1.1");
      client.println("Host: graph.api.smartthings.com");
      client.println("User-Agent: Mozilla/5.0");
      client.println("Connection: close");
      client.println();

      delay(5000);

      client.stop();
     } 
    else 
    {
      Serial.println("connection failed");
    }
  }
  if(digitalRead(BUTTON_DESK_UPPER) == HIGH)
  {
    if (client.connect(server, 80)) 
    {
      Serial.println("connected");
      client.println("GET /api/smartapps/installations/REPLACE HTTP/1.1");
      client.println("Host: graph.api.smartthings.com");
      client.println("User-Agent: Mozilla/5.0");
      client.println("Connection: close");
      client.println();

      delay(5000);

      client.stop();
     } 
    else 
    {
      Serial.println("connection failed");
    }
  }
  if(digitalRead(BUTTON_SLIDING) == HIGH)
  {
    if (client.connect(server, 80)) 
    {
      Serial.println("connected");
      client.println("GET /api/smartapps/installations/REPLACE HTTP/1.1");
      client.println("Host: graph.api.smartthings.com");
      client.println("User-Agent: Mozilla/5.0");
      client.println("Connection: close");
      client.println();

      delay(5000);

      client.stop();
     } 
    else 
    {
      Serial.println("connection failed");
    }
  }
}

It is still a bit (see: extremely) rough around the edges but it works! I'm still working on making it look a bit better and finding a way to mount it in some sort of useful position.

If you are interested in reusing the code you are welcome to - just replace the REPLACE strings with the token, switch ID and action with your own and change the button inputs to the inputs you have chosen. The MAC address I used was the default one from the example code (my shield did not have a MAC on it) so you'll probably want to change that and the IP assignment depending on your environment. You can find the repository on Github.

in weather station, arduino, raspberry pi, xbee, temperature, weather, humidity

I love learning new things, I also love monitoring things, so I decided to marry these two concepts and make a weather station from scratch. The project initially started using a number of guides, primarily a few adafruit guides like:

These two guides got me started - I dug up a Raspberry Pi I wasn't using, bought myself a breadboard, a temperature sensor (specifically the DS18B20), got some copper wire and went to work! My first attempt looked pretty much like the example provided in the TMP36 Temperature Guide and just spit out data to the terminal via a Python script.

So I could get the data, and I could display it, this was a triumph. So naturally, my next task was to do something useful with this. I fired up a MySQL server on one of my home servers and went to work. I eventually modified my original script to insert the temperature into a MySQL database. Once the data was in a database, the sky was the limit. You can see the script I currently use on GitHub.

I ended up leaving the weather station as it was for a while, the Raspberry Pi + Sensor lived in my "wiring closet" (I keep my networking stuff and servers in there, right next to the holiday decorations) and kept an eye on the temperature there. What use is a weather station that can only monitor the temperature in a closet? My next step was to add a "remote" probe in the form of a DS18B20 in a weather proof container. I wired this second temperature probe up to the breadboard and hung it just outside of my closet door to monitor the temperature of the house.

So I was happy as a clam at this point, monitoring the temperature in the closet and outside the closet. But what was the weather like outside? This expansion required quite a bit more planning and reading. What I ultimately ended up doing was buying an Arduino, Xbee, DHT11, two more DS18B20s (one with the weather coating, the other without), and a wired DHT22. The Xbee was used to communicate with the base station (the Raspberry Pi), one temperature probe hangs out the window in my bedroom along with the wired DHT22 (humidity), these probes measure the temperature and humidity outside. I also have another DS18B20 that lives in the breadboard to measure the temperature in the bedroom (why not, right?).

I then wrote a script to get the data from each probe and place it in a comma seperated format for easier interpretation by the base station. You can see that script here.

Once this was working, I added a second Xbee to the breadboard on the original Raspberry Pi, this listened for communications from the remote Xbee and wrote it to a serial port. The original script was then modified to also listen on that serial port and insert that data into the database.

At this point, I had a bunch of data and nothing to do with it, so I made a dashboard thing in Python (flask) to show the data. The repo for that code is available here.

This was one of my favorite projects thus far, the best part really is how extensible a weather station is. Since I have sensors inside and outside, I can use this data to interface with my Nest, I also considered using this to hook into a wireless humidifier (once humidity is X% turn on, turn off at Y%) and so on. If you are interested in the weather at all and want to try your hand at electronics, this is a great getting started project in my opinion.