sandbox / Mbed OS simple-mbed-client-example

Dependencies:   simple-mbed-client

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

main.cpp

Committer:
janjongboom
Date:
2016-05-15
Revision:
13:d4da5e6aa952
Parent:
12:31e60c8bb7f6
Child:
14:ddc258abaaac

File content as of revision 13:d4da5e6aa952:

/*
 * 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 "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);

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

// patternUpdated is called when the value of led/0/pattern changes
void patternUpdated(string newPattern) {
	pc.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() {
	pc.printf("registered\n");
	registeredLed = 0;
}
void unregistered() {
	pc.printf("unregistered\n");
	registeredLed = 1;
}

// btn_count now lives in the cloud, so just manipulate it...
void fall() {
	btn_count = btn_count + 1;
}

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

int main() {
    pc.baud(115200);
    
    Ticker t;
    t.attach(&toggleLed, 1.0f);
    
    btn.fall(&fall);
    
    // I want C++11 lambdas!
    // here we define functions that should live in the cloud
    client.define_function("music/0/play", &play);

    if (lwip.connect() != 0) {		// connect to the internet
    	pc.printf("Connect to eth failed...\n");
    	return 1;
    }					

    //lwipv4_socket_init();			// do I need this?
    pc.printf("IP address %s\r\n", lwip.getIPAddress());
    
    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) {}
}