Stap 9: Arduino Sketch
// Arduino sketch #include #include #include #include #include #include #include "rgb_lcd.h" rgb_lcd lcd; // make some custom characters: byte heart[8] = { 0b00000, 0b01010, 0b11111, 0b11111, 0b11111, 0b01110, 0b00100, 0b00000 }; byte smiley[8] = { 0b00000, 0b00000, 0b01010, 0b00000, 0b00000, 0b10001, 0b01110, 0b00000 }; byte armsDown[8] = { 0b00100, 0b01010, 0b00100, 0b00100, 0b01110, 0b10101, 0b00100, 0b01010 }; byte armsUp[8] = { 0b00100, 0b01010, 0b00100, 0b10101, 0b01110, 0b00100, 0b00100, 0b01010 }; // Defining Macros #define DEBUG 1 // Analog Pins Definition const int light = 0; const int temp = 1; const int moisture = 2; const int waterFlow = 3; // Digital Pins Definition const int touch = 2; const int bulbRelay = 4; const int fanRelay = 7; const int pump = 8; const int ledPin = 13; //IPC pins int notifier_pin = 3; int js_subscriber_pin = 6; FILE *fromarduino, *toarduino; int i = 0; int c; // Timer unsigned long timeOut = 0; bool newdata = false; int showType = 0; int mapLight; int mapTemp; int mapMoisture; String lightString; String tempString; String moistureString; int B = 3975; char charVal[10]; char ipcString[200]; //Touch Bool boolean touchStarted = false; boolean tweetState = false; String plantName = ""; String lightState = ""; char *incomingString; char *splitVal; String input = ""; int counter = 0; int lastIndex = 0; const int numberOfPieces = 6; String pieces[numberOfPieces]; // Funtion to print error void printError(char *str) { Serial.print("Error: "); Serial.println(str); } void setup() { Serial.begin(115200); pinMode(light, INPUT); pinMode(temp, INPUT); pinMode(moisture, INPUT); pinMode(waterFlow, INPUT); pinMode(bulbRelay, OUTPUT); pinMode(fanRelay, OUTPUT); pinMode(pump, OUTPUT); pinMode(ledPin, OUTPUT); pinMode(notifier_pin, OUTPUT); pinMode(js_subscriber_pin, INPUT_PULLUP); // set up the LCD's number of columns and rows: lcd.begin(16, 2); lcd.clear(); lcd.setRGB(0, 255, 0); lcd.createChar(0, heart); lcd.createChar(1, smiley); lcd.createChar(3, armsDown); lcd.createChar(4, armsUp); lcd.setCursor(0, 0); lcd.write(4); lcd.write(4); lcd.setCursor(4, 0); lcd.print("GREENBIT"); lcd.setCursor(14, 0); lcd.write(4); lcd.write(4); digitalWrite(bulbRelay, HIGH); digitalWrite(fanRelay, HIGH); //Interrupts Initialization attachInterrupt(touch, touchTweet, CHANGE); attachInterrupt(js_subscriber_pin, subscriberEvent, RISING); delay(10); } void loop() { lcd.setCursor(0, 1); lcd.print(plantName); // lcd.write(3); // delay(10); // lcd.write(4); // delay(10); int setTemp, setLight, setMoisture; if(timeOut == 0) timeOut = millis(); if ((millis()-timeOut) >= 1000) { Serial.println("Test"); mapLight = analogRead(light); mapTemp = analogRead(temp); mapMoisture = analogRead(moisture); //Light processing mapLight = map(mapLight, 0, 800, 0, 100); //Temperature processing float floatTemp = (float)(1023-mapTemp)*10000/mapTemp; int tempCelsius=1/(log(floatTemp/10000)/B+1/298.15)-273.15; String lightString = String(mapLight); String tempString = String(tempCelsius); String moistureString = String(mapMoisture); Serial.print(tempCelsius); Serial.print(mapLight); Serial.println(mapMoisture); String finalString = tempString + "," + moistureString + "," + lightString; finalString.toCharArray(ipcString, finalString.length()+1); Serial.println(ipcString); publishData(); notifyWorld(); delay(100); timeOut = millis(); } //Temperature control if(mapTemp > setTemp) { do { digitalWrite(fanRelay, HIGH); }while(mapTemp < (setTemp - 5)); } else if(mapTemp < setTemp) { do { digitalWrite(fanRelay,LOW); }while(mapTemp < (setTemp + 5)); } //Light Control if(lightState == "0") { digitalWrite(bulbRelay, LOW); } else digitalWrite(bulbRelay, HIGH); //Moisture and Motor Control if(mapMoisture > setMoisture) { do { digitalWrite(pump, HIGH); }while(mapMoisture < (setMoisture - 5)); } else if(mapMoisture < setMoisture) { digitalWrite(pump,LOW); }while(mapMoisture < (setMoisture + 5)); } //loop ends //ISR void touchTweet() { digitalWrite(ledPin,HIGH); if(mapTemp < 20) { tweetState = 1; lcd.setRGB(200, 0, 0); lcd.setCursor(0, 1); lcd.write((unsigned char)0); lcd.setCursor(3, 1); lcd.print("I AM SAD"); } else { tweetState = 0; lcd.setRGB(0, 200, 0); lcd.setCursor(0, 1); lcd.print("Light: "); lcd.print(mapLight); for (int positionCounter = 0; positionCounter < 13; positionCounter++) { // scroll one position left: lcd.scrollDisplayLeft(); delay(150); } lcd.setCursor(2, 1); lcd.print("I"); lcd.setCursor(4, 1); lcd.write((unsigned char)0); lcd.setCursor(6, 1); lcd.print("GREENBIT"); } Serial.println("Touch Detected!!!"); } //Read message from js notification file void subscriberEvent() { toarduino = fopen("/home/root/ipc_codes/js_notification_out.txt","r"); //Opening message from JS if (toarduino) { while ((c = getc(toarduino)) != EOF) { if(c != 10)//new line { Serial.print((char)c); } } Serial.println(""); Serial.println("----------------"); fclose(toarduino); } } void publishData() { fromarduino = fopen ("/home/root/ipc_codes/arduino_notification_out.txt", "w+"); fprintf(fromarduino, "[%s]", ipcString); fclose(fromarduino); } //Nofity any body connected to this interrupt (C++ program and NodeJS) program void notifyWorld() { digitalWrite(notifier_pin, HIGH); delay(100); digitalWrite(notifier_pin, LOW); } //Print data on Serial monitor and to adjust the sensor values upto 3 digits void print_data(int val) { int new_val; if(val<0) new_val = 0; else if(val>255) new_val=255; else new_val = val; if(new_val<10) { Serial.print(0); Serial.print(0); Serial.print(new_val); } else if(new_val>=10 && new_val<100) { Serial.print(0); Serial.print(new_val); } else Serial.print(new_val); }