13. Interface and application programming

This week I worked on defining my final project idea and started to getting used to the documentation process.

Honestly, it was my first time to program an application and I thought I will be so difficult to learn how to deal with it. but, the most important thing is that everything has a beginning, and that’s why in Fabacacdemy I found the best support to achieve the new, from my instructor, Neil… to learn How to Make ALMOST Anything.Help and learn will always be given at Fabacademy to those who ask for it.

This weeks Assignment:

  • Individual assignment:

    • Write an application that interfaces a user with an input &/or output device that you made.
  • Group assignment:

    • Compare as many tool options as possible.

For this weeks group assignment you can check our page by clicking here


First part:(individual assignment):

  • So what does An interface and application programming or (API) mean ?

  • An application program interface (API) is a set of routines, protocols, and tools for building software applications. Basically, an API specifies how software components should interact. Additionally, APIs are used when programming graphical user interface (GUI) components. A good API makes it easier to develop a program by providing all the building blocks. A programmer then puts the blocks together.

  • Most Used Programming Languages for API: 1- PHP

    2- NodeJS

    3- Python

    4- Java

    5- cURL

    6- C# / .NET

    7- Objective-C

    8- RapidQL

  • I used tutorials for Python to learn how to work with it .

  • Different tutorial links:

link1

link2

I started to develop a simple app with 3 Buttons:

  • Button1(Click Me to work )

  • Button2(Click Me to stop )

  • Button3(QUIT )

This is my simple app , see the image below:

Python code

 import tkinter as tk

class Application(tk.Frame):

    def _init_(self, master=None):
        super()._init_(master)
        self.pack()
        self.create_widgets()

    def create_widgets(self):
        self.tkLabel = tk.Label(self)
        self.tkLabel["text"] = "willkommen"
        self.tkLabel.pack()

        self.tkEntry = tk.Entry(self)
        self.tkEntry.pack()

        self.tkButton1 = tk.Button(self)
        self.tkButton1["text"] = "Click Me to work "
        self.tkButton1["command"] = self.work
        self.tkButton1.pack()

        self.tkButton2 = tk.Button(self)
        self.tkButton2["text"] = "Click Me to STOP"
        self.tkButton2["command"] = self.say_bye
        self.tkButton2.pack()

        self.tkButton3 = tk.Button(self, text="Leave", fg="blue", command=self.master.destroy)
        self.tkButton3.pack()



    def work(self):
        print("Hello Humanity  ")

    def say_bye(self):
        print("bye bye ")

root = tk.Tk()
app = Application(master=root)
app.master.title(" Fabacademy 2020 Med Ayoub Aissaoui")
app.master.geometry("200x200")
app.mainloop()

After that I tried to work with something a little bit complex, I created an app that allows me to switch the Arduino LED.

Python LED code:

import tkinter as tk
import serial

class Application(tk.Frame):

    def _init_(self, master=None):
        super()._init_(master)
        self.pack()
        self.mySerial = serial.Serial('COM4')  # open serial port
        self.create_widgets()

    def create_widgets(self):

        self.tkLabel = tk.Label(self)
        self.tkLabel.pack()

        self.tkButton1 = tk.Button(self, text="On Me/Off Me!", command=self.sendData)
        self.tkButton1.pack()

        self.tkButton2 = tk.Button(self, text="Close Me", command=self.quit)
        self.tkButton2.pack()

    def sendData(self):
        self.mySerial.write("L".encode())

    def quit(self):
        self.mySerial.close()
        self.master.destroy()


root = tk.Tk()
app = Application(master=root)
app.master.title("LED Blink Blink ")
app.master.geometry("900x600")
app.mainloop()

*I choose my final project input device the Ultrasonic sensor and I tried to read its data using an application. This app allowed to calculate the distance between the sensor and the object . I started by writing on Pycharm.

ULtrasonic sensor Python code

import tkinter as tk
import serial

class Application(tk.Frame):

    def __init__(self, master=None):
        super().__init__(master)
        self.pack()
        self.mySerial = serial.Serial('COM4')  # open serial port
        self.create_widgets()

    def create_widgets(self):

        self.tkCanvas = tk.Canvas(self, bg="black", height=100, width=300)
        self.tkCanvas.pack()

        self.tkFrame = tk.Frame(self)
        self.tkFrame.pack()

        self.tkLabel = tk.Label(self.tkFrame)
        self.tkLabel.pack()

        self.tkButton1 = tk.Button(self.tkFrame, text="QUIT", command=self.quit)
        self.tkButton1.pack()

    def updateCanvas(self):
        line = str(self.mySerial.readline())
        line = line[2:]
        line = line[:len(line) - 5]
        self.tkLabel["text"] = line
        self.tkCanvas.delete("all")
        self.tkCanvas.create_rectangle(10, 10, int(line)+20, 90, fill="#FDFEFE")

    def quit(self):
        self.mySerial.close()
        self.master.destroy()


root = tk.Tk()
app = Application(master=root)
app.master.title("ULTRASONIC SENSOR")
app.master.geometry("600x400")


while True:
    app.updateCanvas()
    app.update_idletasks()
    app.update()

Video:

LED TEST

Ultrasonic sensor TEST

I tried to use the processing app to create an application to read the data from the ultrasonic sensor, for that I used the board from my Input device week

So, what is processing?

Processing is a flexible software sketchbook and a language for learning how to code within the context of the visual arts. Since 2001, Processing has promoted software literacy within the visual arts and visual literacy within technology. There are tens of thousands of students, artists, designers, researchers, and hobbyists who use Processing for learning and prototyping.

Below are the tips I used to succeed :

1- Download the processing app from Google

2- Inzip the obtained file

3- Upload the same code I used in my Input device week to my board .

5- Upload the arduino code to the board using the FTDI

The Arduino code :

#include <Mouse.h>  

const int trigpin= 21;  
const int echopin= 20;  
long duration;  
int distance;  
void setup(){  
  pinMode(trigpin,OUTPUT);  
  pinMode(echopin,INPUT);  
  Serial.begin(9600);  
}  

void loop(){  
  digitalWrite(trigpin,HIGH);  
  delayMicroseconds(10);  
  digitalWrite(trigpin,LOW);  
  duration=pulseIn(echopin,HIGH);  
  distance = duration*0.034/2;  
  Serial.println(distance);  
}  

6- Open the processing app

7- Paste this code and there is an important thing to consider is that you need to select the right serial COM in ure arduino because the processing app is linked to the linked port in my case my board is attached to COM6 so, I need to change the COM in the processing code to This final.

import processing.serial.*;    
Serial myPort;    
String data="" ;  
PFont  myFont;    

void setup(){  
  size(1366,900); // size of processing window  
  background(0);// setting background color to black  
  myPort = new Serial(this, "COM6", 9600);  
  myPort.bufferUntil('\n');  
}  

void draw(){  
  background(0);  
  textAlign(CENTER);  
  fill(255);  
  text(data,820,400);  
  textSize(100);  
  fill(#4B5DCE);  
  text("              Distance :        cm",450,400);  
  noFill();  
  stroke(#4B5DCE);  
}  

void serialEvent(Serial myPort){  
   data=myPort.readStringUntil('\n');  
}  

And the work is done ^^