Michael Walker / Mbed 2 deprecated RPC_RemoteSensing

Dependencies:   EthernetNetIf mbed TMP102 HTTPServer ADJD-S371_ColourSens

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002 Copyright (c) 2010 ARM Ltd
00003  
00004 Permission is hereby granted, free of charge, to any person obtaining a copy
00005 of this software and associated documentation files (the "Software"), to deal
00006 in the Software without restriction, including without limitation the rights
00007 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008 copies of the Software, and to permit persons to whom the Software is
00009 furnished to do so, subject to the following conditions:
00010  
00011 The above copyright notice and this permission notice shall be included in
00012 all copies or substantial portions of the Software.
00013  
00014 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020 THE SOFTWARE.
00021 */
00022 #include "mbed.h"
00023 #include "EthernetNetIf.h"
00024 #include "HTTPServer.h"
00025 #include "RPCFunction.h"
00026 #include "RPCVariable.h"
00027 #include "SerialRPCInterface.h"
00028 #include "ADJDColourSensor.h"
00029 #include "TMP102.h"
00030 #include "scp1000.h"
00031 LocalFileSystem fs("webfs");
00032 
00033 EthernetNetIf eth;  
00034 HTTPServer svr;
00035 
00036 void getColour(char * input, char * Output);
00037 void getTemperature(char *input, char * Output);
00038 void getPressure(char *input, char * Output);
00039 void getSCPTemperature(char *input, char * Output);
00040 
00041 //Set up sensors
00042 TMP102 Temperature(p9, p10, 0x90);
00043 ADJDColourSensor Colour(p28,p27, p22);
00044 SCP1000 Pressure(p5,p6,p7,p8);
00045 DigitalIn PIR(p21, "PIR");
00046 AnalogIn Light(p20, "Light");
00047 
00048 //Set up custom RPC
00049 RPCFunction GetTemp(&getTemperature, "Temperature");
00050 RPCFunction GetPressure(&getPressure, "Pressure");
00051 RPCFunction GetSCPTemperature(&getSCPTemperature, "SCPtemperature");
00052 RPCFunction GetColour(&getColour, "Colour");
00053  
00054 //Serial RPC used for testing 
00055 //SerialRPCInterface applet(USBTX, USBRX, 115200);
00056 
00057 
00058 int main() {
00059     Base::add_rpc_class<AnalogIn>();
00060     Base::add_rpc_class<AnalogOut>();
00061     Base::add_rpc_class<DigitalIn>();
00062     Base::add_rpc_class<DigitalOut>();
00063     Base::add_rpc_class<DigitalInOut>();
00064     Base::add_rpc_class<PwmOut>();
00065     Base::add_rpc_class<Timer>();
00066     Base::add_rpc_class<BusOut>();
00067     Base::add_rpc_class<BusIn>();
00068     Base::add_rpc_class<BusInOut>();
00069     Base::add_rpc_class<Serial>();
00070 
00071   printf("Setting up...\n");
00072   EthernetErr ethErr = eth.setup();
00073   if(ethErr)
00074   {
00075     printf("Error %d in setup.\n", ethErr);
00076     return -1;
00077   }
00078   printf("Setup OK\n");
00079   
00080   FSHandler::mount("/webfs", "/files"); //Mount /webfs path on /files web path
00081   FSHandler::mount("/webfs", "/"); //Mount /webfs path on web root path
00082   
00083   svr.addHandler<SimpleHandler>("/hello");
00084   svr.addHandler<RPCHandler>("/rpc");
00085   svr.addHandler<FSHandler>("/files");
00086   svr.addHandler<FSHandler>("/"); //Default handler
00087   //Example : Access to mbed.htm : http://a.b.c.d/mbed.htm or http://a.b.c.d/files/mbed.htm
00088   
00089   svr.bind(80);
00090   
00091   printf("Listening...\n");
00092   //Listen indefinitely
00093   while(true){
00094      Net::poll();
00095     //Do own thing here
00096     
00097     //This could for example be data logging.
00098         
00099   }
00100   
00101 }
00102 
00103 //RPC Functions  - these are used to wrap the sensor libraries that other wise would not be acessible over RPC.
00104 void getTemperature(char *input, char * Output){
00105     float f = Temperature.read();
00106     sprintf(Output, "%f", f);
00107 }
00108 void getPressure(char *input, char * Output){
00109     float f = Pressure.read();
00110     sprintf(Output, "%f",f);
00111 }
00112 void getSCPTemperature(char *input, char * Output){
00113     float f = Pressure.readTemperature();
00114     sprintf(Output, "%f", f);
00115 }
00116 void getColour(char *input, char * Output){
00117     float clear = Colour.read();
00118     sprintf(Output, "%f,%f,%f,%f,", clear/500, Colour.red/500, Colour.green/500, Colour.blue/500);
00119 }