Austin Blackstone / Mbed 2 deprecated mbed-client-classic-example-lwip

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of mbed-client-classic-example-lwip by sandbox

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * Copyright (c) 2015 ARM Limited. All rights reserved.
00003  * SPDX-License-Identifier: Apache-2.0
00004  * Licensed under the Apache License, Version 2.0 (the License); you may
00005  * not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include "EthernetInterface.h"
00017 #include "mbed-client/m2minterfacefactory.h"
00018 #include "mbed-client/m2mdevice.h"
00019 #include "mbed-client/m2minterfaceobserver.h"
00020 #include "mbed-client/m2minterface.h"
00021 #include "mbed-client/m2mobjectinstance.h"
00022 #include "mbed-client/m2mresource.h"
00023 #include "security.h"
00024 #include "ns_trace.h"
00025 
00026 #include "mbed.h"
00027 #include "simple_client.h"
00028 
00029 
00030 Serial output(USBTX, USBRX);
00031 AnalogIn currentSensor(A0);
00032 Ticker getCurrent;
00033 
00034 
00035 #if defined(TARGET_K64F)
00036 #define OBS_BUTTON SW2
00037 #define UNREG_BUTTON SW3
00038 #endif
00039 
00040 
00041 EthernetInterface eth;
00042 // Instantiate the class which implements
00043 // LWM2M Client API
00044 MbedClient mbed_client;
00045 
00046 // Set up Hardware interrupt button.
00047 InterruptIn obs_button(OBS_BUTTON);
00048 InterruptIn unreg_button(UNREG_BUTTON);
00049 
00050 // Network interaction must be performed outside of interrupt context
00051 Semaphore updates(0);
00052 volatile bool registered = false;
00053 osThreadId mainThread;
00054 
00055 void unregister() {
00056     registered = false;
00057     updates.release();
00058 }
00059 
00060 void update() {
00061     updates.release();
00062 }
00063 
00064 volatile float max_value = 0;
00065 volatile float amplitude_current = 0;
00066 volatile float effective_value = 0;
00067 
00068 void calcCurrent()
00069 {
00070     int count = 0;
00071     float x = 0;
00072     while(count++ < 10) {
00073         x = currentSensor.read();
00074         if(x == 0 && _relay == 0) {
00075             max_value = 0;
00076         } else if (x > max_value)  {
00077             max_value = x;
00078         }
00079     }
00080     amplitude_current = max_value*3.3/800*2000000;
00081     effective_value=amplitude_current/1.414;
00082 }
00083 
00084 // Entry point to the program
00085 int main() {
00086    
00087     // Keep track of the main thread
00088     mainThread = osThreadGetId();
00089 
00090     // Sets the console baud-rate
00091     output.baud(115200);
00092     output.printf("Its Aliiiive!!!\r\n");
00093 
00094     // This sets up the network interface configuration which will be used
00095     // by LWM2M Client API to communicate with mbed Device server.
00096     eth.init(); //Use DHCP
00097     eth.connect();
00098 
00099     //lwipv4_socket_init();
00100     output.printf("IP address %s\r\n", eth.getIPAddress());
00101 
00102     // On press of SW3 button on K64F board, example application
00103     // will call unregister API towards mbed Device Server
00104     //unreg_button.fall(&mbed_client,&MbedClient::test_unregister);
00105     unreg_button.fall(&unregister);
00106 
00107     // On press of SW2 button on K64F board, example application
00108     // will send observation towards mbed Device Server
00109     obs_button.fall(&update);
00110     
00111     getCurrent.attach(&calcCurrent, 0.5f);
00112 
00113     // Create LWM2M Client API interface to manage register and unregister
00114     mbed_client.create_interface();
00115 
00116     // Create LWM2M server object specifying mbed device server
00117     // information.
00118     M2MSecurity* register_object = mbed_client.create_register_object();
00119 
00120     // Create LWM2M device object specifying device resources
00121     // as per OMA LWM2M specification.
00122     M2MDevice* device_object = mbed_client.create_device_object();
00123 
00124     // Create Generic object specifying custom resources
00125     M2MObject* generic_object = mbed_client.create_generic_object();
00126 
00127     // Add all the objects that you would like to register
00128     // into the list and pass the list for register API.
00129     M2MObjectList object_list;
00130     //object_list.push_back(device_object);
00131     object_list.push_back(generic_object);
00132 
00133     mbed_client.set_register_object(register_object);
00134 
00135     // Register with mbed Device Connector
00136     mbed_client.test_register(register_object, object_list);
00137     registered = true;
00138        uint32_t count = 0;
00139 
00140     while (true) {
00141         int update = updates.wait(1000);
00142 
00143         if (!registered) {
00144             break;
00145         } else if (update) {
00146             mbed_client.update_resource();
00147         } else if(++count%25 == 0) {
00148             mbed_client.test_update_register();
00149             output.printf("\r\nRegistration Updated\r\n");
00150         }
00151         output.printf(".");
00152         mbed_client.updateCurrent(effective_value);
00153         output.printf("max = %f, amp=%f eff = %fmA, \r\n",max_value,amplitude_current,effective_value);
00154     }
00155 
00156     mbed_client.test_unregister();
00157 }
00158 
00159