Home automation step 1

This is the first step in designing, prototyping and building a home automation system based on available hardware and some home made software. This step involves sensing some data and sending it to a remote server that does something useful with the data.
A rough sketch with the component looks like this and will be expanded in the future when new ideas pop up.

Step 1Different parts in the system:

Temperature sensor

The sensor is a simple Dallas 1-Wire temperature sensor which is read by the Arduino board. The sensor has a precision of 0.5 degree which is not a lot but will be sufficient for home use.

Arduino Ethernet

This is an Arduino board with an Ethernet connector, the temperature sensor is connected with it and is read by the OneWire library. The PubSubClient is used to connect to the MQTT broker.

client = PubSubClient(ip, 1883, callback, ethernetClient );
client.publish("sensors/temperature/1", temp);


Mosquitto is an Open Source MQTT broker available for Debian Wheezy via APT. Every time we read a new temperature a message is published on the topic sensors/temperature/1. With using an independent broker we decouple the publisher and the subscriber on the data. The Arduino sourcecode is quite simple and stable because it only report the temperature it has read to the broker. When we decide to change the datastore the Arduino code does not need any modifications.


The worker is a simple Python program that listens on the topic for new messages. It constantly waits for new messages and when one is received the data is stored into the database.
In short it works like this

def on_message(mosq, msg):
   con = pg.DB(dbname='gateway', host='localhost', user='***', passwd='***')
   con.insert('temperature', { 'device_id': 1, 'temperature': msg.payload })

mqttc = mosquitto.Mosquitto(MQTT_SERVER)
mqttc.on_message = on_message
mqttc.subscribe("sensors/temperature/#", 0)

Every message that is received will result in an insert query to save the data into the database.

Further work

This is just a start and prototype in designing a working home automation system. Further steps involve multiple and different sensors like humidity, light etc.. and after that the step to actually controlling something can be made.

6 thoughts on “Home automation step 1”

  1. Hi…..I am heading in the same direction that you are. I have designed my boards for my home and they work nicely. Been toying with different approaches for automating though. I love the arduino, and have been working with MQTT. My problem comes to publishing, especially while the main VOID loop is running. I read the sensors ( I have 5 right now) the void loop does things based on those temps. I want to publish the temps to topics, so that I can have other arduinos do things based on the published messages on the broker…..MY problem is I have the MQTT broker working, and I can run the mqtt_basic sketch ( provided with the library) and it all works, I see the messages if I subscribe. My problem is getting my original sketch to publish even temperatures…..I looked at your snippet, and I have basically the same thing, yet on my debug, all I see is PINGREQ and the arduino answering……how do you publish temperatures( as integers) to a topic?

    1. Hi

      I’m using the PubSubClient library to publish the temperature with client.publish(“sensors/temperature/1”, temp); but in the same sketch I’m not subscribing on a topic.

      My main loop looks like this, very straightforward and it works when I just publish messages:

      // a longer delay will let mqtt timeout, a keepalive message should be sent earlier

      time = time + 1;

      // Only send a message every 60 seconds
      if (time == 60) {
      time = 0;

      // MQTT client loop processing

      I’ll try to also subscribe on a topic to make a more advanced example

      1. Sounds great! I look forward to it. Maybe Im making this harder than I need to. What this first project is is controlling pumps and valves in my heating and cooling system. Everything is based on what the temperatures are, so I need to be constantly monitoring them. I want to publish the temps to a topic…..so part of the main loop looks like:

        void loop() {

        int reading = analogRead(EXCH);
        float voltage = reading * 5.0;
        voltage /= 1024.0;
        float temperatureC = (voltage – 0.5) * 100;
        float EXCH = (temperatureC * 9.0 / 5.0) + 32.0;

        int reading1 = analogRead(SOLAR);
        float voltage1 = reading1 * 5.0;
        voltage1 /= 1024.0;
        float temperatureC1 = (voltage1 – 0.5) * 100;
        float SOLAR = (temperatureC1 * 9.0 / 5.0) + 32.0;

        I want to arduino here to then publish the temps (EXCH and SOLAR) to a topic……If I add client.publish( “sensors/temps”, EXCH); I get the error that states it cant deal with a floating val. Thats fine I changed the floating temps to integers, which netted me with an error it cant handle them either. I think this all lies in that in that Im going about this wrong…..perhaps If i see your code Ill figure out what Im doing wrong…….

        Appreciate the reply, Im totally enjoying the learning process……Im more a Elect Engineer by trade. I can figure out the code, but get snagged some times

        1. The real problem is indeed the float conversion, I had exact the same problem and I fixed it with using the dtostrf function.

          char temp[50];
          dtostrf(value, 2, 2, temp);

          This will convert the float to a string, that string can be published to the topic.

          So that should work 🙂

          1. Thanks, that was the right direction I needed…….

            I modified it a little but after reading the dtostrf command I was able to get the ball rolling
            This part of the main loop relies on the biomass exchange temp reading being above 200F. If its is then it opens two water loops, turns on the associated pumps and opens the valves. I found though if you do 2 publish statements back to back to different topics, add a delay of about 500ms. I originally didnt have that in there, so when the arduino booted, it posted to the topics twice, then stopped. I guessed the mosquitto server couldnt decipher then incoming data that fast. The delay(500) cleared that up.

            if (EXCH >200.00) {

            digitalWrite(DigiPinLOOPA, HIGH);
            digitalWrite(DigiPinLOOPB, HIGH);
            digitalWrite(DigiPinCF, LOW);
            char charMsg[10];
            dtostrf(EXCH, 4, 2, charMsg);
            client.publish(“ENGTemps/EXCH”,charMsg); delay(500);
            client.publish(“mainTopic”, “EXCH >200F All Loops ONLINE”);delay(500);


            Thanks for pointing me in the right direction. Now that I know how to convert floating values to char strings for MQTT, the rest will start falling into place!

            Now I can start having the additional arduinos subscribing to topics. I also want to add in the ability to query this data to a database, so that I can build some sort of custom webpage and display the data in real time….

            Again thanks and good luck on your project as well.


  2. Hello,

    i use the same approach on my homeautomation for one year and half (migrating from my old solution based on xpl). I work with java, controlling my alarm, shutter, light, cctv moreover mqtt really easy to implement on android !

Comments are closed.