Friday, February 3, 2017

A mobile phone activated remote switch to turn on an appliance

The Need: I have been playing with Raspberry PI interfaced sensors that send data to the cloud from our home or in a manufacturing plant using a WiFi or Ethernet link to the Internet. Unfortunately, WiFi networks can be flaky and one doesn’t always have Internet available at all locations especially remote areas. My friend Sanjay who is an avid pilot and who has multiple airplanes parked in a cold hanger outside of Detroit suburbs had a need. Since he didn’t have an internet connection at his hanger, he wanted a cell phone activated switch that would turn on a ‘heater blanket’ that would warm up his airplane engine while he was driving up to the hanger. This way he would have a warm plane waiting for him. The challenge was to do this remotely by cell phone.

Search for a solution/platform

My first decision was whether to use Raspberry Pi or Arduino. I had used Raspberry Pi before but didn’t have much experience interfacing Raspberry Pi with GSM. The interface required a lot of shell programming. The connection of Arduino with the GSM chip appeared to be more straightforward and didn’t have major operating system steps. Plus I visited my brother in India who is an expert in microcontrollers. He showed me a installation of  a GPS/GSM board  for Arduino that would transmit constant location data. It used a SIM 800 chip. However, communicating with the GSM board required knowing and using a number “AT” commands, which seemed tedious. He suggested I look for a board that has a built in library of routines, that would do all the phone functions for me. We quickly came across the FONA 808 platform from Adafruit. Not only does it have a high quality board, it also comes with extensive library that one can utilize for making calls, sending and receiving text messages etc. I didn’t really need a GPS functionality but ordered a board with GPS capability just in case I need it later for another project.

I ordered a FONA 808 from Adafruit and also a Arduino Uno and a headset so that I could make voice calls Besides the FONA 808 I ordered a LiPoly battery, an indoor GPS and a flexible GSM antenna both with uFl connector and a TING SIM card that has low rates using a T-Mobile 2G network that runs GSM. USmobile also has some attractive low cost plans with low voice/data/sms usage as well.

Time to put everything together

I started soldering the pins for the FONA 808 board. After soldering a number of small Arduino boards this wasn’t  that difficult. I did use magnifying glass on a stand so that I could see the locations where I needed to solder the pins without damaging the board or the chip. The biggest challenge was trying to fit the uFL antenna’s on the board. The GSM antenna went  on easily. Unfortunately, the GPS antenna socket got bent and I no longer have that functionality!! Next time I will use a different antenna holder or be more careful with the installation.

I loaded the FONA library and was able to download a test program on the Arduino. By Christmas day I was sending and receiving messages and also able to make phone calls using my Samsung headphone/mic. I now had a working 2G cell phone on the T-Mobile GSM network.

Now that I had a working phone it was time to break down the project into  a series of steps. These were:
  1.  Learn how to turn on a relay on or off using the Arduino microcontroller.
  2. Receive a SMS message from another phone to Arduino through the GSM board.
  3. Parse SMS message on the Arduino and identify the “ON/OFF” command.
  4. Convert ON/OFF message to a command that turns on and off a relay switch.
  5. Attach a sensor to the Arduino and send temperature readings from the “engine” to the activating cell phone.


Turning on a relay: I used source code/board design from a Sparkfun kit.  I connected a electromechanical relay to two LEDs – a yellow for ON  and a red for OFF. The relay was activated when a ‘digitalwrite’ signal was sent to a relaypin either as a HIGH or a LOW. This message was sent to a transistor which either sends an ON or OFF signal to the relay. I was able test to test the functionality of the relay using a separate test sketch (see attached video), where the relay repeatedly comes on or off every few seconds.

Receive messages and parsing them: The next step was to receive messages on the Arduino controller using the FONA  board. Once I connected the Transmit (TRX) pin of the FONA with the Receive (RX) switch of the Arduino board (and vice versa) and powered the FONA board with a 5V and Ground for power from the Arduino board, the FONA was able to connect with a local cell phone tower and establish a GSM connection. This shows up as a blinking blue light. I used a FONA_SMS_Response example sketch from the FONA library.  The sketch receives a text message and sends an acknowledgement to the sender. From this code one can save the text message received and the CallerID of the phone that sent the activation SMS message.

Now that I had established the SMS functionality it was time to ‘recognize’ the SMS text.  It was easier to get input from the library as a character array then convert it to a ‘string’ object using the ‘String’ function. Once you have a string object you can manipulate it using a number of string functions such as substring. I had two code words embedded in my text message one for ON and the other for OFF. This ensures that the program only responds to a pre-programmed mobile number of the owner. My daughter asked that in case we get a text from an unauthorized phone we send them an electronic “Boo!” I was able to modify the original code for receiving messages and to it the command recognition section and a relay activation section.

An additional functionality was to periodically send the temperature of the ‘engine’ using a DHT22 sensor that senses both temperature and humidity. This sensor is connected to the Arduiono board through a digital pin 8. I included two libraries in my code the ‘DHT.h’ library for the temperature humidity sensor and ‘Adafruit_FONA.h’ for the SMS/Phone functionality of the FONA 808 card. The sensor sends temperature in F. the temperature reading is converted to a two decimal character string.  This character string is appended to a descriptor header “Relay On, Temp(F)=” and sent by return SMS to the phone number.  [I am still working on incorporating the time functionality in Arduino (someday through a working GPS card) so that the Arduino can periodically send a text message with temperature of the engine. The combined setup looks like below.
Figure 1 Complete setup - Arduino Uno, FONA 808 (foreground) a relay switch and a DHT sensor

Real World test: Now it was time to test a real world application. I bought a 5V relay module on eBay that can turn on or off a 110 V/10A electrical connection. I spliced open a two prong extension cord and connected one of the spliced ends to the middle slot and one to ‘Normal Open’ (NO) setting. I connected a lamp to the switch and was able to turn on and off the lamp using a SMS message!! In real life instead of a lamp the relay will turn on/off a 500 W heater blanket on the airplane.

Later on one evening my friend Sanjay was able to send a text message from Detroit suburbs to my relay and turn it on remotely.

All in all it was a very satisfying project to finished. I learned a lot about GSM hardware and string manipulation in C, to parse text commands out of SMS text. I also learned about relays. Finally, I discovered the world of inexpensive 2G cell phone plans where one can get 100 mins of talk and text for less than $10/month – what a huge difference compared to a $70-$80/month Verizon or AT&T plan.

Future Development

  1.        Activate the GPS chip to receive time and send out periodic SMS of the temperature of the         engine.
  2.        Shut off the relay when temperature reaches a certain value.
  3.        Instead of using SMS use a GPRS connection with a web server where the user can log into the web server and turn on or off the relay. It would be more heavy on data usage.