Sample code that uses EvrythngApi library.
Dependencies: EvrythngApi mbed
Revision 0:85d6be554642, committed 2012-08-30
- Comitter:
- vladounet
- Date:
- Thu Aug 30 13:57:45 2012 +0000
- Commit message:
- Initial commit
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EvrythngApi.lib Thu Aug 30 13:57:45 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/vladounet/code/EvrythngApi/#7162d0e030f5
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/eventqueue.cpp Thu Aug 30 13:57:45 2012 +0000 @@ -0,0 +1,46 @@ +/* + * (c) Copyright 2012 EVRYTHNG Ltd London / Zurich + * www.evrythng.com + * + * --- DISCLAIMER --- + * + * EVRYTHNG provides this source code "as is" and without warranty of any kind, + * and hereby disclaims all express or implied warranties, including without + * limitation warranties of merchantability, fitness for a particular purpose, + * performance, accuracy, reliability, and non-infringement. + * + * Author: Michel Yerly + * + */ +#include "mbed.h" +#include "eventqueue.h" +#include <time.h> + +#include "util.h" + +void EventQueue::put(int64_t time, EventType type) +{ + Event event; + event.time = time; + event.type = type; + queue.push(event); +} + +EventType EventQueue::waitNext(int64_t currentTime) +{ + Event event = queue.top(); + queue.pop(); + int64_t w = event.time - currentTime; + if (w > 0) wait_ms(w); + return event.type; +} + +bool EventQueue::empty() +{ + return queue.empty(); +} + +int EventQueue::size() +{ + return queue.size(); +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/eventqueue.h Thu Aug 30 13:57:45 2012 +0000 @@ -0,0 +1,45 @@ +/* + * (c) Copyright 2012 EVRYTHNG Ltd London / Zurich + * www.evrythng.com + * + * --- DISCLAIMER --- + * + * EVRYTHNG provides this source code "as is" and without warranty of any kind, + * and hereby disclaims all express or implied warranties, including without + * limitation warranties of merchantability, fitness for a particular purpose, + * performance, accuracy, reliability, and non-infringement. + * + * Author: Michel Yerly + * + */ +#include <queue> +#include <stdint.h> + +enum EventType { + EVT_SET_PROP, + EVT_GET_PROP +}; + +struct Event { + int64_t time; + EventType type; +}; + +class EventComparison +{ +public: + bool operator() (const Event& lhs, const Event&rhs) const { + return (lhs.time>rhs.time); + } +}; + +class EventQueue +{ +public: + void put(int64_t time, EventType type); + EventType waitNext(int64_t currentTime); + bool empty(); + int size(); +private: + std::priority_queue<Event, std::vector<Event>, EventComparison> queue; +}; \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Aug 30 13:57:45 2012 +0000 @@ -0,0 +1,178 @@ +/* + * (c) Copyright 2012 EVRYTHNG Ltd London / Zurich + * www.evrythng.com + * + * --- DISCLAIMER --- + * + * EVRYTHNG provides this source code "as is" and without warranty of any kind, + * and hereby disclaims all express or implied warranties, including without + * limitation warranties of merchantability, fitness for a particular purpose, + * performance, accuracy, reliability, and non-infringement. + * + * + * --- READ ME --- + * + * This is a demo application that uses EVRYTHNG's mbed wrapper to read and + * write value to EVRYTHNG's engine. Please refer to the online documentation + * available at http://dev.evrythng.com/mbed. + * + * + * Author: Michel Yerly + * + */ + +#include "mbed.h" +#include "EthernetInterface.h" +#include <time.h> +#include "EvrythngApi.h" +#include "util.h" +#include "eventqueue.h" + +/* + * Configuration + */ + +// Your EVRYTHNG api key. +const char* API_KEY = ""; + +// Your thng id, the one that represents your mbed device. +const char* THNG_ID = ""; + +// The property you want values to be pushed to. +const char* THNG_PROP_SET = "prop3"; + +// The property you want values to be read from. +const char* THNG_PROP_GET = "prop3"; + + + +DigitalOut led1(LED1); +DigitalOut led2(LED2); +DigitalOut led3(LED3); +DigitalOut led4(LED4); + +static char displayVal = 0; + +/* + * Displays a value between 0 and 15 as binary on the 4 LEDs. + */ +void display(char x) +{ + led1 = x & (char)8 ? 1 : 0; + led2 = x & (char)4 ? 1 : 0; + led3 = x & (char)2 ? 1 : 0; + led4 = x & (char)1 ? 1 : 0; + displayVal = x; +} + +/* + * Flashes the 4 LEDs, and restores the state of the last display(). + */ +void flash() +{ + char s = displayVal; + for (int i = 0; i < 2; ++i) { + display(0x0F); + wait(0.1); + display(0x00); + wait(0.1); + } + display(s); +} + +/* + * Function to get the number of milliseconds elapsed since the system startup. + * It is imprecise and if called repetitively and quickly, it may always return + * the same result. It MUST be called at least every 30 minutes. + * NOTE: I'm not proud of it. + */ +int64_t getMillis() +{ + static Timer tmr; + static int64_t clk = 0; + + tmr.stop(); + clk += tmr.read_ms(); + tmr.reset(); + tmr.start(); + + char str[21]; + char* end; + sprinti64(str, clk, &end); + *end = '\0'; + dbg.printf("%s ", str); + + return clk; +} + +/* + * Entry point. + * + * This demo application uses an event loop to set and get property values + * on EVRYTHNG engine at given intervals. + */ +int main() +{ + dbg.printf("Initializing ethernet interface\r\n"); + EthernetInterface eth; + eth.init(); //Use DHCP + eth.connect(); + dbg.printf("IP address: %s\r\n", eth.getIPAddress()); + + EvrythngApi api(API_KEY); + + // intervals in ms + const int SET_INTERVAL = 5000; + const int GET_INTERVAL = 2000; + + int curVal = 0; + display(curVal); + + int putVal = 0; + char strPutVal[3]; + int64_t timestamp = 1345736694000LL; // 2012-08-23 10:44:54am EST + + EventQueue eventQueue; + eventQueue.put(getMillis()+GET_INTERVAL, EVT_GET_PROP); + eventQueue.put(getMillis()+SET_INTERVAL, EVT_SET_PROP); + + while (!eventQueue.empty()) { + EventType event = eventQueue.waitNext(getMillis()); + + switch (event) { + + case EVT_SET_PROP: + + eventQueue.put(SET_INTERVAL + getMillis(), EVT_SET_PROP); + + putVal = (putVal + 1) & 0xF; + sprintf(strPutVal, "%d", putVal); + timestamp++; + api.setThngPropertyValue(THNG_ID, THNG_PROP_SET, string(strPutVal), timestamp); + + break; + + case EVT_GET_PROP: + + eventQueue.put(GET_INTERVAL + getMillis(), EVT_GET_PROP); + + string v; + int r = api.getThngPropertyValue(THNG_ID, THNG_PROP_GET, v); + if (r == 0) { + flash(); + int newVal = atoi(v.c_str()); + if (newVal != curVal) { + curVal = newVal; + display(curVal); + } + } + break; + } + } + + dbg.printf("Disconnecting ethernet interface\r\n"); + eth.disconnect(); + + dbg.printf("Done.\r\n"); + while(1) {} +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Aug 30 13:57:45 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/10b9abbe79a6 \ No newline at end of file