Aplikasi Arduino
Code sebagai berikut adalah contoh dari aplikasi yang akan melakukan pengiriman sms melalui modem jika ada perubahan I/O pada input Arduino.
Juga Proses Akuisisi Data terhadap Raspberry PI
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F,16,2);
#include <SoftwareSerial.h>
SoftwareSerial wavecom(2,3);
char incoming_char=0;
const int sensorIn = A0;
int mVperAmp = 185;
double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;
char L1='0';
char L2='0';
char L3='0';
char L4='0';
const int tombol_1 = 12; // the number of the pushbutton pin
const int tombol_2 = 11;
const int tombol_3 = 10; // the number of the pushbutton pin
const int tombol_4 = 9;
const int lampu_1 = 4; // the number of the LED pin
const int lampu_2 = 5;
const int lampu_3 = 6;
const int lampu_4 = 7;
int Tombol_1 = 1; // variable for reading the pushbutton status
int Tombol_2 = 1;
int Tombol_3 = 1;
int Tombol_4 = 1;
int K1=0;
int K2=0;
int K3=0;
int K4=0;
String inputString = "";
void setup() {
lcd.init(); // initialize the lcd
lcd.backlight();
pinMode(lampu_1, OUTPUT);
pinMode(lampu_2, OUTPUT);
pinMode(lampu_3, OUTPUT);
pinMode(lampu_4, OUTPUT);
digitalWrite(lampu_1, HIGH); //relay diseting high, relay jenis aktif low, low = hidup , high = mati;
digitalWrite(lampu_2, HIGH);
digitalWrite(lampu_3, HIGH);
digitalWrite(lampu_4, HIGH);
pinMode(tombol_1, INPUT);
pinMode(tombol_2, INPUT);
pinMode(tombol_3, INPUT);
pinMode(tombol_4, INPUT);
digitalWrite(tombol_1, HIGH);
digitalWrite(tombol_2, HIGH);
digitalWrite(tombol_3, HIGH);
digitalWrite(tombol_4, HIGH);
Serial.begin(9600);
//Serial.print("kirim sms");
wavecom.begin(9600);
wavecom.flush();
wavecom.print("AT+CMGS="); //kirim sms
wavecom.print("081219590150");
wavecom.write(13);
wavecom.println("SMS OK");
wavecom.write(26);
delay(2000);
// wavecom.begin(9600);
wavecom.print("AT+CMGF=1\r"); // set SMS mode to text
delay(100);
wavecom.println("AT+CNMI=2,2,0,0,0\r");
wavecom.print("AT+CLIP=1\r"); // turn on caller ID notification
delay(100);
Serial.println("AT+CMGD=1,4"); //Delete all SMS in box
inputString.reserve(200);
}
void loop() {
terima_sms ();
Voltage = getVPP(); //RUMUS PERHITUNGAN SENSOR
VRMS = (Voltage/2.0) *0.707;
AmpsRMS = (VRMS * 1000)/mVperAmp;
if ( AmpsRMS < 0.09 ) {
//AmpsRMS = 0;
}
float daya = 220 * AmpsRMS; //PERHITUNGAN DAYA
// Serial.print("ARUS PEMAKAIAN: "); //SERIAL PEMBACAAA SERIAL MONITOR
// Serial.print(AmpsRMS);
// Serial.println(" Amper");
// Serial.print("DAYA PEMAKAIAN: ");
// Serial.print(daya);
// Serial.println(" watt");
// Serial.println();
Serial.print(AmpsRMS);
Serial.print("#");
Serial.print(Voltage);
Serial.print("#");
Serial.print(daya);
Serial.print("#");
Serial.print(L1);
Serial.print("#");
Serial.print(L2);
Serial.print("#");
Serial.print(L3);
Serial.print("#");
Serial.print(L4);
Serial.print("#");
Serial.println("");
lcd.setCursor(0,0);
lcd.print("Arus: ");
lcd.setCursor(6,0);
lcd.print(AmpsRMS);
lcd.print(" A ");
lcd.setCursor(0,1);
lcd.print("Daya: ");
lcd.setCursor(6,1);
lcd.print(daya);
lcd.print(" W ");
Tombol_1 = digitalRead(tombol_1);
Tombol_2 = digitalRead(tombol_2);
Tombol_3 = digitalRead(tombol_3);
Tombol_4 = digitalRead(tombol_4);
if (Tombol_1== LOW) {
if(K1 == 0) {
sms_lampu_1_hidup ();
K1=1; }
else {
sms_lampu_1_mati ();
K1=0; }
delay (200) ; }
if (Tombol_2== LOW) {
if(K2 == 0) {
sms_lampu_2_hidup ();
K2=1; }
else {
sms_lampu_2_mati ();
K2=0; }
delay (200) ; }
if (Tombol_3== LOW) {
if(K3 == 0) {
sms_lampu_3_hidup();
K3=1; }
else {
sms_lampu_3_mati();
K3=0; }
delay (200) ; }
if (Tombol_4 == LOW) {
if(K4 == 0) {
sms_lampu_4_hidup();
K4=1; }
else {
sms_lampu_4_mati();
K4=0; }
delay (200) ; }
delay(1000);
//lcd.setCursor(6,0);
//lcd.print(" ");
//lcd.setCursor(6,1);
//lcd.print(" ");
} //loop
float getVPP() {
float result;
int readValue; //value read from the sensor
int maxValue = 0; // store max value here
int minValue = 1024; // store min value here
uint32_t start_time = millis();
while((millis()-start_time) < 100) {
readValue = analogRead(sensorIn);
if (readValue > maxValue) {
maxValue = readValue; }
if (readValue < minValue) {
minValue = readValue; } }
result = ((maxValue - minValue) * 5.0)/1024.0;
return result;
}
void terima_sms () {
//Serial.println("sms aktif");
if(wavecom.available() >0) {
incoming_char=wavecom.read();
if (incoming_char=='#') {
delay(10);
incoming_char=wavecom.read();
if (incoming_char=='A') {
L1='1';
//Serial.print("lampu 1 hidup");
sms_lampu_1_hidup();
Serial.println("AT+CMGD=1,4"); //Delete all SMS in box
delay(10);}
// incoming_char=wavecom.read();
else if (incoming_char=='a') {
//Serial.print("lampu 1 mati");
L1='0';
sms_lampu_1_mati();
Serial.println("AT+CMGD=1,4"); //Delete all SMS in box
delay(10); }
// incoming_char=wavecom.read();
else if (incoming_char=='B') {
//Serial.print("lampu 2 hidup");
L2='1';
sms_lampu_2_hidup();
Serial.println("AT+CMGD=1,4"); //Delete all SMS in box
delay(10);}
// incoming_char=wavecom.read();
else if (incoming_char=='b') {
//Serial.print("lampu 2 mati");
L2='0';
sms_lampu_2_mati();
Serial.println("AT+CMGD=1,4"); //Delete all SMS in box
delay(10); }
// incoming_char=wavecom.read();
else if (incoming_char=='C') {
//Serial.print("lampu 3 hidup");
L3='1';
sms_lampu_3_hidup();
Serial.println("AT+CMGD=1,4"); //Delete all SMS in box
delay(10);}
// incoming_char=wavecom.read();
else if (incoming_char=='c') {
//Serial.print("lampu 3 mati");
L3='0';
sms_lampu_3_mati();
Serial.println("AT+CMGD=1,4"); //Delete all SMS in box
delay(10); }
// incoming_char=wavecom.read();
else if (incoming_char=='D') {
//Serial.print("lampu 4 hidup");
L4='1';
sms_lampu_4_hidup();
Serial.println("AT+CMGD=1,4"); //Delete all SMS in box
delay(10);}
// incoming_char=wavecom.read();
else if (incoming_char=='d') {
//Serial.print("lampu 4 mati");
L4='0';
sms_lampu_4_mati();
Serial.println("AT+CMGD=1,4"); //Delete all SMS in box
delay(10); }
else if (incoming_char=='E') {
//Serial.print("lampu 4 hidup");
L1='1';
L2='1';
L3='1';
L4='1';
sms_lampu_1_hidup();
sms_lampu_2_hidup();
sms_lampu_3_hidup();
sms_lampu_4_hidup();
Serial.println("AT+CMGD=1,4"); //Delete all SMS in box
delay(10);}
//
else if (incoming_char=='e') {
//Serial.print("lampu 4 mati");
L1='0';
L2='0';
L3='0';
L4='0';
sms_lampu_1_mati();
sms_lampu_2_mati();
sms_lampu_3_mati();
sms_lampu_4_mati();
Serial.println("AT+CMGD=1,4"); //Delete all SMS in box
delay(10); }
}
}
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
if (inChar=='A') {
L1='1';
sms_lampu_1_hidup();
Serial.println("AT+CMGD=1,4"); //Delete all SMS in box
delay(10);}
else if (inChar=='a') {
L1='0';
sms_lampu_1_mati();
Serial.println("AT+CMGD=1,4"); //Delete all SMS in box
delay(10); }
else if (inChar=='B') {
//Serial.print("lampu 2 hidup");
L2='1';
sms_lampu_2_hidup();
Serial.println("AT+CMGD=1,4"); //Delete all SMS in box
delay(10);}
else if (inChar=='b') {
L2='0';
sms_lampu_2_mati();
Serial.println("AT+CMGD=1,4"); //Delete all SMS in box
delay(10); }
else if (inChar=='C') {
//Serial.print("lampu 3 hidup");
L3='1';
sms_lampu_3_hidup();
Serial.println("AT+CMGD=1,4"); //Delete all SMS in box
delay(10);}
else if (inChar=='c') {
//Serial.print("lampu 3 mati");
L3='0';
sms_lampu_3_mati();
Serial.println("AT+CMGD=1,4"); //Delete all SMS in box
delay(10); }
else if (inChar=='D') {
//Serial.print("lampu 4 hidup");
L4='1';
sms_lampu_4_hidup();
Serial.println("AT+CMGD=1,4"); //Delete all SMS in box
delay(10);}
else if (inChar=='d') {
//Serial.print("lampu 4 mati");
L4='0';
sms_lampu_4_mati();
Serial.println("AT+CMGD=1,4"); //Delete all SMS in box
delay(10); }
else if (inChar=='E') {
L1='1';
L2='1';
L3='1';
L4='1';
sms_lampu_1_hidup();
sms_lampu_2_hidup();
sms_lampu_3_hidup();
sms_lampu_4_hidup();
Serial.println("AT+CMGD=1,4"); //Delete all SMS in box
delay(10);}
else if (inChar=='e') {
L1='0';
L2='0';
L3='0';
L4='0';
sms_lampu_1_mati();
sms_lampu_2_mati();
sms_lampu_3_mati();
sms_lampu_4_mati();
Serial.println("AT+CMGD=1,4"); //Delete all SMS in box
delay(10); }
}
}
void sms_lampu_1_hidup () {
L1='1';
digitalWrite(lampu_1,LOW);
//Serial.print("kirim sms");
no_penerima ();
wavecom.println("lampu 1 hidup");
sms_daya (); }
void sms_lampu_1_mati () {
L1='0';
digitalWrite(lampu_1,HIGH);
//Serial.print("kirim sms");
no_penerima ();
wavecom.println("lampu 1 mati");
sms_daya (); }
void sms_lampu_2_hidup () {
L2='1';
digitalWrite(lampu_2,LOW);
//Serial.print("kirim sms");
no_penerima ();
wavecom.println("lampu 2 hidup");
sms_daya (); }
void sms_lampu_2_mati () {
L2='0';
digitalWrite(lampu_2,HIGH);
//Serial.print("kirim sms");
no_penerima ();
wavecom.println("lampu 2 mati");
sms_daya (); }
void sms_lampu_3_hidup () {
L3='1';
digitalWrite(lampu_3,LOW);
//Serial.print("kirim sms");
no_penerima ();
wavecom.println("lampu 3 hidup");
sms_daya (); }
void sms_lampu_3_mati () {
L3='0';
digitalWrite(lampu_3,HIGH);
//Serial.print("kirim sms");
no_penerima ();
wavecom.println("lampu 3 mati");
sms_daya (); }
void sms_lampu_4_hidup () {
L4='1';
digitalWrite(lampu_4,LOW);
//Serial.print("kirim sms");
no_penerima ();
wavecom.println("lampu 4 hidup");
sms_daya (); }
void sms_lampu_4_mati () {
L4='0';
digitalWrite(lampu_4,HIGH);
//Serial.print("kirim sms");
no_penerima ();
wavecom.println("lampu 4 mati");
sms_daya (); }
void no_penerima () {
wavecom.flush();
wavecom.print("AT+CMGS="); //kirim sms
wavecom.print("081219590150");
wavecom.write(13); }
void sms_daya () {
Voltage = getVPP(); //RUMUS PERHITUNGAN SENSOR
VRMS = (Voltage/2.0) *0.707;
AmpsRMS = (VRMS * 1000)/mVperAmp;
if ( AmpsRMS < 0.08 ) {
AmpsRMS = 0;}
float daya = 220 * AmpsRMS;
wavecom.print("Arus Pemakaian: ");
wavecom.print(AmpsRMS);
wavecom.println(" A");
wavecom.print("Beban Total: ");
wavecom.print(daya);
wavecom.print(" W");
wavecom.write(26);
delay(4000); }
============
Python.py
import serial
import mysql.connector as mdb
from time import gmtime, strftime
from random import randint
db = mdb.connect(host='127.0.0.1',user='root',password='',database='wm10maret')
cursor = db.cursor()
def mtrim(s):
if s.endswith(" "): s = s[:-1]
if s.startswith(" "): s = s[1:]
return s
#/dev/ttyACM0
read_serial0=""
ser = serial.Serial('COM1',9600)
s = [0,1]
while True:
read_serial=ser.readline()
print read_serial
read_serial=mtrim(read_serial)
###########
cursor.execute("SELECT id,lampu,onoff FROM tb_remote where status='0'")
for row in cursor.fetchall():
idx=row[0]
lampu=row[1]
onoff=row[2]
print lampu
print onoff
print idx
print "@@@@@@@@@@@@@@@"
if lampu=="1" and onoff=="1":
ser.flush()
ser.write("A")
elif lampu=="1" and onoff=="0":
ser.flush()
ser.write("a")
elif lampu=="2" and onoff=="1":
ser.flush()
ser.write("B")
elif lampu=="2" and onoff=="0":
ser.flush()
ser.write("b")
elif lampu=="3" and onoff=="1":
ser.flush()
ser.write("C")
elif lampu=="3" and onoff=="0":
ser.flush()
ser.write("c")
elif lampu=="4" and onoff=="1":
ser.flush()
ser.write("D")
elif lampu=="4" and onoff=="0":
ser.flush()
ser.write("d")
elif lampu=="E":
ser.flush()
ser.write("E")
elif lampu=="e":
ser.flush()
ser.write("e")
sql = "UPDATE `tb_remote` set status='1' where id='%s'" % (idx)
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
#db.close()
###########
if len(read_serial) >6:
if read_serial!=read_serial0:
c=read_serial.split("#")
if len(c)>=6:
read_serial0=read_serial
I=c[0]
V=c[1]
W=c[2]
P=c[3]
Q=c[4]
R=c[5]
S=c[6]
print "I=",I
print "V=",V
print "P=",W
print "1=",P
print "2=",Q
print "3=",R
print "4=",S
tgl=strftime("%Y-%m-%d", gmtime())
jam=strftime("%H:%M:%S", gmtime())
sql = 'INSERT INTO `tb_history` ( `tanggal`, `jam`, `arus`, `tegangan`, `daya`,`L1`,`L2`,`L3`,`L4`) VALUES ("%s", "%s", "%s","%s","%s", "%s", "%s", "%s", "%s")' % (tgl, jam, I,V,W,P,Q,R,S)
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
print"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
#$sudo -i
#cd /home/pi/Desktop/py
#python Help.py
#ls /dev/tty*
CREATE TABLE IF NOT EXISTS `tb_history` (
`id` int(30) NOT NULL AUTO_INCREMENT,
`tanggal` date NOT NULL,
`jam` time NOT NULL,
`arus` varchar(30) NOT NULL,
`tegangan` varchar(30) NOT NULL,
`daya` varchar(30) NOT NULL,
`keterangan` varchar(30) NOT NULL,
`L1` char(1) NOT NULL,
`L2` char(1) NOT NULL,
`L3` char(1) NOT NULL,
`L4` char(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=218 ;
INSERT INTO `tb_history` (`id`, `tanggal`, `jam`, `arus`, `tegangan`, `daya`, `keterangan`, `L1`, `L2`, `L3`, `L4`) VALUES
(6, '2017-01-18', '10:00:00', '12', '220', '300', '-', '1', '1', '0', '0'),
(7, '2017-01-18', '10:00:00', '15', '220', '324', '-', '1', '1', '0', '0'),
CREATE TABLE IF NOT EXISTS `tb_remote` (
`id` int(15) NOT NULL AUTO_INCREMENT,
`tanggal` date NOT NULL,
`jam` time NOT NULL,
`lampu` varchar(1) NOT NULL,
`onoff` varchar(1) NOT NULL,
`status` varchar(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
INSERT INTO `tb_remote` (`id`, `tanggal`, `jam`, `lampu`, `onoff`, `status`) VALUES
(2, '2017-03-10', '20:27:17', '4', '1', '1'),
(3, '2017-03-10', '20:28:27', '4', '0', '1');