Following last weeks development and assignments, we had to establish an interface/application with one of our devices that we used in a previous assignment. In my case it was the environment sensor DHT11 - it is capable to measure temperature and humidity.
In my last assignments I succesfull embedded the sensorin a setup with my microprocessor board and a LCD display.
Now it was the goal to read the data from the microcontroller an display it on my computer instead on a LCD. So I had to use the serial communication to read in the data to process and to display them in an application window.
For this I decide to go with Python a programming language that has the right libraries for graphical interface. So for this assignment I used the matplotlib library !link to matplotlib in the download section). Matplotlib is a Python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms.
Essential was to declare the correct serial device - in my case it was:ser = serial.Serial('/dev/tty.usbserial-FT9MHVRX',115200)
and then I also added the labels for the x and y axis like:
andplt.ylabel('Temperature C')
plt.xlabel('Time')
import serial
import numpy as np
from matplotlib import pyplot as plt
ser = serial.Serial('/dev/tty.usbserial-FT9MHVRX', 115200)
plt.ion() # set plot to animated
plt.ylabel('Temperature C')
plt.xlabel('Time')
ydata = [0] * 200
ax1=plt.axes()
# make plot
line, = plt.plot(ydata)
plt.ylim([10,40])
# start data collection
while True:
data = ser.readline().rstrip() # read data from serial
# port and strip line endings
if len(data.split(".")) == 2:
ymin = float(min(ydata))-10
ymax = float(max(ydata))+10
plt.ylim([ymin,ymax])
ydata.append(data)
del ydata[0]
line.set_xdata(np.arange(len(ydata)))
line.set_ydata(ydata) # update the data
plt.draw() # update the plot
plt.pause(1)
/*
FAB LAB Test Sketch
FAB ACADEMY 2017
Konstantin Stamatopoulos
Input & Output device
*/
#include <DHT11.h> //this is the library for the humidity sensor
#include <Wire.h>
#include <LCD.h> //this is the library for the LCD Display
#include <LiquidCrystal_I2C.h> //this is the library for the LCD I2C communication
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
//setting LCD Addr, En, Rw, Rs, d4, d5, d6, d7, backlightpin, polarity
int pin = A0; // assigning the pin to the DHT11
DHT11 dht11(pin);
double Fahrenheit(double celsius) { //Celsius to Fahrenheit conversion
return ((double)(9 / 5) * celsius) + 32;
}
double Kelvin(double celsius) { //Celsius to Kelvin conversion
return celsius + 273.15;
}
void setup() {
Serial.begin(115200);
lcd.begin(16, 2); //initialisation of the LCD screen 16 characters and 2 lines
lcd.backlight(); // backlight on
lcd.clear(); // clears lcd screen
lcd.print("Fab Academy 2017"); //prints out message to LCD screen
delay(3000); // delay / wait for 3 seconds
lcd.clear(); // clears lcd screen
lcd.print("Humidity & temp"); //prints out message to LCD screen
delay(3000); // delay / wait for 3 seconds
lcd.clear(); // clears lcd screen
lcd.print("Starting....."); //prints out message to LCD screen
delay(3000); // delay / wait for 3 seconds
}
void loop() {
int err;
float temp, humi;
if ((err = dht11.read(humi, temp)) == 0)
{
lcd.clear();
delay(500); // delay / wait for half second
lcd.setCursor(0, 0); //assigning the position of the temp value 1st position and 1st line
lcd.print("Temp");
lcd.setCursor(0, 1); //assigning the position of the temp value 1st position and 2nd line
lcd.print("Humidity");
lcd.setCursor(9, 0); //assigning the position of the temp value 9th position and 1st line
lcd.print(temp);
lcd.print(" C");
Serial.println(temp);
lcd.setCursor(9, 1); //assigning the position of the temp value 9th position and 2nd line
lcd.print(humi);
lcd.print(" %");
//Serial.println(humi);
delay(1000); // delay / wait for 1 second
}
else
{
lcd.println();
lcd.print("Error No :");
lcd.print(err);
lcd.println();
}
}