Advertisement
Guest User

Untitled

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