mbed client on ethernet with LWIP

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

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

Committer:
mbedAustin
Date:
Thu Jun 09 17:08:36 2016 +0000
Revision:
11:cada08fc8a70
Parent:
4:dcd0494556be
Commit for public Consumption

Who changed what in which revision?

UserRevisionLine numberNew contents of line
geky 4:dcd0494556be 1 /*
geky 4:dcd0494556be 2 * Copyright (c) 2015 ARM Limited. All rights reserved.
geky 4:dcd0494556be 3 * SPDX-License-Identifier: Apache-2.0
geky 4:dcd0494556be 4 * Licensed under the Apache License, Version 2.0 (the License); you may
geky 4:dcd0494556be 5 * not use this file except in compliance with the License.
geky 4:dcd0494556be 6 * You may obtain a copy of the License at
geky 4:dcd0494556be 7 *
geky 4:dcd0494556be 8 * http://www.apache.org/licenses/LICENSE-2.0
geky 4:dcd0494556be 9 *
geky 4:dcd0494556be 10 * Unless required by applicable law or agreed to in writing, software
geky 4:dcd0494556be 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
geky 4:dcd0494556be 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
geky 4:dcd0494556be 13 * See the License for the specific language governing permissions and
geky 4:dcd0494556be 14 * limitations under the License.
geky 4:dcd0494556be 15 */
geky 4:dcd0494556be 16 #include "EthernetInterface.h"
geky 4:dcd0494556be 17 #include "mbed-client/m2minterfacefactory.h"
geky 4:dcd0494556be 18 #include "mbed-client/m2mdevice.h"
geky 4:dcd0494556be 19 #include "mbed-client/m2minterfaceobserver.h"
geky 4:dcd0494556be 20 #include "mbed-client/m2minterface.h"
geky 4:dcd0494556be 21 #include "mbed-client/m2mobjectinstance.h"
geky 4:dcd0494556be 22 #include "mbed-client/m2mresource.h"
geky 4:dcd0494556be 23 #include "security.h"
geky 4:dcd0494556be 24 #include "ns_trace.h"
geky 4:dcd0494556be 25
geky 4:dcd0494556be 26 #include "mbed.h"
mbedAustin 11:cada08fc8a70 27 #include "simple_client.h"
geky 4:dcd0494556be 28
geky 4:dcd0494556be 29
geky 4:dcd0494556be 30 Serial output(USBTX, USBRX);
mbedAustin 11:cada08fc8a70 31 AnalogIn currentSensor(A0);
mbedAustin 11:cada08fc8a70 32 Ticker getCurrent;
geky 4:dcd0494556be 33
geky 4:dcd0494556be 34
geky 4:dcd0494556be 35 #if defined(TARGET_K64F)
geky 4:dcd0494556be 36 #define OBS_BUTTON SW2
geky 4:dcd0494556be 37 #define UNREG_BUTTON SW3
geky 4:dcd0494556be 38 #endif
geky 4:dcd0494556be 39
geky 4:dcd0494556be 40
geky 4:dcd0494556be 41 EthernetInterface eth;
geky 4:dcd0494556be 42 // Instantiate the class which implements
geky 4:dcd0494556be 43 // LWM2M Client API
geky 4:dcd0494556be 44 MbedClient mbed_client;
geky 4:dcd0494556be 45
geky 4:dcd0494556be 46 // Set up Hardware interrupt button.
geky 4:dcd0494556be 47 InterruptIn obs_button(OBS_BUTTON);
geky 4:dcd0494556be 48 InterruptIn unreg_button(UNREG_BUTTON);
geky 4:dcd0494556be 49
geky 4:dcd0494556be 50 // Network interaction must be performed outside of interrupt context
geky 4:dcd0494556be 51 Semaphore updates(0);
geky 4:dcd0494556be 52 volatile bool registered = false;
geky 4:dcd0494556be 53 osThreadId mainThread;
geky 4:dcd0494556be 54
geky 4:dcd0494556be 55 void unregister() {
geky 4:dcd0494556be 56 registered = false;
geky 4:dcd0494556be 57 updates.release();
geky 4:dcd0494556be 58 }
geky 4:dcd0494556be 59
geky 4:dcd0494556be 60 void update() {
geky 4:dcd0494556be 61 updates.release();
geky 4:dcd0494556be 62 }
geky 4:dcd0494556be 63
mbedAustin 11:cada08fc8a70 64 volatile float max_value = 0;
mbedAustin 11:cada08fc8a70 65 volatile float amplitude_current = 0;
mbedAustin 11:cada08fc8a70 66 volatile float effective_value = 0;
geky 4:dcd0494556be 67
mbedAustin 11:cada08fc8a70 68 void calcCurrent()
mbedAustin 11:cada08fc8a70 69 {
mbedAustin 11:cada08fc8a70 70 int count = 0;
mbedAustin 11:cada08fc8a70 71 float x = 0;
mbedAustin 11:cada08fc8a70 72 while(count++ < 10) {
mbedAustin 11:cada08fc8a70 73 x = currentSensor.read();
mbedAustin 11:cada08fc8a70 74 if(x == 0 && _relay == 0) {
mbedAustin 11:cada08fc8a70 75 max_value = 0;
mbedAustin 11:cada08fc8a70 76 } else if (x > max_value) {
mbedAustin 11:cada08fc8a70 77 max_value = x;
mbedAustin 11:cada08fc8a70 78 }
mbedAustin 11:cada08fc8a70 79 }
mbedAustin 11:cada08fc8a70 80 amplitude_current = max_value*3.3/800*2000000;
mbedAustin 11:cada08fc8a70 81 effective_value=amplitude_current/1.414;
mbedAustin 11:cada08fc8a70 82 }
geky 4:dcd0494556be 83
geky 4:dcd0494556be 84 // Entry point to the program
geky 4:dcd0494556be 85 int main() {
mbedAustin 11:cada08fc8a70 86
geky 4:dcd0494556be 87 // Keep track of the main thread
geky 4:dcd0494556be 88 mainThread = osThreadGetId();
geky 4:dcd0494556be 89
geky 4:dcd0494556be 90 // Sets the console baud-rate
geky 4:dcd0494556be 91 output.baud(115200);
mbedAustin 11:cada08fc8a70 92 output.printf("Its Aliiiive!!!\r\n");
geky 4:dcd0494556be 93
geky 4:dcd0494556be 94 // This sets up the network interface configuration which will be used
geky 4:dcd0494556be 95 // by LWM2M Client API to communicate with mbed Device server.
geky 4:dcd0494556be 96 eth.init(); //Use DHCP
geky 4:dcd0494556be 97 eth.connect();
geky 4:dcd0494556be 98
geky 4:dcd0494556be 99 //lwipv4_socket_init();
geky 4:dcd0494556be 100 output.printf("IP address %s\r\n", eth.getIPAddress());
geky 4:dcd0494556be 101
geky 4:dcd0494556be 102 // On press of SW3 button on K64F board, example application
geky 4:dcd0494556be 103 // will call unregister API towards mbed Device Server
geky 4:dcd0494556be 104 //unreg_button.fall(&mbed_client,&MbedClient::test_unregister);
geky 4:dcd0494556be 105 unreg_button.fall(&unregister);
geky 4:dcd0494556be 106
geky 4:dcd0494556be 107 // On press of SW2 button on K64F board, example application
geky 4:dcd0494556be 108 // will send observation towards mbed Device Server
geky 4:dcd0494556be 109 obs_button.fall(&update);
mbedAustin 11:cada08fc8a70 110
mbedAustin 11:cada08fc8a70 111 getCurrent.attach(&calcCurrent, 0.5f);
geky 4:dcd0494556be 112
geky 4:dcd0494556be 113 // Create LWM2M Client API interface to manage register and unregister
geky 4:dcd0494556be 114 mbed_client.create_interface();
geky 4:dcd0494556be 115
geky 4:dcd0494556be 116 // Create LWM2M server object specifying mbed device server
geky 4:dcd0494556be 117 // information.
geky 4:dcd0494556be 118 M2MSecurity* register_object = mbed_client.create_register_object();
geky 4:dcd0494556be 119
geky 4:dcd0494556be 120 // Create LWM2M device object specifying device resources
geky 4:dcd0494556be 121 // as per OMA LWM2M specification.
geky 4:dcd0494556be 122 M2MDevice* device_object = mbed_client.create_device_object();
geky 4:dcd0494556be 123
geky 4:dcd0494556be 124 // Create Generic object specifying custom resources
geky 4:dcd0494556be 125 M2MObject* generic_object = mbed_client.create_generic_object();
geky 4:dcd0494556be 126
geky 4:dcd0494556be 127 // Add all the objects that you would like to register
geky 4:dcd0494556be 128 // into the list and pass the list for register API.
geky 4:dcd0494556be 129 M2MObjectList object_list;
mbedAustin 11:cada08fc8a70 130 //object_list.push_back(device_object);
geky 4:dcd0494556be 131 object_list.push_back(generic_object);
geky 4:dcd0494556be 132
geky 4:dcd0494556be 133 mbed_client.set_register_object(register_object);
geky 4:dcd0494556be 134
geky 4:dcd0494556be 135 // Register with mbed Device Connector
geky 4:dcd0494556be 136 mbed_client.test_register(register_object, object_list);
geky 4:dcd0494556be 137 registered = true;
mbedAustin 11:cada08fc8a70 138 uint32_t count = 0;
geky 4:dcd0494556be 139
geky 4:dcd0494556be 140 while (true) {
mbedAustin 11:cada08fc8a70 141 int update = updates.wait(1000);
mbedAustin 11:cada08fc8a70 142
geky 4:dcd0494556be 143 if (!registered) {
mbedAustin 11:cada08fc8a70 144 break;
geky 4:dcd0494556be 145 } else if (update) {
mbedAustin 11:cada08fc8a70 146 mbed_client.update_resource();
mbedAustin 11:cada08fc8a70 147 } else if(++count%25 == 0) {
mbedAustin 11:cada08fc8a70 148 mbed_client.test_update_register();
mbedAustin 11:cada08fc8a70 149 output.printf("\r\nRegistration Updated\r\n");
geky 4:dcd0494556be 150 }
mbedAustin 11:cada08fc8a70 151 output.printf(".");
mbedAustin 11:cada08fc8a70 152 mbed_client.updateCurrent(effective_value);
mbedAustin 11:cada08fc8a70 153 output.printf("max = %f, amp=%f eff = %fmA, \r\n",max_value,amplitude_current,effective_value);
geky 4:dcd0494556be 154 }
geky 4:dcd0494556be 155
geky 4:dcd0494556be 156 mbed_client.test_unregister();
geky 4:dcd0494556be 157 }
geky 4:dcd0494556be 158
geky 4:dcd0494556be 159