Advertisement
adria_junyent

Arduino code for Sim900 + Carriots reporting

Oct 19th, 2015
898
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.43 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2.  
  3. SoftwareSerial GPRS(7, 8);
  4.  
  5. int buttonState1, buttonState2;
  6. int lastButtonState1, lastButtonState2;
  7. long lastDebounceTime1 = 0;  // the last time the output pin was toggled
  8. long lastDebounceTime2 = 0;  // the last time the output pin was toggled
  9. long debounceDelay = 50;    // the debounce time; increase if the output flickers
  10. int pump_on=0;
  11. int was_pumping=0;
  12. int retries=0;
  13.  
  14. int state=0;
  15. int next_state=0;
  16. int next_state_if_timeout=0;
  17. int next_state_if_no_OK=0;
  18. int timeout=0;
  19. long time_before_wait=0;
  20.  
  21. #define WEBSITE  "api.carriots.com"
  22. #define API_KEY "API KEY FROM THE SETTINGS PAGE IN CARRIOTS"
  23. #define DEVICE  "DEVICE ID FROM THE DEVICES PAGE IN CARRIOTS"
  24.  
  25. void setup()
  26. {
  27.   GPRS.begin(19200);               // the GPRS baud rate  
  28.   Serial.begin(19200);             // the Serial port of Arduino baud rate.
  29.  
  30.   pinMode(11,INPUT);
  31.   digitalWrite(11,HIGH);
  32.   pinMode(12,INPUT);
  33.   digitalWrite(12,HIGH);
  34.  
  35.   setupdebounce();
  36. }
  37.  
  38. void setupdebounce(){
  39.     buttonState1=lastButtonState1=digitalRead(11);
  40.     buttonState2=lastButtonState2=digitalRead(12);
  41. }
  42.  
  43. void update_reading(){
  44.     int reading1 = digitalRead(11);
  45.     int reading2 = digitalRead(12);
  46.  
  47.     if (reading1 != lastButtonState1) lastDebounceTime1 = millis();
  48.     if (reading2 != lastButtonState2) lastDebounceTime2 = millis();
  49.  
  50.     if ((millis() - lastDebounceTime1) > debounceDelay) {
  51.         if (reading1 != buttonState1) {
  52.             buttonState1 = reading1;
  53.         }
  54.     }
  55.     if ((millis() - lastDebounceTime2) > debounceDelay) {
  56.         if (reading2 != buttonState2) {
  57.             buttonState2 = reading2;
  58.             if (buttonState2 == LOW) pump_on=1;
  59.             else pump_on=0;
  60.         }
  61.     }
  62.  
  63.     lastButtonState1 = reading1;  
  64.     lastButtonState2 = reading2;
  65. }
  66.  
  67.  
  68. void powerUpOrDown()
  69. {
  70.   pinMode(9, OUTPUT);
  71.   digitalWrite(9,LOW);
  72.   delay(1000);
  73.   digitalWrite(9,HIGH);
  74.   delay(2000);
  75.   digitalWrite(9,LOW);
  76.   delay(4000);
  77.   pinMode(9, INPUT);
  78. }
  79.  
  80. int flushGPRS(){
  81.     char c1=' ',c2=' ';
  82.     int found=0;
  83.     Serial.println("---\nFlushing:");
  84.     while(GPRS.available()){
  85.         c1=c2;
  86.         c2=GPRS.read();
  87.         if(c1=='O'&&c2=='K') found=1;
  88.         Serial.write(c2);
  89.         }  
  90.     Serial.println("---");
  91.  
  92.     return found;
  93. }
  94.  
  95. void loop()
  96. {
  97.     int found;
  98.     String data;
  99.     int length;
  100.  
  101.     switch(state){
  102.         case 0:
  103.             update_reading();
  104.             if(!was_pumping&&pump_on){ was_pumping=1; state=1; Serial.println("Pump switched on!");}
  105.             if(was_pumping&&!pump_on){ was_pumping=0; state=1; Serial.println("Pump switched off!");
  106.             break;
  107.         case 1:
  108.             flushGPRS();
  109.             GPRS.println("AT");
  110.             Serial.println("Ar: AT");
  111.             time_before_wait=millis();
  112.             timeout=500;
  113.             state=2;
  114.             next_state=3;
  115.             next_state_if_timeout=3;
  116.             next_state_if_no_OK=3;
  117.             break;
  118.         case 2:
  119.             if(GPRS.available()&&retries<10){
  120.                  found=flushGPRS();
  121.                  if(found){ Serial.println("OK!"); retries=0; state=next_state;}
  122.                  else{ Serial.println("NOT OK?"); retries++; state=next_state_if_no_OK;}
  123.             }
  124.             else{
  125.                 if((millis()-time_before_wait>timeout)||retries>=10){
  126.                     Serial.println("TIMEOUT");
  127.                     retries=0;
  128.                     state=69;
  129.                     next_state=next_state_if_timeout;
  130.                 }
  131.             }
  132.             break;
  133.         case 3:
  134.             delay(1000);
  135.             flushGPRS();
  136.             GPRS.println("AT+CIPSHUT");
  137.             Serial.println("Ar: AT+CIPSHUT");
  138.             time_before_wait=millis();
  139.             timeout=1500;
  140.             state=2;
  141.             next_state=4;
  142.             next_state_if_no_OK=3;
  143.             next_state_if_timeout=3;
  144.             break;
  145.         case 4:
  146.             delay(1000);
  147.             flushGPRS();
  148.             GPRS.println("AT+CIPMUX=0");
  149.             Serial.println("Ar: AT+CIPMUX=0");
  150.             time_before_wait=millis();
  151.             timeout=1500;
  152.             state=2;
  153.             next_state=5;
  154.             next_state_if_no_OK=3;
  155.             next_state_if_timeout=3;
  156.             break;
  157.         case 5:
  158.             delay(1000);
  159.             flushGPRS();
  160.             GPRS.println("AT+CSTT=\"mobiledata\",\"\",\"\"");
  161.             Serial.println("Ar: AT+CSTT=\"mobiledata\",\"\",\"\"");
  162.             time_before_wait=millis();
  163.             timeout=1500;
  164.             state=2;
  165.             next_state=6;
  166.             next_state_if_no_OK=5;
  167.             next_state_if_timeout=3;
  168.             break;
  169.         case 6:
  170.             delay(1000);
  171.             flushGPRS();
  172.             GPRS.println("AT+CIICR");
  173.             Serial.println("Ar: AT+CIICR");
  174.             delay(2000);
  175.             time_before_wait=millis();
  176.             timeout=3000;
  177.             state=2;
  178.             next_state=7;
  179.             next_state_if_no_OK=6;
  180.             next_state_if_timeout=3;
  181.             break;
  182.         case 7:
  183.             delay(1000);
  184.             flushGPRS();
  185.             GPRS.println("AT+CIFSR");
  186.             Serial.println("Ar: AT+CIFSR");
  187.             time_before_wait=millis();
  188.             timeout=3000;
  189.             state=2;
  190.             next_state=8;
  191.             next_state_if_no_OK=8;
  192.             next_state_if_timeout=3;
  193.             break;
  194.         case 8:
  195.             delay(1000);
  196.             flushGPRS();
  197.             GPRS.println("AT+CIPSPRT=1");
  198.             Serial.println("Ar: AT+CIPSPRT=1");
  199.             time_before_wait=millis();
  200.             timeout=1000;
  201.             state=2;
  202.             next_state=9;
  203.             next_state_if_no_OK=8;
  204.             next_state_if_timeout=3;
  205.             break;
  206.         case 9:
  207.             delay(1000);
  208.             flushGPRS();
  209.             GPRS.println("AT+CIPSTART=\"TCP\",\"api.carriots.com\",\"80\"");
  210.             Serial.println("Ar: AT+CIPSTART=\"TCP\",\"api.carriots.com\",\"80\"");
  211.             delay(2000);
  212.             time_before_wait=millis();
  213.             timeout=1000;
  214.             state=2;
  215.             next_state=10;
  216.             next_state_if_no_OK=9;
  217.             next_state_if_timeout=3;
  218.             break;
  219.         case 10:
  220.             delay(1000);
  221.             flushGPRS();
  222.             GPRS.println("AT+CIPSEND");
  223.             Serial.println("Ar: AT+CIPSEND");  
  224.             time_before_wait=millis();
  225.             timeout=3000;
  226.             state=2;
  227.             next_state=11;
  228.             next_state_if_no_OK=11;
  229.             next_state_if_timeout=3;
  230.             break;
  231.         case 11:
  232.             delay(1000);
  233.             flushGPRS();
  234.             data = "{\"protocol\":\"v2\",\"device\":\""+String(DEVICE)+"\",\"at\":\"now\",\"data\":{\"State\":\""+String(was_pumping,DEC)+"\"}}";
  235.             length = data.length();    
  236.                 GPRS.println("POST /streams HTTP/1.1");
  237.             GPRS.println("Host: api.carriots.com");
  238.             GPRS.println("Accept: application/json");
  239.             GPRS.println("User-Agent: Arduino-Carriots");
  240.             GPRS.println("Content-Type: application/json");
  241.             GPRS.println("carriots.apikey: " + String(API_KEY));
  242.             GPRS.println("Content-Length: " + String(length));
  243.             GPRS.println("Connection: close");
  244.             GPRS.println();
  245.             GPRS.println(data);
  246.                 Serial.println("Ar: POST /streams HTTP/1.1");
  247.             Serial.println("Ar: Host: api.carriots.com");
  248.             Serial.println("Ar: Accept: application/json");
  249.             Serial.println("Ar: User-Agent: Arduino-Carriots");
  250.             Serial.println("Ar: Content-Type: application/json");
  251.             Serial.println("Ar: carriots.apikey: " + String(API_KEY));
  252.             Serial.println("Ar: Content-Length: " + String(length));
  253.             Serial.println("Ar: Connection: close");
  254.             Serial.println();
  255.             Serial.println(data);      
  256.             delay(500);
  257.             flushGPRS();
  258.             GPRS.write(26);
  259.             GPRS.println();
  260.             Serial.write(26);
  261.             Serial.println();
  262.             delay(3000);
  263.             time_before_wait=millis();
  264.             timeout=1000;
  265.             state=2;
  266.             next_state=12;
  267.             next_state_if_no_OK=12;
  268.             next_state_if_timeout=3;
  269.             break;
  270.         case 12:
  271.             flushGPRS();
  272.             GPRS.println("AT+CIPCLOSE");
  273.             Serial.println("Ar: AT+CIPCLOSE");
  274.             time_before_wait=millis();
  275.             timeout=5000;
  276.             state=2;
  277.             next_state=13;
  278.             next_state_if_no_OK=13;
  279.             next_state_if_timeout=3;
  280.             break;                                     
  281.         case 13:
  282.             flushGPRS();
  283.             GPRS.println("AT+CIPSHUT");
  284.             Serial.println("Ar: AT+CIPSHUT");
  285.             time_before_wait=millis();
  286.             timeout=5000;
  287.             state=2;
  288.             next_state=0;
  289.             next_state_if_no_OK=0;
  290.             next_state_if_timeout=3;
  291.             break;                                     
  292.         case 69:
  293.             Serial.println("Switching the GPRS on");
  294.             powerUpOrDown();
  295.             time_before_wait=millis();
  296.             timeout=1000;
  297.             state=2;
  298.             next_state=3;
  299.             next_state_if_no_OK=3;
  300.             next_state_if_timeout=69;
  301.             break;
  302.         }          
  303.     }
  304.    
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement