cellular port

Dependencies:   Beep C027_Support C12832 LM75B MMA7660 mbed-rtos mbed nsdl_lib

This is a port of the NSDL HelloWorld for cellular.

To run the example you need a C027 and the ARM mbed application shield. The example uses cellular instead of ethernet and takes the true position from the GPS instead of using a fixed position.

Committer:
mazgch
Date:
Fri Jun 20 12:11:44 2014 +0000
Revision:
12:2799b212c729
Parent:
11:fa12b9f50538
Child:
13:03ba47370829
update libraries and make it more portable for shields

Who changed what in which revision?

UserRevisionLine numberNew contents of line
terohoo 0:2edbfea18d23 1 #include "mbed.h"
mazgch 10:443e7f741c8e 2 #include "rtos.h"
mazgch 8:bd9096c4784c 3 #include "C12832.h"
bogdanm 2:7e489126fe7a 4 #include "nsdl_support.h"
bogdanm 2:7e489126fe7a 5 #include "dbg.h"
bogdanm 2:7e489126fe7a 6 // Include various resources
bogdanm 2:7e489126fe7a 7 #include "temperature.h"
bogdanm 2:7e489126fe7a 8 #include "light.h"
mazgch 11:fa12b9f50538 9 #include "gps_res.h"
bogdanm 2:7e489126fe7a 10 #include "relay.h"
terohoo 0:2edbfea18d23 11
mazgch 8:bd9096c4784c 12 #include "UDPSocket.h"
mazgch 8:bd9096c4784c 13 #include "Endpoint.h"
mazgch 8:bd9096c4784c 14
mazgch 8:bd9096c4784c 15 //------------------------------------------------------------------------------------
mazgch 8:bd9096c4784c 16 // You need to configure these cellular modem / SIM parameters.
mazgch 8:bd9096c4784c 17 // These parameters are ignored for LISA-C200 variants and can be left NULL.
mazgch 8:bd9096c4784c 18 //------------------------------------------------------------------------------------
mazgch 8:bd9096c4784c 19 #include "MDM.h"
mazgch 11:fa12b9f50538 20 #include "GPS.h"
mazgch 8:bd9096c4784c 21 //! Set your secret SIM pin here (e.g. "1234"). Check your SIM manual.
mazgch 8:bd9096c4784c 22 #define SIMPIN NULL
mazgch 8:bd9096c4784c 23 /*! The APN of your network operator SIM, sometimes it is "internet" check your
mazgch 8:bd9096c4784c 24 contract with the network operator. You can also try to look-up your settings in
mazgch 8:bd9096c4784c 25 google: https://www.google.de/search?q=APN+list */
mazgch 9:e4c916c6cb02 26 #define APN NULL
mazgch 8:bd9096c4784c 27 //! Set the user name for your APN, or NULL if not needed
mazgch 8:bd9096c4784c 28 #define USERNAME NULL
mazgch 8:bd9096c4784c 29 //! Set the password for your APN, or NULL if not needed
mazgch 8:bd9096c4784c 30 #define PASSWORD NULL
mazgch 8:bd9096c4784c 31 //------------------------------------------------------------------------------------
mazgch 8:bd9096c4784c 32
mazgch 8:bd9096c4784c 33 static C12832 lcd(D11, D13, D12, D7, D10);
terohoo 0:2edbfea18d23 34
bogdanm 2:7e489126fe7a 35 // ****************************************************************************
bogdanm 2:7e489126fe7a 36 // Configuration section
bogdanm 2:7e489126fe7a 37
bogdanm 2:7e489126fe7a 38 // NSP configuration
terohoo 0:2edbfea18d23 39 /* Change this IP address to that of your NanoService Platform installation */
mazgch 8:bd9096c4784c 40 static const char* NSP_ADDRESS = "nanoservice-demo.mbed.org"; /* demo NSP, web interface at http://nanoservice-demo.mbed.org*/
bogdanm 2:7e489126fe7a 41 static const int NSP_PORT = 5683;
mazgch 10:443e7f741c8e 42 char endpoint_name[16] = "mbed-cellular";
bogdanm 2:7e489126fe7a 43 uint8_t ep_type[] = {"mbed_device"};
bogdanm 2:7e489126fe7a 44 uint8_t lifetime_ptr[] = {"1200"};
terohoo 0:2edbfea18d23 45
bogdanm 2:7e489126fe7a 46 // ****************************************************************************
bogdanm 2:7e489126fe7a 47 // NSP initialization
bogdanm 2:7e489126fe7a 48
bogdanm 2:7e489126fe7a 49 UDPSocket server;
bogdanm 2:7e489126fe7a 50 Endpoint nsp;
bogdanm 2:7e489126fe7a 51
bogdanm 2:7e489126fe7a 52 static void nsp_init()
bogdanm 2:7e489126fe7a 53 {
terohoo 0:2edbfea18d23 54 server.init();
terohoo 0:2edbfea18d23 55 server.bind(NSP_PORT);
terohoo 0:2edbfea18d23 56
bogdanm 2:7e489126fe7a 57 nsp.set_address(NSP_ADDRESS, NSP_PORT);
bogdanm 2:7e489126fe7a 58
bogdanm 2:7e489126fe7a 59 NSDL_DEBUG("name: %s", endpoint_name);
mazgch 11:fa12b9f50538 60 NSDL_DEBUG("NSP=%s - port %d", NSP_ADDRESS, NSP_PORT);
mazgch 11:fa12b9f50538 61 NSDL_DEBUG("NSDL Demo Portal\r\n"
mazgch 11:fa12b9f50538 62 " Website: http://%s\r\n"
mazgch 11:fa12b9f50538 63 " Username: demo\r\n"
mazgch 11:fa12b9f50538 64 " Password: demo", NSP_ADDRESS);
mazgch 8:bd9096c4784c 65
terohoo 0:2edbfea18d23 66 lcd.locate(0,22);
bogdanm 3:52c1b649eb04 67 lcd.printf("EP name:%s\n", endpoint_name);
bogdanm 2:7e489126fe7a 68 }
terohoo 0:2edbfea18d23 69
bogdanm 2:7e489126fe7a 70 // ****************************************************************************
bogdanm 2:7e489126fe7a 71 // Resource creation
terohoo 0:2edbfea18d23 72
bogdanm 2:7e489126fe7a 73 static int create_resources()
bogdanm 2:7e489126fe7a 74 {
bogdanm 2:7e489126fe7a 75 sn_nsdl_resource_info_s *resource_ptr = NULL;
bogdanm 2:7e489126fe7a 76 sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
bogdanm 2:7e489126fe7a 77
bogdanm 2:7e489126fe7a 78 NSDL_DEBUG("Creating resources");
terohoo 0:2edbfea18d23 79
terohoo 0:2edbfea18d23 80 /* Create resources */
bogdanm 2:7e489126fe7a 81 resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s));
terohoo 0:2edbfea18d23 82 if(!resource_ptr)
terohoo 0:2edbfea18d23 83 return 0;
terohoo 0:2edbfea18d23 84 memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s));
terohoo 0:2edbfea18d23 85
bogdanm 2:7e489126fe7a 86 resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s));
terohoo 0:2edbfea18d23 87 if(!resource_ptr->resource_parameters_ptr)
terohoo 0:2edbfea18d23 88 {
bogdanm 2:7e489126fe7a 89 nsdl_free(resource_ptr);
terohoo 0:2edbfea18d23 90 return 0;
terohoo 0:2edbfea18d23 91 }
terohoo 0:2edbfea18d23 92 memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s));
terohoo 0:2edbfea18d23 93
bogdanm 2:7e489126fe7a 94 // Static resources
mazgch 11:fa12b9f50538 95 #define _PT(str) sizeof(str)-1, (uint8_t*)str
mazgch 11:fa12b9f50538 96 #define _RSC(str) (uint8_t*)str, sizeof(str)-1
mazgch 11:fa12b9f50538 97 nsdl_create_static_resource(resource_ptr, _PT("dev/mfg"), 0, 0, _RSC("Sensinode / u-blox"));
mazgch 11:fa12b9f50538 98 nsdl_create_static_resource(resource_ptr, _PT("dev/mdl"), 0, 0, _RSC("NSDL-C cellular mbed device"));
terohoo 0:2edbfea18d23 99
bogdanm 2:7e489126fe7a 100 // Dynamic resources
bogdanm 2:7e489126fe7a 101 create_temperature_resource(resource_ptr);
bogdanm 2:7e489126fe7a 102 create_light_resource(resource_ptr);
bogdanm 2:7e489126fe7a 103 create_gps_resource(resource_ptr);
bogdanm 2:7e489126fe7a 104 create_relay_resource(resource_ptr);
terohoo 0:2edbfea18d23 105
terohoo 0:2edbfea18d23 106 /* Register with NSP */
bogdanm 2:7e489126fe7a 107 endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
terohoo 0:2edbfea18d23 108 if(sn_nsdl_register_endpoint(endpoint_ptr) != 0)
mazgch 8:bd9096c4784c 109 printf("NSP registering failed\r\n");
terohoo 0:2edbfea18d23 110 else
mazgch 8:bd9096c4784c 111 printf("NSP registering OK\r\n");
bogdanm 2:7e489126fe7a 112 nsdl_clean_register_endpoint(&endpoint_ptr);
terohoo 0:2edbfea18d23 113
bogdanm 2:7e489126fe7a 114 nsdl_free(resource_ptr->resource_parameters_ptr);
bogdanm 2:7e489126fe7a 115 nsdl_free(resource_ptr);
terohoo 0:2edbfea18d23 116 return 1;
terohoo 0:2edbfea18d23 117 }
terohoo 0:2edbfea18d23 118
bogdanm 2:7e489126fe7a 119 // ****************************************************************************
bogdanm 2:7e489126fe7a 120 // Program entry point
mazgch 11:fa12b9f50538 121 void ndslTask(void const* argument)
terohoo 0:2edbfea18d23 122 {
mazgch 11:fa12b9f50538 123 MDMRtos<MDMSerial> mdm;
bogdanm 2:7e489126fe7a 124
mazgch 8:bd9096c4784c 125 //mdm.setDebug(4); // enable this for debugging issues
mazgch 8:bd9096c4784c 126 if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD))
mazgch 11:fa12b9f50538 127 return;
mazgch 8:bd9096c4784c 128
mazgch 8:bd9096c4784c 129 lcd.locate(0,11);
mazgch 8:bd9096c4784c 130
bogdanm 2:7e489126fe7a 131 // Initialize NSP node
bogdanm 2:7e489126fe7a 132 nsp_init();
bogdanm 2:7e489126fe7a 133
bogdanm 2:7e489126fe7a 134 // Initialize NSDL stack
bogdanm 2:7e489126fe7a 135 nsdl_init();
bogdanm 2:7e489126fe7a 136
bogdanm 2:7e489126fe7a 137 // Create NSDL resources
bogdanm 2:7e489126fe7a 138 create_resources();
bogdanm 2:7e489126fe7a 139
bogdanm 2:7e489126fe7a 140 // Run the NSDL event loop (never returns)
bogdanm 2:7e489126fe7a 141 nsdl_event_loop();
mazgch 8:bd9096c4784c 142
mazgch 8:bd9096c4784c 143 mdm.disconnect();
mazgch 8:bd9096c4784c 144 mdm.powerOff();
terohoo 0:2edbfea18d23 145 }
mazgch 11:fa12b9f50538 146
mazgch 11:fa12b9f50538 147 void gpsTask(void const* argument)
mazgch 11:fa12b9f50538 148 {
mazgch 11:fa12b9f50538 149 GPSI2C gps;
mazgch 11:fa12b9f50538 150 char buf[256];
mazgch 11:fa12b9f50538 151 while (1) {
mazgch 11:fa12b9f50538 152 int ret = gps.getMessage(buf, sizeof(buf));
mazgch 11:fa12b9f50538 153 if (ret > 0) {
mazgch 11:fa12b9f50538 154 int len = LENGTH(ret);
mazgch 11:fa12b9f50538 155 if ((PROTOCOL(ret) == GPSParser::NMEA) && (len > 6))
mazgch 11:fa12b9f50538 156 {
mazgch 11:fa12b9f50538 157 if (!strncmp("$GPGLL", buf, 6)) {
mazgch 11:fa12b9f50538 158 double la = 0, lo = 0;
mazgch 11:fa12b9f50538 159 char ch;
mazgch 11:fa12b9f50538 160 if (gps.getNmeaAngle(1,buf,len,la) &&
mazgch 11:fa12b9f50538 161 gps.getNmeaAngle(3,buf,len,lo) &&
mazgch 11:fa12b9f50538 162 gps.getNmeaItem(6,buf,len,ch) && ch == 'A')
mazgch 11:fa12b9f50538 163 {
mazgch 11:fa12b9f50538 164 printf("gps update lat %.6f lon %.6f\r\n", la, lo);
mazgch 11:fa12b9f50538 165 gps_resource_set(la,lo);
mazgch 11:fa12b9f50538 166 }
mazgch 11:fa12b9f50538 167 }
mazgch 11:fa12b9f50538 168 }
mazgch 11:fa12b9f50538 169 } else
mazgch 11:fa12b9f50538 170 Thread::wait(100);
mazgch 11:fa12b9f50538 171 }
mazgch 11:fa12b9f50538 172 }
mazgch 11:fa12b9f50538 173
mazgch 11:fa12b9f50538 174 int main()
mazgch 11:fa12b9f50538 175 {
mazgch 11:fa12b9f50538 176 Serial pc(USBTX, USBRX);
mazgch 11:fa12b9f50538 177 pc.baud(115200);
mazgch 11:fa12b9f50538 178 lcd.cls();
mazgch 11:fa12b9f50538 179 lcd.locate(0,0);
mazgch 11:fa12b9f50538 180 lcd.printf("mbed NanoService demo");
mazgch 11:fa12b9f50538 181 NSDL_DEBUG("mbed NanoService Example App 0.1\r\n");
mazgch 11:fa12b9f50538 182
mazgch 11:fa12b9f50538 183 Thread task1(ndslTask, NULL, osPriorityNormal, 4096);
mazgch 11:fa12b9f50538 184 Thread task2(gpsTask, NULL, osPriorityNormal, 2048);
mazgch 11:fa12b9f50538 185
mazgch 12:2799b212c729 186 DigitalOut led(LED1);
mazgch 11:fa12b9f50538 187 while(true) {
mazgch 11:fa12b9f50538 188 led = !led;
mazgch 11:fa12b9f50538 189 wait_ms(500);
mazgch 11:fa12b9f50538 190 }
mazgch 11:fa12b9f50538 191 }
mazgch 11:fa12b9f50538 192
mazgch 11:fa12b9f50538 193