Advertisement
Guest User

Untitled

a guest
May 24th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.50 KB | None | 0 0
  1. #include "Filters.h"
  2. #include "Sim800l.h"
  3. #include <SoftwareSerial.h>
  4. Sim800l Sim800l;
  5.  
  6. float testFrequency = 60;                     // test signal frequency (Hz)
  7. float windowLength = 20.0/testFrequency;     // how long to average the signal, for statistist
  8.  
  9. unsigned long printPeriod = 1000;
  10. unsigned long previousMillis = 0;
  11.  
  12. unsigned long workStartTime = millis();
  13.  
  14. unsigned long idleCounter = 0;
  15. unsigned long workCounter = 0;
  16. unsigned long washingTime = 0;
  17. unsigned long washingCount = 0;
  18.  
  19. const unsigned long switchFromIdleToWork = 5; //measurement count to change state of work
  20. const unsigned long switchFromWorkToIdle = 120; //measurement count to change state of work
  21. const unsigned long minimumWorkTimeNotification = 10*60*1000; //how many millis to accept period as washing
  22. const unsigned long minimumIdleTimeNotification = 5*60*1000; //how many millis after washing to send notification
  23.  
  24. const double workMargin = 0.1; //in amps
  25. boolean worked = false;
  26. boolean working = false;
  27. int sensorValue;
  28.  
  29. String textToSend;
  30.  
  31. void setup() {
  32.   Serial.begin(9600);
  33.   Sim800l.begin();
  34.   delay(10);
  35. }
  36.  
  37. void loop() {
  38.   RunningStatistics inputStats;                 // create statistics to look at the raw test signal
  39.   inputStats.setWindowSecs( windowLength );
  40.    
  41.   while( true ) {  
  42.     sensorValue = analogRead(A0);  // read the analog in value:
  43.     inputStats.input(sensorValue);  // log to Stats function        
  44.     if((unsigned long)(millis() - previousMillis) >= printPeriod) {
  45.       previousMillis = millis();   // update time
  46.       double sigma = inputStats.sigma();
  47.       double ampsValue = 0.0496087 * sigma - 0.0336928; //amps correction, original formula: 0.0496087*sigma - 0.0236928;
  48.       if (ampsValue < 0){
  49.         ampsValue = 0;
  50.       }
  51.       analyzeAmps(ampsValue);
  52.     }
  53.   }
  54. }
  55.  
  56. void sendNotifications(unsigned long washingTime){
  57.       unsigned long hours = ((washingTime/(1000*60*60))%24);
  58.       unsigned long minutes = ((washingTime/(1000*60))%60);
  59.       unsigned long seconds = ((washingTime/1000)%60);
  60.       washingCount++;
  61.       textToSend = "Koniec prania. Czas: ";
  62.       textToSend += hours;
  63.       textToSend += "h ";
  64.       textToSend += minutes;
  65.       textToSend += "min ";
  66.       textToSend += seconds;
  67.       textToSend += "s.";
  68.       char* text = " ";
  69.       textToSend.toCharArray(text, textToSend.length());
  70.       Serial.println(Sim800l.sendSms("+48***",text));
  71.       Serial.println(textToSend);      
  72. }
  73.  
  74. void analyzeAmps(double amps){
  75.   //debug
  76.   Serial.print(" washingTime: ");
  77.   Serial.print(washingTime/1000);
  78.   Serial.print("\tprad: ");
  79.   Serial.print(amps);
  80.   Serial.print(" working: ");
  81.   Serial.print(working);
  82.   Serial.print("\tilosc pran: ");
  83.   Serial.println(washingCount);
  84.  
  85.   if (amps > workMargin){ //working
  86.     workCounter++;    
  87.     if (workCounter >= switchFromIdleToWork){          
  88.       idleCounter = 0;    
  89.       if (!working){
  90.         workStartTime = millis();
  91.       }
  92.       working = true;
  93.     }
  94.     if(((millis() - workStartTime) > minimumWorkTimeNotification)){      
  95.       worked = true;
  96.     }  
  97.  
  98.   }else{ //idle    
  99.     idleCounter++;  
  100.     if (working && idleCounter >= switchFromWorkToIdle){
  101.       workCounter = 0;      
  102.       working = false;
  103.       workStartTime = 0;
  104.       if (worked){
  105.         washingTime = millis() - workStartTime;
  106.         sendNotifications(washingTime);
  107.         worked = false;
  108.         washingTime = 0;
  109.       }
  110.     }else if (!working){
  111.       workCounter = 0;  
  112.     }
  113.   }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement