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