YADU SHARON
BACK


Interface and Application Programming



The assignment for this week is to " write an application that interfaces with an input and/or output device that you made, comparing as many tool options as possible". I have already done this in my input Devices week(Week 11).



The language i used is python. I used pygame and math libraries for the UI and other mathematical calculations. For communicating with the microcontroller i used the library called serial which enables serial communication.

In my application there is red circle which can be moved using the Joystick.



This circle can be moved in four directions using the Joystick. The microcontroller reads the adc value from the Joystick and send some values according to it via ftdi cable. The python code receives the values using the library "serial" and moves the red circle according to it.



Here is the python code

    
import sys, pygame, math
from pygame.locals import *
import time, serial

ser = serial.Serial(port='/dev/ttyUSB0',baudrate=9600,parity=serial.PARITY_ODD)

# set up a bunch of constants
WHITE    = (255, 255, 255)
BLACK    = (  0,   0,   0)
RED      = (255,   0,   0)

BGCOLOR = WHITE

WINDOWWIDTH = 600	
WINDOWHEIGHT = 600 

pygame.init()
FPSCLOCK = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.set_caption('Move Circle')
FPS = 300
x=300
y=300
while True:
	


	
	while ser.inWaiting()>0:
		data = ser.read()
	if data=='a':
		x+= 2
	elif data== 'c':
		x-= 2
	elif data=='e':
		x+= 1
	elif data== 'd':
		x-= 1
	
	
	DISPLAYSURF.fill(BGCOLOR)
	
	pygame.draw.circle(DISPLAYSURF, RED,(x,y), 40)
	

	pygame.display.update()
	FPSCLOCK.tick(FPS)
    
    
    


Wifi Message box
Now I am trying to interface ESP 8266 . My plan is to make a message box, which will show the message we send from our computer. The message box contains ESP8266 and both the wifi module and the computer should be in same network. This time I am using Tkinter for the GUI.

Since there is no milling bit here for milling pcb (all got broken) i will be using a board with atmega328P which i made already and a breadboard

The components i used are
  • Atmega328 board(which i made already)
  • 16x2 LCD display
  • ESP8266
  • 5v to 3.3v converter

  • I set up a server in the esp8266 module. For communicating with it we use AT commands . Once we connect the wifi module in a network, it will automatically connect the next time when we turn on it. So there is no need to connect in the same network all the time. So when it is powered the micro controller sends the AT commands for creating a TCP server in port 80. Then it sends commands to show its IP address. Micro controller read it and show it in the LCD. So we get the IP of the message box. These are the setups which happens in the Message box. Next we can look in to the UI part in the PC.

    In my UI there is option to type the IP of the message box. I put this option because the IP is dynamic and it may change when we connect the next time. So when the message box is powered look to the LCD for the IP and type it in the python application. Once it connect we can type the message in the box given for that. There is a button for sending , if u click it will send that string to the server which is running in the ESP8266. Microcontroller will read it and will be displayed in the LCD.

    Here is the python code for the UI and client.
    
        
    from Tkinter import *
    import sys
    import socket
    
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    
    
    def label2():
        mylabel2 =Label(text='IP').place(x=20,y=40)
    def button_action():
    
        text1= textinput.get()
    
        sock.sendall(text1)
        textlabel1 =Label(text=text1,width=43,height=10,bg="#fff",bd=2,relief=SUNKEN).place(x=25,y=120)
        entry1.delete(0,END)
    
    def enter_action(self):
        button_action()
    
    def button_port_action():
        #server_address = (, 80)
        ip = portinput.get()
    
        sock.connect((ip,80))
    
    
    
    tk = Tk()
    tk.geometry('400x400+500+200')
    tk.title('Python Powered')
    textinput=StringVar()
    portinput =StringVar()
    textlabel1 =Label(width=43,height=10,bg="#fff",bd=2,relief=SUNKEN).place(x=25,y=120)
    label2()
    
    #label1 =Label(text='Are you sure').place(x=30,y=50)
    button1 = Button(tk,text='Send', command= button_action).place(x=325,y=67)
    entry1= Entry(tk,textvariable=textinput,width=35)
    entry1.bind("",enter_action)
    entry1.place(x=20,y=70)
    
    port_entry= Entry(tk,textvariable=portinput,width=20)
    port_entry.place(x=50,y=40)
    Button_port = Button(tk,text='Set', command= button_port_action).place(x=325,y=37)
    
    
    #label2 =Label(text='Label 2').pack()
    
    
    
    tk.mainloop()
    
        
        
        
    This is my aim. But i have some trouble at the micro controller part, in reading the message.Since there are many datas coming from the ESP module we have to filter and take only the message data which is sent. Here i have some problem and i am still working on it.

    Here is my .c file which has some problem. I wrote the program for creating the server and showing the message. But it can show the IP but not the message we send to it. When we send message it will show some other letters. I have to look in to it and fix it.