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@16:71ec73ea9b6d, 2016-05-24 (annotated)
- Committer:
- Jan Jongboom
- Date:
- Tue May 24 15:02:19 2016 -0500
- Revision:
- 16:71ec73ea9b6d
- Parent:
- 14:ddc258abaaac
- Child:
- 17:6d69aa1b393f
Move from mbed.bld to mbed-os.lib
Who changed what in which revision?
| User | Revision | Line number | New 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 | */ |
| Jan Jongboom |
16:71ec73ea9b6d | 16 | #include <string> |
| geky | 4:dcd0494556be | 17 | #include "mbed.h" |
| janjongboom | 13:d4da5e6aa952 | 18 | #include "security.h" // security file from connector.mbed.com |
| janjongboom | 13:d4da5e6aa952 | 19 | #include "LWIPInterface.h" // using Ethernet for connectivity |
| janjongboom | 13:d4da5e6aa952 | 20 | #include "simple-mbed-client.h" // simple-mbed-client |
| geky | 4:dcd0494556be | 21 | |
| janjongboom | 13:d4da5e6aa952 | 22 | // So these lines are unique to connecting etc... but then it's just a normal program |
| geky | 4:dcd0494556be | 23 | |
| janjongboom | 13:d4da5e6aa952 | 24 | Serial pc(USBTX, USBRX); // talk back to the computer |
| janjongboom | 13:d4da5e6aa952 | 25 | LWIPInterface lwip; // define the Ethernet interface |
| janjongboom | 13:d4da5e6aa952 | 26 | SimpleMbedClient client; |
| geky | 4:dcd0494556be | 27 | |
| janjongboom | 13:d4da5e6aa952 | 28 | // Declare some peripherals here |
| janjongboom | 13:d4da5e6aa952 | 29 | DigitalOut led(LED_RED); |
| janjongboom | 13:d4da5e6aa952 | 30 | DigitalOut registeredLed(LED_BLUE, 1); |
| janjongboom | 13:d4da5e6aa952 | 31 | InterruptIn btn(SW3); |
| geky | 4:dcd0494556be | 32 | |
| janjongboom | 14:ddc258abaaac | 33 | // We need a semaphore because we cannot update value in interrupt context |
| janjongboom | 14:ddc258abaaac | 34 | Semaphore updates(0); |
| janjongboom | 14:ddc258abaaac | 35 | |
| janjongboom | 13:d4da5e6aa952 | 36 | // play function, invoked thru mbed Client |
| janjongboom | 13:d4da5e6aa952 | 37 | void play(void* args) { |
| Jan Jongboom |
16:71ec73ea9b6d | 38 | printf("I'm gonna play a song!\n"); |
| janjongboom | 13:d4da5e6aa952 | 39 | } |
| geky | 0:9ad9d701b1c3 | 40 | |
| janjongboom | 13:d4da5e6aa952 | 41 | // patternUpdated is called when the value of led/0/pattern changes |
| janjongboom | 13:d4da5e6aa952 | 42 | void patternUpdated(string newPattern) { |
| Jan Jongboom |
16:71ec73ea9b6d | 43 | printf("Got a new pattern... %s\n", newPattern.c_str()); |
| janjongboom | 13:d4da5e6aa952 | 44 | } |
| geky | 4:dcd0494556be | 45 | |
| janjongboom | 13:d4da5e6aa952 | 46 | // Here we define some mbed Client resources |
| janjongboom | 13:d4da5e6aa952 | 47 | SimpleResourceInt btn_count = client.define_resource("button/0/clicks", 0, M2MBase::GET_ALLOWED, true); |
| janjongboom | 13:d4da5e6aa952 | 48 | // need a callback when a PUT request comes in? Just pass in a callback |
| janjongboom | 13:d4da5e6aa952 | 49 | SimpleResourceString pattern = client.define_resource("led/0/pattern", "500:500:500", &patternUpdated); |
| geky | 4:dcd0494556be | 50 | |
| janjongboom | 13:d4da5e6aa952 | 51 | // Status callbacks if you're interested in that |
| janjongboom | 13:d4da5e6aa952 | 52 | void registered() { |
| Jan Jongboom |
16:71ec73ea9b6d | 53 | printf("registered\n"); |
| Jan Jongboom |
16:71ec73ea9b6d | 54 | registeredLed = 0; |
| janjongboom | 13:d4da5e6aa952 | 55 | } |
| janjongboom | 13:d4da5e6aa952 | 56 | void unregistered() { |
| Jan Jongboom |
16:71ec73ea9b6d | 57 | printf("unregistered\n"); |
| Jan Jongboom |
16:71ec73ea9b6d | 58 | registeredLed = 1; |
| geky | 4:dcd0494556be | 59 | } |
| geky | 4:dcd0494556be | 60 | |
| janjongboom | 14:ddc258abaaac | 61 | // we cannot do blocking stuff here, or device will crash |
| janjongboom | 14:ddc258abaaac | 62 | // release semaphore and update btn_count in the while() loop... |
| janjongboom | 13:d4da5e6aa952 | 63 | void fall() { |
| Jan Jongboom |
16:71ec73ea9b6d | 64 | updates.release(); |
| janjongboom | 13:d4da5e6aa952 | 65 | } |
| janjongboom | 13:d4da5e6aa952 | 66 | |
| janjongboom | 13:d4da5e6aa952 | 67 | // normal function |
| janjongboom | 13:d4da5e6aa952 | 68 | void toggleLed() { |
| Jan Jongboom |
16:71ec73ea9b6d | 69 | led = !led; |
| geky | 4:dcd0494556be | 70 | } |
| geky | 4:dcd0494556be | 71 | |
| geky | 4:dcd0494556be | 72 | int main() { |
| janjongboom | 13:d4da5e6aa952 | 73 | pc.baud(115200); |
| Jan Jongboom |
16:71ec73ea9b6d | 74 | |
| janjongboom | 13:d4da5e6aa952 | 75 | Ticker t; |
| janjongboom | 13:d4da5e6aa952 | 76 | t.attach(&toggleLed, 1.0f); |
| Jan Jongboom |
16:71ec73ea9b6d | 77 | |
| janjongboom | 13:d4da5e6aa952 | 78 | btn.fall(&fall); |
| Jan Jongboom |
16:71ec73ea9b6d | 79 | |
| janjongboom | 13:d4da5e6aa952 | 80 | // here we define functions that should live in the cloud |
| Jan Jongboom |
16:71ec73ea9b6d | 81 | client.define_function("song/0/play", &play); |
| geky | 4:dcd0494556be | 82 | |
| Jan Jongboom |
16:71ec73ea9b6d | 83 | int connect = lwip.connect(); // can swap out for other network iface |
| Jan Jongboom |
16:71ec73ea9b6d | 84 | if (connect != 0) { |
| Jan Jongboom |
16:71ec73ea9b6d | 85 | printf("Connect to eth failed...\n"); |
| Jan Jongboom |
16:71ec73ea9b6d | 86 | return 1; |
| Jan Jongboom |
16:71ec73ea9b6d | 87 | } |
| geky | 4:dcd0494556be | 88 | |
| Jan Jongboom |
16:71ec73ea9b6d | 89 | printf("IP address %s\r\n", lwip.get_ip_address()); |
| Jan Jongboom |
16:71ec73ea9b6d | 90 | |
| Jan Jongboom |
16:71ec73ea9b6d | 91 | bool setup = client.setup(&lwip); // start mbed client! |
| janjongboom | 13:d4da5e6aa952 | 92 | if (!setup) { |
| Jan Jongboom |
16:71ec73ea9b6d | 93 | printf("Setting up mbed_client failed...\n"); |
| Jan Jongboom |
16:71ec73ea9b6d | 94 | return 1; |
| geky | 4:dcd0494556be | 95 | } |
| janjongboom | 14:ddc258abaaac | 96 | |
| janjongboom | 13:d4da5e6aa952 | 97 | client.on_registered(®istered); |
| janjongboom | 13:d4da5e6aa952 | 98 | client.on_unregistered(&unregistered); |
| Jan Jongboom |
16:71ec73ea9b6d | 99 | |
| janjongboom | 14:ddc258abaaac | 100 | while (1) { |
| Jan Jongboom |
16:71ec73ea9b6d | 101 | int v = updates.wait(25000); |
| Jan Jongboom |
16:71ec73ea9b6d | 102 | |
| Jan Jongboom |
16:71ec73ea9b6d | 103 | if (v == 1) { |
| Jan Jongboom |
16:71ec73ea9b6d | 104 | btn_count = btn_count + 1; |
| Jan Jongboom |
16:71ec73ea9b6d | 105 | // printf is a bit inflexible, static_cast to get the right value |
| Jan Jongboom |
16:71ec73ea9b6d | 106 | printf("btn_count is now %d\n", static_cast<int>(btn_count)); |
| Jan Jongboom |
16:71ec73ea9b6d | 107 | } |
| Jan Jongboom |
16:71ec73ea9b6d | 108 | |
| Jan Jongboom |
16:71ec73ea9b6d | 109 | // call keep_alive at least every 30s. |
| Jan Jongboom |
16:71ec73ea9b6d | 110 | client.keep_alive(); |
| janjongboom | 14:ddc258abaaac | 111 | } |
| geky | 4:dcd0494556be | 112 | } |
