mbed RPC Library Examples

details

This tutorial is an exmple to control mbed platfrom from smart phone using mbed RPC. The smart phone application is based on kivy library of Python. This example performs below 3 major functions.

  • Control 3 color LED (On/Off) in the mbed platform
  • Check LED status (using TTS function in the Smartphone and check the color of the LED – Green,Blue,Red )
  • Get the temperature and humidity information from mbed platform(Using smart phone TTS function)

rpc-1

 

mbed HTTP RPC Server

If you click below links, you can download the codes for mbed rpc and HTTP server. At the mbed on-line compiler, if you compile below codes, you can control mbed platform using HTTP data.

#include "mbed.h"
#include "EthernetInterface.h"
#include "HTTPServer.h"
#include "mbed_rpc.h"
#include "DHT.h"
 
RpcDigitalOut led1(D9,"led1");
RpcDigitalOut led2(D10,"led2");
RpcDigitalOut led3(D11,"led3");
 
//RPCVarialbe<float> RPCTemperature(&GetTemperature, "Temperature");
//RPCVarialbe<float> RPCHumidity(&GetHumidity, "Humidity");
void Get_Temp_and_Humidity(Arguments * input, Reply * output);
 
RPCFunction Temp_and_Humidity_Finder(&Get_Temp_and_Humidity, "Temp_and_Humidity_Finder");
 
EthernetInterface eth;  
HTTPServer svr;
 
DHT sensor(D4, DHT11);
 
void Get_Temp_and_Humidity(Arguments * input, Reply *output){
    int error = 0;
    float h = 0.0f, c = 0.0f;
    char arg[100];
 
    error = sensor.readData();
    if (0 == error) {
        c   = sensor.ReadTemperature(CELCIUS);
        h   = sensor.ReadHumidity();
        sprintf(arg,"Temperature in Celcius: %4.2f, Humidity is %4.2f",c, h);
    
        output->putData(arg);
    }
}
 
int main() {
  //Turn the LEDs off
  uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x32, 0x23, 0x42}; 
  led1.write(0);
  led2.write(0);
  led3.write(0);
  
  RPC::add_rpc_class<RpcDigitalOut>();
 
  printf("Setting up...\n");
  eth.init(mac_addr);
  int ethErr = eth.connect();
  if(ethErr < 0)
  {
    printf("Error %d in setup.\n", ethErr);
    return -1;
  }
  svr.addHandler<HTTPRpcRequestHandler>("/rpc");
  
  //attach server to port 80
  printf("Listening...\n");
  svr.start(80, &eth);
    
  Timer tm;
  tm.start();
  //Listen indefinitely
  while(true)
  {
    svr.poll();
    if(tm.read()>.5)
    {
      tm.start();
    }
  }
}

 

  • ~ 8 : Register the GPIOs to RPC to control 3 color LEDs
  • 15 : Register Custom Function to RPC to acquire temperature and humidity value
  • 22~35 : Custom Function
    • If the request is received from mbed RPC client, it extracts the temperature and humidity data and transmits them to RPC client.
  • 54 : Register HTTPRPC Request

Smartphone Simple Application

This smart phone example is based on Python. For the UI, the kivy library is used. In order to operate the examle, you must do below

Download Qpython

If you click below link, you can download the Qpython program for python operation.

QPython Download

python

Please download and install kivy library for python UI and AndroidHelper for TTS(Text to Speech) function. If you follow below, you can get them.

python-1

python-2

Edit Source Code

It is very uncomfortable to write the source code at the smart phone. Fortunately, the Qpython supports the FTP functio to transmit the code from PC to smart phone.

<Setting for FTP>

python-3

<Upload Source Code to Smart Phone>

ftp-1

ftp-2

In this article, we made the ‘project3/RPC_TTS’ folder and copy the kivy.py and mbedRPC.py.

ftp-3

Below is the soure code of kivy.py

#-*-coding:utf8;-*-
#qpy:2
#qpy:kivy
 
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from mbedRPC import *
import androidhelper
 
class CommandScreen(GridLayout):
    def __init__(self, **kwargs):
        super(CommandScreen, self).__init__(**kwargs)
        self.cols = 2
        self.row = 2
        self.row_force_default=True
        self.row_default_height=200
        
        self.add_widget(Label(text='Device IP'))
        self.m_text_device_ip = TextInput(multiline=False,text="192.168.18.121")
        self.add_widget(self.m_text_device_ip)
        
        self.m_button_init = Button(text="Init")
        self.m_button_init.bind(on_press=self.mbedInit)
        self.add_widget(self.m_button_init)
        
        self.m_button_GetTemp = Button(text="Get Temp")
        self.m_button_GetTemp.bind(on_press=self.GetTemp)
        self.add_widget(self.m_button_GetTemp)
        
        self.btn_onLed1 = Button(text="Turn On",background_color=(1,0,0,1))
        self.btn_onLed1.bind(on_press=self.TurnOnLed1)
        self.add_widget(self.btn_onLed1)
        
        self.btn_offLed1 = Button(text="Turn Off",background_color=(1,0,0,1))
        self.btn_offLed1.bind(on_press=self.TurnOffLed1)
        self.add_widget(self.btn_offLed1)
        
        self.btn_onLed2 = Button(text="Turn On",background_color=(0,1,0,1))
        self.btn_onLed2.bind(on_press=self.TurnOnLed2)
        self.add_widget(self.btn_onLed2)
        
        self.btn_offLed2 = Button(text="Turn Off",background_color=(0,1,0,1))
        self.btn_offLed2.bind(on_press=self.TurnOffLed2)
        self.add_widget(self.btn_offLed2)
 
        self.btn_onLed3 = Button(text="Turn On",background_color=(0,0,1,1))
        self.btn_onLed3.bind(on_press=self.TurnOnLed3)
        self.add_widget(self.btn_onLed3)
        
        self.btn_offLed3 = Button(text="Turn Off",background_color=(0,0,1,1))
        self.btn_offLed3.bind(on_press=self.TurnOffLed3)
        self.add_widget(self.btn_offLed3)
 
        self.btn_getLedStatus = Button(text="LED Status")
        self.btn_getLedStatus.bind(on_press=self.GetLedStatus)
        self.add_widget(self.btn_getLedStatus)
        
        self.btn_getA0Status = Button(text="Get A0 Status")
        self.btn_getA0Status.bind(on_press=self.GetA0Status)
        self.add_widget(self.btn_getA0Status)
        
 
    def mbedInit(self,event):
        self.droid = androidhelper.Android()
        
        self.mbed = HTTPRPC(self.m_text_device_ip.text)
        self.led1 = DigitalOut(self.mbed,"led1")
        self.led2 = DigitalOut(self.mbed,"led2")
        self.led3 = DigitalOut(self.mbed,"led3")
        self.temp_humidity = RPCFunction(self.mbed,"Temp_and_Humidity_Finder")
 
        box = BoxLayout(orientation='vertical')
        box.add_widget(Label(text='Init Complete'))
        button_close = Button(text='Close me!')
        box.add_widget(button_close)
 
        popup = Popup(title='message', content=box,
                      size_hint=(None,None),size=(400,400), auto_dismiss=False)
 
        button_close.bind(on_press=popup.dismiss)
        popup.open()
        
        
    def GetTemp(self,event):
        resp = self.temp_humidity.run("")
        self.droid.ttsSpeak(resp)
 
    def TurnOnLed1(self,event):
        self.led1.write(1)
        
    def TurnOffLed1(self,event):
        self.led1.write(0)
        
    def TurnOnLed2(self,event):
        self.led2.write(1)
        
    def TurnOffLed2(self,event):
        self.led2.write(0)
 
    def TurnOnLed3(self,event):
        self.led3.write(1)
        
    def TurnOffLed3(self,event):
        self.led3.write(0)
         
    def GetLedStatus(self,event):
        if( self.led1.read()[0] == '1' ): self.droid.ttsSpeak("Turn on red.")
        if( self.led2.read()[0] == '1' ): self.droid.ttsSpeak("Turn on green.")
        if( self.led3.read()[0] == '1' ): self.droid.ttsSpeak("Turn on blue.")
        
    def GetA0Status(self,event):
        print ''
 
class MyApp(App):
    def build(self):
        return CommandScreen()        
 
if __name__ == '__main__':
    MyApp().run()
  • 15 ~ 65 : Initialize App Button and UI
  • 68 ~ 86 : Initialize mbed RPC library
  • 89 : Receive the temperature and humidity data frommbed platform and ouptut through TTS
  • 93 ~ 109 : LED1/2/3 On/Off
  • 111 ~ 114 : Check the LED color of mbed platform and output through TTS

smart_phone

This tutorial is an exmple to control mbed platfrom from smart phone using mbed RPC. The smart phone application is based on kivy library of Python. This example performs below 3 major functions.

  • Control 3 color LED (On/Off) in the mbed platform
  • Check LED status (using TTS function in the Smartphone and check the color of the LED – Green,Blue,Red )
  • Get the temperature and humidity information from mbed platform(Using smart phone TTS function)

rpc-1

 

mbed HTTP RPC Server

If you click below links, you can download the codes for mbed rpc and HTTP server. At the mbed on-line compiler, if you compile below codes, you can control mbed platform using HTTP data.

#include "mbed.h"
#include "EthernetInterface.h"
#include "HTTPServer.h"
#include "mbed_rpc.h"
#include "DHT.h"
 
RpcDigitalOut led1(D9,"led1");
RpcDigitalOut led2(D10,"led2");
RpcDigitalOut led3(D11,"led3");
 
//RPCVarialbe<float> RPCTemperature(&GetTemperature, "Temperature");
//RPCVarialbe<float> RPCHumidity(&GetHumidity, "Humidity");
void Get_Temp_and_Humidity(Arguments * input, Reply * output);
 
RPCFunction Temp_and_Humidity_Finder(&Get_Temp_and_Humidity, "Temp_and_Humidity_Finder");
 
EthernetInterface eth;  
HTTPServer svr;
 
DHT sensor(D4, DHT11);
 
void Get_Temp_and_Humidity(Arguments * input, Reply *output){
    int error = 0;
    float h = 0.0f, c = 0.0f;
    char arg[100];
 
    error = sensor.readData();
    if (0 == error) {
        c   = sensor.ReadTemperature(CELCIUS);
        h   = sensor.ReadHumidity();
        sprintf(arg,"Temperature in Celcius: %4.2f, Humidity is %4.2f",c, h);
    
        output->putData(arg);
    }
}
 
int main() {
  //Turn the LEDs off
  uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x32, 0x23, 0x42}; 
  led1.write(0);
  led2.write(0);
  led3.write(0);
  
  RPC::add_rpc_class<RpcDigitalOut>();
 
  printf("Setting up...\n");
  eth.init(mac_addr);
  int ethErr = eth.connect();
  if(ethErr < 0)
  {
    printf("Error %d in setup.\n", ethErr);
    return -1;
  }
  svr.addHandler<HTTPRpcRequestHandler>("/rpc");
  
  //attach server to port 80
  printf("Listening...\n");
  svr.start(80, &eth);
    
  Timer tm;
  tm.start();
  //Listen indefinitely
  while(true)
  {
    svr.poll();
    if(tm.read()>.5)
    {
      tm.start();
    }
  }
}

 

  • ~ 8 : Register the GPIOs to RPC to control 3 color LEDs
  • 15 : Register Custom Function to RPC to acquire temperature and humidity value
  • 22~35 : Custom Function
    • If the request is received from mbed RPC client, it extracts the temperature and humidity data and transmits them to RPC client.
  • 54 : Register HTTPRPC Request

Smartphone Simple Application

This smart phone example is based on Python. For the UI, the kivy library is used. In order to operate the examle, you must do below

Download Qpython

If you click below link, you can download the Qpython program for python operation.

QPython Download

python

Please download and install kivy library for python UI and AndroidHelper for TTS(Text to Speech) function. If you follow below, you can get them.

python-1

python-2

Edit Source Code

It is very uncomfortable to write the source code at the smart phone. Fortunately, the Qpython supports the FTP functio to transmit the code from PC to smart phone.

<Setting for FTP>

python-3

<Upload Source Code to Smart Phone>

ftp-1

ftp-2

In this article, we made the ‘project3/RPC_TTS’ folder and copy the kivy.py and mbedRPC.py.

ftp-3

Below is the soure code of kivy.py

#-*-coding:utf8;-*-
#qpy:2
#qpy:kivy
 
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from mbedRPC import *
import androidhelper
 
class CommandScreen(GridLayout):
    def __init__(self, **kwargs):
        super(CommandScreen, self).__init__(**kwargs)
        self.cols = 2
        self.row = 2
        self.row_force_default=True
        self.row_default_height=200
        
        self.add_widget(Label(text='Device IP'))
        self.m_text_device_ip = TextInput(multiline=False,text="192.168.18.121")
        self.add_widget(self.m_text_device_ip)
        
        self.m_button_init = Button(text="Init")
        self.m_button_init.bind(on_press=self.mbedInit)
        self.add_widget(self.m_button_init)
        
        self.m_button_GetTemp = Button(text="Get Temp")
        self.m_button_GetTemp.bind(on_press=self.GetTemp)
        self.add_widget(self.m_button_GetTemp)
        
        self.btn_onLed1 = Button(text="Turn On",background_color=(1,0,0,1))
        self.btn_onLed1.bind(on_press=self.TurnOnLed1)
        self.add_widget(self.btn_onLed1)
        
        self.btn_offLed1 = Button(text="Turn Off",background_color=(1,0,0,1))
        self.btn_offLed1.bind(on_press=self.TurnOffLed1)
        self.add_widget(self.btn_offLed1)
        
        self.btn_onLed2 = Button(text="Turn On",background_color=(0,1,0,1))
        self.btn_onLed2.bind(on_press=self.TurnOnLed2)
        self.add_widget(self.btn_onLed2)
        
        self.btn_offLed2 = Button(text="Turn Off",background_color=(0,1,0,1))
        self.btn_offLed2.bind(on_press=self.TurnOffLed2)
        self.add_widget(self.btn_offLed2)
 
        self.btn_onLed3 = Button(text="Turn On",background_color=(0,0,1,1))
        self.btn_onLed3.bind(on_press=self.TurnOnLed3)
        self.add_widget(self.btn_onLed3)
        
        self.btn_offLed3 = Button(text="Turn Off",background_color=(0,0,1,1))
        self.btn_offLed3.bind(on_press=self.TurnOffLed3)
        self.add_widget(self.btn_offLed3)
 
        self.btn_getLedStatus = Button(text="LED Status")
        self.btn_getLedStatus.bind(on_press=self.GetLedStatus)
        self.add_widget(self.btn_getLedStatus)
        
        self.btn_getA0Status = Button(text="Get A0 Status")
        self.btn_getA0Status.bind(on_press=self.GetA0Status)
        self.add_widget(self.btn_getA0Status)
        
 
    def mbedInit(self,event):
        self.droid = androidhelper.Android()
        
        self.mbed = HTTPRPC(self.m_text_device_ip.text)
        self.led1 = DigitalOut(self.mbed,"led1")
        self.led2 = DigitalOut(self.mbed,"led2")
        self.led3 = DigitalOut(self.mbed,"led3")
        self.temp_humidity = RPCFunction(self.mbed,"Temp_and_Humidity_Finder")
 
        box = BoxLayout(orientation='vertical')
        box.add_widget(Label(text='Init Complete'))
        button_close = Button(text='Close me!')
        box.add_widget(button_close)
 
        popup = Popup(title='message', content=box,
                      size_hint=(None,None),size=(400,400), auto_dismiss=False)
 
        button_close.bind(on_press=popup.dismiss)
        popup.open()
        
        
    def GetTemp(self,event):
        resp = self.temp_humidity.run("")
        self.droid.ttsSpeak(resp)
 
    def TurnOnLed1(self,event):
        self.led1.write(1)
        
    def TurnOffLed1(self,event):
        self.led1.write(0)
        
    def TurnOnLed2(self,event):
        self.led2.write(1)
        
    def TurnOffLed2(self,event):
        self.led2.write(0)
 
    def TurnOnLed3(self,event):
        self.led3.write(1)
        
    def TurnOffLed3(self,event):
        self.led3.write(0)
         
    def GetLedStatus(self,event):
        if( self.led1.read()[0] == '1' ): self.droid.ttsSpeak("Turn on red.")
        if( self.led2.read()[0] == '1' ): self.droid.ttsSpeak("Turn on green.")
        if( self.led3.read()[0] == '1' ): self.droid.ttsSpeak("Turn on blue.")
        
    def GetA0Status(self,event):
        print ''
 
class MyApp(App):
    def build(self):
        return CommandScreen()        
 
if __name__ == '__main__':
    MyApp().run()
  • 15 ~ 65 : Initialize App Button and UI
  • 68 ~ 86 : Initialize mbed RPC library
  • 89 : Receive the temperature and humidity data frommbed platform and ouptut through TTS
  • 93 ~ 109 : LED1/2/3 On/Off
  • 111 ~ 114 : Check the LED color of mbed platform and output through TTS

smart_phone

COMMENTS

Please Login to comment
  Subscribe  
Notify of
POSTED BY