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@29:dd6231df71bb, 2016-09-06 (annotated)
- Committer:
- Jan Jongboom
- Date:
- Tue Sep 06 13:38:46 2016 +0200
- Revision:
- 29:dd6231df71bb
- Parent:
- 28:f5e41738699d
- Child:
- 34:1e8d914a2876
Override button in config, update mbed-os to latest master, update easy-connect
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 |
17:6d69aa1b393f | 6 | Serial pc(USBTX, USBRX); |
| janjongboom | 13:d4da5e6aa952 | 7 | SimpleMbedClient client; |
| geky | 4:dcd0494556be | 8 | |
| Jan Jongboom |
23:16f397924e3d | 9 | DigitalOut led(LED1); |
| Jan Jongboom |
23:16f397924e3d | 10 | DigitalOut blinkLed(LED2); |
| Jan Jongboom |
29:dd6231df71bb | 11 | InterruptIn btn(MBED_CONF_APP_BUTTON); |
| janjongboom | 14:ddc258abaaac | 12 | Semaphore updates(0); |
| janjongboom | 14:ddc258abaaac | 13 | |
| Jan Jongboom |
17:6d69aa1b393f | 14 | void patternUpdated(string v) { |
| Jan Jongboom |
17:6d69aa1b393f | 15 | printf("New pattern: %s\n", v.c_str()); |
| janjongboom | 13:d4da5e6aa952 | 16 | } |
| geky | 4:dcd0494556be | 17 | |
| Jan Jongboom |
17:6d69aa1b393f | 18 | SimpleResourceInt btn_count = client.define_resource("button/0/clicks", 0, M2MBase::GET_ALLOWED); |
| Jan Jongboom |
17:6d69aa1b393f | 19 | SimpleResourceString pattern = client.define_resource("led/0/pattern", "500:500:500:500:500:500:500", &patternUpdated); |
| geky | 4:dcd0494556be | 20 | |
| janjongboom | 13:d4da5e6aa952 | 21 | void fall() { |
| Jan Jongboom |
16:71ec73ea9b6d | 22 | updates.release(); |
| janjongboom | 13:d4da5e6aa952 | 23 | } |
| janjongboom | 13:d4da5e6aa952 | 24 | |
| janjongboom | 13:d4da5e6aa952 | 25 | void toggleLed() { |
| Jan Jongboom |
16:71ec73ea9b6d | 26 | led = !led; |
| geky | 4:dcd0494556be | 27 | } |
| geky | 4:dcd0494556be | 28 | |
| Jan Jongboom |
17:6d69aa1b393f | 29 | void registered() { |
| Jan Jongboom |
28:f5e41738699d | 30 | pc.printf("Registered\r\n"); |
| Jan Jongboom |
17:6d69aa1b393f | 31 | } |
| Jan Jongboom |
17:6d69aa1b393f | 32 | void unregistered() { |
| Jan Jongboom |
28:f5e41738699d | 33 | pc.printf("Unregistered\r\n"); |
| Jan Jongboom |
17:6d69aa1b393f | 34 | } |
| Jan Jongboom |
17:6d69aa1b393f | 35 | |
| Jan Jongboom |
17:6d69aa1b393f | 36 | void play(void* args) { |
| Jan Jongboom |
17:6d69aa1b393f | 37 | stringstream ss(pattern); |
| Jan Jongboom |
17:6d69aa1b393f | 38 | string item; |
| Jan Jongboom |
17:6d69aa1b393f | 39 | while(getline(ss, item, ':')) { |
| Jan Jongboom |
17:6d69aa1b393f | 40 | wait_ms(atoi((const char*)item.c_str())); |
| Jan Jongboom |
17:6d69aa1b393f | 41 | blinkLed = !blinkLed; |
| Jan Jongboom |
17:6d69aa1b393f | 42 | } |
| Jan Jongboom |
17:6d69aa1b393f | 43 | } |
| Jan Jongboom |
17:6d69aa1b393f | 44 | |
| geky | 4:dcd0494556be | 45 | int main() { |
| janjongboom | 13:d4da5e6aa952 | 46 | pc.baud(115200); |
| Jan Jongboom |
16:71ec73ea9b6d | 47 | |
| Jan Jongboom |
17:6d69aa1b393f | 48 | btn.fall(&fall); |
| Jan Jongboom |
17:6d69aa1b393f | 49 | |
| janjongboom | 13:d4da5e6aa952 | 50 | Ticker t; |
| janjongboom | 13:d4da5e6aa952 | 51 | t.attach(&toggleLed, 1.0f); |
| Jan Jongboom |
16:71ec73ea9b6d | 52 | |
| Jan Jongboom |
17:6d69aa1b393f | 53 | client.define_function("led/0/play", &play); |
| Jan Jongboom |
16:71ec73ea9b6d | 54 | |
| Jan Jongboom |
23:16f397924e3d | 55 | NetworkInterface* network = easy_connect(true); |
| Jan Jongboom |
17:6d69aa1b393f | 56 | if (!network) { |
| Jan Jongboom |
16:71ec73ea9b6d | 57 | return 1; |
| Jan Jongboom |
16:71ec73ea9b6d | 58 | } |
| geky | 4:dcd0494556be | 59 | |
| Jan Jongboom |
23:16f397924e3d | 60 | struct MbedClientOptions opts = client.get_default_options(); |
| Jan Jongboom |
23:16f397924e3d | 61 | opts.ServerAddress = MBED_SERVER_ADDRESS; |
| Jan Jongboom |
23:16f397924e3d | 62 | bool setup = client.setup(opts, network); |
| janjongboom | 13:d4da5e6aa952 | 63 | if (!setup) { |
| Jan Jongboom |
17:6d69aa1b393f | 64 | printf("Client setup failed\n"); |
| Jan Jongboom |
16:71ec73ea9b6d | 65 | return 1; |
| geky | 4:dcd0494556be | 66 | } |
| janjongboom | 14:ddc258abaaac | 67 | |
| janjongboom | 13:d4da5e6aa952 | 68 | client.on_registered(®istered); |
| janjongboom | 13:d4da5e6aa952 | 69 | client.on_unregistered(&unregistered); |
| Jan Jongboom |
16:71ec73ea9b6d | 70 | |
| janjongboom | 14:ddc258abaaac | 71 | while (1) { |
| Jan Jongboom |
16:71ec73ea9b6d | 72 | int v = updates.wait(25000); |
| Jan Jongboom |
16:71ec73ea9b6d | 73 | if (v == 1) { |
| Jan Jongboom |
16:71ec73ea9b6d | 74 | btn_count = btn_count + 1; |
| Jan Jongboom |
17:6d69aa1b393f | 75 | printf("Button count is now %d\n", static_cast<int>(btn_count)); |
| Jan Jongboom |
16:71ec73ea9b6d | 76 | } |
| Jan Jongboom |
16:71ec73ea9b6d | 77 | client.keep_alive(); |
| janjongboom | 14:ddc258abaaac | 78 | } |
| geky | 4:dcd0494556be | 79 | } |
