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 Bongjun Hur

Xively Demo

Board Pic

LPC11U68 + WIZ550io (WIZnet) + DS18B20 Temp Sensor https://31.media.tumblr.com/d63982be5ee62cb7889cde9e685ee757/tumblr_n8s27sY62b1sama12o1_500.jpg

PC Screenshot

/media/uploads/Bongjun/img135.png

Committer:
kaizen
Date:
Mon Oct 13 10:29:06 2014 +0000
Revision:
14:5a9ba3d8c9ca
Parent:
13:ab5db87a253a
Modified for using KL25Z Board and change to W5500Interface for using latest version.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 0:90ba0f51aa64 1 #include "mbed.h"
kaizen 14:5a9ba3d8c9ca 2 #include "W5500Interface/EthernetInterface.h"
lz307 6:3c44af154da8 3 #include "xively.h"
lz307 6:3c44af154da8 4 #include "xi_err.h"
lz307 6:3c44af154da8 5
kaizen 14:5a9ba3d8c9ca 6 #define XI_FEED_ID YOUR-FEED-ID // set Xively Feed ID (numerical, no quoutes)
kaizen 14:5a9ba3d8c9ca 7 #define XI_API_KEY "Your API Key" // set Xively API key (double-quoted string)
samux 1:49e1e9ed6e39 8
Bongjun 7:5ce11b80fb04 9 //Serial pc(USBTX, USBRX);
Bongjun 7:5ce11b80fb04 10 DigitalOut myled(LED1);
kaizen 14:5a9ba3d8c9ca 11
kaizen 14:5a9ba3d8c9ca 12 #define _SENSOR_DS18B20
Bongjun 10:6497effa1a26 13
kaizen 14:5a9ba3d8c9ca 14 #ifdef _SENSOR_DS18B20 // if there is sensor.
Bongjun 10:6497effa1a26 15 #include "DS18B20.h"
Bongjun 10:6497effa1a26 16 #endif
samux 1:49e1e9ed6e39 17
kaizen 14:5a9ba3d8c9ca 18 #define _DHCP
Bongjun 13:ab5db87a253a 19
lz307 6:3c44af154da8 20
lz307 6:3c44af154da8 21 int main()
lz307 6:3c44af154da8 22 {
Bongjun 7:5ce11b80fb04 23 #if defined(TARGET_LPC1114)
Bongjun 7:5ce11b80fb04 24 SPI spi(dp2, dp1, dp6); // mosi, miso, sclk
kaizen 14:5a9ba3d8c9ca 25 EthernetInterface eth(&spi, dp25, dp26); // spi, cs, reset
kaizen 14:5a9ba3d8c9ca 26 DS18B20 sensor(D3, DS18B20::RES_12_BIT); // Dallas 1-wire
Bongjun 7:5ce11b80fb04 27 #elif defined(TARGET_LPC1768)
Bongjun 7:5ce11b80fb04 28 SPI spi(p11, p12, p13); // mosi, miso, sclk
kaizen 14:5a9ba3d8c9ca 29 EthernetInterface eth(&spi, p14, p15); // spi, cs, reset
Bongjun 7:5ce11b80fb04 30 #elif defined(TARGET_LPC11U68)
Bongjun 7:5ce11b80fb04 31 SPI spi(P0_9, P0_8, P1_29); // mosi, miso, sclk
kaizen 14:5a9ba3d8c9ca 32 EthernetInterface eth(&spi, P0_2, P1_28);//, nRESET(p9); // reset pin is dummy, don't affect any pin of WIZ550io
Bongjun 7:5ce11b80fb04 33 spi.format(8,0); // 8bit, mode 0
Bongjun 7:5ce11b80fb04 34 spi.frequency(7000000); // 7MHz
Bongjun 7:5ce11b80fb04 35 wait(1); // 1 second for stable state
kaizen 14:5a9ba3d8c9ca 36 DS18B20 sensor(P1_25, DS18B20::RES_12_BIT); // Dallas 1-wire
kaizen 14:5a9ba3d8c9ca 37 #elif defined(TARGET_KL25Z)
kaizen 14:5a9ba3d8c9ca 38 Serial pc(USBTX, USBRX);
kaizen 14:5a9ba3d8c9ca 39 pc.baud(115200);
kaizen 14:5a9ba3d8c9ca 40 printf("spi init\r\n");
kaizen 14:5a9ba3d8c9ca 41 SPI spi(D11, D12, D13); // mosi, miso, sclk
kaizen 14:5a9ba3d8c9ca 42 wait(1); // 1 second for stable state
kaizen 14:5a9ba3d8c9ca 43 EthernetInterface eth(&spi, D10, D9);//scs(D10), nRESET(PTA20)
kaizen 14:5a9ba3d8c9ca 44 printf("App Start\r\n");
kaizen 14:5a9ba3d8c9ca 45 wait(1); // 1 second for stable state
kaizen 14:5a9ba3d8c9ca 46 DS18B20 sensor(D3, DS18B20::RES_12_BIT); // Dallas 1-wire
kaizen 14:5a9ba3d8c9ca 47 #elif defined(TARGET_NUCLEO_F030R8)
kaizen 14:5a9ba3d8c9ca 48 Serial pc(USBTX, USBRX);
kaizen 14:5a9ba3d8c9ca 49 pc.baud(115200);
kaizen 14:5a9ba3d8c9ca 50 printf("spi init\r\n");
kaizen 14:5a9ba3d8c9ca 51 SPI spi(D11, D12, D13); // mosi, miso, sclk
kaizen 14:5a9ba3d8c9ca 52 wait(1); // 1 second for stable state
kaizen 14:5a9ba3d8c9ca 53 EthernetInterface eth(&spi, D10, D9);//scs(D10), nRESET(PTA20)
kaizen 14:5a9ba3d8c9ca 54 printf("App Start\r\n");
kaizen 14:5a9ba3d8c9ca 55 wait(1); // 1 second for stable state
Bongjun 7:5ce11b80fb04 56 #endif
Bongjun 7:5ce11b80fb04 57
Bongjun 7:5ce11b80fb04 58 #ifdef _DHCP // didn't test yet in DHCP
Bongjun 7:5ce11b80fb04 59 eth.init(); //Use DHCP
Bongjun 7:5ce11b80fb04 60 eth.connect();
Bongjun 7:5ce11b80fb04 61 printf("Initialized, MAC: %s\n", eth.getMACAddress());
Bongjun 7:5ce11b80fb04 62 printf("Connected, IP: %s, MASK: %s, GW: %s\n",
Bongjun 7:5ce11b80fb04 63 eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
Bongjun 7:5ce11b80fb04 64 #else
Bongjun 7:5ce11b80fb04 65 // as your env. change to real IP address and so on.
Bongjun 13:ab5db87a253a 66 int ret = eth.init("222.98.173.211", "255.255.255.192", "222.98.173.254");
Bongjun 7:5ce11b80fb04 67 if (!ret) {
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 } else {
Bongjun 7:5ce11b80fb04 72 printf("Error eth.init() - ret = %d\n", ret);
Bongjun 7:5ce11b80fb04 73 return -1;
Bongjun 7:5ce11b80fb04 74 }
Bongjun 7:5ce11b80fb04 75 #endif
Bongjun 7:5ce11b80fb04 76
Bongjun 7:5ce11b80fb04 77 // Sensor variables
kaizen 14:5a9ba3d8c9ca 78 #ifdef _SENSOR_DS18B20 // if there is sensor.
Bongjun 7:5ce11b80fb04 79 char sensorBuf[25];
kaizen 14:5a9ba3d8c9ca 80
Bongjun 10:6497effa1a26 81 //Before using this sensor, should called sensor.count() once.
Bongjun 10:6497effa1a26 82 printf("DS18B20 Configuration\n\r");
Bongjun 10:6497effa1a26 83 DS18B20::ROM_Code_t ROM_Code;
Bongjun 10:6497effa1a26 84 sensor.ReadROM(&ROM_Code);
Bongjun 10:6497effa1a26 85 printf("Family code: 0x%X\n\r", ROM_Code.BYTES.familyCode);
Bongjun 10:6497effa1a26 86 printf("Serial Number: ");
Bongjun 10:6497effa1a26 87 for (unsigned i = 6; i != 0; --i) {
Bongjun 10:6497effa1a26 88 printf("%02X%s", ROM_Code.BYTES.serialNo[i-1], (i != 1)?":":"\r\n");
Bongjun 10:6497effa1a26 89 }
Bongjun 10:6497effa1a26 90 printf("CRC: 0x%X\r\n", ROM_Code.BYTES.CRC);
Bongjun 10:6497effa1a26 91 /*
Bongjun 10:6497effa1a26 92 printf("\n\rRunning temperature conversion...\n\r");
Bongjun 10:6497effa1a26 93 while (1) {
Bongjun 10:6497effa1a26 94 printf("Temperature is: %.4fC\n\r", sensor.GetTemperature());
Bongjun 10:6497effa1a26 95 wait(10);
Bongjun 10:6497effa1a26 96 }
Bongjun 10:6497effa1a26 97 */
Bongjun 10:6497effa1a26 98 #endif
lz307 6:3c44af154da8 99
lz307 6:3c44af154da8 100 xi_feed_t feed;
lz307 6:3c44af154da8 101 memset( &feed, NULL, sizeof( xi_feed_t ) );
lz307 6:3c44af154da8 102
lz307 6:3c44af154da8 103 feed.feed_id = XI_FEED_ID;
lz307 6:3c44af154da8 104 feed.datastream_count = 1;
samux 0:90ba0f51aa64 105
lz307 6:3c44af154da8 106 feed.datastreams[0].datapoint_count = 1;
lz307 6:3c44af154da8 107 xi_datastream_t* orientation_datastream = &feed.datastreams[0];
Bongjun 7:5ce11b80fb04 108 strcpy( orientation_datastream->datastream_id, "Channel_Test1" );
lz307 6:3c44af154da8 109 xi_datapoint_t* current_orientation = &orientation_datastream->datapoints[0];
lz307 6:3c44af154da8 110
lz307 6:3c44af154da8 111 // create the cosm library context
lz307 6:3c44af154da8 112 xi_context_t* xi_context
lz307 6:3c44af154da8 113 = xi_create_context( XI_HTTP, XI_API_KEY, feed.feed_id );
lz307 6:3c44af154da8 114
lz307 6:3c44af154da8 115 // check if everything works
lz307 6:3c44af154da8 116 if( xi_context == NULL ) {
lz307 6:3c44af154da8 117 return -1;
lz307 6:3c44af154da8 118 }
lz307 6:3c44af154da8 119
lz307 6:3c44af154da8 120 while(1) {
kaizen 14:5a9ba3d8c9ca 121 #ifdef _SENSOR_DS18B20 // if there is sensor.
Bongjun 10:6497effa1a26 122 /*
Bongjun 7:5ce11b80fb04 123 if (sensor_cnt) {
Bongjun 7:5ce11b80fb04 124 result = sensor.startReading(true); // start sensor readings and wait
Bongjun 7:5ce11b80fb04 125 if (result == DS18X20_OK) {
Bongjun 7:5ce11b80fb04 126 sensor.getReading(sensorBuf, 0); // get result into buf
Bongjun 7:5ce11b80fb04 127 xi_set_value_str( current_orientation, sensorBuf );
Bongjun 7:5ce11b80fb04 128 }
Bongjun 7:5ce11b80fb04 129 else
Bongjun 7:5ce11b80fb04 130 {
Bongjun 7:5ce11b80fb04 131 xi_set_value_str( current_orientation, "0" );
Bongjun 7:5ce11b80fb04 132 }
Bongjun 7:5ce11b80fb04 133
Bongjun 7:5ce11b80fb04 134 } else {
Bongjun 7:5ce11b80fb04 135 printf("No Sensor");
Bongjun 7:5ce11b80fb04 136 }
Bongjun 10:6497effa1a26 137 */
Bongjun 10:6497effa1a26 138 // use New library for DS18B20
Bongjun 10:6497effa1a26 139 sprintf(sensorBuf,"%4.2f", sensor.GetTemperature());
kaizen 14:5a9ba3d8c9ca 140 printf("Sensor Buf : %s\r\n",sensorBuf);
Bongjun 10:6497effa1a26 141 xi_set_value_str( current_orientation, sensorBuf );
Bongjun 10:6497effa1a26 142 #else
Bongjun 10:6497effa1a26 143 xi_set_value_str( current_orientation, "27" );
Bongjun 10:6497effa1a26 144 #endif
Bongjun 7:5ce11b80fb04 145
Bongjun 8:a0edf4bce160 146 printf( "update..." );
lz307 6:3c44af154da8 147 // send to xively server
lz307 6:3c44af154da8 148 xi_feed_update( xi_context, &feed );
kaizen 14:5a9ba3d8c9ca 149 printf( "done...\r\n" );
Bongjun 13:ab5db87a253a 150 wait( 5.0 );
lz307 6:3c44af154da8 151 }
lz307 6:3c44af154da8 152 }