![](/media/cache/group/arm_mbed.PNG.50x50_q85.png)
Project with example IPSO resources for LED bar, Gas Sensor, and Light Sensor
Dependencies: EthernetInterface LED_Bar mbed-rtos mbed nsdl_lib
main.cpp@1:76ab3521eb51, 2014-10-20 (annotated)
- Committer:
- michaeljkoster
- Date:
- Mon Oct 20 02:17:21 2014 +0000
- Revision:
- 1:76ab3521eb51
- Parent:
- 0:93cc592ffdb5
- Child:
- 4:69ea903414fe
Add presence sensor resource
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
michaeljkoster | 0:93cc592ffdb5 | 1 | #include "mbed.h" |
michaeljkoster | 0:93cc592ffdb5 | 2 | #include "EthernetInterface.h" |
michaeljkoster | 0:93cc592ffdb5 | 3 | #include "nsdl_support.h" |
michaeljkoster | 0:93cc592ffdb5 | 4 | #include "dbg.h" |
michaeljkoster | 0:93cc592ffdb5 | 5 | #include "IAP.h" |
michaeljkoster | 0:93cc592ffdb5 | 6 | |
michaeljkoster | 0:93cc592ffdb5 | 7 | // Include resources |
michaeljkoster | 0:93cc592ffdb5 | 8 | #include "LEDbar.h" |
michaeljkoster | 0:93cc592ffdb5 | 9 | #include "IPSO_illuminance.h" |
michaeljkoster | 0:93cc592ffdb5 | 10 | #include "gas_sensor.h" |
michaeljkoster | 1:76ab3521eb51 | 11 | #include "IPSO_presence.h" |
michaeljkoster | 0:93cc592ffdb5 | 12 | |
michaeljkoster | 0:93cc592ffdb5 | 13 | Serial pc(USBTX, USBRX); // tx, rx |
michaeljkoster | 0:93cc592ffdb5 | 14 | |
michaeljkoster | 0:93cc592ffdb5 | 15 | // **************************************************************************** |
michaeljkoster | 0:93cc592ffdb5 | 16 | // Configuration section |
michaeljkoster | 0:93cc592ffdb5 | 17 | |
michaeljkoster | 0:93cc592ffdb5 | 18 | // Ethernet configuration |
michaeljkoster | 0:93cc592ffdb5 | 19 | /* Define this to enable DHCP, otherwise manual address configuration is used */ |
michaeljkoster | 0:93cc592ffdb5 | 20 | #define DHCP |
michaeljkoster | 0:93cc592ffdb5 | 21 | |
michaeljkoster | 0:93cc592ffdb5 | 22 | /* Manual IP configurations, if DHCP not defined */ |
michaeljkoster | 0:93cc592ffdb5 | 23 | #define IP "10.0.0.199" |
michaeljkoster | 0:93cc592ffdb5 | 24 | #define MASK "255.255.255.0" |
michaeljkoster | 0:93cc592ffdb5 | 25 | #define GW "10.0.0.1" |
michaeljkoster | 0:93cc592ffdb5 | 26 | |
michaeljkoster | 0:93cc592ffdb5 | 27 | extern "C" void mbed_mac_address(char *mac) |
michaeljkoster | 0:93cc592ffdb5 | 28 | { |
michaeljkoster | 0:93cc592ffdb5 | 29 | static char buf[64] = {0}; |
michaeljkoster | 0:93cc592ffdb5 | 30 | IAP iap; |
michaeljkoster | 0:93cc592ffdb5 | 31 | int32_t *block = iap.read_serial(); |
michaeljkoster | 0:93cc592ffdb5 | 32 | uint32_t serial_number[5] = {0}; |
michaeljkoster | 0:93cc592ffdb5 | 33 | |
michaeljkoster | 0:93cc592ffdb5 | 34 | memset(buf, 0, sizeof(buf)); |
michaeljkoster | 0:93cc592ffdb5 | 35 | serial_number[0] = *(block); |
michaeljkoster | 0:93cc592ffdb5 | 36 | serial_number[1] = *(block+1); |
michaeljkoster | 0:93cc592ffdb5 | 37 | // we only want bottom 16 bits of word1 (MAC bits 32-47) |
michaeljkoster | 0:93cc592ffdb5 | 38 | // and bit 9 forced to 1, bit 8 forced to 0 |
michaeljkoster | 0:93cc592ffdb5 | 39 | // Locally administered MAC, reduced conflicts |
michaeljkoster | 0:93cc592ffdb5 | 40 | // http://en.wikipedia.org/wiki/MAC_address |
michaeljkoster | 0:93cc592ffdb5 | 41 | //serial_number[0] |= 0x00000200; |
michaeljkoster | 0:93cc592ffdb5 | 42 | //serial_number[0] &= 0x0000FEFF; |
michaeljkoster | 0:93cc592ffdb5 | 43 | memcpy(mac, (uint8_t*) &serial_number[0], 6); |
michaeljkoster | 0:93cc592ffdb5 | 44 | mac[0] |= 0x02; |
michaeljkoster | 0:93cc592ffdb5 | 45 | mac[0] &= 0xFE; |
michaeljkoster | 0:93cc592ffdb5 | 46 | mac[5] |= 0x02; |
michaeljkoster | 0:93cc592ffdb5 | 47 | mac[5] &= 0xFE; |
michaeljkoster | 0:93cc592ffdb5 | 48 | |
michaeljkoster | 0:93cc592ffdb5 | 49 | // snprintf(buf, 16, "%4X%08X", serial_number[0], serial_number[1]); |
michaeljkoster | 0:93cc592ffdb5 | 50 | } |
michaeljkoster | 0:93cc592ffdb5 | 51 | |
michaeljkoster | 0:93cc592ffdb5 | 52 | |
michaeljkoster | 0:93cc592ffdb5 | 53 | // NSP configuration |
michaeljkoster | 0:93cc592ffdb5 | 54 | /* Change this IP address to that of your NanoService Platform installation */ |
michaeljkoster | 0:93cc592ffdb5 | 55 | static const char* NSP_ADDRESS = "23.102.162.118"; // coen296.cloudapp.net |
michaeljkoster | 0:93cc592ffdb5 | 56 | //static const char* NSP_ADDRESS = "192.168.1.200"; // local mDS server |
michaeljkoster | 0:93cc592ffdb5 | 57 | static const int NSP_PORT = 5683; |
michaeljkoster | 0:93cc592ffdb5 | 58 | char endpoint_name[24] = "mbedDEMO-"; |
michaeljkoster | 0:93cc592ffdb5 | 59 | uint8_t ep_type[] = {"DEMO"}; |
michaeljkoster | 0:93cc592ffdb5 | 60 | uint8_t lifetime_ptr[] = {"60"}; |
michaeljkoster | 0:93cc592ffdb5 | 61 | |
michaeljkoster | 0:93cc592ffdb5 | 62 | // **************************************************************************** |
michaeljkoster | 0:93cc592ffdb5 | 63 | // Ethernet initialization |
michaeljkoster | 0:93cc592ffdb5 | 64 | |
michaeljkoster | 0:93cc592ffdb5 | 65 | EthernetInterface eth; |
michaeljkoster | 0:93cc592ffdb5 | 66 | static void ethernet_init() |
michaeljkoster | 0:93cc592ffdb5 | 67 | { |
michaeljkoster | 0:93cc592ffdb5 | 68 | |
michaeljkoster | 0:93cc592ffdb5 | 69 | /* Initialize network */ |
michaeljkoster | 0:93cc592ffdb5 | 70 | #ifdef DHCP |
michaeljkoster | 0:93cc592ffdb5 | 71 | NSDL_DEBUG("DHCP in use\r\n"); |
michaeljkoster | 0:93cc592ffdb5 | 72 | eth.init(); |
michaeljkoster | 0:93cc592ffdb5 | 73 | NSDL_DEBUG("eth.init\r\n"); |
michaeljkoster | 0:93cc592ffdb5 | 74 | #else |
michaeljkoster | 0:93cc592ffdb5 | 75 | eth.init(IP, MASK, GW); |
michaeljkoster | 0:93cc592ffdb5 | 76 | NSDL_DEBUG("eth.init\r\n"); |
michaeljkoster | 0:93cc592ffdb5 | 77 | #endif |
michaeljkoster | 0:93cc592ffdb5 | 78 | if(eth.connect(30000) == 0) |
michaeljkoster | 0:93cc592ffdb5 | 79 | pc.printf("Connect OK\n\r"); |
michaeljkoster | 0:93cc592ffdb5 | 80 | |
michaeljkoster | 0:93cc592ffdb5 | 81 | NSDL_DEBUG("IP Address:%s ", eth.getIPAddress()); |
michaeljkoster | 0:93cc592ffdb5 | 82 | } |
michaeljkoster | 0:93cc592ffdb5 | 83 | |
michaeljkoster | 0:93cc592ffdb5 | 84 | // **************************************************************************** |
michaeljkoster | 0:93cc592ffdb5 | 85 | // NSP initialization |
michaeljkoster | 0:93cc592ffdb5 | 86 | |
michaeljkoster | 0:93cc592ffdb5 | 87 | UDPSocket server; |
michaeljkoster | 0:93cc592ffdb5 | 88 | Endpoint nsp; |
michaeljkoster | 0:93cc592ffdb5 | 89 | |
michaeljkoster | 0:93cc592ffdb5 | 90 | char macbytes[6] = {0}; |
michaeljkoster | 0:93cc592ffdb5 | 91 | char MAC[20]; |
michaeljkoster | 0:93cc592ffdb5 | 92 | |
michaeljkoster | 0:93cc592ffdb5 | 93 | static void nsp_init() |
michaeljkoster | 0:93cc592ffdb5 | 94 | { |
michaeljkoster | 0:93cc592ffdb5 | 95 | server.init(); |
michaeljkoster | 0:93cc592ffdb5 | 96 | server.bind(NSP_PORT); |
michaeljkoster | 0:93cc592ffdb5 | 97 | |
michaeljkoster | 0:93cc592ffdb5 | 98 | nsp.set_address(NSP_ADDRESS, NSP_PORT); |
michaeljkoster | 0:93cc592ffdb5 | 99 | |
michaeljkoster | 0:93cc592ffdb5 | 100 | mbed_mac_address(macbytes); |
michaeljkoster | 0:93cc592ffdb5 | 101 | |
michaeljkoster | 0:93cc592ffdb5 | 102 | sprintf(MAC, "%02X%02X%02X%02X%02X%02X", macbytes[0], macbytes[1], macbytes[2], macbytes[3], macbytes[4], macbytes[5]); |
michaeljkoster | 0:93cc592ffdb5 | 103 | pc.printf("MAC: %s\r\n", MAC); |
michaeljkoster | 0:93cc592ffdb5 | 104 | |
michaeljkoster | 0:93cc592ffdb5 | 105 | strncat(endpoint_name, MAC, 12); |
michaeljkoster | 0:93cc592ffdb5 | 106 | |
michaeljkoster | 0:93cc592ffdb5 | 107 | NSDL_DEBUG("name: %s", endpoint_name); |
michaeljkoster | 0:93cc592ffdb5 | 108 | NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT); |
michaeljkoster | 0:93cc592ffdb5 | 109 | |
michaeljkoster | 0:93cc592ffdb5 | 110 | } |
michaeljkoster | 0:93cc592ffdb5 | 111 | |
michaeljkoster | 0:93cc592ffdb5 | 112 | // **************************************************************************** |
michaeljkoster | 0:93cc592ffdb5 | 113 | // Resource creation |
michaeljkoster | 0:93cc592ffdb5 | 114 | |
michaeljkoster | 0:93cc592ffdb5 | 115 | static int create_resources() |
michaeljkoster | 0:93cc592ffdb5 | 116 | { |
michaeljkoster | 0:93cc592ffdb5 | 117 | sn_nsdl_resource_info_s *resource_ptr = NULL; |
michaeljkoster | 0:93cc592ffdb5 | 118 | sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; |
michaeljkoster | 0:93cc592ffdb5 | 119 | |
michaeljkoster | 0:93cc592ffdb5 | 120 | NSDL_DEBUG("Creating resources"); |
michaeljkoster | 0:93cc592ffdb5 | 121 | |
michaeljkoster | 0:93cc592ffdb5 | 122 | /* Create resources */ |
michaeljkoster | 0:93cc592ffdb5 | 123 | resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s)); |
michaeljkoster | 0:93cc592ffdb5 | 124 | if(!resource_ptr) |
michaeljkoster | 0:93cc592ffdb5 | 125 | return 0; |
michaeljkoster | 0:93cc592ffdb5 | 126 | memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s)); |
michaeljkoster | 0:93cc592ffdb5 | 127 | |
michaeljkoster | 0:93cc592ffdb5 | 128 | resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s)); |
michaeljkoster | 0:93cc592ffdb5 | 129 | if(!resource_ptr->resource_parameters_ptr) |
michaeljkoster | 0:93cc592ffdb5 | 130 | { |
michaeljkoster | 0:93cc592ffdb5 | 131 | nsdl_free(resource_ptr); |
michaeljkoster | 0:93cc592ffdb5 | 132 | return 0; |
michaeljkoster | 0:93cc592ffdb5 | 133 | } |
michaeljkoster | 0:93cc592ffdb5 | 134 | memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s)); |
michaeljkoster | 0:93cc592ffdb5 | 135 | |
michaeljkoster | 0:93cc592ffdb5 | 136 | // Static resources |
michaeljkoster | 0:93cc592ffdb5 | 137 | nsdl_create_static_resource(resource_ptr, sizeof("3/0/0")-1, (uint8_t*) "3/0/0", 0, 0, (uint8_t*) "mbedDEMO", sizeof("mbedDEMO")-1); |
michaeljkoster | 0:93cc592ffdb5 | 138 | nsdl_create_static_resource(resource_ptr, sizeof("3/0/1")-1, (uint8_t*) "3/0/1", 0, 0, (uint8_t*) "DEMO", sizeof("DEMO")-1); |
michaeljkoster | 0:93cc592ffdb5 | 139 | |
michaeljkoster | 0:93cc592ffdb5 | 140 | // Dynamic resources |
michaeljkoster | 0:93cc592ffdb5 | 141 | create_LEDbar_resource(resource_ptr); |
michaeljkoster | 0:93cc592ffdb5 | 142 | create_IPSO_illuminance_resource(resource_ptr); |
michaeljkoster | 0:93cc592ffdb5 | 143 | create_gas_sensor_resource(resource_ptr); |
michaeljkoster | 1:76ab3521eb51 | 144 | create_IPSO_presence_resource(resource_ptr); |
michaeljkoster | 0:93cc592ffdb5 | 145 | |
michaeljkoster | 0:93cc592ffdb5 | 146 | /* Register with NSP */ |
michaeljkoster | 0:93cc592ffdb5 | 147 | endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); |
michaeljkoster | 0:93cc592ffdb5 | 148 | if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) |
michaeljkoster | 0:93cc592ffdb5 | 149 | pc.printf("NSP registering failed\r\n"); |
michaeljkoster | 0:93cc592ffdb5 | 150 | else |
michaeljkoster | 0:93cc592ffdb5 | 151 | pc.printf("NSP registering OK\r\n"); |
michaeljkoster | 0:93cc592ffdb5 | 152 | nsdl_clean_register_endpoint(&endpoint_ptr); |
michaeljkoster | 0:93cc592ffdb5 | 153 | |
michaeljkoster | 0:93cc592ffdb5 | 154 | nsdl_free(resource_ptr->resource_parameters_ptr); |
michaeljkoster | 0:93cc592ffdb5 | 155 | nsdl_free(resource_ptr); |
michaeljkoster | 0:93cc592ffdb5 | 156 | return 1; |
michaeljkoster | 0:93cc592ffdb5 | 157 | } |
michaeljkoster | 0:93cc592ffdb5 | 158 | |
michaeljkoster | 0:93cc592ffdb5 | 159 | // **************************************************************************** |
michaeljkoster | 0:93cc592ffdb5 | 160 | // Program entry point |
michaeljkoster | 0:93cc592ffdb5 | 161 | |
michaeljkoster | 0:93cc592ffdb5 | 162 | int main() |
michaeljkoster | 0:93cc592ffdb5 | 163 | { |
michaeljkoster | 0:93cc592ffdb5 | 164 | |
michaeljkoster | 0:93cc592ffdb5 | 165 | NSDL_DEBUG("Hello mDS Demo Endpoint Application\n"); |
michaeljkoster | 0:93cc592ffdb5 | 166 | |
michaeljkoster | 0:93cc592ffdb5 | 167 | // Initialize Ethernet interface first |
michaeljkoster | 0:93cc592ffdb5 | 168 | ethernet_init(); |
michaeljkoster | 0:93cc592ffdb5 | 169 | |
michaeljkoster | 0:93cc592ffdb5 | 170 | // Initialize NSP node |
michaeljkoster | 0:93cc592ffdb5 | 171 | nsp_init(); |
michaeljkoster | 0:93cc592ffdb5 | 172 | |
michaeljkoster | 0:93cc592ffdb5 | 173 | // Initialize NSDL stack |
michaeljkoster | 0:93cc592ffdb5 | 174 | nsdl_init(); |
michaeljkoster | 0:93cc592ffdb5 | 175 | |
michaeljkoster | 0:93cc592ffdb5 | 176 | // Create NSDL resources |
michaeljkoster | 0:93cc592ffdb5 | 177 | create_resources(); |
michaeljkoster | 0:93cc592ffdb5 | 178 | |
michaeljkoster | 0:93cc592ffdb5 | 179 | // Run the NSDL event loop (never returns) |
michaeljkoster | 0:93cc592ffdb5 | 180 | nsdl_event_loop(); |
michaeljkoster | 0:93cc592ffdb5 | 181 | } |