Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: simple-mbed-client
Fork of simple-mbed-client-example by
main.cpp@34:1e8d914a2876, 2016-12-02 (annotated)
- Committer:
- Jan Jongboom
- Date:
- Fri Dec 02 15:44:32 2016 +0800
- Revision:
- 34:1e8d914a2876
- Parent:
- 29:dd6231df71bb
Move to mbed OS 5.2.3, get rid of Semaphore
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| geky | 4:dcd0494556be | 1 | #include "mbed.h" |
| Jan Jongboom |
17:6d69aa1b393f | 2 | #include "security.h" |
| Jan Jongboom |
17:6d69aa1b393f | 3 | #include "easy-connect.h" |
| Jan Jongboom |
17:6d69aa1b393f | 4 | #include "simple-mbed-client.h" |
| geky | 4:dcd0494556be | 5 | |
| Jan Jongboom |
34:1e8d914a2876 | 6 | EventQueue eventQueue(32 * EVENTS_EVENT_SIZE); // Event queue! |
| Jan Jongboom |
34:1e8d914a2876 | 7 | |
| janjongboom | 13:d4da5e6aa952 | 8 | SimpleMbedClient client; |
| geky | 4:dcd0494556be | 9 | |
| Jan Jongboom |
34:1e8d914a2876 | 10 | // Declare peripherals |
| Jan Jongboom |
34:1e8d914a2876 | 11 | DigitalOut connectivityLed(LED2); // Blinks while connecting, turns solid when connected |
| Jan Jongboom |
34:1e8d914a2876 | 12 | DigitalOut augmentedLed(LED1, BUILTIN_LED_OFF); // LED that can be controlled through Connector |
| Jan Jongboom |
34:1e8d914a2876 | 13 | InterruptIn btn(MBED_CONF_APP_BUTTON); // Button that sends it's count to Connector |
| janjongboom | 14:ddc258abaaac | 14 | |
| Jan Jongboom |
34:1e8d914a2876 | 15 | Ticker connectivityTicker; |
| Jan Jongboom |
34:1e8d914a2876 | 16 | |
| Jan Jongboom |
34:1e8d914a2876 | 17 | // Callback function when the pattern resource is updated |
| Jan Jongboom |
17:6d69aa1b393f | 18 | void patternUpdated(string v) { |
| Jan Jongboom |
17:6d69aa1b393f | 19 | printf("New pattern: %s\n", v.c_str()); |
| janjongboom | 13:d4da5e6aa952 | 20 | } |
| geky | 4:dcd0494556be | 21 | |
| Jan Jongboom |
34:1e8d914a2876 | 22 | // Define resources here. They act as normal variables, but are exposed to the internet... |
| Jan Jongboom |
17:6d69aa1b393f | 23 | SimpleResourceInt btn_count = client.define_resource("button/0/clicks", 0, M2MBase::GET_ALLOWED); |
| Jan Jongboom |
17:6d69aa1b393f | 24 | SimpleResourceString pattern = client.define_resource("led/0/pattern", "500:500:500:500:500:500:500", &patternUpdated); |
| geky | 4:dcd0494556be | 25 | |
| janjongboom | 13:d4da5e6aa952 | 26 | void fall() { |
| Jan Jongboom |
34:1e8d914a2876 | 27 | btn_count = btn_count + 1; |
| Jan Jongboom |
34:1e8d914a2876 | 28 | printf("Button count is now %d\r\n", static_cast<int>(btn_count)); |
| janjongboom | 13:d4da5e6aa952 | 29 | } |
| janjongboom | 13:d4da5e6aa952 | 30 | |
| Jan Jongboom |
34:1e8d914a2876 | 31 | void toggleConnectivityLed() { |
| Jan Jongboom |
34:1e8d914a2876 | 32 | connectivityLed = !connectivityLed; |
| geky | 4:dcd0494556be | 33 | } |
| geky | 4:dcd0494556be | 34 | |
| Jan Jongboom |
17:6d69aa1b393f | 35 | void registered() { |
| Jan Jongboom |
34:1e8d914a2876 | 36 | printf("Registered\r\n"); |
| Jan Jongboom |
34:1e8d914a2876 | 37 | |
| Jan Jongboom |
34:1e8d914a2876 | 38 | connectivityTicker.detach(); |
| Jan Jongboom |
34:1e8d914a2876 | 39 | connectivityLed = BUILTIN_LED_ON; |
| Jan Jongboom |
17:6d69aa1b393f | 40 | } |
| Jan Jongboom |
17:6d69aa1b393f | 41 | |
| Jan Jongboom |
17:6d69aa1b393f | 42 | void play(void* args) { |
| Jan Jongboom |
34:1e8d914a2876 | 43 | connectivityLed = BUILTIN_LED_OFF; |
| Jan Jongboom |
34:1e8d914a2876 | 44 | |
| Jan Jongboom |
34:1e8d914a2876 | 45 | // Parse the pattern string, and toggle the LED in that pattern |
| Jan Jongboom |
17:6d69aa1b393f | 46 | stringstream ss(pattern); |
| Jan Jongboom |
17:6d69aa1b393f | 47 | string item; |
| Jan Jongboom |
17:6d69aa1b393f | 48 | while(getline(ss, item, ':')) { |
| Jan Jongboom |
17:6d69aa1b393f | 49 | wait_ms(atoi((const char*)item.c_str())); |
| Jan Jongboom |
34:1e8d914a2876 | 50 | augmentedLed = !augmentedLed; |
| Jan Jongboom |
17:6d69aa1b393f | 51 | } |
| Jan Jongboom |
34:1e8d914a2876 | 52 | |
| Jan Jongboom |
34:1e8d914a2876 | 53 | augmentedLed = BUILTIN_LED_OFF; |
| Jan Jongboom |
34:1e8d914a2876 | 54 | connectivityLed = BUILTIN_LED_ON; |
| Jan Jongboom |
17:6d69aa1b393f | 55 | } |
| Jan Jongboom |
17:6d69aa1b393f | 56 | |
| geky | 4:dcd0494556be | 57 | int main() { |
| Jan Jongboom |
34:1e8d914a2876 | 58 | // Run a separate Thread to process events in |
| Jan Jongboom |
34:1e8d914a2876 | 59 | rtos::Thread et; |
| Jan Jongboom |
34:1e8d914a2876 | 60 | et.start(callback(&eventQueue, &EventQueue::dispatch_forever)); |
| Jan Jongboom |
16:71ec73ea9b6d | 61 | |
| Jan Jongboom |
34:1e8d914a2876 | 62 | // Handle interrupts on the eventQueue - so we don't do things in interrupt context |
| Jan Jongboom |
34:1e8d914a2876 | 63 | btn.fall(eventQueue.event(&fall)); |
| Jan Jongboom |
17:6d69aa1b393f | 64 | |
| Jan Jongboom |
34:1e8d914a2876 | 65 | // Toggle the built-in LED every second (until we're connected, see `registered` function) |
| Jan Jongboom |
34:1e8d914a2876 | 66 | connectivityTicker.attach(eventQueue.event(toggleConnectivityLed), 1.0f); |
| Jan Jongboom |
16:71ec73ea9b6d | 67 | |
| Jan Jongboom |
34:1e8d914a2876 | 68 | // Keep alive ticker (every 25 seconds) |
| Jan Jongboom |
34:1e8d914a2876 | 69 | eventQueue.call_every(25000, callback(&client, &SimpleMbedClient::keep_alive)); |
| Jan Jongboom |
34:1e8d914a2876 | 70 | |
| Jan Jongboom |
34:1e8d914a2876 | 71 | // Functions can be executed through mbed Device Connector via POST calls |
| Jan Jongboom |
17:6d69aa1b393f | 72 | client.define_function("led/0/play", &play); |
| Jan Jongboom |
16:71ec73ea9b6d | 73 | |
| Jan Jongboom |
34:1e8d914a2876 | 74 | // Connect to the internet (see mbed_app.json for the connectivity method) |
| Jan Jongboom |
23:16f397924e3d | 75 | NetworkInterface* network = easy_connect(true); |
| Jan Jongboom |
17:6d69aa1b393f | 76 | if (!network) { |
| Jan Jongboom |
16:71ec73ea9b6d | 77 | return 1; |
| Jan Jongboom |
16:71ec73ea9b6d | 78 | } |
| geky | 4:dcd0494556be | 79 | |
| Jan Jongboom |
34:1e8d914a2876 | 80 | // Connect to mbed Device Connector |
| Jan Jongboom |
23:16f397924e3d | 81 | struct MbedClientOptions opts = client.get_default_options(); |
| Jan Jongboom |
23:16f397924e3d | 82 | opts.ServerAddress = MBED_SERVER_ADDRESS; |
| Jan Jongboom |
34:1e8d914a2876 | 83 | |
| Jan Jongboom |
23:16f397924e3d | 84 | bool setup = client.setup(opts, network); |
| janjongboom | 13:d4da5e6aa952 | 85 | if (!setup) { |
| Jan Jongboom |
17:6d69aa1b393f | 86 | printf("Client setup failed\n"); |
| Jan Jongboom |
16:71ec73ea9b6d | 87 | return 1; |
| geky | 4:dcd0494556be | 88 | } |
| janjongboom | 14:ddc258abaaac | 89 | |
| janjongboom | 13:d4da5e6aa952 | 90 | client.on_registered(®istered); |
| Jan Jongboom |
16:71ec73ea9b6d | 91 | |
| Jan Jongboom |
34:1e8d914a2876 | 92 | while (1) {} |
| geky | 4:dcd0494556be | 93 | } |
