google_calendar_and_mbed_controller

mBed - Google API source code

A few people have asked me to post the source code. Here it is. Even though I have been programming for years in various database and IBM mainframe languages, I've never done much C, so feel free to send me a few tips. I'm not sure why, for example, it works only when ctTime and buffer[21] are declared outside of the subroutine they're used in??

All the libraries used can all be found on the mbed website.

 #include "mbed.h"
 #include "DS18B20.h"
 #include "DS18S20.h"
 #include "OneWireDefs.h"
 #include "EthernetNetIf.h"
 #include "HTTPClient.h"
 #include "NTPClient.h"
 #define THERMOMETER DS18S20
 #define HOSTNAME "mbed"
 string calendar = "<<< YOUR CALENDAR PRIVATE ADDRESS >>>";
 EthernetNetIf eth(HOSTNAME);
 HTTPClient http;
 NTPClient ntp;
 THERMOMETER device(true, false, false, p21);
 DigitalOut led1(LED1, "led1");
 DigitalOut led2(LED2, "led2");
 DigitalOut led3(LED3, "led3");
 DigitalOut led4(LED4, "led4");
 DigitalOut heater(p10);
 time_t ctTime;
 char buffer[21];
 int targetTemp = 0;
 string getURL() {
  char url[256];
  char endTime[21];
  char startTime[21];
  ctTime = time(NULL);
  strftime(buffer, 20, "%Y-%m-%dT%H:%M:00Z", localtime(&ctTime));
  strcpy(startTime,buffer);
  ctTime = time(NULL)+60;
  strftime(buffer, 20, "%Y-%m-%dT%H:%M:00Z", localtime(&ctTime));
  strcpy(endTime,buffer);       
  //printf("start %s\n",startTime);
  //printf("end   %s\n",endTime);
  sprintf(url, "%s?start-min=%s&start-max=%s", calendar, startTime, endTime);
  //printf("url   %s\n",url);
  return url;
  }
  string getPage(char url[256]) {
  printf("\nQuery Google API... \n");
  HTTPText txt("text/html",5000);
  HTTPResult r = http.get(url, &txt);
 
  if (r==HTTP_OK) {
     printf("Result Ok\n");
     led2 = 0;
     return txt.gets();
  } else {
     printf("Error %d\n",r);
     led2 = 1;
     return "FAIL";
  }
  }
  void getAPI() {
  string testres;
  string results;
  char to[3];
  char url[256];
  strcpy(url,(char*)getURL().c_str());
  results = getPage(url);
  //printf("results API %s\n",results);
  
  if (results == "FAIL") {
     printf("%s\n","Bad Result");
     // Make no changes to the target temperature
  } else {
     testres = strstr(results.c_str(),"<title type='html'>");
     //printf("\n\n\n%s\n",testres);
     if (testres == "") {
        // no temperature set for this time
        printf("%s\n","No calendar entry for this time");
        targetTemp = 0;
        led3 = 0;
     } else {
        // grab the temperature (this assumes I've entered a 2 digit int in the event title)
        strncpy(to, testres.c_str()+19, 2);
        printf("result: %s\n", to);
        targetTemp = atoi(to);
        led3 = 1;
     }
  }
 }
 void getNTP() {
  ctTime = time(NULL);
  printf("Current time is (UTC): %d %s\n", ctTime, ctime(&ctTime));
  printf("NTP setTime...\n");
  Host server(IpAddr(), 123, "pool.ntp.org");
  printf("Result : %d\n", ntp.setTime(server));
  ctTime = time(NULL);
  printf("Time is now (UTC): %d %s\n", ctTime, ctime(&ctTime));
 }
 void checkTemperature() {
  float temp = device.readTemperature();
  if (temp > (targetTemp+0.5)) {
     heater = 0;
     led4 = 0;
  } 
  if (temp < targetTemp) {
     heater = 1;
     led4 = 1;
  }
 }
 int main() {
  EthernetErr ethErr;
  int count = 0;
  do {
      printf("Setting up %d...\n", ++count);
      ethErr = eth.setup();
      if (ethErr) printf("Timeout\n", ethErr);
  } while (ethErr != ETH_OK);
  printf("Connected OK\n");
  const char* hwAddr = eth.getHwAddr();
  printf("HW address : %02x:%02x:%02x:%02x:%02x:%02x\n",
         hwAddr[0], hwAddr[1], hwAddr[2],
         hwAddr[3], hwAddr[4], hwAddr[5]);
  IpAddr ethIp = eth.getIp();
  printf("IP address : %d.%d.%d.%d\n", ethIp[0], ethIp[1], ethIp[2], ethIp[3]);
  printf("Check router DHCP table for name : %s\n", eth.getHostname());
  getNTP();
  getAPI();
  
  Timer pollTemp;
  pollTemp.start();
  Timer pollAPI;
  pollAPI.start();
  Timer pollNTP;
  pollNTP.start();
  Timer lookAlive;
  lookAlive.start();
  
  while (true) {
      if (lookAlive.read() > 1) {
         led1 = !led1;
         lookAlive.start();
      }
      if (pollTemp.read() > 10) {
          checkTemperature();
          pollTemp.start();
      }
      if (pollAPI.read() > 30) {
          getAPI();
          printf("Target Temp: %i\n",targetTemp);
          pollAPI.start();
      }
      if (pollNTP.read() > 21000) {
          pollNTP.start();
          getNTP();
      }
  }
 }
google_calendar_and_mbed_controller.txt · Last modified: 2012/06/17 11:05 by tomgee