haopeng tang / Mbed 2 deprecated STM32F746_LWM2M_Client

Dependencies:   F7_Ethernet mbed-rtos mbed nsdl_lib

Fork of rtos_basic by Mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //#include "mbed.h"
00002 //#include "rtos.h"
00003 // 
00004 //DigitalOut led1(LED1);
00005 //Serial pc(USBTX,USBRX);//tx rx
00006 //Thread thread;
00007 //Mutex stdio_mutex;
00008 //uint8_t threadindex[3];
00009 //long count[3];
00010 //void led2_thread() {
00011 //    while (true) {
00012 //        //led2 = !led2;
00013 //        pc.printf("Hello World!\n");
00014 //        Thread::wait(1000);
00015 //        
00016 //    }
00017 //}
00018 //void printString(void const *args)
00019 //{
00020 //    while(true)
00021 //    {
00022 //        stdio_mutex.lock();
00023 //        pc.printf("My Thread Name is %d,Thread ID is %d\n",(uint8_t *)args,Thread::gettid()); 
00024 //        count[*(uint8_t *)args-1]++;
00025 //        stdio_mutex.unlock();   
00026 //        Thread::yield();
00027 //    } 
00028 //}
00029 // 
00030 ////int main() {
00031 ////    pc.baud(115200);
00032 ////    thread.start(led2_thread);
00033 ////    
00034 ////    while (true) {
00035 ////        led1 = !led1;
00036 ////        Thread::wait(500);
00037 ////    }
00038 ////}
00039 //int main(){
00040 //    pc.baud(115200);
00041 //    threadindex[0]=1;
00042 //    threadindex[1]=2;
00043 //    threadindex[2]=3;
00044 //    Thread thread1(printString,(void*)threadindex,osPriorityNormal);
00045 //    Thread thread2(printString,(void*)threadindex,osPriorityNormal);
00046 //    Thread thread3(printString,(void*)threadindex,osPriorityNormal);
00047 //    while(1){
00048 //        stdio_mutex.lock();
00049 //        pc.printf("Thread1 count is %ld,Thread2 count is %ld,Thread3 count is %ld.\n",count[0],count[1],count[2]);
00050 //        stdio_mutex.unlock();
00051 //        Thread::wait(1000);
00052 //    }
00053 //}
00054 ///*************************************************************************/
00055 ////测试ok
00056 //#include "mbed.h"
00057 //#include "rtos.h"
00058 //#include "EthernetInterface.h"
00059 //DigitalOut led(LED1);
00060 //int main() {
00061 //EthernetInterface eth;
00062 ////eth.init(); Use DHCP
00063 //eth.init("192.168.0.110","255.255.255.0","192.168.0.1");
00064 //eth.connect();
00065 //printf("IP Address is %s\n", eth.getIPAddress());
00066 //printf("MAC Address is %s\n", eth.getMACAddress());
00067 //printf("GateWay Address is %s\n", eth.getGateway());
00068 //while(1)
00069 //{
00070 //    led=!led;
00071 //    wait(1);
00072 //}
00073 //}
00074 /*************************************************************************/
00075 //测试ok
00076 #include "mbed.h"
00077 #include "rtos.h"
00078 #include "dbg.h"
00079 #include "nsdl_support.h"
00080 #include "EthernetInterface.h"
00081 #include "temperature.h"
00082 #include "light.h"
00083 #include "gps.h"
00084 #include "relay.h"
00085 DigitalOut led(LED1);
00086 Serial pc(USBTX,USBRX);//tx rx
00087 // ****************************************************************************
00088 // Configuration section
00089 
00090 // Ethernet configuration
00091 /* Define this to enable DHCP, otherwise manual address configuration is used */
00092 //#define DHCP
00093 
00094 /* Manual IP configurations, if DHCP not defined */
00095 #define IP      "192.168.1.210"
00096 #define MASK    "255.255.255.0"
00097 #define GW      "192.168.1.1"
00098 // NSP configuration
00099 /* Change this IP address to that of your NanoService Platform installation */
00100 //static const char* NSP_ADDRESS = "217.140.101.20"; /* public mbed demo server */ 
00101 static const char* NSP_ADDRESS = "192.168.1.100"; /* demo NSP, web interface at http://nanoservice-demo.mbed.org*/ 
00102 //static const char* NSP_ADDRESS = "192.168.1.200"; /* demo NSP, web interface at http://nanoservice-demo.mbed.org*/ 
00103 
00104 //static const char* NSP_ADDRESS = "5.39.83.206"; //http://leshan.eclipse.org/#/clients
00105 static const int NSP_PORT = 5683;
00106 char endpoint_name[16] = "mbed-thp";
00107 uint8_t ep_type[] = {"mbed_device"};
00108 uint8_t lifetime_ptr[] = {"1200"};
00109 // ****************************************************************************
00110 // Ethernet initialization
00111 
00112 EthernetInterface eth;
00113 
00114 static void ethernet_init()
00115 {
00116     char mbed_uid[33]; // for creating unique name for the board
00117 
00118     /* Initialize network */
00119 #ifdef DHCP
00120     NSDL_DEBUG("DHCP in use\r\n");
00121     eth.init();
00122 #else
00123     eth.init(IP, MASK, GW);
00124 #endif
00125     if(eth.connect(30000) == 0)
00126         pc.printf("Connect OK\n\r");
00127 
00128     //mbed_interface_uid(mbed_uid);
00129     mbed_uid[8] = '\0';
00130     //strncat(endpoint_name, mbed_uid + 27, 15 - strlen(endpoint_name));
00131 
00132     //lcd.locate(0,11);
00133     //lcd.printf("IP:%s", eth.getIPAddress());
00134 
00135     NSDL_DEBUG("IP Address:%s ", eth.getIPAddress());
00136 }
00137 // ****************************************************************************
00138 // NSP initialization
00139 
00140 UDPSocket server;
00141 Endpoint nsp;
00142 
00143 static void nsp_init()
00144 {
00145     server.init();
00146     server.bind(NSP_PORT);
00147 
00148     nsp.set_address(NSP_ADDRESS, NSP_PORT);
00149     
00150     NSDL_DEBUG("name: %s", endpoint_name);
00151     NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT);
00152 
00153     //lcd.locate(0,22);
00154     //lcd.printf("EP name:%s\n", endpoint_name);
00155 }
00156 // ****************************************************************************
00157 // Resource creation
00158 
00159 static int create_resources()
00160 {
00161     sn_nsdl_resource_info_s *resource_ptr = NULL;
00162     sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
00163     
00164     NSDL_DEBUG("Creating resources");
00165 
00166     /* Create resources */
00167     resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s));
00168     if(!resource_ptr)
00169         return 0;
00170     memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s));
00171 
00172     resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s));
00173     if(!resource_ptr->resource_parameters_ptr)
00174     {
00175         nsdl_free(resource_ptr);
00176         return 0;
00177     }
00178     memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s));
00179 
00180     // Static resources
00181     nsdl_create_static_resource(resource_ptr, sizeof("dev/mfg")-1, (uint8_t*) "dev/mfg", 0, 0,  (uint8_t*) "Sensinode", sizeof("Sensinode")-1);
00182     nsdl_create_static_resource(resource_ptr, sizeof("dev/mdl")-1, (uint8_t*) "dev/mdl", 0, 0,  (uint8_t*) "NSDL-C mbed device", sizeof("NSDL-C mbed device")-1);
00183 
00184     // Dynamic resources
00185     create_temperature_resource(resource_ptr);
00186     create_light_resource(resource_ptr);
00187     create_gps_resource(resource_ptr);
00188     create_relay_resource(resource_ptr);
00189 
00190         /* Register with NSP */
00191     endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
00192     if(sn_nsdl_register_endpoint(endpoint_ptr) != 0)
00193         pc.printf("NSP registering failed\r\n");
00194     else
00195         pc.printf("NSP registering OK\r\n");
00196     nsdl_clean_register_endpoint(&endpoint_ptr);
00197 
00198     nsdl_free(resource_ptr->resource_parameters_ptr);
00199     nsdl_free(resource_ptr);
00200     return 1;
00201 }
00202 int main() {
00203     pc.baud(115200);
00204     
00205     // Initialize Ethernet interface first
00206     ethernet_init();
00207     
00208     // Initialize NSP node
00209     nsp_init();
00210     
00211     // Initialize NSDL stack
00212     nsdl_init();
00213     
00214     // Create NSDL resources
00215     create_resources();
00216     
00217     // Run the NSDL event loop (never returns)
00218     nsdl_event_loop();
00219 //    eth.init(); //Use DHCP
00220 //    eth.init("192.168.0.110","255.255.255.0","192.168.0.1");
00221 //    eth.connect();
00222 //    pc.printf("IP Address is %s\n", eth.getIPAddress());
00223 //    pc.printf("MAC Address is %s\n", eth.getMACAddress());
00224 //    pc.printf("GateWay Address is %s\n", eth.getGateway());
00225 //    while(1)
00226 //    {
00227 //        led=!led;
00228 //        wait(1);
00229 //    }
00230 }