Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SoftwareSerial.h>
- SoftwareSerial GPRS(7, 8);
- int buttonState1, buttonState2;
- int lastButtonState1, lastButtonState2;
- long lastDebounceTime1 = 0; // the last time the output pin was toggled
- long lastDebounceTime2 = 0; // the last time the output pin was toggled
- long debounceDelay = 50; // the debounce time; increase if the output flickers
- int pump_on=0;
- int was_pumping=0;
- int retries=0;
- int state=0;
- int next_state=0;
- int next_state_if_timeout=0;
- int next_state_if_no_OK=0;
- int timeout=0;
- long time_before_wait=0;
- #define WEBSITE "api.carriots.com"
- #define API_KEY "API KEY FROM THE SETTINGS PAGE IN CARRIOTS"
- #define DEVICE "DEVICE ID FROM THE DEVICES PAGE IN CARRIOTS"
- void setup()
- {
- GPRS.begin(19200); // the GPRS baud rate
- Serial.begin(19200); // the Serial port of Arduino baud rate.
- pinMode(11,INPUT);
- digitalWrite(11,HIGH);
- pinMode(12,INPUT);
- digitalWrite(12,HIGH);
- setupdebounce();
- }
- void setupdebounce(){
- buttonState1=lastButtonState1=digitalRead(11);
- buttonState2=lastButtonState2=digitalRead(12);
- }
- void update_reading(){
- int reading1 = digitalRead(11);
- int reading2 = digitalRead(12);
- if (reading1 != lastButtonState1) lastDebounceTime1 = millis();
- if (reading2 != lastButtonState2) lastDebounceTime2 = millis();
- if ((millis() - lastDebounceTime1) > debounceDelay) {
- if (reading1 != buttonState1) {
- buttonState1 = reading1;
- }
- }
- if ((millis() - lastDebounceTime2) > debounceDelay) {
- if (reading2 != buttonState2) {
- buttonState2 = reading2;
- if (buttonState2 == LOW) pump_on=1;
- else pump_on=0;
- }
- }
- lastButtonState1 = reading1;
- lastButtonState2 = reading2;
- }
- void powerUpOrDown()
- {
- pinMode(9, OUTPUT);
- digitalWrite(9,LOW);
- delay(1000);
- digitalWrite(9,HIGH);
- delay(2000);
- digitalWrite(9,LOW);
- delay(4000);
- pinMode(9, INPUT);
- }
- int flushGPRS(){
- char c1=' ',c2=' ';
- int found=0;
- Serial.println("---\nFlushing:");
- while(GPRS.available()){
- c1=c2;
- c2=GPRS.read();
- if(c1=='O'&&c2=='K') found=1;
- Serial.write(c2);
- }
- Serial.println("---");
- return found;
- }
- void loop()
- {
- int found;
- String data;
- int length;
- switch(state){
- case 0:
- update_reading();
- if(!was_pumping&&pump_on){ was_pumping=1; state=1; Serial.println("Pump switched on!");}
- if(was_pumping&&!pump_on){ was_pumping=0; state=1; Serial.println("Pump switched off!");
- break;
- case 1:
- flushGPRS();
- GPRS.println("AT");
- Serial.println("Ar: AT");
- time_before_wait=millis();
- timeout=500;
- state=2;
- next_state=3;
- next_state_if_timeout=3;
- next_state_if_no_OK=3;
- break;
- case 2:
- if(GPRS.available()&&retries<10){
- found=flushGPRS();
- if(found){ Serial.println("OK!"); retries=0; state=next_state;}
- else{ Serial.println("NOT OK?"); retries++; state=next_state_if_no_OK;}
- }
- else{
- if((millis()-time_before_wait>timeout)||retries>=10){
- Serial.println("TIMEOUT");
- retries=0;
- state=69;
- next_state=next_state_if_timeout;
- }
- }
- break;
- case 3:
- delay(1000);
- flushGPRS();
- GPRS.println("AT+CIPSHUT");
- Serial.println("Ar: AT+CIPSHUT");
- time_before_wait=millis();
- timeout=1500;
- state=2;
- next_state=4;
- next_state_if_no_OK=3;
- next_state_if_timeout=3;
- break;
- case 4:
- delay(1000);
- flushGPRS();
- GPRS.println("AT+CIPMUX=0");
- Serial.println("Ar: AT+CIPMUX=0");
- time_before_wait=millis();
- timeout=1500;
- state=2;
- next_state=5;
- next_state_if_no_OK=3;
- next_state_if_timeout=3;
- break;
- case 5:
- delay(1000);
- flushGPRS();
- GPRS.println("AT+CSTT=\"mobiledata\",\"\",\"\"");
- Serial.println("Ar: AT+CSTT=\"mobiledata\",\"\",\"\"");
- time_before_wait=millis();
- timeout=1500;
- state=2;
- next_state=6;
- next_state_if_no_OK=5;
- next_state_if_timeout=3;
- break;
- case 6:
- delay(1000);
- flushGPRS();
- GPRS.println("AT+CIICR");
- Serial.println("Ar: AT+CIICR");
- delay(2000);
- time_before_wait=millis();
- timeout=3000;
- state=2;
- next_state=7;
- next_state_if_no_OK=6;
- next_state_if_timeout=3;
- break;
- case 7:
- delay(1000);
- flushGPRS();
- GPRS.println("AT+CIFSR");
- Serial.println("Ar: AT+CIFSR");
- time_before_wait=millis();
- timeout=3000;
- state=2;
- next_state=8;
- next_state_if_no_OK=8;
- next_state_if_timeout=3;
- break;
- case 8:
- delay(1000);
- flushGPRS();
- GPRS.println("AT+CIPSPRT=1");
- Serial.println("Ar: AT+CIPSPRT=1");
- time_before_wait=millis();
- timeout=1000;
- state=2;
- next_state=9;
- next_state_if_no_OK=8;
- next_state_if_timeout=3;
- break;
- case 9:
- delay(1000);
- flushGPRS();
- GPRS.println("AT+CIPSTART=\"TCP\",\"api.carriots.com\",\"80\"");
- Serial.println("Ar: AT+CIPSTART=\"TCP\",\"api.carriots.com\",\"80\"");
- delay(2000);
- time_before_wait=millis();
- timeout=1000;
- state=2;
- next_state=10;
- next_state_if_no_OK=9;
- next_state_if_timeout=3;
- break;
- case 10:
- delay(1000);
- flushGPRS();
- GPRS.println("AT+CIPSEND");
- Serial.println("Ar: AT+CIPSEND");
- time_before_wait=millis();
- timeout=3000;
- state=2;
- next_state=11;
- next_state_if_no_OK=11;
- next_state_if_timeout=3;
- break;
- case 11:
- delay(1000);
- flushGPRS();
- data = "{\"protocol\":\"v2\",\"device\":\""+String(DEVICE)+"\",\"at\":\"now\",\"data\":{\"State\":\""+String(was_pumping,DEC)+"\"}}";
- length = data.length();
- GPRS.println("POST /streams HTTP/1.1");
- GPRS.println("Host: api.carriots.com");
- GPRS.println("Accept: application/json");
- GPRS.println("User-Agent: Arduino-Carriots");
- GPRS.println("Content-Type: application/json");
- GPRS.println("carriots.apikey: " + String(API_KEY));
- GPRS.println("Content-Length: " + String(length));
- GPRS.println("Connection: close");
- GPRS.println();
- GPRS.println(data);
- Serial.println("Ar: POST /streams HTTP/1.1");
- Serial.println("Ar: Host: api.carriots.com");
- Serial.println("Ar: Accept: application/json");
- Serial.println("Ar: User-Agent: Arduino-Carriots");
- Serial.println("Ar: Content-Type: application/json");
- Serial.println("Ar: carriots.apikey: " + String(API_KEY));
- Serial.println("Ar: Content-Length: " + String(length));
- Serial.println("Ar: Connection: close");
- Serial.println();
- Serial.println(data);
- delay(500);
- flushGPRS();
- GPRS.write(26);
- GPRS.println();
- Serial.write(26);
- Serial.println();
- delay(3000);
- time_before_wait=millis();
- timeout=1000;
- state=2;
- next_state=12;
- next_state_if_no_OK=12;
- next_state_if_timeout=3;
- break;
- case 12:
- flushGPRS();
- GPRS.println("AT+CIPCLOSE");
- Serial.println("Ar: AT+CIPCLOSE");
- time_before_wait=millis();
- timeout=5000;
- state=2;
- next_state=13;
- next_state_if_no_OK=13;
- next_state_if_timeout=3;
- break;
- case 13:
- flushGPRS();
- GPRS.println("AT+CIPSHUT");
- Serial.println("Ar: AT+CIPSHUT");
- time_before_wait=millis();
- timeout=5000;
- state=2;
- next_state=0;
- next_state_if_no_OK=0;
- next_state_if_timeout=3;
- break;
- case 69:
- Serial.println("Switching the GPRS on");
- powerUpOrDown();
- time_before_wait=millis();
- timeout=1000;
- state=2;
- next_state=3;
- next_state_if_no_OK=3;
- next_state_if_timeout=69;
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement