From 6d35e848ab3fc399c960e1f73bbd3342db457f62 Mon Sep 17 00:00:00 2001 From: finikokasulanovenda <13515029@std.stei.itb.ac.id> Date: Wed, 11 Apr 2018 19:44:44 +0700 Subject: [PATCH] Add main_2 --- src/main_2/main_2.ino | 89 ++++++++++------------------------------- src/python/getSerial.py | 24 +++++------ 2 files changed, 32 insertions(+), 81 deletions(-) diff --git a/src/main_2/main_2.ino b/src/main_2/main_2.ino index 2cc6127..0baa12e 100644 --- a/src/main_2/main_2.ino +++ b/src/main_2/main_2.ino @@ -3,7 +3,6 @@ #include <Wire.h> #include <LiquidCrystal_I2C.h> #include <ArduinoJson.h> -//#include <TimeLib.h> const int ledPin = 2; const int buttonPin = 4; @@ -43,7 +42,8 @@ const byte digit_pattern[10] = B01101111, // 9 }; -unsigned int counter = 0; +int counter = 0; +int sign = 50; bool isSuccess; int years; @@ -52,10 +52,6 @@ int light; char json[] = "{\"success\":true,\"year\":3,\"humidity\":5,\"light\":10}"; - -//#define TIME_MSG_LEN 11 // time sync to PC is HEADER and unix time_t as ten ascii digits -//#define TIME_HEADER 'T' // Header tag for serial time sync message - void setup() { // put your setup code here, to run once: Serial.begin(9600); @@ -79,26 +75,23 @@ void setup() { // Turn on the blacklight and print a message. lcd.backlight(); ReadJson(); - lcd.setCursor(0,0); + //lcd.autoscroll(); } -void loop() { - - int i; - unsigned int digit_base; - - counter++; - - digit_base = 10; +void loop() { + counter = counter + sign; + if (counter >= 255) { + sign = -50; + } else if (counter <= 0) { + sign = 25; + } // get the value to be displayed and update one digit - //update_one_digit(years % digit_base); - - - lcd.autoscroll(); - //lcd.print("Hello! Welcome to GreenTyNo :)"); - delay(1000); + PythonCommunication(); + lcd.setCursor(0,0); + lcd.scrollDisplayLeft(); + // put your main code here, to run repeatedly: buttonState = digitalRead(buttonPin); @@ -106,7 +99,6 @@ void loop() { buzzerHigh = false; } - //Serial.print("Soil Moisture = "); Serial.print(readSoil()); if (readSoil() > 60) { // Serial.println("wet soil"); @@ -123,22 +115,20 @@ void loop() { } if(buzzerHigh) { - digitalWrite(buzzer, HIGH); - delay(200); - digitalWrite(buzzer, LOW); - delay(1000); + analogWrite(buzzer, counter); + //delay(200); + //digitalWrite(buzzer, LOW); + //delay(1000); } valueLDR = analogRead(sensorLDR); // read the value from the sensor - //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.print(':'); Serial.println(lux0); lcd.print(GenerateSoilandLightText(readSoil(),lux0)); -// delay(1000); //​take a reading every second - PythonCommunication(); + delay(1000); //​take a reading every second } void PythonCommunication() { @@ -153,7 +143,7 @@ void PythonCommunication() { } String GenerateSoilandLightText(int soil, int light) { - return ("GreenTyno Humidity : "+ String(soil) + " RB Light : " + String(light) +" Lux"); + return ("GreenTyno Humidity "+ String(soil) + " RB Light " + String(light) +" Lux "); } int readSoil() { @@ -165,9 +155,7 @@ int readSoil() { return 1023-val; //send current moisture value } -void update_one_digit(int data) -{ - int i; +void update_one_digit(int data) { byte pattern; // get the digit pattern to be updated @@ -206,41 +194,6 @@ String GenerateJSONString() { totalString = "|GreenTyno | Humidity : "+ humidityString + " Light : " + lightString +" |"; return (totalString); } -/* -int GetCurrentTime() { - //GetPCTime(); - //setTime(0); - //Serial.println(Serial.available()); - if (Serial.available() > 0) { - GetPCTime(); - } - if(timeStatus() != timeNotSet) { - Serial.println(year()); - return(year()); - } else { - return (0); - } -} - - -void GetPCTime() { - // if time available from serial port, sync the DateTime library - while(Serial.available() >= TIME_MSG_LEN ){ // time message - //Serial.println(Serial.read()); - if( Serial.read() == TIME_HEADER ) { - time_t pctime = 0; - for(int i=0; i < TIME_MSG_LEN -1; i++){ - char charRead= Serial.read(); - if( charRead >= '0' && charRead <= '9') { - pctime = (10 * pctime) + (charRead - '0') ; // convert digits to a number - } - } - setTime(pctime); - Serial.println(pctime); - //Serial.println(); - } - } -}*/ void InitializeClient() { pinMode(13, OUTPUT); diff --git a/src/python/getSerial.py b/src/python/getSerial.py index 56ebae4..6accd3e 100644 --- a/src/python/getSerial.py +++ b/src/python/getSerial.py @@ -4,25 +4,23 @@ with urllib.request.urlopen("https://tranquil-citadel-63668.herokuapp.com/api/st data = json.loads(url.read().decode()) year = data['year'] -print(year) ser = serial.Serial('COM8') # open serial port print(ser.name) # check which port was really used -# ser.write(b'\x04') # write a string -# ser.write(struct.pack('>B',1)) -# print(b'4' + b'0') -# print(struct.pack('b',3)) -# print(struct.pack('>B',year + 48)) +ser.write(struct.pack('>B',year + 48)) while True : line = ser.readline() - ser.write(struct.pack('>B',year + 48)) - # ser.write(struct.pack('>i',year)) raw = line.decode().split('\r\n')[0] arr = raw.split(':') - print(arr) - k = arr[0] - c = arr[1] - print(k) - print(c) + humidity = arr[0] + light = arr[1] + + #postUrl = "http://localhost/api-greentyno/updateStatus?humidity=" + humidity + "&light=" + light + #print(postUrl) + # print(humidity) + # print(light) + + # with urllib.request.urlopen(postUrl) as url: + # print()# data = json.loads(url.read().decode()) ser.close() # close port \ No newline at end of file -- GitLab