Evrythng API Example for W5500 (WIZnet chip)

Dependencies:   EvrythngApi mbed

Fork of EvrythngApiExample by Evry Thng

Evrythng Example for W5500.

Committer:
Bongjun
Date:
Tue Aug 26 03:50:45 2014 +0000
Revision:
2:91570f1e9e4f
Parent:
1:5cdbdabe6577
first port project for Evrythng example with W5500Interface.; IE is ok, but in case of Chrome sometimes goes to block.;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vladounet 0:85d6be554642 1 /*
vladounet 0:85d6be554642 2 * (c) Copyright 2012 EVRYTHNG Ltd London / Zurich
vladounet 0:85d6be554642 3 * www.evrythng.com
vladounet 0:85d6be554642 4 *
vladounet 0:85d6be554642 5 * --- DISCLAIMER ---
vladounet 0:85d6be554642 6 *
vladounet 0:85d6be554642 7 * EVRYTHNG provides this source code "as is" and without warranty of any kind,
vladounet 0:85d6be554642 8 * and hereby disclaims all express or implied warranties, including without
vladounet 0:85d6be554642 9 * limitation warranties of merchantability, fitness for a particular purpose,
vladounet 0:85d6be554642 10 * performance, accuracy, reliability, and non-infringement.
vladounet 0:85d6be554642 11 *
vladounet 0:85d6be554642 12 *
vladounet 0:85d6be554642 13 * --- READ ME ---
vladounet 0:85d6be554642 14 *
vladounet 0:85d6be554642 15 * This is a demo application that uses EVRYTHNG's mbed wrapper to read and
vladounet 0:85d6be554642 16 * write value to EVRYTHNG's engine. Please refer to the online documentation
vladounet 0:85d6be554642 17 * available at http://dev.evrythng.com/mbed.
vladounet 0:85d6be554642 18 *
vladounet 0:85d6be554642 19 *
vladounet 0:85d6be554642 20 * Author: Michel Yerly
vladounet 0:85d6be554642 21 *
vladounet 0:85d6be554642 22 */
vladounet 0:85d6be554642 23
vladounet 0:85d6be554642 24 #include "mbed.h"
vladounet 0:85d6be554642 25 #include "EthernetInterface.h"
vladounet 0:85d6be554642 26 #include <time.h>
vladounet 0:85d6be554642 27 #include "EvrythngApi.h"
vladounet 0:85d6be554642 28 #include "util.h"
vladounet 0:85d6be554642 29 #include "eventqueue.h"
vladounet 0:85d6be554642 30
vladounet 0:85d6be554642 31 /*
vladounet 0:85d6be554642 32 * Configuration
vladounet 0:85d6be554642 33 */
Bongjun 1:5cdbdabe6577 34
vladounet 0:85d6be554642 35 // Your EVRYTHNG api key.
Bongjun 1:5cdbdabe6577 36 const char* API_KEY = "893TNJV2vmdCsxDEpBqTwc2yIExFuPtuRInmRqGixhcpDZktrIv7YP2VeIsXrTMwtRF991OEJZutZYNI";
vladounet 0:85d6be554642 37
vladounet 0:85d6be554642 38 // Your thng id, the one that represents your mbed device.
Bongjun 1:5cdbdabe6577 39 const char* THNG_ID = "UdtxHEK4PeKwtwEnqa2wgshc";
vladounet 0:85d6be554642 40
vladounet 0:85d6be554642 41 // The property you want values to be pushed to.
vladounet 0:85d6be554642 42 const char* THNG_PROP_SET = "prop3";
vladounet 0:85d6be554642 43
vladounet 0:85d6be554642 44 // The property you want values to be read from.
vladounet 0:85d6be554642 45 const char* THNG_PROP_GET = "prop3";
vladounet 0:85d6be554642 46
vladounet 0:85d6be554642 47
vladounet 0:85d6be554642 48
vladounet 0:85d6be554642 49 DigitalOut led1(LED1);
vladounet 0:85d6be554642 50 DigitalOut led2(LED2);
vladounet 0:85d6be554642 51 DigitalOut led3(LED3);
vladounet 0:85d6be554642 52 DigitalOut led4(LED4);
vladounet 0:85d6be554642 53
vladounet 0:85d6be554642 54 static char displayVal = 0;
vladounet 0:85d6be554642 55
vladounet 0:85d6be554642 56 /*
vladounet 0:85d6be554642 57 * Displays a value between 0 and 15 as binary on the 4 LEDs.
vladounet 0:85d6be554642 58 */
vladounet 0:85d6be554642 59 void display(char x)
vladounet 0:85d6be554642 60 {
vladounet 0:85d6be554642 61 led1 = x & (char)8 ? 1 : 0;
vladounet 0:85d6be554642 62 led2 = x & (char)4 ? 1 : 0;
vladounet 0:85d6be554642 63 led3 = x & (char)2 ? 1 : 0;
vladounet 0:85d6be554642 64 led4 = x & (char)1 ? 1 : 0;
vladounet 0:85d6be554642 65 displayVal = x;
vladounet 0:85d6be554642 66 }
vladounet 0:85d6be554642 67
vladounet 0:85d6be554642 68 /*
vladounet 0:85d6be554642 69 * Flashes the 4 LEDs, and restores the state of the last display().
vladounet 0:85d6be554642 70 */
vladounet 0:85d6be554642 71 void flash()
vladounet 0:85d6be554642 72 {
vladounet 0:85d6be554642 73 char s = displayVal;
vladounet 0:85d6be554642 74 for (int i = 0; i < 2; ++i) {
vladounet 0:85d6be554642 75 display(0x0F);
vladounet 0:85d6be554642 76 wait(0.1);
vladounet 0:85d6be554642 77 display(0x00);
vladounet 0:85d6be554642 78 wait(0.1);
vladounet 0:85d6be554642 79 }
vladounet 0:85d6be554642 80 display(s);
vladounet 0:85d6be554642 81 }
vladounet 0:85d6be554642 82
Bongjun 1:5cdbdabe6577 83 /*
vladounet 0:85d6be554642 84 * Function to get the number of milliseconds elapsed since the system startup.
vladounet 0:85d6be554642 85 * It is imprecise and if called repetitively and quickly, it may always return
vladounet 0:85d6be554642 86 * the same result. It MUST be called at least every 30 minutes.
vladounet 0:85d6be554642 87 * NOTE: I'm not proud of it.
vladounet 0:85d6be554642 88 */
vladounet 0:85d6be554642 89 int64_t getMillis()
vladounet 0:85d6be554642 90 {
vladounet 0:85d6be554642 91 static Timer tmr;
vladounet 0:85d6be554642 92 static int64_t clk = 0;
Bongjun 1:5cdbdabe6577 93
vladounet 0:85d6be554642 94 tmr.stop();
vladounet 0:85d6be554642 95 clk += tmr.read_ms();
vladounet 0:85d6be554642 96 tmr.reset();
vladounet 0:85d6be554642 97 tmr.start();
Bongjun 1:5cdbdabe6577 98
vladounet 0:85d6be554642 99 char str[21];
vladounet 0:85d6be554642 100 char* end;
vladounet 0:85d6be554642 101 sprinti64(str, clk, &end);
vladounet 0:85d6be554642 102 *end = '\0';
vladounet 0:85d6be554642 103 dbg.printf("%s ", str);
Bongjun 1:5cdbdabe6577 104
vladounet 0:85d6be554642 105 return clk;
vladounet 0:85d6be554642 106 }
vladounet 0:85d6be554642 107
vladounet 0:85d6be554642 108 /*
vladounet 0:85d6be554642 109 * Entry point.
vladounet 0:85d6be554642 110 *
vladounet 0:85d6be554642 111 * This demo application uses an event loop to set and get property values
vladounet 0:85d6be554642 112 * on EVRYTHNG engine at given intervals.
vladounet 0:85d6be554642 113 */
vladounet 0:85d6be554642 114 int main()
vladounet 0:85d6be554642 115 {
vladounet 0:85d6be554642 116 dbg.printf("Initializing ethernet interface\r\n");
Bongjun 1:5cdbdabe6577 117 // EthernetInterface eth;
Bongjun 1:5cdbdabe6577 118 #if defined(TARGET_LPC1114)
Bongjun 1:5cdbdabe6577 119 SPI spi(dp2, dp1, dp6); // mosi, miso, sclk
Bongjun 1:5cdbdabe6577 120 EthernetInterface eth(&spi, dp25, dp26); // spi, cs, reset
Bongjun 1:5cdbdabe6577 121 wait(1); // 1 second for stable state
Bongjun 1:5cdbdabe6577 122 #elif defined(TARGET_LPC1768)
Bongjun 1:5cdbdabe6577 123 SPI spi(p11, p12, p13); // mosi, miso, sclk
Bongjun 1:5cdbdabe6577 124 EthernetInterface eth(&spi, p14, p15); // spi, cs, reset
Bongjun 1:5cdbdabe6577 125 wait(1); // 1 second for stable state
Bongjun 1:5cdbdabe6577 126 #elif defined(TARGET_LPC11U68)
Bongjun 1:5cdbdabe6577 127 SPI spi(P0_9, P0_8, P1_29); // mosi, miso, sclk
Bongjun 1:5cdbdabe6577 128 EthernetInterface eth(&spi, P0_2, P1_28);//, nRESET(p9); // reset pin is dummy, don't affect any pin of WIZ550io
Bongjun 1:5cdbdabe6577 129 spi.format(8,0); // 8bit, mode 0
Bongjun 1:5cdbdabe6577 130 spi.frequency(7000000); // 7MHz
Bongjun 1:5cdbdabe6577 131 wait(1); // 1 second for stable state
Bongjun 1:5cdbdabe6577 132 #endif
Bongjun 1:5cdbdabe6577 133
vladounet 0:85d6be554642 134 eth.init(); //Use DHCP
Bongjun 1:5cdbdabe6577 135 dbg.printf("init\r\n");
vladounet 0:85d6be554642 136 eth.connect();
vladounet 0:85d6be554642 137 dbg.printf("IP address: %s\r\n", eth.getIPAddress());
vladounet 0:85d6be554642 138
vladounet 0:85d6be554642 139 EvrythngApi api(API_KEY);
vladounet 0:85d6be554642 140
Bongjun 1:5cdbdabe6577 141 /*
Bongjun 1:5cdbdabe6577 142 // intervals in ms
Bongjun 1:5cdbdabe6577 143 const int SET_INTERVAL = 5000;
Bongjun 1:5cdbdabe6577 144 const int GET_INTERVAL = 2000;
Bongjun 1:5cdbdabe6577 145 */
vladounet 0:85d6be554642 146 int curVal = 0;
vladounet 0:85d6be554642 147 display(curVal);
vladounet 0:85d6be554642 148
vladounet 0:85d6be554642 149 int putVal = 0;
Bongjun 1:5cdbdabe6577 150 char strPutVal[3];
Bongjun 1:5cdbdabe6577 151 // int64_t timestamp = 1345736694000LL; // 2012-08-23 10:44:54am EST
Bongjun 1:5cdbdabe6577 152 int64_t timestamp = 1408494102000LL; // 08/20/2014 @ 12:21am in UTC
Bongjun 1:5cdbdabe6577 153
Bongjun 1:5cdbdabe6577 154 /*
Bongjun 1:5cdbdabe6577 155
vladounet 0:85d6be554642 156
Bongjun 1:5cdbdabe6577 157 EventQueue eventQueue;
Bongjun 1:5cdbdabe6577 158 eventQueue.put(getMillis()+GET_INTERVAL, EVT_GET_PROP);
Bongjun 1:5cdbdabe6577 159 eventQueue.put(getMillis()+SET_INTERVAL, EVT_SET_PROP);
Bongjun 1:5cdbdabe6577 160
Bongjun 1:5cdbdabe6577 161 while (!eventQueue.empty()) {
Bongjun 1:5cdbdabe6577 162 EventType event = eventQueue.waitNext(getMillis());
vladounet 0:85d6be554642 163
Bongjun 1:5cdbdabe6577 164 switch (event) {
Bongjun 1:5cdbdabe6577 165
Bongjun 1:5cdbdabe6577 166 case EVT_SET_PROP:
Bongjun 1:5cdbdabe6577 167 eventQueue.put(SET_INTERVAL + getMillis(), EVT_SET_PROP);
vladounet 0:85d6be554642 168
Bongjun 1:5cdbdabe6577 169 putVal = (putVal + 1) & 0xF;
Bongjun 1:5cdbdabe6577 170 sprintf(strPutVal, "%d", putVal);
Bongjun 1:5cdbdabe6577 171 timestamp++;
Bongjun 1:5cdbdabe6577 172 api.setThngPropertyValue(THNG_ID, THNG_PROP_SET, string(strPutVal), timestamp);
vladounet 0:85d6be554642 173
Bongjun 1:5cdbdabe6577 174 break;
vladounet 0:85d6be554642 175
Bongjun 1:5cdbdabe6577 176 case EVT_GET_PROP:
Bongjun 1:5cdbdabe6577 177 eventQueue.put(GET_INTERVAL + getMillis(), EVT_GET_PROP);
vladounet 0:85d6be554642 178
Bongjun 1:5cdbdabe6577 179 string v;
Bongjun 1:5cdbdabe6577 180 int r = api.getThngPropertyValue(THNG_ID, THNG_PROP_GET, v);
Bongjun 1:5cdbdabe6577 181 if (r == 0) {
Bongjun 1:5cdbdabe6577 182 flash();
Bongjun 1:5cdbdabe6577 183 int newVal = atoi(v.c_str());
Bongjun 1:5cdbdabe6577 184 if (newVal != curVal) {
Bongjun 1:5cdbdabe6577 185 curVal = newVal;
Bongjun 1:5cdbdabe6577 186 display(curVal);
Bongjun 1:5cdbdabe6577 187 }
Bongjun 1:5cdbdabe6577 188 }
Bongjun 1:5cdbdabe6577 189 break;
Bongjun 1:5cdbdabe6577 190 }
Bongjun 1:5cdbdabe6577 191 }
Bongjun 1:5cdbdabe6577 192 */
vladounet 0:85d6be554642 193
Bongjun 1:5cdbdabe6577 194 // change to very simple demo, put value and get. finish.
Bongjun 1:5cdbdabe6577 195
Bongjun 1:5cdbdabe6577 196 putVal = 28;
Bongjun 1:5cdbdabe6577 197 dbg.printf("Putting value is %d\r\n", putVal);
Bongjun 1:5cdbdabe6577 198 sprintf(strPutVal, "%d", putVal);
Bongjun 1:5cdbdabe6577 199 timestamp++;
Bongjun 1:5cdbdabe6577 200 api.setThngPropertyValue(THNG_ID, THNG_PROP_SET, string(strPutVal), timestamp);
Bongjun 1:5cdbdabe6577 201
Bongjun 1:5cdbdabe6577 202 string v;
Bongjun 1:5cdbdabe6577 203 int r = api.getThngPropertyValue(THNG_ID, THNG_PROP_GET, v);
Bongjun 1:5cdbdabe6577 204 if (r == 0) {
Bongjun 1:5cdbdabe6577 205 flash();
Bongjun 1:5cdbdabe6577 206 int newVal = atoi(v.c_str());
Bongjun 1:5cdbdabe6577 207 display(newVal);
Bongjun 1:5cdbdabe6577 208 dbg.printf("Getting value is %d\r\n", newVal);
vladounet 0:85d6be554642 209 }
vladounet 0:85d6be554642 210
vladounet 0:85d6be554642 211 dbg.printf("Disconnecting ethernet interface\r\n");
vladounet 0:85d6be554642 212 eth.disconnect();
vladounet 0:85d6be554642 213
vladounet 0:85d6be554642 214 dbg.printf("Done.\r\n");
vladounet 0:85d6be554642 215 while(1) {}
vladounet 0:85d6be554642 216 }