Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3.  
  4. //const char* ssid = "iPhone Mukhammad";
  5. //const char* password = "12344321";
  6. //const char* ssid = "TECH_BFA3";
  7. //const char* password = "092115066";
  8. const char* ssid = "KUY";
  9. const char* password = "";
  10. WiFiClient espClient;
  11. //IPAddress ip(192,168,4,4);
  12. //IPAddress gateway(192,168,4,1);
  13. //IPAddress subnet(255,255,255,0);
  14.  
  15. #define mytopic "data/waterflow"
  16. #define typeSensor 0
  17. #include <PubSubClient.h>
  18. char mqtt_server[] = "192.168.43.171";
  19. //char mqtt_server[] = "192.168.0.31";
  20. PubSubClient client(espClient);
  21.  
  22. byte sensorInterrupt = 0; // 0 = digital pin 2
  23. byte sensorPin = D2;
  24.  
  25. // The hall-effect flow sensor outputs approximately 4.5 pulses per second per
  26. // litre/minute of flow.
  27. float calibrationFactor = 4.5;
  28.  
  29. volatile byte pulseCount;
  30.  
  31. float flowRate;
  32. unsigned int flowMilliLitres;
  33. unsigned long totalMilliLitres;
  34.  
  35. unsigned long oldTime;
  36.  
  37. void setup() {
  38. Serial.begin(115200); // Starts the serial communication
  39. WiFi.mode(WIFI_STA);
  40. WiFi.begin(ssid, password);
  41. // WiFi.config(ip,gateway,subnet);
  42. while (WiFi.waitForConnectResult() != WL_CONNECTED) {
  43. Serial.println("Connection Failed! Rebooting...");
  44. delay(5000);
  45. Serial.print(".");
  46. }
  47.  
  48. client.connect("1","hcix","hcix");
  49. client.setServer(mqtt_server, 1883);
  50. while (!client.connected()) {
  51. client.connect("1","hcix","hcix");
  52. client.setServer(mqtt_server, 1883);
  53. Serial.print("Attempting MQTT connection...");
  54. if (client.connect("waterflow")) {
  55. Serial.println("connected");
  56. } else {
  57. Serial.print("failed, rc=");
  58. Serial.print(client.state());
  59. Serial.println(" try again in 5 seconds");
  60. delay(5000);
  61. }
  62. }
  63.  
  64. //waterflow
  65. pinMode(sensorPin, INPUT);
  66. digitalWrite(sensorPin, HIGH);
  67. pulseCount = 0;
  68. flowRate = 0.0;
  69. flowMilliLitres = 0;
  70. totalMilliLitres = 0;
  71. oldTime = 0;
  72.  
  73. // The Hall-effect sensor is connected to pin 2 which uses interrupt 0.
  74. // Configured to trigger on a FALLING state change (transition from HIGH
  75. // state to LOW state)
  76. attachInterrupt(digitalPinToInterrupt(sensorPin), pulseCounter, FALLING);
  77. }
  78.  
  79. void loop() {
  80. if((millis() - oldTime) > 1000) // Only process counters once per second
  81. {
  82. // Disable the interrupt while calculating flow rate and sending the value to
  83. // the host
  84. detachInterrupt(sensorInterrupt);
  85.  
  86. // Because this loop may not complete in exactly 1 second intervals we calculate
  87. // the number of milliseconds that have passed since the last execution and use
  88. // that to scale the output. We also apply the calibrationFactor to scale the output
  89. // based on the number of pulses per second per units of measure (litres/minute in
  90. // this case) coming from the sensor.
  91. flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
  92.  
  93. // Note the time this processing pass was executed. Note that because we've
  94. // disabled interrupts the millis() function won't actually be incrementing right
  95. // at this point, but it will still return the value it was set to just before
  96. // interrupts went away.
  97. oldTime = millis();
  98.  
  99. // Divide the flow rate in litres/minute by 60 to determine how many litres have
  100. // passed through the sensor in this 1 second interval, then multiply by 1000 to
  101. // convert to millilitres.
  102. flowMilliLitres = (flowRate / 60) * 1000;
  103.  
  104. // Add the millilitres passed in this second to the cumulative total
  105. totalMilliLitres += flowMilliLitres;
  106.  
  107. unsigned int frac;
  108.  
  109. // Print the flow rate for this second in litres / minute
  110. // Serial.print("Flow rate: ");
  111. // Serial.print(int(flowRate)); // Print the integer part of the variable
  112. // Serial.print("."); // Print the decimal point
  113. // // Determine the fractional part. The 10 multiplier gives us 1 decimal place.
  114. // frac = (flowRate - int(flowRate)) * 10;
  115. // Serial.print(frac, DEC) ; // Print the fractional part of the variable
  116. // Serial.print("L/min");
  117.  
  118. /*
  119. // Print the number of litres flowed in this second
  120. Serial.print(" Current Liquid Flowing: "); // Output separator
  121. Serial.print(flowMilliLitres);
  122. Serial.print("mL/Sec");
  123.  
  124. // Print the cumulative total of litres flowed since starting
  125. Serial.print(" Output Liquid Quantity: "); // Output separator
  126. Serial.print(totalMilliLitres);
  127. Serial.println("mL");
  128. */
  129.  
  130. // Reset the pulse counter so we can start incrementing again
  131. pulseCount = 0;
  132.  
  133. // Enable the interrupt again now that we've finished sending output
  134. attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
  135.  
  136. // if(flowRate != 0){
  137. String msg = String(typeSensor);
  138. msg += ":";
  139. msg += int(flowRate);
  140. msg += ".";
  141. frac = (flowRate - int(flowRate)) * 10;
  142. msg += frac;
  143.  
  144. Serial.println(msg);
  145. client.publish(mytopic, String(msg).c_str());
  146. // }
  147. }
  148. }
  149.  
  150. /*
  151. Insterrupt Service Routine
  152. */
  153. void pulseCounter()
  154. {
  155. // Increment the pulse counter
  156. pulseCount++;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement