diff --git a/src/main_2/main_2.ino b/src/main_2/main_2.ino index d781a95d8cc6b87688546fa3a0eb18ed9b54add9..14665c1a58a32295cc55973bbe218216790b8519 100644 --- a/src/main_2/main_2.ino +++ b/src/main_2/main_2.ino @@ -91,8 +91,7 @@ void loop() { digit_base = 10; // get the value to be displayed and update one digit - //update_one_digit(counter % digit_base); - update_one_digit(years % digit_base); + // update_one_digit(years % digit_base); lcd.setCursor(0,0); lcd.autoscroll(); @@ -107,20 +106,20 @@ void loop() { buzzerHigh = false; } - Serial.print("Soil Moisture = "); - Serial.println(readSoil()); + // Serial.print("Soil Moisture = "); + //Serial.println(readSoil()); if (readSoil() > 60) { - Serial.println("wet soil"); + // Serial.println("wet soil"); digitalWrite(ledPin, LOW); } else { - Serial.println("dry soil"); + // Serial.println("dry soil"); digitalWrite(ledPin, HIGH); // digitalWrite(buzzer, HIGH); // delay(200); // digitalWrite(buzzer, LOW); // delay(1000); buzzerHigh = true; - Serial.println("alarm on"); + //Serial.println("alarm on"); } if(buzzerHigh) { @@ -131,13 +130,27 @@ void loop() { } valueLDR = analogRead(sensorLDR); // read the value from the sensor - Serial.print("cahaya: "); //prints the values coming from the sensor on the screen + //Serial.print("cahaya: "); //prints the values coming from the sensor on the screen float vout0 = valueLDR*0.0048828125; // calculate the voltage int lux0 = 500/(res0*((5-vout0)/vout0)); // calculate the Lux Serial.println(lux0); lcd.print(GenerateSoilandLightText(readSoil(),lux0)); // delay(1000); //​take a reading every second + PythonCommunication(); +} + +void PythonCommunication() { + if (Serial.available() > 0) { + //byte test2 = Serial.read() - '0'; + //update_one_digit(test2); + int test = Serial.read(); + char charRead= Serial.read(); + if(charRead >= '0' && charRead <= '9') { + test = (10 * test) + (charRead - '0') ; // convert digits to a number + } + //update_one_digit(5); + } } String GenerateSoilandLightText(int soil, int light) { @@ -204,9 +217,6 @@ int GetCurrentTime() { } if(timeStatus() != timeNotSet) { Serial.println(year()); - Serial.println(); - Serial.println(); - Serial.println(); return(year()); } else { return (0); @@ -247,7 +257,7 @@ void Connect(const char* url) { while (client.available()) { const char* clientRead = client.read(); - Serial.print(clientRead); + // Serial.print(clientRead); } Serial.flush(); diff --git a/src/python/pythonRead.py b/src/python/pythonRead.py new file mode 100644 index 0000000000000000000000000000000000000000..7e433dbf00be4895e93afb5ffd0908c7aa13e736 --- /dev/null +++ b/src/python/pythonRead.py @@ -0,0 +1,40 @@ +import serial, urllib.request, json, time, struct + +with urllib.request.urlopen("https://tranquil-citadel-63668.herokuapp.com/api/status") as url: + data = json.loads(url.read().decode()) + +year = data['year'] + +# data['year'] = 4 + +# params = json.dumps(data).encode('utf8') +# req = urllib.request.Request('https://tranquil-citadel-63668.herokuapp.com/api/status', + # data=params,headers={'Content-Type': 'application/json'}) + +# response = urllib.request.urlopen(req) +# response = urllib.request.urlopen(req, json.dumps(data)) +# print(response.read().decode('utf8')) + +ser = serial.Serial() +ser.baudrate = 19200 +ser.port = 'COM8' +ser.timeout = 1 + +ser.open() +ser.flush() + +time.sleep(3) +# ser.write(b'1') +# print(struct.pack('b', data['year'])) +# ser.write(struct.pack('b',data['year'])) +# b = ser.readline() +# time.sleep(2) +ser_bytes = ser.readline() +decoded_bytes = int(ser_bytes[0:len(ser_bytes)-2].decode("utf-8")) +print(decoded_bytes) +# print(struct.unpack('p',ser.read())) +# print(b) +# print(ser.readable) +# print(ser.readline()) +# print(struct.unpack('<i',ser.readline())) +ser.close() \ No newline at end of file