Evrythng API Example for W5500 (WIZnet chip)

Dependencies:   EvrythngApi mbed

Fork of EvrythngApiExample by Evry Thng

Evrythng Example for W5500.

main.cpp

Committer:
Bongjun
Date:
2014-08-26
Revision:
2:91570f1e9e4f
Parent:
1:5cdbdabe6577

File content as of revision 2:91570f1e9e4f:

/*
 * (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 = "893TNJV2vmdCsxDEpBqTwc2yIExFuPtuRInmRqGixhcpDZktrIv7YP2VeIsXrTMwtRF991OEJZutZYNI";

// Your thng id, the one that represents your mbed device.
const char* THNG_ID = "UdtxHEK4PeKwtwEnqa2wgshc";

// 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;
#if defined(TARGET_LPC1114)
    SPI spi(dp2, dp1, dp6); // mosi, miso, sclk
    EthernetInterface eth(&spi, dp25, dp26); // spi, cs, reset
    wait(1); // 1 second for stable state
#elif defined(TARGET_LPC1768)
    SPI spi(p11, p12, p13); // mosi, miso, sclk
    EthernetInterface eth(&spi, p14, p15); // spi, cs, reset
    wait(1); // 1 second for stable state
#elif defined(TARGET_LPC11U68)
    SPI spi(P0_9, P0_8, P1_29); // mosi, miso, sclk
    EthernetInterface eth(&spi, P0_2, P1_28);//, nRESET(p9); // reset pin is dummy, don't affect any pin of WIZ550io
    spi.format(8,0); // 8bit, mode 0
    spi.frequency(7000000); // 7MHz
    wait(1); // 1 second for stable state
#endif

    eth.init(); //Use DHCP
    dbg.printf("init\r\n");
    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
    int64_t timestamp = 1408494102000LL; // 08/20/2014 @ 12:21am in UTC

    /*


        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;
            }
        }
    */

// change to very simple demo, put value and get. finish.

    putVal = 28;
    dbg.printf("Putting value is %d\r\n", putVal);
    sprintf(strPutVal, "%d", putVal);
    timestamp++;
    api.setThngPropertyValue(THNG_ID, THNG_PROP_SET, string(strPutVal), timestamp);

    string v;
    int r = api.getThngPropertyValue(THNG_ID, THNG_PROP_GET, v);
    if (r == 0) {
        flash();
        int newVal = atoi(v.c_str());
        display(newVal);
        dbg.printf("Getting value is %d\r\n", newVal);
    }

    dbg.printf("Disconnecting ethernet interface\r\n");
    eth.disconnect();

    dbg.printf("Done.\r\n");
    while(1) {}
}