Skip to content

Instantly share code, notes, and snippets.

@sadv1r
Last active August 29, 2015 14:18
Show Gist options
  • Save sadv1r/d425cba44b1639f7603b to your computer and use it in GitHub Desktop.
Save sadv1r/d425cba44b1639f7603b to your computer and use it in GitHub Desktop.
const int ledPin9 = 9;
const int ledPin8 = 8;
const int ledPin7 = 7;
const int ledPin6 = 6;
int pin;
int state;
void setup() {
pinMode(ledPin9, OUTPUT);
pinMode(ledPin8, OUTPUT);
pinMode(ledPin7, OUTPUT);
pinMode(ledPin6, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available() == 2) {
Serial.write('a');
pin = Serial.read();
Serial.write(pin);
state = Serial.read();
Serial.write(state);
if (pin == '1') {
Serial.write('q');
if (state == 'h') {
Serial.write('h');
digitalWrite(ledPin9, HIGH);
} else {
Serial.write('l');
digitalWrite(ledPin9, LOW);
}
}
if (pin == '2') {
Serial.write('q');
if (state == 'h') {
Serial.write('h');
digitalWrite(ledPin8, HIGH);
} else {
Serial.write('l');
digitalWrite(ledPin8, LOW);
}
}
if (pin == '3') {
Serial.write('q');
if (state == 'h') {
Serial.write('h');
digitalWrite(ledPin7, HIGH);
} else {
Serial.write('l');
digitalWrite(ledPin7, LOW);
}
}
if (pin == '4') {
Serial.write('q');
if (state == 'h') {
Serial.write('h');
digitalWrite(ledPin6, HIGH);
} else {
Serial.write('l');
digitalWrite(ledPin6, LOW);
}
}
}
delay(500);
}
import mysql.connector
import serial
import time
lastIdState=[2,2,2,2]
try:
ser = serial.Serial('/dev/ttyUSB0', 9600)
print ('Arduino connection established')
except Exception,e:
print str(e)
print ('Please wait...')
time.sleep(5)
print ('Configuration OK. Starting While')
while True:
print ('Iteration begin')
try:
con = mysql.connector.connect(user='root', password='***', host='127.0.0.1', database='aquarium')
print ('MySQL connection established')
cursor = con.cursor()
cursor.execute("SELECT * FROM relay")
results = cursor.fetchall()
print (results)
i = 0
for row in results:
if(lastIdState[i] != row[1]):
lastIdState[i] = row[1]
ser.write(str(row[0]))
if(row[1] == 1):
ser.write('h')
print('Turning On: ' + str(row[0]))
elif(row[1] == 0):
ser.write('l')
print('Turning Off: ' + str(row[0]))
else:
print ('State Error: ' + str(row[1]))
time.sleep(0.5)
i += 1
except Exception,e:
print str(e)
finally:
con.close()
print ('MySQL connection closed')
print ('Iteration end\n')
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment