#include //Software Serial Port #include #include #include #define APIKEY "blablablabla" // replace your pachube api key here #define FEEDID 12345 // replace your feed ID #define USERAGENT "blablabla" // user agent is the project name byte mac[] = { 0xBL, 0xAB, 0xLA, 0xBL, 0xAB, 0xBA}; IPAddress ip(999,999,9,9); EthernetClient client; char server[] = "api.pachube.com"; Servo myservo; #define RxD 2 #define TxD 3 #define DEBUG_ENABLED 1 SoftwareSerial blueToothSerial(RxD,TxD); #define START_CMD_CHAR '*' #define END_CMD_CHAR '#' #define DIV_CMD_CHAR '|' #define CMD_DIGITALWRITE 10 #define CMD_ANALOGWRITE 11 #define CMD_TEXT 12 #define CMD_READ_ARDUDROID 13 #define MAX_COMMAND 20 // max command number code. used for error checking. #define MIN_COMMAND 10 // minimum command number code. used for error checking. #define IN_STRING_LENGHT 40 #define MAX_ANALOGWRITE 255 #define PIN_HIGH 3 #define PIN_LOW 2 int lichtwaarde = 0; int positie_grootste_lichtwaarde = 0; int lichtwaardegeheugen; String inText; int pos =1; unsigned long tijd; long intervaltijd = 600000; long totaleintervaltijd = 600000; int led = 6; unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds boolean lastConnected = false; // state of the connection last time through the main loop void setup() { Serial.begin(9600); if (Ethernet.begin(mac) == 0) { Serial.println("Failed to configure Ethernet using DHCP"); // DHCP failed, so use a fixed IP address: Ethernet.begin(mac, ip); } myservo.attach(7); pinMode(RxD, INPUT); pinMode(TxD, OUTPUT); pinMode(led, OUTPUT); setupBlueToothConnection(); } void loop() { // if there's incoming data from the net connection. // send it out the serial port. This is for debugging // purposes only: if (client.available()) { char d = client.read(); Serial.print(d); } // if there's no net connection, but there was one last time // through the loop, then stop the client: if (!client.connected() && lastConnected) { Serial.println(); Serial.println("disconnecting."); client.stop(); } // if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) { // sendData(analogRead(A5)); // } // store the state of the connection for next time through // the loop: lastConnected = client.connected(); tijd = millis(); if ((totaleintervaltijd - tijd) < 3000) { digitalWrite(led,HIGH); } if (((totaleintervaltijd - tijd) <1) && (!client.connected())) { totaleintervaltijd = totaleintervaltijd + intervaltijd; lichtwaarde = 0; lichtwaardegeheugen = 0; for(pos = 3; pos < 179; pos += 10) { myservo.write(pos); delay(500); if (analogRead(A5) > lichtwaarde) { positie_grootste_lichtwaarde = pos; lichtwaardegeheugen = analogRead(A5); lichtwaarde = lichtwaardegeheugen; } } delay(2000); myservo.write(positie_grootste_lichtwaarde); sendData(lichtwaardegeheugen); delay(1); digitalWrite(led,LOW); } int ard_command = 0; int pin_num = 0; int pin_value = 0; char get_char = ' '; //read serial // wait for incoming data if (blueToothSerial.available() < 1) return; // if serial empty, return to loop(). // parse incoming command start flag get_char = blueToothSerial.read(); if (get_char != START_CMD_CHAR) return; // if no command start flag, return to loop(). // parse incoming command type ard_command = blueToothSerial.parseInt(); // read the command // Serial.println(ard_command); // parse incoming pin# and value pin_num = blueToothSerial.parseInt(); // read the pin pin_value = blueToothSerial.parseInt(); // read the value if (ard_command == CMD_DIGITALWRITE){ if (pin_value == PIN_LOW) pin_value = LOW; else if (pin_value == PIN_HIGH) pin_value = HIGH; else return; // error in pin value. return. set_digitalwrite( pin_num, pin_value); // Uncomment this function if you wish to use return; // return from start of loop() } //krijg analoge data van ardudroid if (ard_command == CMD_ANALOGWRITE) { analogWrite( pin_num, int(pin_value) ); //add you code hier if (pin_num == 11) { myservo.write(int(pin_value)); delay(100); } return; // Done. return to loop(); } // 4) SEND DATA TO ARDUDROID if (ard_command == CMD_READ_ARDUDROID) { // char send_to_android[] = "Place your text here." ; // Serial.println(send_to_android); // Example: Sending text // blueToothSerial.print(" Analog 0 = "); // blueToothSerial.println(analogRead(A0)); // Example: Read and send Analog pin value to Arduino return; // Done. return to loop(); } if (ard_command == CMD_TEXT){ inText =""; //clears variable for new input while (blueToothSerial.available()) { char c = blueToothSerial.read(); //gets one byte from serial buffer Serial.print(c); delay(5); if (c == END_CMD_CHAR) { // if we the complete string has been read // add your code here break; } else { if (c != DIV_CMD_CHAR) { inText += c; delay(5); } } } } } void setupBlueToothConnection() { blueToothSerial.begin(9600); blueToothSerial.print("AT"); delay(400); blueToothSerial.print("AT+DEFAULT"); // Restore all setup value to factory setup delay(2000); blueToothSerial.print("AT+NAMERaf"); // set the bluetooth name as "SeeedBTSlave" ,the length of bluetooth name must less than 12 characters. delay(400); blueToothSerial.print("AT+PIN1234"); // set the pair code to connect delay(400); blueToothSerial.print("AT+AUTH1"); // delay(400); blueToothSerial.flush(); } void set_digitalwrite(int pin_num, int pin_value) { switch (pin_num) { case 13: pinMode(13, OUTPUT); digitalWrite(13, pin_value); // add your code here break; case 12: pinMode(12, OUTPUT); digitalWrite(12, pin_value); // add your code here break; case 11: pinMode(11, OUTPUT); digitalWrite(11, pin_value); // add your code here break; case 10: pinMode(10, OUTPUT); blueToothSerial.println("scannen...geduld!"); delay(1); digitalWrite(10, pin_value); lichtwaarde = 0; lichtwaardegeheugen = 0; for(pos = 3; pos < 179; pos += 10) { myservo.write(pos); delay(500); if (analogRead(A5) > lichtwaarde) { positie_grootste_lichtwaarde = pos; lichtwaardegeheugen = analogRead(A5); lichtwaarde = lichtwaardegeheugen; } } delay(2000); myservo.write(positie_grootste_lichtwaarde); delay(1); blueToothSerial.print(positie_grootste_lichtwaarde); blueToothSerial.print(" "); blueToothSerial.println(lichtwaarde); delay(1); // add your code here break; case 9: pinMode(9, OUTPUT); digitalWrite(9, pin_value); // add your code here blueToothSerial.println("geduld!"); delay(1); myservo.write(3); delay(1); delay(2000); blueToothSerial.print(3); blueToothSerial.print(" "); blueToothSerial.println(analogRead(A5)); delay(1); break; case 8: pinMode(8, OUTPUT); digitalWrite(8, pin_value); blueToothSerial.println("geduld!"); delay(1); // add your code here myservo.write(179); delay(1); delay(2000); blueToothSerial.println(179); blueToothSerial.print(" "); blueToothSerial.println(analogRead(A5)); delay(1); break; case 7: pinMode(7, OUTPUT); digitalWrite(7, pin_value); blueToothSerial.println("geduld!"); delay(1); // add your code here myservo.write(88); delay(1); delay(2000); blueToothSerial.println(88); blueToothSerial.print(" "); blueToothSerial.println(analogRead(A5)); delay(1); // add your code here break; case 6: pinMode(6, OUTPUT); digitalWrite(6, pin_value); // add your code here break; case 5: pinMode(5, OUTPUT); digitalWrite(5, pin_value); // add your code here break; case 4: pinMode(4, OUTPUT); digitalWrite(4, pin_value); // add your code here break; case 3: pinMode(3, OUTPUT); digitalWrite(3, pin_value); // add your code here break; case 2: pinMode(2, OUTPUT); digitalWrite(2, pin_value); // add your code here break; // default: // if nothing else matches, do the default // default is optional } } // this method makes a HTTP connection to the server: void sendData(int thisData) { // if there's a successful connection: if (client.connect(server, 80)) { Serial.println("connecting..."); // send the HTTP PUT request: client.print("PUT /v2/feeds/"); client.print(FEEDID); client.println(".csv HTTP/1.1"); client.println("Host: api.pachube.com"); client.print("X-PachubeApiKey: "); client.println(APIKEY); client.print("User-Agent: "); client.println(USERAGENT); client.print("Content-Length: "); // calculate the length of the sensor reading in bytes: // 8 bytes for "sensor1," + number of digits of the data: int thisLength = 8 + getLength(thisData); client.println(thisLength); // last pieces of the HTTP PUT request: client.println("Content-Type: text/csv"); client.println("Connection: close"); client.println(); // here's the actual content of the PUT request: client.print("sensor1,"); client.println(thisData); } else { // if you couldn't make a connection: Serial.println("connection failed"); Serial.println(); Serial.println("disconnecting."); client.stop(); } // note the time that the connection was made or attempted: lastConnectionTime = millis(); } int getLength(int someValue) { // there's at least one byte: int digits = 1; // continually divide the value by ten, // adding one to the digit count for each // time you divide, until you're at 0: int dividend = someValue /10; while (dividend > 0) { dividend = dividend /10; digits++; } // return the number of digits: return digits; }