Matt Daubneys Blog

Tag: arduino

Arduinos, MQTT and Light levels

by on Sep.04, 2011, under arduino, EasyRadio, Fun, programming, ubuntu

Since watching Andy Pipers talk on MQTT at Oggcamp, I’ve been trying to understand and use Mosquitto with my Arduinos. I’ve got a few sensors lying around to test this with (and have ordered some more \o/ ) and have started to have some success.

Installing Mosquitto is a doddle. I added the ppa to one of my household servers, then just apt-get install mosquitto. MQTT broker up and running in a matter of minutes. The first thing I setup to check if it was working was a simple python program that would just connect to the broker and send “Hello World” to the “hello” topic. I wrote a second little python script to just listen to the “hello” topic and print out the message from any updates. Most of the code was borrowed from this blog post which was incredibly helpful :)

With that running happily, I moved on a stage. Digging out an old shield for my arduino that I made to control the lights in my flat using some home easy sockets and an RF chip, I added an LDR and used the thermister that was already on the board. This simple set of components means I can now monitor both the temperature and light levels in one of the rooms of my flat. Using an ethernet shield for the arduino also means that I can then report that information to my Mosquitto broker using the arduino mqtt library.

What will I do with all this information? Well, initially I’m going to write an mqtt to sql bridge (might use this an excuse to learn postgres now mysql has an uncertain OSS future) and will setup some scripts to graph the information. I’ll probably change this sketch to control the lights in the flat, so I could have a machine somewhere sending a message to the mqtt broker at a set time to turn the lights on in the bedroom (a simple mqtt powered alarm clock :) ). Hopefully I’ll write a small application using the app indicator framework for Ubuntu (and growl for OSX) to tell me when certain thresholds are passed, especially for the other sensors I have coming (Alcohol, Gas, Smoke and humidity). I might write another bridge to use the google cloud messaging system to add an alert on my phone as well.

There’s a whole world of possibilities out there, thanks to so many people building these various components and libraries that make hacking fun toys so much easier :) Many thanks to all those who have developed the frameworks, libraries and services that I’m using and for making the F/OSS to make life even easier.

1 Comment :, , , , more...

Arduino powered lights and heating!

by on Nov.07, 2010, under arduino, learning, ubuntu, Uncategorized

Over the past few weeks as it gets colder, I’ve really started to notice the significant bite in heating costs from our flat being largely electrically heated. The main issue with this system is that none of the heaters have thermostats, they are either on, or off. As each heater is 2kW or greater, and there are 4 of them in the flat, that means at any one time we could be using 8kWh of electricity. Which is a lot of money during the day!

The solution? Build a thermostat for them (and replace 2 with a gas convection heater). The circuit for this is quite simple thanks to the fantastic home easy set of sockets. I bought a pack of three sockets (with a remote) and a light bulb holder (we’ll come back to that later). On top of this I needed a temperature sensor (tmp36), a 433MHz transmitter , a 433MHz reciever and an arduino uno.

Thanks to the lovely folks at the Home Easy Hacking Wiki getting this lot to work together with an arduino is easy as anything! Here is the basic circuit:

Home Easy Transmitter DiagramThis is only the transmitter/temperature sensor part. Initially you’ll need to build a receiver to get the ID of your remote. This can be found at this page in the arduino playground. Once you have your remotes ID, you just need a simple arduino sketch to turn the socket thats plugged into the heater on/off! Here it is….

HomeEasy homeEasy;
boolean isOn;
int incomingByte = 0;	// for incoming serial data
float timeOn=-900000;
int myCode = 1595082;  
 
void setup()
{
       Serial.begin(9600);
       homeEasy = HomeEasy();
       homeEasy.init();
       isOn = false; // 1 = On, 0 = Off
}
 
void loop()
{
  float temp = getTemp(0);
  Serial.println(temp);
  if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();
    // say what you got:
    if (incomingByte == 111){
      //turn on the group
      homeEasy.sendAdvancedProtocolMessage(myCode, 0, true, true );
      homeEasy.sendAdvancedProtocolMessage(myCode, 0, true, true );
    }
    else if (incomingByte == 102){
      // turn off the group
      homeEasy.sendAdvancedProtocolMessage(myCode, 0, false, true );
      homeEasy.sendAdvancedProtocolMessage(myCode, 0, false, true );
    }
  }
  if (temp < 18.0 && isOn == false && millis()-timeOn > 300000 ) {
     //turn on the heaters
     homeEasy.sendAdvancedProtocolMessage(myCode, 0, true, false );
     homeEasy.sendAdvancedProtocolMessage(myCode, 0, true, false );
     isOn = true;
     timeOn = millis();
  }
  else if (temp > 18.0 && isOn == true && millis()-timeOn > 300000 ) {
    homeEasy.sendAdvancedProtocolMessage(myCode, 0, false, false );
    homeEasy.sendAdvancedProtocolMessage(myCode, 0, false, false );
    isOn = false;
    timeOn = millis();
  }
}
 
/*
* getVoltage() – returns the voltage on the analog input defined by
* pin
*/
float getTemp(int pin){
return (((analogRead(pin) * .004882814)-0.5)*100.0); //converting from a 0 to 1023 digital range
                                        // to 0 to 5 volts (each 1 reading equals ~ 5 millivolts
}

This code ensures that there is no change in status for at least 5 minutes, basically so the socket doesn’t go continuously on/off and damage something! There is also a hook for a serial input to turn on the “group” of the remote. This is to turn on the light in my bedroom. I’m in the process of writing two simple bits of python to turn a light on/of depending which is run. This means I can set up a cron to turn on a light at a given time! Simple alarm clock :)

To get this code to work you’ll need the homeeasy library from here. You may need to alter the pin allocation in homeeasy.cpp, but that shouldn’t be too hard to do!

Next thing to do on this project is to get my Revo setup as a little wireless server box to graph the temperature changes and run the alarm clock. Conveniently it’s being replaced with a mac mini this week so has become available to complete the project. This will be a simple ubuntu box, running ubuntu server and little else really. Thought it may gain an ldap database for another, slightly different project.

1 Comment :, , , , , more...

Arduino and Easy Radios

by on Feb.04, 2010, under arduino, EasyRadio, programming, ubuntu

A few days ago I received a pair of Easy Radios to help with a project I’m working on. Since I’ve never used these before (having only got my first arduino a couple of weeks ago) I had a dig on the net for information on how to use them. Since this information was quite sparse, I thought I’d put up my experimental rig to show it working. This setup requires 2 arduinos, an easy radio transmitter and an easy radio receiver.  First, the transmitter layout.

Transmitter Layout

Transmitter Layout

Now the receiver layout.

Receiver Layout

Receiver Layout

and finally the code.

#reciever code

1
2
3
4
5
6
7
8
void setup() {
    Serial.begin(19200);
}
void loop() {
    if (Serial.available()){
        Serial.print(Serial.read(), BYTE);
    }
}
1
2
3
4
5
6
7
8
9
#transmitter code
void setup() {
    Serial.begin(19200);
}
 
void loop() {
    Serial.write("Hello\n");
    delay(1000);
}

This should result in the receiver outputting “Hello” over and over across the USB serial in the arduino environments serial monitor. If you have any problems, feel free to give me a shout! Oh, make sure you unplug the easy radio receiver from the arduino before flashing it, it’ll throw nasty errors otherwise. (Simply unplug digital 0 pin on the arduino board)

Leave a Comment :, , , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...