Oct 09

Home Improvement DIY: WiFi Gas Detector with Text Alerts

Hey guys, back with another little project that I’ve been fiddling with for the past week. With kids around, every parent is thinking how to make their home safer for the little ones and for everybody in general. One of the most dangerous thing in the house can be the stove and since we have a gas-powered one, I always wondered why there are no simple gas detectors that can be used around the stove, just to alert instantly that gas may be leaking.

sparkWell, that was the moment when I decided to build one of my own. Having a Particle (Spark) Photon lying around, I decided to use that as a foundation for the project. I like the fact that they are very small and cheap, and also can be flashed over the Wi-Fi. Having that settled, I needed the gas sensor and some kind of alerting system.

For the gas sensor I went for an
MQ-4 gas sensor because it can detect methane (CH4) as well as natural gas (CNG).
I found out later that it’s quite sensitive to CO2 as well, which turned out to be very useful while testing the setup. If you’re planning to use one in your projects and never used one before, remember that the gas sensogassensorrs have a burn-in time when they are powered. That is to say that the sensor needs time to warm up (anywhere between 2 and 10 minutes). This is because the actual sensor has a heating element that helps detect these gases and while that is cold, it will give erroneous readings.

Now on to the alerts. Half of the alerting system was simple, add a beeper next to the sensor which would work perfectly when somebody is around. However I still had to sort out the second half, for the cases when somebody was not in the immediate vicinity. For that I wanted to have text/SMS alerts to my phone and possibly to a buzzer_secondary phone, for example my wife’s phone. Now I realized that I should probably get one of our neighborhood FD guys phone number in there too. :)

Knowing that the Photon is not the most robust controller in terms of notifications (I know there’s the IFTTT or other alerting systems) but I wanted something small, reliable and really not depending on various 3rd parties in the cloud. So I made a small web app on my local web server that would send me specific text message when a URL is hit. Many cell carriers offer now the possibility to send somebody an email to a specific email address, and that email will be turned into an SMS and sent to the phone, so that turned out to be a good enough solution for me.

So the final sequence would be the following: gas sensor detects gas, starts beeping and at the same time triggers a URL which in turn sends an email that gets transformed by the carrier into a text message on my phone. The code that runs this will actually send an alert every 30 seconds while the gas sensor is being triggered. This way I know when the gas has dispersed. Here’s the photon firmware code:

#include "HttpClient/HttpClient.h"

TCPClient client;
//add your server address here. In my case it was a LAN address similar to
char server[] = "";
//add your server port, usually if it's a standard http request the port is probably 80
int port = 80;
//Initialize timer to 0
int AlertSentSecsAgo = 0;
//This is used to publish Spark events
char strTxt[40];
//Define the PIN for the gas sensor
int GasSensor = A0;
//Define the PIN for the buzzer
int Beeper = D0;
//Define how often to send text alerts
int AlertFrequency = 30;

void setup() 
     pinMode(Beeper, OUTPUT);
     pinMode(GasSensor, INPUT);

void loop()
     //I had to fine tune this value at which the alert is triggered based on some trial tests
     if (analogRead(GasSensor) > 750)
          //If we sent an alert more than 30 seconds ago, send another one
          if (AlertSentSecsAgo >= AlertFrequency)
               //Send alert every 30 seconds
               AlertSentSecsAgo = 0;
               sprintf(strTxt, "%u", analogRead(GasSensor));
               //Publish an event to the spark dashboard with the current value of the sensor
               Spark.publish("Gas Sensor Triggered", strTxt); 
          digitalWrite(Beeper, HIGH); //Sound the buzzer
          digitalWrite(Beeper, LOW);  //Silence the buzzer
          sprintf(strTxt, "%u", analogRead(GasSensor));
          Spark.publish("Gas Sensor:", strTxt);
          //Make sure that the alert will be sent next time the sensor is triggered
          AlertSentSecsAgo = AlertFrequency + 1; 
          delay(5000); //Wait 5 seconds before checking the sensor again

void sendAlert()
     if (client.connect(server, port))
          client.println("GET /<your address to the text web app or service here> HTTP/1.0");
          client.print("Host: ");
          client.println("Accept: text/html, text/plain");

Finally, for the case I chose a wall surface-mount phone jack box. The size perfectly matched the components that I had.IMG_3404a

List of parts used in this project:

  • Particle Photon ($19 on Particle Store)
  • MQ4 Gas Sensor ($5.50 on Amazon)
  • 5V Buzzer ($3.99 on Amazon)
  • Wall mounted phone jack box ($3.00 at Home Depot)
  • Right-angle USB Mini B cable ($3.99 on Amazon)
  • Jumper wires

Here are a few pictures of the finished project.



Aug 19

Home improvement DIY: WiFi Internet radio for your home using in-ceiling speakers

For a while now I’ve been planning to start on this little project but didn’t have either time or parts to actually see it through. Finally last week I decided to prioritize it and I can happily say that it turned out to be a very fun and engaging.

So, in short, this is about having access to thousands of free radio stations online and streaming seamlessly to a home audio system. Since this was more of a trial for me, the audio system consisted of in-ceiling speakers for the kitchen.

To start off, I planned where I would place the speakers. Not knowing yet how big they will be, all I could do was more of getting an idea how the kitchen is laid out and where most of the activities will take place. Being a new house, I did not have all the furniture in place yet. Finally I decided on having two speakers, one over the fridge/stove/sink area and
the second closer to the formal dining area. The plan for the setup was to have a device capable of streaming online audio content, speakers and possibly an amplifier in between (depending on whether the speakers had it built-in or not). With that decided, I started shopping for speakers.

The Speakers:

InCeilingAudio3I knew from the beginning that I will not use this for party style loud music but mostly ambient music or newscasts so after comparing models, specifications and prices I settled on the Polk TC80i. These are 8-inchers so they do pack a punch but my hopes were that the punch would actually go towards sound quality rather than quantity.
When they arrived I was again impressed by the Polk packaging and product quality. Very nicely packed with very much attention to protect all scratch-able surfaces. Well done Polk. The speaker specs can be found here in detail.



Another piece of the puzzle that was decided pretty early on, was the streaming device. Being quite familiar with micro-controllers and having tinkered on other projects containing bare-bone PCs and other miniaturized systems, I decided that the Raspberry PI Model B would be a perfect fit for this one. Model A would work too, but in this case I wanted to invest a bit more, in case I will re-purpose the PI in the future. At the time of writing the Model A was retailing for about $20 on Amazon.com whereas Model B was around $45. Still very cheap for a credit card sized computer. While at it, I also got a wireless adapter for it (it does not have wireless capabilities built-in) and a nice clear case for it. The wireless dongle was one of the most popular ones used by the PI community: the Edimax EW-7811Un 150Mbps 11n Wi-Fi USB Adapter.

When it comes to streaming software it was quite easy. A while ago I came across a DIY Internet radio forum that mentioned PI MusicBox as a good streaming source. I gave it a try and found out that it was exactly what I needed for this project. It’s extremely easy to install on the Raspberry PI on a micro SD card. Basically you need to format a micro SD, install the MusicBox image and plug it into the Raspberry PI. That’s it. The official PI MusicBox website has the detailed steps for that, so I won’t list them here.

The Amplifier:

Having the speakers and streaming device sortedInCeilingAudio10 out it was time to shop for the final piece that would tie these together. That is the amplifier. The main criteria for the amplifier was that it needs to be very small, low profile and just enough to drive the two speakers. The place where this would spend it’s amplifying days was on top of a kitchen cupboard, so a remote control was also not in the plan. I initially started looking for one that would be smart enough to detect when the audio-in connection becomes active and turn on by itself, but then I was advised that even if it’s on all the time, it makes a very small difference since the
consumption on most recent amplifiers is close to zero when not in use.
InCeilingAudio1That being said I went for the SMSL SA50 50Wx2 TDA7492 Class D Amplifier which retailed for about $65. This is a 50W continuous average power little powerhouse that has received outstanding reviews since it was released.  In my case the speakers are 8 Ohms 125W max so I realistically expect about 30W per channel to be delivered at peak levels to the speakers at a reasonable ambient volume.





InCeilingAudio17Here is a connection diagram for the entire layout


Time to start the installation. I knew from the beginning that the speakers InCeilingAudio16would be the most challenging. Not only because I had to cut these big 8-inch holes in the brand new ceiling but also because wires and studs usually don’t go well together, especially when they don’t come up at the same time. Using a good stud finder I mapped all joists inside the kitchen ceiling and based on the orientation, I chose a cupboard where I would place the amplifier and the Raspberry PI so that the wires to the speakers would run along the joists and not perpendicular. These two components are small enough to go inside the cupboard but since I have a small gap between the cupboard and the ceiling, I placed them on top, well hidden from the view. Armed with the templates that came with the speakers and the having the joists outlined, I marked the two placements for the speakers exactly in between joists.
InCeilingAudio14Making the holes was fairly easy using a drywall saw and I had them made in a matter of a minutes. After these were done, I made small holes in the ceiling on top of the cupboard very close to the wall where they would not be visible. These small holes were in line between the same set of joists between which the speakers were placed.
With a wire fishing line I ran the speaker wires from each small opening to the speaker location, connected and mounted the InCeilingAudio13speakers.

Once the speakers were connected to the amplifier, it was time to find a power source location from where to power the PI and the amplifier. The PI requires a 5V power supply and it only needs about 750mA at peak usage so I simply used an iPhone charger since it’s very small and rated for more than enough amps. The amplifier came with its own power supply, which was a bit big for my taste, but since it was hidden, I decided to keep it.


Initially I was planning to run a low profile extension cord from the counter top-level outlets through the cupboard, however, I realized that the hood fan was very close to the location so I simply added a three-way power splitter inside the hood fan and ran a cord to the top of the cupboard from there. This was hidden in a white wire conduit. Another good location is the microwave power outlet which is usually behind the microwave if you have one located at cupboard level.

After running some calibration tests, in order to set the correct volume on the amplifier, I turned the software volume in MusicBox up as much as I could where no distortion would be noticeable and considered that as the loudest volume I want to have. To my surprise the amplifier had to be turned to only about 1/3 of its full range. Why is that good? Because that means the amplifier does not need to do a lot of work to have a reasonable sound level, which in turns means less power consumption and less heating.
All in all a good and fun little project. Now I am looking to expand this setup to multiple rooms in the house.

Here are a few more pics during the installation:

In-ceiling Internet Radio

[img src=http://www.thebrainspike.com/wp-content/flagallery/in-ceiling-internet-radio/thumbs/thumbs_inceilingaudio1.jpg]450Amp and PI dimensions
[img src=http://www.thebrainspike.com/wp-content/flagallery/in-ceiling-internet-radio/thumbs/thumbs_inceilingaudio2.jpg]440Amp and PI dimensions
[img src=http://www.thebrainspike.com/wp-content/flagallery/in-ceiling-internet-radio/thumbs/thumbs_inceilingaudio3.jpg]390Speaker front
[img src=http://www.thebrainspike.com/wp-content/flagallery/in-ceiling-internet-radio/thumbs/thumbs_inceilingaudio4.jpg]390Amp and PI dimensions
[img src=http://www.thebrainspike.com/wp-content/flagallery/in-ceiling-internet-radio/thumbs/thumbs_inceilingaudio5.jpg]410Speaker back
[img src=http://www.thebrainspike.com/wp-content/flagallery/in-ceiling-internet-radio/thumbs/thumbs_inceilingaudio6.jpg]440Speaker back
[img src=http://www.thebrainspike.com/wp-content/flagallery/in-ceiling-internet-radio/thumbs/thumbs_inceilingaudio7.jpg]410Final look
[img src=http://www.thebrainspike.com/wp-content/flagallery/in-ceiling-internet-radio/thumbs/thumbs_inceilingaudio8.jpg]510Ceiling space between joists. Never run audio/video wires along high voltage conduits. It's always a good idea to pass conduits at a 90 degree angle to minimize interference
[img src=http://www.thebrainspike.com/wp-content/flagallery/in-ceiling-internet-radio/thumbs/thumbs_inceilingaudio9.jpg]410Amp and PI on top of cabinet during initial setup
[img src=http://www.thebrainspike.com/wp-content/flagallery/in-ceiling-internet-radio/thumbs/thumbs_inceilingaudio10.jpg]390Amp connections
[img src=http://www.thebrainspike.com/wp-content/flagallery/in-ceiling-internet-radio/thumbs/thumbs_inceilingaudio11.jpg]330Speaker mounting bracket
[img src=http://www.thebrainspike.com/wp-content/flagallery/in-ceiling-internet-radio/thumbs/thumbs_inceilingaudio12.jpg]360Installed speaker without grill
[img src=http://www.thebrainspike.com/wp-content/flagallery/in-ceiling-internet-radio/thumbs/thumbs_inceilingaudio13.jpg]400Installed speaker with grill
[img src=http://www.thebrainspike.com/wp-content/flagallery/in-ceiling-internet-radio/thumbs/thumbs_inceilingaudio14.jpg]390Ran wire through joist in order to align it with the top of the cupboard
[img src=http://www.thebrainspike.com/wp-content/flagallery/in-ceiling-internet-radio/thumbs/thumbs_inceilingaudio15.jpg]370Tweeter orientation should be adjusted based on the room layout
[img src=http://www.thebrainspike.com/wp-content/flagallery/in-ceiling-internet-radio/thumbs/thumbs_inceilingaudio16.jpg]450Speaker mounting location in relation to joists
[img src=http://www.thebrainspike.com/wp-content/flagallery/in-ceiling-internet-radio/thumbs/thumbs_inceilingaudio17.jpg]350Wiring diagram
[img src=http://www.thebrainspike.com/wp-content/flagallery/in-ceiling-internet-radio/thumbs/thumbs_inceilingaudio18.jpg]310Raspberry PI 2 Model B+

Jan 22

How to fix URL problem on Windows hosting with Godaddy

Here is another tidbit that made me lose some sleep over the past few days. Well, not actually sleep, but a few good hours pulling my hairs and another hour on the phone with the Godaddy support. I have to admit that they are quite polite and nice to talk to.

Read the rest of this entry »

Older posts «