WEEK 14: Interface & Application programming
Taking advantage of the input device board of the week with its code I am going to proceed to make an interface to see the distance and the data received on the PCB to the computer and to be seen by means of a graph. First of all, the first thing I have done is to create a Thingspeak account.
Then I created a channel where I called it distance and where the interface can be seen. There are also important data that I will have to insert in the Python code, for example: Chanel_ID and write_ key. I will explain it later.
I have searched the internet for a code that works for Arduino-python-thingspeak and I will insert that code in the Python program.
Ardu-python-thingspeak:
This repository has codes to send data to thingspeak channel from arduino through a python script running in the laptop. Upload the program into arduino and ceheck whether it's sending data through serial bus using serial monitor. steps for python: 1.downlod python 2.download pyserial package (https://pypi.python.org/pypi/pyserial) and thingspeak python API package (https://pypi.python.org/pypi/thingspeak/), unzip the file, go to to the folder which has setup.py through command prompt, then run the following command setup.py install 3. open python-serial.py program change the comport name to the comport your arduino is connecte to. 4.Run the program and check whether the program is receiving data from arduino. 5. Open pythonserial-thingspeak.py, change the values of 'channel_id' and and 'write_key' to your respective id and key. 6. Run the code, open your thingspeak dash board and check whether the data is being plotted.
I have not written code for acknowledging the reception and detecting loss of data while transmission. try to write codes for that.
This is the link where I found it:
https://github.com/iamsumo/Ardu-python-thingspeak/blob/master/pythonserial-thingspeak.pyI will have to modify this code and put correctly the Chanel_id and write_key data to match the thingspeak data. For the sake of clarity I show two images.
import serial
import time
import thingspeak
channel_id = 2211441
write_key = '45PF0COIRW695IB4'
a=0
ser = serial.Serial()
ser.baudrate = 9600
ser.port = 'COM6'
ser.open()
channel=thingspeak.Channel(id=channel_id,write_key=write_key) while 'true':
a=ser.readline();
time.sleep(0.1)
print(a)
response = channel.update({1:a})
time.sleep(0.090)
------------------------------------#instalay python3 -- Instalar python3
# pip3 install thingspeak==0.4 -- instalar libreria para enviar datos a thingspeak
# pip3 install pyserial -- Instalar libreria para leer datos del puerto serie
# python3 pythonserial-thingspeak.py -- Ejecutar programa que lee del puerto serie y manda a thingspeak
------------------------------------Then I installed Python on the computer. I also had to install a library to send the data to thingspeak, install another library to read the data from the serial port and also run the program that reads from the serial port and sends to thingspeak.
Once I got everything correct I made a video showing everything.
To summarize:
The microcontroller has the Arduino program inserted that connects to the distance sensor and reads how far away the object is and that distance sends it through the serial port. Then it arrives to the PC and the PC is running the Python program that reads the data that it has sent and sends it to the thingspeak web. Then we connect to that web and there you can see in what distance of time and what distance is the one that was at that moment. And every 10 seconds a new data appears.