I want to make example which can use mbed RPC library and Python interface so I made this project. This project is led controller and temperature/humidity checker with android application.
In brief, this project has function as below.
- Controlling tri-color led in the mbed platform ( On/Off )
- Checking current led status ( It can check led color using TTS(Text to Speech) on Android)
- Checking temperature and humidity information using Android
This example used mbed RPC library to control WIzwiki-W7500 board with Android and Android application is made using kivy library of Python.
Contents
This chapter explain how to implement source code for mbed platform and Android application. I made mbed project as below Link. After compile this project and then upload this binary, You can control mbed platform via RPC interface using HTTP data.
In this project, I used libraries as below.
- DHT : Library to control temperature/humidity sensor
- HTTPServer : Library for using HTTP server in mbed platform
- mbed-rpc : Library for mbed RPC
- WIZnetInterface : Library for using ethernet without software stack. This library is for WIZnet products.
#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, 0x00, 0x00, 0x00};
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, ð);
Timer tm;
tm.start();
//Listen indefinitely
while(true)
{
svr.poll();
if(tm.read()>.5)
{
tm.start();
}
}
}
- 7~8 : Create RpcDigitalOut for controlling led of mbed platform
- 15 : Create RPCFunction to get temperature and humidity information
- 22~35 : Implementation of custom function
- If it received request from RPC client, this function get temperature and humidity information and then send “Temperature in Celcius : …” data to RPC client.
- 54 : Create handler for HTTPRPC
In this chapter, I explain how to make android application using kivy library of Python. This android application can control mbed device and get information from mbed device. For implementation and operation this application, you have to have step as below.
Download QpythonYou have to use Qpython in order to operate python on Android. You can download Qpython using this URL.
You will need kivy library to implement UI program and AndroidHelper library to use TTS(Text to Speech) on Android. You can download this libraries as below.
I think it is very inconvenient to implement using Qpython because Android phone has small screen and can’t use keyboard. So I write source code using my PC and then copy source code to Qpython using it’s FTP function. In this chapter, I explain how to use FTP function in Qpython in order to copy python source.
Setting for FTPIf you did this procedure, you can use FTP function of Qpython.
This picture express that How to copy source code to Qpython from PC. I used “ALFTP”
If you are successful to connect to FTP of Qpython, you can see folder tree as below picture. You can use source code in your Android after uploading source code.
I made folder as “project3/RPC_TTS” and then copy kivy.py and mbedRPC.py. mbedRPC.py is mbed python RPC library and kivy.py is for UI of Android application.
#-*-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 UI of this application
- 68 ~ 86 : Initialize mbed RPC library
- 89 : Speak temperature and humidity information which is received from mbed platform
- 93 ~ 109 : Blue/Green/Red led On/Off function
- 111 ~ 114 : Speak led color which is received from mbed platform
Demo Video
Comments