sandbox / Mbed OS simple-mbed-client-example

Dependencies:   simple-mbed-client

Fork of simple-mbed-client-example by Jan Jongboom

main.cpp

Committer:
Jan Jongboom
Date:
2016-05-24
Revision:
16:71ec73ea9b6d
Parent:
14:ddc258abaaac
Child:
17:6d69aa1b393f

File content as of revision 16:71ec73ea9b6d:

/*
 * Copyright (c) 2015 ARM Limited. All rights reserved.
 * SPDX-License-Identifier: Apache-2.0
 * Licensed under the Apache License, Version 2.0 (the License); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <string>
#include "mbed.h"
#include "security.h"				// security file from connector.mbed.com
#include "LWIPInterface.h"			// using Ethernet for connectivity
#include "simple-mbed-client.h"		// simple-mbed-client

// So these lines are unique to connecting etc... but then it's just a normal program

Serial pc(USBTX, USBRX);			// talk back to the computer
LWIPInterface lwip;					// define the Ethernet interface
SimpleMbedClient client;

// Declare some peripherals here
DigitalOut led(LED_RED);
DigitalOut registeredLed(LED_BLUE, 1);
InterruptIn btn(SW3);

// We need a semaphore because we cannot update value in interrupt context
Semaphore updates(0);

// play function, invoked thru mbed Client
void play(void* args) {
    printf("I'm gonna play a song!\n");
}

// patternUpdated is called when the value of led/0/pattern changes
void patternUpdated(string newPattern) {
    printf("Got a new pattern... %s\n", newPattern.c_str());
}

// Here we define some mbed Client resources
SimpleResourceInt btn_count = client.define_resource("button/0/clicks", 0, M2MBase::GET_ALLOWED, true);
// need a callback when a PUT request comes in? Just pass in a callback
SimpleResourceString pattern = client.define_resource("led/0/pattern", "500:500:500", &patternUpdated);

// Status callbacks if you're interested in that
void registered() {
    printf("registered\n");
    registeredLed = 0;
}
void unregistered() {
    printf("unregistered\n");
    registeredLed = 1;
}

// we cannot do blocking stuff here, or device will crash
// release semaphore and update btn_count in the while() loop...
void fall() {
    updates.release();
}

// normal function
void toggleLed() {
    led = !led;
}

int main() {
    pc.baud(115200);

    Ticker t;
    t.attach(&toggleLed, 1.0f);

    btn.fall(&fall);

    // here we define functions that should live in the cloud
    client.define_function("song/0/play", &play);

    int connect = lwip.connect(); // can swap out for other network iface
    if (connect != 0) {
        printf("Connect to eth failed...\n");
        return 1;
    }

    printf("IP address %s\r\n", lwip.get_ip_address());

    bool setup = client.setup(&lwip); // start mbed client!
    if (!setup) {
        printf("Setting up mbed_client failed...\n");
        return 1;
    }

    client.on_registered(&registered);
    client.on_unregistered(&unregistered);

    while (1) {
        int v = updates.wait(25000);

        if (v == 1) {
            btn_count = btn_count + 1;
            // printf is a bit inflexible, static_cast to get the right value
            printf("btn_count is now %d\n", static_cast<int>(btn_count));
        }

        // call keep_alive at least every 30s.
        client.keep_alive();
    }
}