Está en la página 1de 2

Biar gak lupa, ini cara membuat alat pantau suhu dan kelembaban ruangan menggunakan Arduino,

Python dan MySql :

Perangkat Keras :

1. Arduino Uno R3 CH340G + USB Kabel


2. DHT11 Temperature and Humidity Module Sensor Kelembaban
3. Jumper Male to Female Cable Dupont 10 cm

Perangkat Lunak :

1. Xampp
2. Python
3. PySerial
4. MysqlDb
5. Arduino Ide

Sketch Arduino :
//you can ignore this part, just for the temperature sensor
#include "DHT.h"
#define DHTPIN A0 //sensor suhu A0
#define DHTTYPE DHT11 //type sensor
DHT dht(DHTPIN, DHTTYPE);

void setup() {
Serial.begin(9600);
dht.begin(); //start the temp reading (agian only for temperature sensor
}

void loop() {
//read the temperature and humidity (temperature sensor specific code)
float h = dht.readHumidity(); //read humidity
float t = dht.readTemperature(); //read temperature (C)

// check if returns are valid


if (isnan(t) || isnan(h)) {
Serial.println("Failed to read from DHT");
} else { //if it read correctly
Serial.print(h); //humidity
Serial.print("\t"); //tab
Serial.println(t); //temperature (C)
delay(1000);
}
}
Kode Python (Os Window)

#!E:/python/python.exe

print "Content-type:text/html"
print ""
import serial
import MySQLdb
import time

#establish connection to MySQL. You'll have to change this for your database.
dbConn = MySQLdb.connect("localhost","root","12345678","test") or die ("could not connect to
database")
#open a cursor to the database
cursor = dbConn.cursor()

device = 'COM3' #this will have to be changed to the serial port you are using
try:
print "Trying...",device
arduino = serial.Serial(device, 9600)
except:
print "Failed to connect on",device

while True:
data = arduino.readline() #read the data from the arduino
pieces = data.split("\t") #split the data by the tab
#Here we are going to insert the data into the Database
if data:
cursor.execute("INSERT INTO weatherData (humidity,tempC) VALUES (%s,%s)",
(pieces[0],pieces[1]))
dbConn.commit() #commit the insert
time.sleep(28800) //interval kirim data dari python ke mysql tiap 8 jam

También podría gustarte