Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Filters.h"
- #include "Sim800l.h"
- #include <SoftwareSerial.h>
- Sim800l Sim800l;
- float testFrequency = 60; // test signal frequency (Hz)
- float windowLength = 20.0/testFrequency; // how long to average the signal, for statistist
- unsigned long printPeriod = 1000;
- unsigned long previousMillis = 0;
- unsigned long workStartTime = millis();
- unsigned long idleCounter = 0;
- unsigned long workCounter = 0;
- unsigned long washingTime = 0;
- unsigned long washingCount = 0;
- const unsigned long switchFromIdleToWork = 5; //measurement count to change state of work
- const unsigned long switchFromWorkToIdle = 120; //measurement count to change state of work
- const unsigned long minimumWorkTimeNotification = 10*60*1000; //how many millis to accept period as washing
- const unsigned long minimumIdleTimeNotification = 5*60*1000; //how many millis after washing to send notification
- const double workMargin = 0.1; //in amps
- boolean worked = false;
- boolean working = false;
- int sensorValue;
- String textToSend;
- void setup() {
- Serial.begin(9600);
- Sim800l.begin();
- delay(10);
- }
- void loop() {
- RunningStatistics inputStats; // create statistics to look at the raw test signal
- inputStats.setWindowSecs( windowLength );
- while( true ) {
- sensorValue = analogRead(A0); // read the analog in value:
- inputStats.input(sensorValue); // log to Stats function
- if((unsigned long)(millis() - previousMillis) >= printPeriod) {
- previousMillis = millis(); // update time
- double sigma = inputStats.sigma();
- double ampsValue = 0.0496087 * sigma - 0.0336928; //amps correction, original formula: 0.0496087*sigma - 0.0236928;
- if (ampsValue < 0){
- ampsValue = 0;
- }
- analyzeAmps(ampsValue);
- }
- }
- }
- void sendNotifications(unsigned long washingTime){
- unsigned long hours = ((washingTime/(1000*60*60))%24);
- unsigned long minutes = ((washingTime/(1000*60))%60);
- unsigned long seconds = ((washingTime/1000)%60);
- washingCount++;
- textToSend = "Koniec prania. Czas: ";
- textToSend += hours;
- textToSend += "h ";
- textToSend += minutes;
- textToSend += "min ";
- textToSend += seconds;
- textToSend += "s.";
- char* text = " ";
- textToSend.toCharArray(text, textToSend.length());
- Serial.println(Sim800l.sendSms("+48***",text));
- Serial.println(textToSend);
- }
- void analyzeAmps(double amps){
- //debug
- Serial.print(" washingTime: ");
- Serial.print(washingTime/1000);
- Serial.print("\tprad: ");
- Serial.print(amps);
- Serial.print(" working: ");
- Serial.print(working);
- Serial.print("\tilosc pran: ");
- Serial.println(washingCount);
- if (amps > workMargin){ //working
- workCounter++;
- if (workCounter >= switchFromIdleToWork){
- idleCounter = 0;
- if (!working){
- workStartTime = millis();
- }
- working = true;
- }
- if(((millis() - workStartTime) > minimumWorkTimeNotification)){
- worked = true;
- }
- }else{ //idle
- idleCounter++;
- if (working && idleCounter >= switchFromWorkToIdle){
- workCounter = 0;
- working = false;
- workStartTime = 0;
- if (worked){
- washingTime = millis() - workStartTime;
- sendNotifications(washingTime);
- worked = false;
- washingTime = 0;
- }
- }else if (!working){
- workCounter = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement