This is a complete application chain using my widget, a Sparkfun WiFly 802.11 module and some custom code in C++ and Python to control a servo from a smart phone or tablet.

It consists of a very simple Android app with one slider that talks to a tiny Ardunio clone that controls a SparkFun WiFly module. I’ve tried to keep it cookbook super simple.

I’ve posted all of the parts, designs and code here and on my controlwidgets.com site, feel free to take a look.

It’s extremely simple. There are three basic parts. The board and the Wifly module, the Ardunio compatible code and libraries that go into that board and a simple Android App written in Python to control it.

First is the board. You can find details on building this board on my controlwidgets.com site- BOARD CONSTRUCTION

Also on the site are my custom Ardunio libraries, you will need two, both on the files page- ARDUNIO LIBRARIES

To build the widget software you will also need a couple of other things to add to your Ardunio build to compile code for the Attiny 1634- See this Page

The following code uses the two libraries and is downloaded into the widget –

Ardunio Clone Code

#include <WiFlyUart.h>
#include <ServoTimer.h>

WiFlyUart wiflyuart;
ServoTimer servotimer;

uint8_t data, s1, s2, s3, s4;
uint16_t sdata;
uint8_t storedata;

void setup()
{
  storedata = 0;
  servotimer.Initialize();
  wiflyuart.Initialize();
  wiflyuart.enableRX0IRQ();
  wiflyuart.enableRX1IRQ();
  sei();
}

void loop()
{
  // get byte from terminal, send to wifly module
  data = wiflyuart.getByte1();
  if(data!=0xff)
  {
     wiflyuart.sendByte0(data);
  }
  
  // get some data from the wifly, if any, send to terminal
  data = wiflyuart.getByte0();
  if(data!=0xff)
  {
     wiflyuart.sendByte1(data);

     if(data == '#')  // is it a servo command?
     {
       storedata = 1;
     }
     
     switch(storedata)
     {
       case 0:
               break;
       case 1:
               storedata = 2;
               break;
       case 2:
               s1 = data;
               storedata = 3;
               break;
       case 3:
               s2 = data;
               storedata = 4;
               break;
       case 4:
               s3 = data;
               storedata = 5;
               break;
       case 5:
               s4 = data;
               storedata = 6;
               break;
       case 6:
               sdata = ((s1 & 0x0f) * 1000) + ((s2 & 0x0f) * 100) + ((s3 & 0x0f) * 10) + (s4 & 0x0f);
               servotimer.setServo(0, sdata);
               storedata = 0;
               break;
     }       
  }
}

You will also need to configure your WiFly so it uses an IP of 10.20.10.1 on port 2000, the instructions for doing that are in the datasheet and user guide on the SparkFun page where you order the widget- Sparkfun Wifly Module

Also important are the build setup to produce the Android application, here are the relevant links:

You can get python here-
Python 2.7

Download for Pygame, I use Python 2.7 32 bit for this (I don’t think the 64 will work for doing Android)
PyGame for Python

This is an add-on to allow you to develop android apps using Python and PyGame.
PyGame for Android Subset

There is also a library of widgets (sliders, dropdowns, buttons, etc) for PyGame called PGU that will also work on Android, IF you make a few changes to it:
Phils Game Utilities

And this link has the changes you will need to make to PGU so it works on Android, it won’t out of the box:
PGU Modifications for Android

If you want to skip building the android app, you can download the completed apk file here:
testgame-1-release.apk


Slider Android App in Python for PyGame



import subprocess, shlex
import pygame
from pgu import gui
import socket
from pygame.locals import *

# Import the android module. If we can't import it, set it to None - this
# lets us test it, and check to see if we want android-specific behavior.
try:
    import android
except ImportError:
    android = None

TIMEREVENT = pygame.USEREVENT
FPS = 30
screen = pygame.display.set_mode((800,480),SWSURFACE)

 # Map the back button to the escape key.
if android:
   android.init()
   android.map_key(android.KEYCODE_BACK, pygame.K_ESCAPE)

# set the timeout to 1 second, just in case it's not there
socket.setdefaulttimeout(1)

# open a connection to the AP in the locomotive
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
    s.connect(('10.20.10.1',2000))
    print "connected to 10.20.10.1"
except:
    pass

strs = subprocess.check_output(shlex.split('ip r l'))
gateway = strs.split('default via')[-1].split()[0]
ip  = strs.split('src')[-1].split()[0]

s.send(gateway)
        
class screenContainer(gui.Table):
    def __init__(self,**params):
        gui.Table.__init__(self,**params)

        fg = (255,255,255)
        self.tr()
        self.td(gui.Label("Speed: ",color=fg),align=1)
        e = gui.HSlider(0, 0, 1000, size=20, width=700, height=80, name='speed')
        e.connect(gui.CHANGE, self.adjustSliderA, (0,e))
        self.td(e)
        self.sv = 0

    def adjustSliderA( self, value):
        (num, slider) = value
        self.sv = slider.value

    def SliderValueA( self ):
        return self.sv
        

app = gui.App()
sc = screenContainer()
c = gui.Container(width=200,height=180)
c.add(sc,0,0)
app.init(c)

pygame.time.set_timer(TIMEREVENT, 1000 / FPS)

##$$$
pygame.init()
 
done = False

while not done:

     for ev in pygame.event.get():
    
        if ev.type == TIMEREVENT:
            screen.fill((100,0,0))
            app.paint()
            pygame.display.flip()
            
        elif ev.type == pygame.KEYDOWN and ev.key == pygame.K_ESCAPE:
            done = True
            break
        else:
            app.event(ev)
            sv = str(sc.SliderValueA()+ 1000 )
            if len(sv) == 1:
               sv = "000" + sv
            if len(sv) == 2:
               sv = "00" + sv
            if len(sv) == 3:
               sv = "0" + sv
            v = "#" + sv + "\r\n"
            s.send(v)
           
pygame.quit()


Here is the servo pulse output:

scope