W5500-Ethernet-Interface Makers / Mbed 2 deprecated WIZ550io_Xively_Demo

Dependencies:   DS18B20_1wire W5500Interface libxively mbed

Fork of WIZ550io_Xively_Demo by Bongjun Hur

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "W5500Interface/EthernetInterface.h"
00003 #include "xively.h"
00004 #include "xi_err.h"
00005 
00006 #define XI_FEED_ID YOUR-FEED-ID // set Xively Feed ID (numerical, no quoutes)
00007 #define XI_API_KEY "Your API Key" // set Xively API key (double-quoted string)
00008 
00009 //Serial pc(USBTX, USBRX);
00010 DigitalOut myled(LED1);
00011 
00012 #define _SENSOR_DS18B20
00013 
00014 #ifdef _SENSOR_DS18B20 // if there is sensor.
00015 #include "DS18B20.h"
00016 #endif
00017 
00018 #define _DHCP
00019 
00020 
00021 int main()
00022 {
00023 #if defined(TARGET_LPC1114)
00024     SPI spi(dp2, dp1, dp6); // mosi, miso, sclk
00025     EthernetInterface eth(&spi, dp25, dp26); // spi, cs, reset
00026     DS18B20 sensor(D3, DS18B20::RES_12_BIT); // Dallas 1-wire
00027 #elif defined(TARGET_LPC1768)
00028     SPI spi(p11, p12, p13); // mosi, miso, sclk
00029     EthernetInterface eth(&spi, p14, p15); // spi, cs, reset
00030 #elif defined(TARGET_LPC11U68)
00031     SPI spi(P0_9, P0_8, P1_29); // mosi, miso, sclk
00032     EthernetInterface eth(&spi, P0_2, P1_28);//, nRESET(p9); // reset pin is dummy, don't affect any pin of WIZ550io
00033     spi.format(8,0); // 8bit, mode 0
00034     spi.frequency(7000000); // 7MHz
00035     wait(1); // 1 second for stable state
00036     DS18B20 sensor(P1_25, DS18B20::RES_12_BIT); // Dallas 1-wire
00037 #elif defined(TARGET_KL25Z)
00038     Serial pc(USBTX, USBRX);
00039     pc.baud(115200);
00040     printf("spi init\r\n");
00041     SPI spi(D11, D12, D13); // mosi, miso, sclk
00042     wait(1); // 1 second for stable state
00043     EthernetInterface eth(&spi, D10, D9);//scs(D10), nRESET(PTA20)
00044     printf("App Start\r\n");
00045     wait(1); // 1 second for stable state
00046     DS18B20 sensor(D3, DS18B20::RES_12_BIT); // Dallas 1-wire
00047 #elif defined(TARGET_NUCLEO_F030R8)    
00048     Serial pc(USBTX, USBRX);
00049     pc.baud(115200);
00050     printf("spi init\r\n");
00051     SPI spi(D11, D12, D13); // mosi, miso, sclk
00052     wait(1); // 1 second for stable state
00053     EthernetInterface eth(&spi, D10, D9);//scs(D10), nRESET(PTA20)
00054     printf("App Start\r\n");
00055     wait(1); // 1 second for stable state
00056 #endif
00057 
00058 #ifdef _DHCP // didn't test yet in DHCP
00059     eth.init(); //Use DHCP
00060     eth.connect();
00061     printf("Initialized, MAC: %s\n", eth.getMACAddress());
00062     printf("Connected, IP: %s, MASK: %s, GW: %s\n",
00063            eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
00064 #else
00065     // as your env. change to real IP address and so on.
00066     int ret = eth.init("222.98.173.211", "255.255.255.192", "222.98.173.254");
00067     if (!ret) {
00068         printf("Initialized, MAC: %s\n", eth.getMACAddress());
00069         printf("Connected, IP: %s, MASK: %s, GW: %s\n",
00070                eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
00071     } else {
00072         printf("Error eth.init() - ret = %d\n", ret);
00073         return -1;
00074     }
00075 #endif
00076 
00077 // Sensor variables
00078 #ifdef _SENSOR_DS18B20 // if there is sensor.
00079     char sensorBuf[25];
00080 
00081 //Before using this sensor, should called sensor.count() once.
00082    printf("DS18B20 Configuration\n\r");
00083    DS18B20::ROM_Code_t ROM_Code;
00084    sensor.ReadROM(&ROM_Code);
00085    printf("Family code: 0x%X\n\r", ROM_Code.BYTES.familyCode);
00086    printf("Serial Number: ");
00087    for (unsigned i = 6; i != 0; --i) {
00088        printf("%02X%s", ROM_Code.BYTES.serialNo[i-1], (i != 1)?":":"\r\n");
00089    }
00090    printf("CRC: 0x%X\r\n", ROM_Code.BYTES.CRC);
00091 /*
00092    printf("\n\rRunning temperature conversion...\n\r");
00093    while (1) {
00094        printf("Temperature is: %.4fC\n\r", sensor.GetTemperature());
00095        wait(10);
00096    }
00097 */    
00098 #endif
00099 
00100     xi_feed_t feed;
00101     memset( &feed, NULL, sizeof( xi_feed_t ) );
00102 
00103     feed.feed_id = XI_FEED_ID;
00104     feed.datastream_count = 1;
00105 
00106     feed.datastreams[0].datapoint_count = 1;
00107     xi_datastream_t* orientation_datastream = &feed.datastreams[0];
00108     strcpy( orientation_datastream->datastream_id, "Channel_Test1" );
00109     xi_datapoint_t* current_orientation = &orientation_datastream->datapoints[0];
00110 
00111     // create the cosm library context
00112     xi_context_t* xi_context
00113     = xi_create_context( XI_HTTP, XI_API_KEY, feed.feed_id );
00114 
00115     // check if everything works
00116     if( xi_context == NULL ) {
00117         return -1;
00118     }
00119 
00120     while(1) {
00121 #ifdef _SENSOR_DS18B20 // if there is sensor.
00122 /*
00123         if (sensor_cnt) {
00124             result = sensor.startReading(true);     // start sensor readings and wait
00125             if (result == DS18X20_OK) {
00126                 sensor.getReading(sensorBuf, 0);         // get result into buf
00127                 xi_set_value_str( current_orientation, sensorBuf );
00128             }
00129             else
00130             {
00131                 xi_set_value_str( current_orientation, "0" );
00132                 }
00133             
00134         } else {
00135             printf("No Sensor");
00136         }
00137 */
00138         // use New library for DS18B20
00139         sprintf(sensorBuf,"%4.2f", sensor.GetTemperature());
00140         printf("Sensor Buf : %s\r\n",sensorBuf);
00141         xi_set_value_str( current_orientation, sensorBuf );
00142 #else
00143         xi_set_value_str( current_orientation, "27" );
00144 #endif
00145 
00146         printf( "update..." );
00147         // send to xively server
00148         xi_feed_update( xi_context, &feed );
00149         printf( "done...\r\n" );
00150         wait( 5.0 );
00151     }
00152 }