Week13 : Interface and Application Programming

Group Work

Group assignmet is here

Processing

What is Processing

Processing was born to make the visual expression (programming, animation, interaction) programming easy. It's based on the Java. It's interface is realy looks Arduino IDE. If you are familiar with Arduino IDE, Processing will be easy to use. I also familiar with Arduino IDE, so, I tried Processing first.
I use the temperature sensor that I made in week11 for the input device.

Coding

Following code is the written code in the sensor board.
The int type handled by Arduino is 2 Byte. But the serial commmunication, data is send 1 Byte at a time. So, it needed a ingenuity.

The value it was got from temperature sensor was too long for serial communication, so that value was divided to high byte and low byte and be send to serial port.
    mySerial.write(t & 255);//low byte
    mySerial.write(t >> 8);// high byte
With using serial communication, I made the temperature interface with Processing. If temperature is high, the circle will be big and red, on the otherhand temperature is low, cirlcle will be small and blue.

Python

What is Python

Python is a programming language that has merits such as
  • Easily program with less code amount (quantity)
  • The code is easy to read
and it is used in a wide field such as AI and web development.

Coding with Python (Serial Communication)

The program that is used in the sensor board is following.
Python has the readline command in the serial communication. So, it is not need to divide the data for sending.
Following Python code is for displaying the temperature in the terminal. It is very simple and easy.

import serial
from numpy import log
ser = serial.Serial(
      port = "/dev/tty.usbserial-AI02RJUC",
      baudrate = 4800,
      parity = serial.PARITY_NONE,
      bytesize = serial.EIGHTBITS,
      stopbits = serial.STOPBITS_ONE,
      timeout = 0,
      xonxoff = 0,
      rtscts = 0,
      )
while True:
    mydata = int(ser.readline());
    B = 3750.0;
    T0 = 298.15;
    R0 = 10000.0;
    R1 = 11200.0;

    rr1 = R1 * mydata / (1024.0 - mydata);
    t = 1 / (log(rr1 / R0) / B + (1 / T0))- 273.15;
    print(t)


ser.close()

Coding with Python (Interface)

Tkinter that is used in Neil's example is toolkit that provide the GUI for python. It is difficult to understand Tk coding at first.


from Tkinter import *
from numpy import log
import serial

WINDOW = 600

def measure(parent,canvas):
    mydata = ser.readline()
    mydata = mydata.replace('\n','')
    mydata = mydata.replace('\r','')

    if mydata.isdigit():
        mydata=int(mydata)
        B = 3750.0
        T0 = 298.15
        R0 = 10000.0
        R1 = 11200.0
        try:
            rr1 = R1 * mydata / (1024.0 - mydata)
            t = 1 / (log(rr1 / R0) / B + (1 / T0))
            t= t- 273.15
            t=round(t,1)

            r=t*5

            fred=int(255*t/40)
            fblue=int(255-255*t/40) #range0-40degree

            color = '#%02x%02x%02x' % (fred, 0,fblue)

            fred=int(255/5)
            canvas.itemconfigure("text",text=t)
            canvas.coords('oval',.5*WINDOW-r,.5*WINDOW-r, .5*WINDOW+r, .5*WINDOW+r)
            canvas.itemconfigure('oval', fill=color)
            canvas.update()
            parent.after_idle(measure,parent,canvas)
        except:
            print("error")
            return



ser = serial.Serial(
      port = "/dev/tty.usbserial-AI02RJUC",
      baudrate = 4800,
      parity = serial.PARITY_NONE,
      bytesize = serial.EIGHTBITS,
      stopbits = serial.STOPBITS_ONE,
      timeout = 1,
      xonxoff = 0,
      rtscts = 0,
      )


root = Tk()
root.title('Temperature (q to exit)')
root.bind('q','exit')
canvas = Canvas(root, width=WINDOW, height=WINDOW, background='white')
canvas.create_text(.5*WINDOW,.05*WINDOW,text="temperature",font=("Helvetica", 24),tags="text",fill="#0000b0")
myc=canvas.create_oval(.5*WINDOW-10,.5*WINDOW-10, .5*WINDOW+10, .5*WINDOW+10, tag="oval",fill="green")
canvas.pack()
root.after(100,measure,root,canvas)
root.mainloop()
The serial read function(it is named measure) is call from Tk every 100ms.
root.after(100,measure,root,canvas)
I set timeout of serial setting was 0 at first, but when that program was run, that looked like freezed. So, I changed the timeout setting to 1. Then it workedd well.
timeout = 1

Files

Processing files can be downloded from here.
Python files can be downloded from here.