Xively Demo - WIZ550io, W5500 (WIZnet) + Temperature Sensor (DS18B20) EthernetInterfaceW5500 + LPC11U68 Xpresso v2 used
Dependencies: DS18B20_1wire W5500Interface libxively mbed
Fork of WIZ550io_Xively_Demo by
Xively Demo
Board Pic
LPC11U68 + WIZ550io (WIZnet) + DS18B20 Temp Sensor
PC Screenshot
main.cpp@8:a0edf4bce160, 2014-07-09 (annotated)
- Committer:
- Bongjun
- Date:
- Wed Jul 09 06:54:05 2014 +0000
- Revision:
- 8:a0edf4bce160
- Parent:
- 7:5ce11b80fb04
- Child:
- 10:6497effa1a26
subtle log print changes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 0:90ba0f51aa64 | 1 | #include "mbed.h" |
Bongjun | 7:5ce11b80fb04 | 2 | //#include "WiflyInterface.h" |
Bongjun | 7:5ce11b80fb04 | 3 | #include "EthernetInterfaceW5500.h" |
Bongjun | 7:5ce11b80fb04 | 4 | #include "DS18B20Sensor.h" |
lz307 | 6:3c44af154da8 | 5 | #include "xively.h" |
lz307 | 6:3c44af154da8 | 6 | #include "xi_err.h" |
lz307 | 6:3c44af154da8 | 7 | |
Bongjun | 8:a0edf4bce160 | 8 | //#define XI_FEED_ID 273104668 // set Xively Feed ID (numerical, no quoutes) |
Bongjun | 8:a0edf4bce160 | 9 | //#define XI_API_KEY "Your API Key" // set Xively API key (double-quoted string) |
Bongjun | 8:a0edf4bce160 | 10 | #define XI_FEED_ID 614972906 // set Xively Feed ID (numerical, no quoutes) |
Bongjun | 8:a0edf4bce160 | 11 | #define XI_API_KEY "Z2CG4NytDaxrk2LRSViyVVvi4kpWPH3bnrHeby24k8JCBZp1" // set Xively API key (double-quoted string) |
samux | 1:49e1e9ed6e39 | 12 | |
Bongjun | 7:5ce11b80fb04 | 13 | //Serial pc(USBTX, USBRX); |
Bongjun | 7:5ce11b80fb04 | 14 | DigitalOut myled(LED1); |
Bongjun | 7:5ce11b80fb04 | 15 | DS18B20Sensor sensor(P1_25); |
samux | 1:49e1e9ed6e39 | 16 | |
samux | 1:49e1e9ed6e39 | 17 | /* wifly object where: |
samux | 1:49e1e9ed6e39 | 18 | * - p9 and p10 are for the serial communication |
samux | 3:3b84102f022e | 19 | * - p25 is for the reset pin |
samux | 3:3b84102f022e | 20 | * - p26 is for the connection status |
samux | 1:49e1e9ed6e39 | 21 | * - "mbed" is the ssid of the network |
samux | 1:49e1e9ed6e39 | 22 | * - "password" is the password |
samux | 5:867d16e948eb | 23 | * - WPA is the security |
samux | 1:49e1e9ed6e39 | 24 | */ |
Bongjun | 7:5ce11b80fb04 | 25 | //WiflyInterface eth(p9, p10, p25, p26, "ssid", "passwd", WPA); |
lz307 | 6:3c44af154da8 | 26 | |
lz307 | 6:3c44af154da8 | 27 | int main() |
lz307 | 6:3c44af154da8 | 28 | { |
Bongjun | 7:5ce11b80fb04 | 29 | #if defined(TARGET_LPC1114) |
Bongjun | 7:5ce11b80fb04 | 30 | SPI spi(dp2, dp1, dp6); // mosi, miso, sclk |
Bongjun | 7:5ce11b80fb04 | 31 | EthernetInterfaceW5500 eth(&spi, dp25, dp26); // spi, cs, reset |
Bongjun | 7:5ce11b80fb04 | 32 | |
Bongjun | 7:5ce11b80fb04 | 33 | #elif defined(TARGET_LPC1768) |
Bongjun | 7:5ce11b80fb04 | 34 | SPI spi(p11, p12, p13); // mosi, miso, sclk |
Bongjun | 7:5ce11b80fb04 | 35 | EthernetInterfaceW5500 eth(&spi, p14, p15); // spi, cs, reset |
Bongjun | 7:5ce11b80fb04 | 36 | |
Bongjun | 7:5ce11b80fb04 | 37 | #elif defined(TARGET_LPC11U68) |
Bongjun | 7:5ce11b80fb04 | 38 | SPI spi(P0_9, P0_8, P1_29); // mosi, miso, sclk |
Bongjun | 7:5ce11b80fb04 | 39 | //WIZnetInterface eth(&spi, P0_2, P1_25); // spi, cs, reset |
Bongjun | 7:5ce11b80fb04 | 40 | //SPI spi(p5, p6, p7); // mosi, miso, sclk |
Bongjun | 7:5ce11b80fb04 | 41 | EthernetInterfaceW5500 eth(&spi, P0_2, P1_28);//, nRESET(p9); // reset pin is dummy, don't affect any pin of WIZ550io |
Bongjun | 7:5ce11b80fb04 | 42 | spi.format(8,0); // 8bit, mode 0 |
Bongjun | 7:5ce11b80fb04 | 43 | spi.frequency(7000000); // 7MHz |
Bongjun | 7:5ce11b80fb04 | 44 | wait(1); // 1 second for stable state |
Bongjun | 7:5ce11b80fb04 | 45 | |
Bongjun | 7:5ce11b80fb04 | 46 | #endif |
Bongjun | 7:5ce11b80fb04 | 47 | /* |
lz307 | 6:3c44af154da8 | 48 | int s = eth.init(); //Use DHCP |
lz307 | 6:3c44af154da8 | 49 | |
lz307 | 6:3c44af154da8 | 50 | if( s != NULL ) { |
lz307 | 6:3c44af154da8 | 51 | printf( "Could not initialise. Will halt!\n" ); |
lz307 | 6:3c44af154da8 | 52 | exit( 0 ); |
lz307 | 6:3c44af154da8 | 53 | } |
lz307 | 6:3c44af154da8 | 54 | |
lz307 | 6:3c44af154da8 | 55 | s = eth.connect(); |
lz307 | 6:3c44af154da8 | 56 | |
lz307 | 6:3c44af154da8 | 57 | if( s == false ) { |
lz307 | 6:3c44af154da8 | 58 | printf( "Could not connect. Will halt!\n" ); |
lz307 | 6:3c44af154da8 | 59 | exit( 0 ); |
lz307 | 6:3c44af154da8 | 60 | } else { |
lz307 | 6:3c44af154da8 | 61 | printf( "IP: %s\n", eth.getIPAddress() ); |
lz307 | 6:3c44af154da8 | 62 | } |
Bongjun | 7:5ce11b80fb04 | 63 | */ |
Bongjun | 7:5ce11b80fb04 | 64 | |
Bongjun | 7:5ce11b80fb04 | 65 | #ifdef _DHCP // didn't test yet in DHCP |
Bongjun | 7:5ce11b80fb04 | 66 | |
Bongjun | 7:5ce11b80fb04 | 67 | eth.init(); //Use DHCP |
Bongjun | 7:5ce11b80fb04 | 68 | eth.connect(); |
Bongjun | 7:5ce11b80fb04 | 69 | //printf("IP Address is %s\n\r", eth.getIPAddress()); |
Bongjun | 7:5ce11b80fb04 | 70 | printf("Initialized, MAC: %s\n", eth.getMACAddress()); |
Bongjun | 7:5ce11b80fb04 | 71 | printf("Connected, IP: %s, MASK: %s, GW: %s\n", |
Bongjun | 7:5ce11b80fb04 | 72 | eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway()); |
Bongjun | 7:5ce11b80fb04 | 73 | |
Bongjun | 7:5ce11b80fb04 | 74 | #else |
Bongjun | 7:5ce11b80fb04 | 75 | // as your env. change to real IP address and so on. |
Bongjun | 8:a0edf4bce160 | 76 | int ret = eth.init("222.98.173.212", "255.255.255.192", "222.98.173.254"); |
Bongjun | 7:5ce11b80fb04 | 77 | if (!ret) { |
Bongjun | 7:5ce11b80fb04 | 78 | printf("Initialized, MAC: %s\n", eth.getMACAddress()); |
Bongjun | 7:5ce11b80fb04 | 79 | printf("Connected, IP: %s, MASK: %s, GW: %s\n", |
Bongjun | 7:5ce11b80fb04 | 80 | eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway()); |
Bongjun | 7:5ce11b80fb04 | 81 | } else { |
Bongjun | 7:5ce11b80fb04 | 82 | printf("Error eth.init() - ret = %d\n", ret); |
Bongjun | 7:5ce11b80fb04 | 83 | return -1; |
Bongjun | 7:5ce11b80fb04 | 84 | } |
Bongjun | 7:5ce11b80fb04 | 85 | #endif |
Bongjun | 7:5ce11b80fb04 | 86 | |
Bongjun | 7:5ce11b80fb04 | 87 | // Sensor variables |
Bongjun | 7:5ce11b80fb04 | 88 | char sensorBuf[25]; |
Bongjun | 7:5ce11b80fb04 | 89 | uint8_t result; |
Bongjun | 7:5ce11b80fb04 | 90 | uint8_t sensor_cnt; |
Bongjun | 7:5ce11b80fb04 | 91 | |
Bongjun | 7:5ce11b80fb04 | 92 | //Before using this sensor, should called sensor.count() once. |
Bongjun | 7:5ce11b80fb04 | 93 | sensor_cnt = sensor.count(); |
lz307 | 6:3c44af154da8 | 94 | |
lz307 | 6:3c44af154da8 | 95 | xi_feed_t feed; |
lz307 | 6:3c44af154da8 | 96 | memset( &feed, NULL, sizeof( xi_feed_t ) ); |
lz307 | 6:3c44af154da8 | 97 | |
lz307 | 6:3c44af154da8 | 98 | feed.feed_id = XI_FEED_ID; |
lz307 | 6:3c44af154da8 | 99 | feed.datastream_count = 1; |
samux | 0:90ba0f51aa64 | 100 | |
lz307 | 6:3c44af154da8 | 101 | feed.datastreams[0].datapoint_count = 1; |
lz307 | 6:3c44af154da8 | 102 | xi_datastream_t* orientation_datastream = &feed.datastreams[0]; |
Bongjun | 7:5ce11b80fb04 | 103 | strcpy( orientation_datastream->datastream_id, "Channel_Test1" ); |
lz307 | 6:3c44af154da8 | 104 | xi_datapoint_t* current_orientation = &orientation_datastream->datapoints[0]; |
lz307 | 6:3c44af154da8 | 105 | |
lz307 | 6:3c44af154da8 | 106 | // create the cosm library context |
lz307 | 6:3c44af154da8 | 107 | xi_context_t* xi_context |
lz307 | 6:3c44af154da8 | 108 | = xi_create_context( XI_HTTP, XI_API_KEY, feed.feed_id ); |
lz307 | 6:3c44af154da8 | 109 | |
lz307 | 6:3c44af154da8 | 110 | // check if everything works |
lz307 | 6:3c44af154da8 | 111 | if( xi_context == NULL ) { |
lz307 | 6:3c44af154da8 | 112 | return -1; |
lz307 | 6:3c44af154da8 | 113 | } |
lz307 | 6:3c44af154da8 | 114 | |
lz307 | 6:3c44af154da8 | 115 | while(1) { |
Bongjun | 7:5ce11b80fb04 | 116 | if (sensor_cnt) { |
Bongjun | 7:5ce11b80fb04 | 117 | result = sensor.startReading(true); // start sensor readings and wait |
Bongjun | 7:5ce11b80fb04 | 118 | if (result == DS18X20_OK) { |
Bongjun | 7:5ce11b80fb04 | 119 | sensor.getReading(sensorBuf, 0); // get result into buf |
Bongjun | 7:5ce11b80fb04 | 120 | xi_set_value_str( current_orientation, sensorBuf ); |
Bongjun | 7:5ce11b80fb04 | 121 | } |
Bongjun | 7:5ce11b80fb04 | 122 | else |
Bongjun | 7:5ce11b80fb04 | 123 | { |
Bongjun | 7:5ce11b80fb04 | 124 | xi_set_value_str( current_orientation, "0" ); |
Bongjun | 7:5ce11b80fb04 | 125 | } |
Bongjun | 7:5ce11b80fb04 | 126 | |
Bongjun | 7:5ce11b80fb04 | 127 | } else { |
Bongjun | 7:5ce11b80fb04 | 128 | printf("No Sensor"); |
Bongjun | 7:5ce11b80fb04 | 129 | } |
Bongjun | 7:5ce11b80fb04 | 130 | |
Bongjun | 8:a0edf4bce160 | 131 | printf( "update..." ); |
lz307 | 6:3c44af154da8 | 132 | // send to xively server |
lz307 | 6:3c44af154da8 | 133 | xi_feed_update( xi_context, &feed ); |
lz307 | 6:3c44af154da8 | 134 | printf( "done...\n" ); |
Bongjun | 7:5ce11b80fb04 | 135 | wait( 10.0 ); |
lz307 | 6:3c44af154da8 | 136 | } |
lz307 | 6:3c44af154da8 | 137 | } |