Forum: Mikrocontroller und Digitale Elektronik Vibrationen mit MCU6050 registrieren - nicht zuverlässig


von Kolja L. (kolja82)


Lesenswert?

Moin

Weiterführung des alten Bastelprojektes: Waschmaschinen Überwachung via 
IoT.

Hardware ist simpel: NodeMCU und zwei MCU6050 auf GY521 Boards.
Sensoren bekommen 3.3V aus der NodeMCU und sind an D1 und D2 via I2C 
angeschlossen.

Prinzipiell funktioniert das Konstrukt auch, ist ja super sensitiv!

Aber leider nicht zuverlässig:

Manchmal muss die NodeMCU nach dem Flaschen aus und wieder eingestekt 
werden, sonst werden keine Werte gemessen, ein Reset reicht nicht.

Bei der Übermittlung der Messwerte gibt es immer wieder Aussetzer, oder 
es werden nur Einsen als Wert übermittelt.

Ich kann mir das leider überhaupt nicht erklären, daher die Bitte an 
Euch,
mal über meinen Code zu schauen:
1
#include <Wire.h> // I2C Lib#include "Wire.h" // I2C Lib
2
#include <ESP8266WebServer.h> //WLAN
3
// https://github.com/griegerc/arduino-gy521/blob/master/gy521-read-raw/gy521-read-raw.ino
4
5
6
const int WAMA_1 = 0x69;
7
const int WAMA_2 = 0x68;
8
int16_t gyrX, gyrY, gyrZ, high_1, high_2;
9
int ausgabe_1 = 0;
10
int ausgabe_2 = 0;
11
unsigned long previousMillis = 0;
12
unsigned long currentMillis = 0;
13
14
/* WLAN */
15
const char* ssid      = "xxx";
16
const char* password  = "xxx";
17
const char* host      = "www.xxx.de";
18
const char* sendto    = "collectdata.php";
19
ESP8266WebServer server(80);
20
/* WLAN */
21
22
void setup()
23
{
24
25
  Serial.begin(115200);
26
  Serial.flush();
27
  Serial.println("Setup: ");
28
29
  /* I2C */
30
  Wire.begin();
31
  ////////////////////////// WAMA EINS  /////////////////////
32
  check_I2c(WAMA_1);
33
  Wire.beginTransmission(WAMA_1); // Begins a transmission to the I2C slave (GY-521 board)
34
  Wire.write(0x6B); // PWR_MGMT_1 register
35
  Wire.write(0); // set to zero (wakes up the MPU-6050)
36
  Wire.endTransmission(true);
37
38
  Wire.beginTransmission(WAMA_1);
39
  Wire.write(0x43);
40
  Wire.endTransmission(false);
41
  Wire.requestFrom(WAMA_1, 6, true);
42
  gyrX = Wire.read() << 8 | Wire.read();
43
  gyrY = Wire.read() << 8 | Wire.read();
44
  gyrZ = Wire.read() << 8 | Wire.read();
45
  high_1 = max(max(abs(gyrX), abs(gyrY)), abs(gyrZ));
46
  Serial.println(high_1);
47
  delay(5);
48
49
50
  ////////////////////////// WAMA ZWEI  /////////////////////
51
  check_I2c(WAMA_2);
52
  Wire.beginTransmission(WAMA_2); // Begins a transmission to the I2C slave (GY-521 board)
53
  Wire.write(0x6B); // PWR_MGMT_1 register
54
  Wire.write(0); // set to zero (wakes up the MPU-6050)
55
  Wire.endTransmission(true);
56
57
  Wire.beginTransmission(WAMA_2);
58
  Wire.write(0x43);
59
  Wire.endTransmission(false);
60
  Wire.requestFrom(WAMA_2, 6, true);
61
  gyrX = Wire.read() << 8 | Wire.read();
62
  gyrY = Wire.read() << 8 | Wire.read();
63
  gyrZ = Wire.read() << 8 | Wire.read();
64
  high_2 = max(max(abs(gyrX), abs(gyrY)), abs(gyrZ));
65
  Serial.println(high_2);
66
  // TEST
67
68
69
70
  /* WLAN */
71
  delay (500);
72
  WiFi.begin(ssid, password);
73
  Serial.println("");
74
  while (WiFi.status() != WL_CONNECTED) {
75
    delay(250);
76
    Serial.print(".");
77
  }
78
  Serial.println(".");
79
  Serial.println(WiFi.localIP());
80
  /* WLAN */
81
82
83
84
}
85
86
///////////// SCAN  ///////////////////////
87
void loop()
88
{
89
  currentMillis = millis();
90
  // Serial.println(currentMillis);
91
92
  if (currentMillis - previousMillis <= 10000) { 
93
94
    ////////////////////////// WAMA EINS  /////////////////////
95
    Wire.beginTransmission(WAMA_1);
96
    Wire.write(0x43);
97
    Wire.endTransmission(false);
98
    Wire.requestFrom(WAMA_1, 6, true);
99
    gyrX = Wire.read() << 8 | Wire.read();
100
    gyrY = Wire.read() << 8 | Wire.read();
101
    gyrZ = Wire.read() << 8 | Wire.read();
102
    high_1 = max(max(abs(gyrX), abs(gyrY)), abs(gyrZ));
103
    //  Serial.println(high_1);
104
    if (high_1 > ausgabe_1) {
105
      ausgabe_1 = high_1;
106
      //   Serial.println(ausgabe_1);
107
    }
108
    //delay(5);
109
110
    ////////////////////////// WAMA ZWEI  /////////////////////
111
    Wire.beginTransmission(WAMA_2);
112
    Wire.write(0x43);
113
    Wire.endTransmission(false);
114
    Wire.requestFrom(WAMA_2, 6, true);
115
    gyrX = Wire.read() << 8 | Wire.read();
116
    gyrY = Wire.read() << 8 | Wire.read();
117
    gyrZ = Wire.read() << 8 | Wire.read();
118
    high_2 = max(max(abs(gyrX), abs(gyrY)), abs(gyrZ));
119
    //  Serial.println(high_2);
120
    if (high_2 > ausgabe_2) {
121
      ausgabe_2 = high_2;
122
      //    Serial.println(ausgabe_2);
123
    }
124
    yield();
125
    //   delay(5);
126
  } // millis
127
128
129
130
131
132
  ////////////////  AUSGABE  ////////////////////
133
  else  {
134
    WiFiClient client;
135
    const int httpPort = 80;
136
    if (!client.connect(host, httpPort)) {
137
      Serial.println("connection failed");
138
      return;
139
    }
140
    // URL zusammenbauen
141
    String url = "/wama/";
142
    url += sendto;
143
    url += "?wama_1=";
144
    url += ausgabe_1;
145
    url += "&wama_2=";
146
    url += ausgabe_2;
147
148
    client.println(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
149
    Serial.println(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
150
151
    delay(10);
152
153
    unsigned long timeout = millis();
154
    while (client.available() == 0) {
155
      if (millis() - timeout > 5000) {
156
        Serial.println(">>> Client Timeout !");
157
        client.stop();
158
        return;
159
      }
160
    }
161
    while (client.available()) {
162
      String line = client.readStringUntil('\r');
163
         Serial.print(line); // Antwort vom Server
164
    }
165
166
    //Serial.print(ausgabe_1);
167
    //Serial.print(" ");
168
    //Serial.println(ausgabe_2);
169
    previousMillis = currentMillis;
170
    ausgabe_1 = 0;
171
    ausgabe_2 = 0;
172
  }
173
} //loop
174
175
176
177
178
179
180
// I2C Adressen prüfen
181
byte check_I2c(byte addr) {  // We are using the return value of the Write.endTransmisstion to see if a device did acknowledge to the address.
182
  byte error;
183
  Wire.beginTransmission(addr);
184
  error = Wire.endTransmission();
185
  Serial.println();
186
187
  if (error == 0)
188
  {
189
    Serial.print(" Device Found at 0x");
190
    Serial.println(addr, HEX);
191
  }
192
  else
193
  {
194
    Serial.print(" No Device Found at 0x");
195
    Serial.println(addr, HEX);
196
  }
197
  return error;
198
}

Die Idee hinter dem Skript:

Es werden abwechselnd die beiden Sensoren (nur die Gyroskop Werte) 
abgefragt und der jeweils größte gemessene Wert gespeichert.
Nach dem Intervall (von 10 Sekunden) wird dieser Wert an die Webseite 
gesendet.

Bin auch für andere Ideen offen, hätte nur gerne zwei Graphen, die die 
Intensität der Vibrationen von den Waschmaschinen deutlich machen.
Also nicht nur "an" und "aus", sondern von Tür zuschlagen bis 
Schleudergang :-)

Danke und Gruß

Kolja

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.