Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Perangkat Keras :
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)
#!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