google_calendar_and_mbed_controller
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
google_calendar_and_mbed_controller [2012/06/17 10:54] – created tomgee | google_calendar_and_mbed_controller [2012/06/17 11:05] (current) – tomgee | ||
---|---|---|---|
Line 1: | Line 1: | ||
{{: | {{: | ||
+ | |||
+ | |||
+ | ===== 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' | ||
+ | |||
+ | |||
+ | All the libraries used can all be found on the mbed website. | ||
+ | |||
+ | |||
+ | # | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | |||
+ | # | ||
+ | # | ||
+ | | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | | ||
+ | char buffer[21]; | ||
+ | int targetTemp = 0; | ||
+ | |||
+ | | ||
+ | char url[256]; | ||
+ | char endTime[21]; | ||
+ | char startTime[21]; | ||
+ | ctTime = time(NULL); | ||
+ | strftime(buffer, | ||
+ | strcpy(startTime, | ||
+ | ctTime = time(NULL)+60; | ||
+ | strftime(buffer, | ||
+ | strcpy(endTime, | ||
+ | // | ||
+ | // | ||
+ | sprintf(url, | ||
+ | // | ||
+ | return url; | ||
+ | } | ||
+ | |||
+ | string getPage(char url[256]) { | ||
+ | printf(" | ||
+ | HTTPText txt(" | ||
+ | HTTPResult r = http.get(url, | ||
+ | |||
+ | if (r==HTTP_OK) { | ||
+ | | ||
+ | led2 = 0; | ||
+ | | ||
+ | } else { | ||
+ | | ||
+ | led2 = 1; | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void getAPI() { | ||
+ | string testres; | ||
+ | string results; | ||
+ | char to[3]; | ||
+ | char url[256]; | ||
+ | strcpy(url, | ||
+ | results = getPage(url); | ||
+ | // | ||
+ | | ||
+ | if (results == " | ||
+ | | ||
+ | // Make no changes to the target temperature | ||
+ | } else { | ||
+ | | ||
+ | // | ||
+ | if (testres == "" | ||
+ | // no temperature set for this time | ||
+ | printf(" | ||
+ | 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, | ||
+ | printf(" | ||
+ | targetTemp = atoi(to); | ||
+ | led3 = 1; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void getNTP() { | ||
+ | ctTime = time(NULL); | ||
+ | printf(" | ||
+ | printf(" | ||
+ | Host server(IpAddr(), | ||
+ | printf(" | ||
+ | ctTime = time(NULL); | ||
+ | printf(" | ||
+ | } | ||
+ | |||
+ | void checkTemperature() { | ||
+ | float temp = device.readTemperature(); | ||
+ | if (temp > (targetTemp+0.5)) { | ||
+ | | ||
+ | led4 = 0; | ||
+ | } | ||
+ | if (temp < targetTemp) { | ||
+ | | ||
+ | led4 = 1; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | int main() { | ||
+ | EthernetErr ethErr; | ||
+ | int count = 0; | ||
+ | do { | ||
+ | printf(" | ||
+ | ethErr = eth.setup(); | ||
+ | if (ethErr) printf(" | ||
+ | } while (ethErr != ETH_OK); | ||
+ | |||
+ | printf(" | ||
+ | const char* hwAddr = eth.getHwAddr(); | ||
+ | printf(" | ||
+ | | ||
+ | | ||
+ | |||
+ | IpAddr ethIp = eth.getIp(); | ||
+ | printf(" | ||
+ | printf(" | ||
+ | |||
+ | 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; | ||
+ | | ||
+ | } | ||
+ | if (pollTemp.read() > 10) { | ||
+ | checkTemperature(); | ||
+ | pollTemp.start(); | ||
+ | } | ||
+ | if (pollAPI.read() > 30) { | ||
+ | getAPI(); | ||
+ | printf(" | ||
+ | pollAPI.start(); | ||
+ | } | ||
+ | if (pollNTP.read() > 21000) { | ||
+ | pollNTP.start(); | ||
+ | getNTP(); | ||
+ | } | ||
+ | } | ||
+ | } |
google_calendar_and_mbed_controller.1339944850.txt.gz · Last modified: 2012/06/17 10:54 by tomgee