Example program for simple-mbed-client, connecting a device to mbed Device Connector.
Dependencies: simple-mbed-client
Fork of simple-mbed-client-example by
main.cpp
00001 #include "mbed.h" 00002 #include "security.h" 00003 #include "easy-connect.h" 00004 #include "simple-mbed-client.h" 00005 00006 SimpleMbedClient client; 00007 00008 // Declare peripherals 00009 DigitalOut connectivityLed(LED2); // Blinks while connecting, turns solid when connected 00010 DigitalOut augmentedLed(LED1, BUILTIN_LED_OFF); // LED that can be controlled through Connector 00011 InterruptIn btn(MBED_CONF_APP_BUTTON); // Button that sends it's count to Connector 00012 00013 Ticker connectivityTicker; 00014 00015 // Callback function when the pattern resource is updated 00016 void patternUpdated(string v) { 00017 printf("New pattern: %s\n", v.c_str()); 00018 } 00019 00020 // Define resources here. They act as normal variables, but are exposed to the internet... 00021 SimpleResourceInt btn_count = client.define_resource("button/0/clicks", 0, M2MBase::GET_ALLOWED); 00022 SimpleResourceString pattern = client.define_resource("led/0/pattern", "500:500:500:500:500:500:500", &patternUpdated); 00023 00024 void fall() { 00025 btn_count = btn_count + 1; 00026 printf("Button count is now %d\r\n", static_cast<int>(btn_count)); 00027 } 00028 00029 void toggleConnectivityLed() { 00030 connectivityLed = !connectivityLed; 00031 } 00032 00033 void registered() { 00034 printf("Registered\r\n"); 00035 00036 connectivityTicker.detach(); 00037 connectivityLed = BUILTIN_LED_ON; 00038 } 00039 00040 void play(void* args) { 00041 connectivityLed = BUILTIN_LED_OFF; 00042 00043 // Parse the pattern string, and toggle the LED in that pattern 00044 string s = static_cast<string>(pattern); 00045 size_t i = 0; 00046 size_t pos = s.find(':'); 00047 while (pos != string::npos) { 00048 wait_ms(atoi(s.substr(i, pos - i).c_str())); 00049 augmentedLed = !augmentedLed; 00050 00051 i = ++pos; 00052 pos = s.find(':', pos); 00053 00054 if (pos == string::npos) { 00055 wait_ms(atoi(s.substr(i, s.length()).c_str())); 00056 augmentedLed = !augmentedLed; 00057 } 00058 } 00059 00060 augmentedLed = BUILTIN_LED_OFF; 00061 connectivityLed = BUILTIN_LED_ON; 00062 } 00063 00064 int main() { 00065 // SimpleClient gives us an event queue which we can use, running on a separate thread (see https://docs.mbed.com/docs/mbed-os-handbook/en/5.1/concepts/events/) 00066 EventQueue* eventQueue = client.eventQueue(); 00067 00068 // Handle interrupts on the eventQueue - so we don't do things in interrupt context 00069 btn.fall(eventQueue->event(&fall)); 00070 00071 // Toggle the built-in LED every second (until we're connected, see `registered` function) 00072 connectivityTicker.attach(eventQueue->event(&toggleConnectivityLed), 1.0f); 00073 00074 // Functions can be executed through mbed Device Connector via POST calls (also defer to the event thread, so we never block) 00075 client.define_function("led/0/play", eventQueue->event(&play)); 00076 00077 // Connect to the internet (see mbed_app.json for the connectivity method) 00078 NetworkInterface* network = easy_connect(true); 00079 if (!network) { 00080 return 1; 00081 } 00082 00083 // Connect to mbed Device Connector 00084 struct MbedClientOptions opts = client.get_default_options(); // opts contains information like the DeviceType 00085 bool setup = client.setup(opts, network); 00086 if (!setup) { 00087 printf("Client setup failed\n"); 00088 return 1; 00089 } 00090 client.on_registered(®istered); 00091 }
Generated on Tue Jul 12 2022 17:22:23 by 1.7.2