Controlling your coffee machine and your room’s light through the Web

(a.k.a. the SmoothWakeAlarm Project).

This is a smooth-wake-alarm system based on the Flyport Ethernet and Lighting Nest. The system can automatically dim your room light before you wake up and also activate the coffee machine on time. An Android app allows you to set the internal alarm clock and an alarm for activating the coffee machine.

The app communicates with the Flyport web server through a REST API. The Flyport includes all the essential libraries for time management (internal alarm, setting time automatically through SNTP). The Lighting Nest has 3 relay switches on board that can be used to activate any external circuit.

Equipment used:

  • 1 Flyport Ethernet module
  • 1 Flyport Lighting Nest
  • 1 Led
  • 1 Resistor (optionally for the LED)
  • Power source

Implementation:

The project consists of two parts: a) the Android application that will be used for setting the wake alarm and setting the time for dimming the light (LED) and activating the coffee machine (using the Lighting Nest’s relay switch). b) The hardware for activating the coffee machine and dimming the LED.

The Flyport Ethernet provides all the essential functionality for receiving the wakeup and coffee machine activation time (through the embedded Web Server) and includes libraries for time control (like getting actual time through SNTP and setting an internal alarm).

The Lighting Nest provides relay switches (up to 5A) that can be used to directly activate the coffee machine.

To emulate the light dimming, PWM is used on a LED. Lighting Nest provides a PWM output on the EXPANSION connector.

One of the cool things is that the time handling is performed completely on the Flyport module since the accompanying libraries make time handling a piece of cake!

The code:

The Flyport comes with a very handy and simple IDE for editing, compiling and uploaded the code on the board. It also comes with  The Flyport projects consist of several source files (many of them auto-generated when creating the project through the IDE wizard). 2 are the basic ones that need editing when creating a custom project: HTTPApp.c and taskFlyport.c. The first one includes all the essential libraries and routines for implementing the HTTP communication part of the Flyport web server (this also means handling the GET requests from clients) and the second one handles the logic flow of the program (variable initialization, main execution loop, etc.).

A sample of the HTTPApp.c that handles the GET request for setting the coffe alarm activation time follows:


HTTP_IO_RESULT HTTPExecuteGet(void)
{
BYTE *hrs;
BYTE *mins;
BYTE filename[20];

// STEP #1:
// The function MPFSGetFilename retrieves the name of the requested cgi,
// in this case "alarm.cgi" and puts it inside the filename variable.
// Make sure BYTE filename[] above is large enough for your longest name
MPFSGetFilename(curHTTP.file, filename, 20);

// STEP #2:
// Handling of the cgi requests

if(!memcmp(filename, "alarm.cgi", 9)) // Is the requested file name "alarm.cgi"?
{
// STEP #3:
// The complete request is contained inside the system variable curHTTP.data.
// Using the function HTTPGetArg is possible to read the arguments
// of the cgi request from curHTTP.data. In this case we are reading the
// argument "hours" and "minutes" from the request "alarm.cgi?hours=x&minutes=y" and we assign it to the
// respective variables.

hrs = HTTPGetArg(curHTTP.data, (BYTE *)"hours");
mins = HTTPGetArg(curHTTP.data, (BYTE *)"minutes");
hours = atoi((char*)hrs);
minutes = atoi((char*)mins);
alarmSet = TRUE;
UARTWrite(1,"Got Alarm request!");
}

if(!memcmp(filename, "coffee.cgi", 10)) // Is the requested file name "coffee.cgi"?
{
hrs = HTTPGetArg(curHTTP.data, (BYTE *)"hours");
mins = HTTPGetArg(curHTTP.data, (BYTE *)"minutes");
coffeeHours = atoi((char*)hrs);
coffeeMinutes = atoi((char*)mins);
coffeeAlarm = TRUE;
UARTWrite(1,"Got Alarm request!");
}
return HTTP_IO_DONE;
}

The taskFlyport.c that uses both the internal clock (RTCC) and SNTP for getting realt time from the Interner is the following:


#include "taskFlyport.h"
#include "time.h"
#include "rtcc.h"

//SETTINGS
int GMT_hour_adding = 3;
int LightDimmingOffset = 5; //Define how many minutes before alarm to start dimming the Light

//TIME VARIABLES
time_t now;
struct tm *ts;
DWORD epoch=0;
DWORD epochtime=0xA2C2A;
t_RTCC mytime;

char dateUTC[100];
char dateUTC1[50];
//HELPFUL VARIABLES
extern BOOL alarmflag;
extern int hours, minutes;
extern int coffeeHours, coffeeMinutes;
extern BOOL alarmSet;
extern BOOL coffeeAlarm;

//Dimming variables
BOOL startDimming;
int dimmStep;
int dimmPercentage;
void FlyportTask()
{
// Flyport waiting for the cable connection
while (!MACLinked);
vTaskDelay(100);
UARTWrite(1,"Flyport ethernet connected to the cable... hello world!\r\n");

vTaskDelay(200);
UARTWrite(1,"waiting...");

while(epoch<epochtime) {
vTaskDelay(50);
epoch=SNTPGetUTCSeconds();
}

UARTWrite(1, "done!\r\n");
coffeeHours = 0;

//GET SNTP time and set it:
epoch=SNTPGetUTCSeconds();
now=(time_t)epoch;
ts = localtime(&now);

ts->tm_hour = (ts->tm_hour + GMT_hour_adding);
// Correct if overflowed hour 0-24 format
if(ts->tm_hour > 24) {
ts->tm_hour = ts->tm_hour - 24;
}
else if(ts->tm_hour < 0) {
ts->tm_hour = ts->tm_hour +24;
}

//Set the RTC according to SNTP time
mytime.year = 12;
mytime.month = 4;
mytime.day = 21;
mytime.hour = ts->tm_hour;
mytime.min = ts->tm_min;
mytime.sec = ts->tm_sec;
RTCCWrite (&mytime);

//Initialize dimming variables
dimmStep = 100/LightDimmingOffset;
dimmPercentage = dimmStep;

while(1)
{
// Main user's firmware loop

//Read the time and check for alarms set
epoch=SNTPGetUTCSeconds();
now=(time_t)epoch;
ts = localtime(&now);

ts->tm_hour = (ts->tm_hour + GMT_hour_adding);
// Correct if overflowed hour 0-24 format
if(ts->tm_hour > 24) {
ts->tm_hour = ts->tm_hour - 24;
}
else if(ts->tm_hour < 0) {
ts->tm_hour = ts->tm_hour +24;
}

if(coffeeAlarm==TRUE) {
//set the coffee machine alarm:
t_RTCC myalarm;
myalarm.hour = coffeeHours;
myalarm.min = coffeeMinutes;
myalarm.sec = 00;
RTCCSetAlarm(&myalarm, 1, EVERY_DAY);
RTCCRunAlarm(1);

coffeeAlarm = FALSE;

//debug:
UARTWrite(1,"Coffee Alarm set!\r\n");
}

strftime(dateUTC1, sizeof(dateUTC1), "%H:%M.%S", ts);

sprintf(dateUTC,"%s\r\n",dateUTC1);
UARTWrite(1,dateUTC);

//Check the dimming alarm
if(alarmSet==TRUE) {
//check if it is dime to start dimming the light:
if(minutes < LightDimmingOffset) {
if((hours -1) == ts->tm_hour && (60-LightDimmingOffset+minutes) == ts->tm_min) {
//start dimming!
startDimming = TRUE;
alarmSet = FALSE;
//turn on the lamp
PWMInit(1,1000,5);
PWMOn(11, 1);
}

}

else {
if(hours == ts->tm_hour && (minutes-LightDimmingOffset) == ts->tm_min) {
//start dimming!
startDimming = TRUE;
alarmSet = FALSE;

//to be used with PWM on Flyport
//turn on the lamp
PWMInit(1,1000,5);
PWMOn(11, 1);
}

}

//Debug:
UARTWrite(1,"Alarm Set\r\n");
}

//Check if the coffee alarm has turned on
if (alarmflag==FALSE) {
UARTWrite(1,"Alarm not triggered\r\n");
}
else {
UARTWrite(1,"Alarm triggered!!!\r\n");
alarmflag = FALSE;
//turn on the coffee machine:
IOPut(P21, toggle);
}

//to be used with PWM on Flyport
//check if dimming has started:
if(startDimming==TRUE && dimmPercentage<100) {
PWMDuty(dimmPercentage, 1);
dimmPercentage+=dimmStep;
}

UARTFlush(1);

//Delay the loop for 10 secs
vTaskDelay(1000);
}
}

The Android application simply has a UI for setting the internal alarm clock and the coffee machine activation time. Both times are sent through a GET request to the Flyport Web server that handles the rest.

Check the video with the demo:

Get the complete Flyport and Android code here.

5 Comments

  1. turki khadija December 14, 2012 5:48 pm Reply

    I like so this code.
    I did a whole information about smart houses.
    It s a real big project that demand a lot of work and technical skills.

    http://www.openimpulse.com

  2. tomi May 8, 2014 8:01 pm Reply

    what we need to change in html if alarm clock we want to activate by web instead of android phone?

    • Charalampos May 14, 2014 2:04 pm Reply

      Hi Tomi!

      If you want to just use the internal webserver of the Flyport you need to make a GET request (on your browser for example) like the following:
      http://ip.address.of.flyport/alarm.cgi?hours=x&minutes=y
      where x and y are the hours and minutes to set the alarm to.

      That should work. Is this what you are asking for?

      Best,
      Charalampos

      • tomi May 25, 2014 9:39 pm Reply

        can you post simple example code in html please?

  3. tomi May 26, 2014 9:22 pm Reply

    Hi Charalampos again

    also i have a problem to implement your code on FlyportPro with starterKIT. Can you help me please!

Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>