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@13:d4da5e6aa952, 2016-05-15 (annotated)
- Committer:
- janjongboom
- Date:
- Sun May 15 20:28:52 2016 +0000
- Revision:
- 13:d4da5e6aa952
- Parent:
- 12:31e60c8bb7f6
- Child:
- 14:ddc258abaaac
Use simple-mbed-client to expose three resources to the cloud
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 | */ |
| geky | 4:dcd0494556be | 16 | #include "mbed.h" |
| janjongboom | 13:d4da5e6aa952 | 17 | #include "security.h" // security file from connector.mbed.com |
| janjongboom | 13:d4da5e6aa952 | 18 | #include "LWIPInterface.h" // using Ethernet for connectivity |
| janjongboom | 13:d4da5e6aa952 | 19 | #include "simple-mbed-client.h" // simple-mbed-client |
| geky | 4:dcd0494556be | 20 | |
| janjongboom | 13:d4da5e6aa952 | 21 | // So these lines are unique to connecting etc... but then it's just a normal program |
| geky | 4:dcd0494556be | 22 | |
| janjongboom | 13:d4da5e6aa952 | 23 | Serial pc(USBTX, USBRX); // talk back to the computer |
| janjongboom | 13:d4da5e6aa952 | 24 | LWIPInterface lwip; // define the Ethernet interface |
| janjongboom | 13:d4da5e6aa952 | 25 | SimpleMbedClient client; |
| geky | 4:dcd0494556be | 26 | |
| janjongboom | 13:d4da5e6aa952 | 27 | // Declare some peripherals here |
| janjongboom | 13:d4da5e6aa952 | 28 | DigitalOut led(LED_RED); |
| janjongboom | 13:d4da5e6aa952 | 29 | DigitalOut registeredLed(LED_BLUE, 1); |
| janjongboom | 13:d4da5e6aa952 | 30 | InterruptIn btn(SW3); |
| geky | 4:dcd0494556be | 31 | |
| janjongboom | 13:d4da5e6aa952 | 32 | // play function, invoked thru mbed Client |
| janjongboom | 13:d4da5e6aa952 | 33 | void play(void* args) { |
| janjongboom | 13:d4da5e6aa952 | 34 | pc.printf("I'm gonna play a song!\n"); |
| janjongboom | 13:d4da5e6aa952 | 35 | } |
| geky | 0:9ad9d701b1c3 | 36 | |
| janjongboom | 13:d4da5e6aa952 | 37 | // patternUpdated is called when the value of led/0/pattern changes |
| janjongboom | 13:d4da5e6aa952 | 38 | void patternUpdated(string newPattern) { |
| janjongboom | 13:d4da5e6aa952 | 39 | pc.printf("Got a new pattern... %s\n", newPattern.c_str()); |
| janjongboom | 13:d4da5e6aa952 | 40 | } |
| geky | 4:dcd0494556be | 41 | |
| janjongboom | 13:d4da5e6aa952 | 42 | // Here we define some mbed Client resources |
| janjongboom | 13:d4da5e6aa952 | 43 | SimpleResourceInt btn_count = client.define_resource("button/0/clicks", 0, M2MBase::GET_ALLOWED, true); |
| janjongboom | 13:d4da5e6aa952 | 44 | // need a callback when a PUT request comes in? Just pass in a callback |
| janjongboom | 13:d4da5e6aa952 | 45 | SimpleResourceString pattern = client.define_resource("led/0/pattern", "500:500:500", &patternUpdated); |
| geky | 4:dcd0494556be | 46 | |
| janjongboom | 13:d4da5e6aa952 | 47 | // Status callbacks if you're interested in that |
| janjongboom | 13:d4da5e6aa952 | 48 | void registered() { |
| janjongboom | 13:d4da5e6aa952 | 49 | pc.printf("registered\n"); |
| janjongboom | 13:d4da5e6aa952 | 50 | registeredLed = 0; |
| janjongboom | 13:d4da5e6aa952 | 51 | } |
| janjongboom | 13:d4da5e6aa952 | 52 | void unregistered() { |
| janjongboom | 13:d4da5e6aa952 | 53 | pc.printf("unregistered\n"); |
| janjongboom | 13:d4da5e6aa952 | 54 | registeredLed = 1; |
| geky | 4:dcd0494556be | 55 | } |
| geky | 4:dcd0494556be | 56 | |
| janjongboom | 13:d4da5e6aa952 | 57 | // btn_count now lives in the cloud, so just manipulate it... |
| janjongboom | 13:d4da5e6aa952 | 58 | void fall() { |
| janjongboom | 13:d4da5e6aa952 | 59 | btn_count = btn_count + 1; |
| janjongboom | 13:d4da5e6aa952 | 60 | } |
| janjongboom | 13:d4da5e6aa952 | 61 | |
| janjongboom | 13:d4da5e6aa952 | 62 | // normal function |
| janjongboom | 13:d4da5e6aa952 | 63 | void toggleLed() { |
| janjongboom | 13:d4da5e6aa952 | 64 | led = !led; |
| geky | 4:dcd0494556be | 65 | } |
| geky | 4:dcd0494556be | 66 | |
| geky | 4:dcd0494556be | 67 | int main() { |
| janjongboom | 13:d4da5e6aa952 | 68 | pc.baud(115200); |
| janjongboom | 13:d4da5e6aa952 | 69 | |
| janjongboom | 13:d4da5e6aa952 | 70 | Ticker t; |
| janjongboom | 13:d4da5e6aa952 | 71 | t.attach(&toggleLed, 1.0f); |
| geky | 4:dcd0494556be | 72 | |
| janjongboom | 13:d4da5e6aa952 | 73 | btn.fall(&fall); |
| janjongboom | 13:d4da5e6aa952 | 74 | |
| janjongboom | 13:d4da5e6aa952 | 75 | // I want C++11 lambdas! |
| janjongboom | 13:d4da5e6aa952 | 76 | // here we define functions that should live in the cloud |
| janjongboom | 13:d4da5e6aa952 | 77 | client.define_function("music/0/play", &play); |
| geky | 4:dcd0494556be | 78 | |
| janjongboom | 13:d4da5e6aa952 | 79 | if (lwip.connect() != 0) { // connect to the internet |
| janjongboom | 13:d4da5e6aa952 | 80 | pc.printf("Connect to eth failed...\n"); |
| janjongboom | 13:d4da5e6aa952 | 81 | return 1; |
| janjongboom | 13:d4da5e6aa952 | 82 | } |
| geky | 4:dcd0494556be | 83 | |
| janjongboom | 13:d4da5e6aa952 | 84 | //lwipv4_socket_init(); // do I need this? |
| janjongboom | 13:d4da5e6aa952 | 85 | pc.printf("IP address %s\r\n", lwip.getIPAddress()); |
| janjongboom | 13:d4da5e6aa952 | 86 | |
| janjongboom | 13:d4da5e6aa952 | 87 | bool setup = client.setup(&lwip); // start mbed client! |
| janjongboom | 13:d4da5e6aa952 | 88 | if (!setup) { |
| janjongboom | 13:d4da5e6aa952 | 89 | printf("Setting up mbed_client failed...\n"); |
| janjongboom | 13:d4da5e6aa952 | 90 | return 1; |
| geky | 4:dcd0494556be | 91 | } |
| janjongboom | 13:d4da5e6aa952 | 92 | |
| janjongboom | 13:d4da5e6aa952 | 93 | client.on_registered(®istered); |
| janjongboom | 13:d4da5e6aa952 | 94 | client.on_unregistered(&unregistered); |
| janjongboom | 13:d4da5e6aa952 | 95 | |
| janjongboom | 13:d4da5e6aa952 | 96 | while (1) {} |
| geky | 4:dcd0494556be | 97 | } |
