Advertisement
Guest User

Untitled

a guest
Feb 27th, 2019
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.36 KB | None | 0 0
  1. #include <DHT.h>
  2. #include <EEPROM.h>
  3. #include <ESP8266WiFi.h>
  4. #include <PubSubClient.h>
  5. #include <Wire.h>
  6.  
  7. #define DHTPIN 2 // D4 what digital pin we're connected to
  8. #define DHTTYPE DHT11 // DHT 11
  9.  
  10. bool debug = false;
  11.  
  12. // DHT humidity sensor
  13. DHT dht(DHTPIN, DHTTYPE);
  14.  
  15. //Wifi stuff
  16. const char* ssid = "";
  17. const char* password = "";
  18. WiFiClient espClient;
  19.  
  20. //mqtt stuff
  21. const char* mqtt_user = "";
  22. const char* mqtt_pass = "";
  23. const char* clientId = "";
  24. const char* MQTTtopic = "";
  25. IPAddress server(192, 168, 1 ,42);
  26. PubSubClient client(server, 1883, espClient);
  27.  
  28. //MPU-6050 stuff
  29. const int MPU_addr=0x68; // I2C address of the MPU-6050
  30. int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
  31. // Vars for an average of the gyro readings.
  32. const int numReadings = 6;
  33. const int threshold = 20000; // Shakiness at which we consider the washing machine to be running
  34. unsigned int readIndex = 0; // the index of the current reading
  35. unsigned long shakiness = 0; // Commulative fluctuation in XYZ acc
  36.  
  37. void setup_wifi() {
  38.  
  39. delay(10);
  40. // We start by connecting to a WiFi network
  41. Serial.println();
  42. Serial.print("Connecting to ");
  43. Serial.println(ssid);
  44.  
  45. WiFi.begin(ssid, password);
  46.  
  47. while (WiFi.status() != WL_CONNECTED) {
  48. delay(500);
  49. Serial.print(".");
  50. }
  51.  
  52. randomSeed(micros());
  53.  
  54. Serial.println("");
  55. Serial.println("WiFi connected");
  56. Serial.println("IP address: ");
  57. Serial.println(WiFi.localIP());
  58. }
  59.  
  60. void reconnect() {
  61. // Loop until we're reconnected
  62. while (!client.connected()) {
  63. Serial.print("Attempting MQTT connection...");
  64. // Create a random client ID
  65. // Attempt to connect
  66. if (client.connect(clientId, mqtt_user, mqtt_pass)) {
  67. Serial.println("connected");
  68. // Once connected, publish an announcement...
  69. // client.publish(MQTTTopic, "Starting...");
  70. } else {
  71. Serial.print("failed, rc=");
  72. Serial.print(client.state());
  73. Serial.println(" try again in 5 seconds");
  74. // Wait 5 seconds before retrying
  75. delay(5000);
  76. }
  77. }
  78. }
  79.  
  80. void setup_gyro(){
  81. Wire.begin();
  82. Wire.beginTransmission(MPU_addr);
  83. Wire.write(0x6B); // PWR_MGMT_1 register
  84. Wire.write(0); // set to zero (wakes up the MPU-6050)
  85. Wire.endTransmission(true);
  86. }
  87.  
  88.  
  89. void setup() {
  90. Serial.begin(115200);
  91. dht.begin();
  92. setup_wifi();
  93. setup_gyro();
  94. client.setServer(server, 1883);
  95. }
  96.  
  97.  
  98. void read_gyro(){
  99. /*
  100. We consider the washing machine to be shaking, when there's much fluctuation in the X, Y, Z readings.
  101. */
  102. int16_t Old_AcX,Old_AcY,Old_AcZ;
  103. shakiness = 0;
  104. readIndex = 0;
  105. for (readIndex; readIndex < numReadings; readIndex++) {
  106. Old_AcX = AcX; Old_AcY = AcY; Old_AcZ = AcZ;
  107. Wire.beginTransmission(MPU_addr);
  108. Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
  109. Wire.endTransmission(false);
  110. Wire.requestFrom(MPU_addr,14,true); // request a total of 14 registers
  111. AcX=Wire.read()<<8|Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
  112. AcY=Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  113. AcZ=Wire.read()<<8|Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
  114. Tmp=Wire.read()<<8|Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
  115. Tmp=(Tmp/340.00+36.53);
  116. // add the reading to the total:
  117. if (readIndex > 0) {
  118. shakiness = shakiness + abs(AcX - Old_AcX) + abs(AcY - Old_AcY) + abs(AcZ - Old_AcZ);
  119. }
  120. delay(333);
  121. }
  122. // calculate the average:
  123. Serial.print("AcX: "); Serial.print(AcX); Serial.print(" AcY: "); Serial.print(AcY);Serial.print(" AcZ: "); Serial.println(AcZ);
  124. Serial.print("shakiness: "); Serial.println(shakiness);
  125. }
  126.  
  127. void loop() {
  128. delay(2000);
  129. read_gyro();
  130.  
  131. // Reading temperature or humidity takes about 250 milliseconds!
  132. // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  133. float h = dht.readHumidity();
  134. // Read temperature as Celsius (the default)
  135. float t = dht.readTemperature();
  136.  
  137. // Check if any reads failed and exit early (to try again).
  138. if (isnan(h) || isnan(t)) {
  139. Serial.println("Failed to read from DHT sensor!");
  140. return;
  141. }
  142.  
  143. // Compute heat index in Celsius (isFahreheit = false)
  144. float hic = dht.computeHeatIndex(t, h, false);
  145.  
  146. // battery connected wiyth 100kOHM resistor to A0
  147. pinMode(A0, INPUT);
  148. unsigned int raw = analogRead(A0);
  149. float volt = raw * 4.2 / 1023.0;
  150.  
  151.  
  152. bool running = false;
  153. if (shakiness > threshold) {
  154. running = true;
  155. }
  156.  
  157.  
  158. // reserve 1 byte of eeprom
  159. EEPROM.begin(2);
  160. byte romstore[2];
  161. romstore[0] = EEPROM.read(0);
  162. romstore[1] = EEPROM.read(1);
  163.  
  164. // 123 in [0] is a marker to detect first use
  165. if (romstore[0] != 123) {
  166. romstore[0] = 123;
  167. // romstore[1] is a marker of how many cycles ago we were running.
  168. // Only 1-10 is of real interest...
  169. romstore[1] = 254;
  170. }
  171.  
  172. // Only report that whe are running when we measure running for the second time.
  173. bool report_running = false;
  174. if (running and romstore[1] == 0) {
  175. report_running = true;
  176. }
  177.  
  178. if (running) {
  179. romstore[1] = 0;
  180. } else if (romstore[1] < 254) {
  181. romstore[1]++;
  182. }
  183. // system_rtc_mem_write(64, rtcStore, 2);
  184. EEPROM.write(0, romstore[0]);
  185. EEPROM.write(1, romstore[1]);
  186. EEPROM.end();
  187.  
  188.  
  189. // ------------------------ Send message
  190.  
  191. if (!client.connected()) {
  192. reconnect();
  193. }
  194. client.loop();
  195.  
  196. String payload = "{\"Humidity\":" + String(h) +
  197. ",\"Temp\":" + String(t) +
  198. ",\"GyroTemp\":" + String(Tmp) +
  199. ",\"Hic\":" + String(hic) +
  200. ",\"Volt\":" + String(volt) +
  201. ",\"Shakiness\":" + String(shakiness) +
  202. //",\"AcX\":" + String(AcX) +
  203. //",\"AcY\":" + String(AcY) +
  204. //",\"AcZ\":" + String(AcZ) +
  205. ",\"running\":" + String(report_running) +
  206. "}";
  207.  
  208. delay(2000);
  209.  
  210. Serial.println(payload);
  211. int succ = false;
  212. int attempts = 10;
  213. while (succ == false && attempts-- > 0) {
  214. succ = client.publish(MQTTtopic, payload.c_str());
  215. Serial.print('Sending: ');
  216. Serial.println(succ);
  217. delay(500);
  218. }
  219.  
  220.  
  221. // we're checking if the machine starts again every minute for ten minutus
  222. // after the last detection of movement.
  223. if (debug != true) {
  224. if (romstore[1] < 10) {
  225. ESP.deepSleep(60e6);
  226. }
  227. // sleep 10 minutes.
  228. ESP.deepSleep(600e6);
  229. }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement