top of page

Email Controlled Thermostat

Here is the result of my latest quickly cobbled together project. A Video demonstration is on the way but until then...

I have rigged up some code and hardware that responds to specific email commands. A script written in Ruby pings the Gmail mail server looking for new emails. When one arrives it checks for one of two commands currently "Turn on heat" or "Turn off". If it sees an email that contains one of those, it communicates with the Arduino over serial which then controls the thermostat with a servo.

The script wasn't too hard to setup thanks to the fact that Ruby has built in email support. Just make sure your gmail is setup for POP3.

So in my example [fromname] represents the email service we a getting emails from and also sending response emails from. So this is the email account you would want to have set up just for receiving command emails. The [toname] is the email to send response emails to and also the email that will be sending to the emails to this script. These would be replaced by your values.

I don't think any of this code is too complex.It Opens an email port, reads new emails, checks the email message for certain strings and then sends out serial port command characters which the arduino then reads and moves the servo to the appropriate positions for my thermostat.

So with that said Here is some ruby code.... I use the fact that the Windows command line can send serial information out to send characters to the arduino. Thus avoiding all of the nasty serial port communication code.

This is setup specifically for gmail accounts. Gmail requires SSL for POP3 receiving and TLS for SMTP sending of emails. The system calls are what is controlling my Arduino code. I was just using 9600bps serial

require 'net/pop' require 'net/smtp' checkInterval = ARGV[0] Net::POP.enable_ssl(OpenSSL::SSL::VERIFY_NONE) puts "Email Checker!" system("mode COM3 BAUD=9600 PARITY=n DATA=8") while true do popreader = Net::POP3.start('pop.gmail.com',995,'[fromname]@gmail.com','password') msgStr = "" rtMsg = "" cmd= false if popreader.mails.empty? puts "No mail." else popreader.each_mail do |msg| msgStr = msg.pop # if cmd == false and msgStr.downcase.include? "[toname]@gmail.com" if msgStr.downcase.include? "turn on heat" puts "Heat turning on..." system("echo h > COM3") rtMsg = "Subject: Thermo Response; Heat On" sender = Net::SMTP.new 'smtp.gmail.com',587 sender.enable_starttls sender.start('google.com','[fromname]@gmail.com','password', :login) do sender.send_message(rtMsg,'[fromname]@gmail.com','[toname]@[todomain]') end elsif msgStr.downcase.include? "turn off" puts "Turning off..." system("echo o > COM3") rtMsg = "Subject: Thermo Response; Climate Control Off" sender = Net::SMTP.new 'smtp.gmail.com',587 sender.enable_starttls sender.start('google.com','[fromname]@gmail.com','password', :login) do sender.send_message(rtMsg,'[fromname]@gmail.com','[toname]@[todomain]') end end cmd = true msg.delete # end end end popreader.finish sleep(checkInterval.to_i) end

The Arduino Code is as follows...

#include <Servo.h> #define THERMO_ARM 3 Servo t_servo; int t_pos = 90; void setup() { t_servo.attach(THERMO_ARM); // attaches thermo arm on pin 3 to the servo object Serial.begin(9600); } void move(char dir){ switch(dir){ case 'h': t_pos=50; break; case 'c': t_pos=110; break; case 'o': t_pos=90; break; }//switch }//move

void loop() { char key = Serial.read(); move(key); t_servo.write(t_pos); delay(15); }

Comments


bottom of page