Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.27 KB | None | 0 0
  1. //Adding libraries
  2. #include <UbidotsMicroESP8266.h>
  3. #include <SparkFun_MAG3110.h>
  4.  
  5. //Instantiating a "MAG3110" (magnetic sensor)
  6. MAG3110 mag = MAG3110(); //Instantiate MAG3110 (magnetic sensor)
  7.  
  8. #define TOKEN  "A1E-m6TFNlkJVzsYvmTgNdgo82hdKcRts9"  // Put here your Ubidots TOKEN
  9. #define WIFISSID "BELL098" // Put here your Wi-Fi SSID
  10. #define PASSWORD "35DCED9E" // Put here your Wi-Fi password
  11.  
  12. Ubidots client(TOKEN);
  13.  
  14. int ledPin = 2;        // GPIO2 of ESP8266
  15. int relayPin = 13; //Pin for the relay signal
  16. bool shutoffValveState;
  17. int refreshRate = 12.5;  //Put refresh rate (milliseconds) here
  18.  
  19. //Creating function to get the total of a set of values to calibrate with
  20. int getTotal(int base, int total) {
  21.   total += base;
  22.   return total;
  23. }
  24.  
  25. //Creating function to create deadzone to prevent false alarms
  26. int countIfChanging(int delta, int counter){
  27.   if ((delta > 10) || (delta < -10)){
  28.     counter++;
  29.     return (counter);
  30.   } else {
  31.     return 0;
  32.   }
  33. }
  34.  
  35. //Creating function to wait 300x refresh rate before sounding alarm
  36. int isChanging(int counter){
  37.   if(counter > 300){
  38.     return 1;
  39.   } else {
  40.     return 0;
  41.   }
  42. }
  43.  
  44. //Creating variables for getTotal
  45. int calX, calY, calZ, totX, totY, totZ;
  46.  
  47. void setup() {
  48.   Serial.begin(115200); //Enter upload speed here
  49.   client.setDataSourceName("Water_meter"); //Enter name of device here
  50.   client.wifiConnection(WIFISSID, PASSWORD); //Connecting to WiFi
  51.  
  52.   mag.initialize(); //Initializes the mag sensor
  53.   digitalWrite(ledPin, LOW); // Turn on LED
  54.   pinMode(relayPin, OUTPUT); // Setting relay pin to output to send instead of receive signals
  55.   //Resetting total(axis) values
  56.   totX = 0;
  57.   totY = 0;
  58.   totZ = 0;
  59.   //Measuring 30 values to calibrate
  60.   for(int j=0;j<30;j++){
  61.     mag.triggerMeasurement(); //Triggers measurement of sensor; must instantiate command to initiate measurement
  62.     mag.readMag(&calX, &calY, &calZ); //Set 3 sensor values (x, y, z) to 3 values entered
  63.     //Finding total of each set of 30 values
  64.     totX = getTotal(calX, totX);
  65.     totY = getTotal(calY, totY);
  66.     totZ = getTotal(calZ, totZ);
  67.   }
  68.   //Finding averages of values
  69.   calX = totX/30;
  70.   calY = totY/30;
  71.   calZ = totZ/30;
  72. }
  73.  
  74. int timeStamp = 0;
  75. //Initializing magnetic axes as 0
  76. int x = 0;
  77. int y = 0;
  78. int z = 0;
  79. //Initializing other variables
  80. int lastX, lastY, lastZ, xCounter, yCounter, zCounter;
  81. //Initializing boolean variables for whether each variable is changing
  82.  
  83. void loop() {
  84.   //pinMode(ledPin, OUTPUT);  //Setting LED to output
  85.   //Setting the previous magnetic values to the current ones
  86.   lastX = x;
  87.   lastY = y;
  88.   lastZ = z;
  89.   if (millis() - timeStamp > 12.5) //Making sure only one measurement happens each second
  90.   {
  91.     timeStamp = millis(); //Keeping the 1 measurement/second
  92.     mag.triggerMeasurement(); //Triggering measurement
  93.   }
  94.  
  95.   if (mag.dataReady()) {  //Making sure the data has been collected before updating variables
  96.     //Read the data
  97.     mag.readMag(&x, &y, &z); //Setting variables x, y and z to the values collected by the sensor
  98.   }
  99.  
  100.   //Calibrating x, y, and z values using previously collected variables
  101.   x -= calX;
  102.   y -= calY;
  103.   z -= calZ;
  104.  
  105.   //Instantiating variables to record changes in x, y, and z
  106.   int deltaX, deltaY, deltaZ;
  107.  
  108.   //Setting "change in" variables to the current value of the axis minus the previous value
  109.   deltaX = x - lastX;
  110.   deltaY = y - lastY;
  111.   deltaZ = z - lastZ;
  112.  
  113.   //Using countIfChanging function to count consecutive seconds during which each axis is changing outside of deadzone
  114.   xCounter = countIfChanging(deltaX, xCounter);
  115.   yCounter = countIfChanging(deltaY, yCounter);
  116.   zCounter = countIfChanging(deltaZ, zCounter);
  117.   //Printing values for debugging reasons
  118.   Serial.println(xCounter);
  119.   Serial.println(yCounter);
  120.   Serial.println(zCounter);
  121.  
  122.   //Printing values so you know what is happening
  123.   Serial.print("X: ");
  124.   Serial.print(x);
  125.   Serial.print(", Y: ");
  126.   Serial.print(y);
  127.   Serial.print(", Z: ");
  128.   Serial.println(z);
  129.   Serial.print("Change in X: ");
  130.   Serial.print(deltaX);
  131.   Serial.print(", Change in Y: ");
  132.   Serial.print(deltaY);
  133.   Serial.print(", Change in Z: ");
  134.   Serial.println(deltaZ);
  135.   //Printing whether x, y, and z are changing
  136.   if(isChanging(xCounter)){
  137.     Serial.print("X Changing, ");
  138.   } else {
  139.     Serial.print("X Not Changing, ");
  140.   }
  141.   if(isChanging(yCounter)){
  142.     Serial.print("Y Changing, ");
  143.   } else {
  144.     Serial.print("Y Not Changing, ");
  145.   }
  146.   if(isChanging(zCounter)){
  147.     Serial.print("Z Changing");
  148.   } else {
  149.     Serial.println("Z Not Changing");
  150.   }
  151.   Serial.println("--------");
  152.   shutoffValveState = client.getValue("5a9c7626c03f973522262884"); //Call value for whether the shutoff is turned on or off
  153.   if(shutoffValveState == true){ //Checks whether the shutoff switch is turned on
  154.     digitalWrite(relayPin, LOW); //Closes switch if shutoff switch is turned on, activating shutoff
  155.   } else {
  156.     digitalWrite(relayPin, HIGH); //Opens switch to save power when shutoff switch is turned off
  157.   }
  158.   //Adding to Ubidots (cloud)
  159.   client.add("change-in-x", deltaX); //Add variable "x"
  160.   client.add("change-in-y", deltaY); //Add variable "y"
  161.   client.add("change-in-z", deltaZ); //Add variable "z"
  162.   client.sendAll(true); //Send variables to client
  163.   delay(refreshRate); //Refresh
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement