Source Code Erschütterungssensor MQTT: Unterschied zwischen den Versionen
(Die Seite wurde neu angelegt: „<pre> /******************************************** * 18 Mar 2021 FG * * Code für das Senden von MQTT Nachrichten nach * der Registierung einer Erschüt…“) |
(kein Unterschied)
|
Version vom 13. Juni 2021, 14:12 Uhr
/******************************************** * 18 Mar 2021 FG * * Code für das Senden von MQTT Nachrichten nach * der Registierung einer Erschütterung mittels * client.publish("mein Topic", "meine Nachricht") * Wobei "mein Topic" für unser Demo "workshop" lauten * sollte * * D8 ---> LED mit Schutzwiderstand anschließen * D1 ---> SCL am Bewegungssensor anschließen * D2 ---> SDA am Bewegungssensor anschließen * D7 ---> SCL am Bewegungssensor anschließen * 3.3V -> VCC am Bewegungssensor anschließen * G ----> GND am Bewegungssensor anschließen * *********************************************/ #include <ESP8266WiFi.h> #include <PubSubClient.h> #include "Wire.h" #define MPU6050_ADDR 0x68 // Alternatively set AD0 to HIGH --> Address = 0x69 #define MPU6050_ACCEL_CONFIG 0x1C // Accelerometer Configuration Register #define MPU6050_PWR_MGT_1 0x6B // Power Management 1 Register #define MPU6050_INT_PIN_CFG 0x37 // Interrupt Pin / Bypass Enable Configuration Register #define MPU6050_INT_ENABLE 0x38 // Interrupt Enable Register #define MPU6050_LATCH_INT_EN 0x05 // Latch Enable Bit for Interrupt #define MPU6050_ACTL 0x07 // Active-Low Enable Bit #define MPU6050_WOM_EN 0x06 // Wake on Motion Enable bit #define MPU6050_WOM_THR 0x1F // Wake on Motion Threshold Register #define MPU6050_MOT_DUR 0x20 // Motion Detection Duration Register #define MPU6050_ACCEL_INTEL_CTRL 0x69 // Accelaration Interrupt Control Register #define MPU6050_SIGNAL_PATH_RESET 0x68 // Signal Path Reset Register byte interruptPin=13; byte ledPin=D8; long timestamp = 0; volatile bool accEvent = false; // KONFIGURATION DES INTERNET-ZUGANGS // ============================================ const char* ssid = "FRITZ!Box Fon WLAN 7360"; const char* password = "..."; String clientId = "ESP8266Client-Box01-"; // ============================================ const char* mqtt_server = "62.75.187.126"; const char* myTobic = "workshop"; WiFiClient espClient; void callback(char* topic, byte* payload, unsigned int length) { } PubSubClient client(espClient); unsigned long lastMsg = 0; #define MSG_BUFFER_SIZE (50) char msg[MSG_BUFFER_SIZE]; int value = 0; void setup_wifi() { delay(10); // We start by connecting to a WiFi network Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } randomSeed(micros()); Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial.print("Attempting MQTT connection..."); if (client.connect(clientId.c_str())) { Serial.println("connected. now subscribing..."); client.subscribe("LED"); String sendThis = clientId + ": connecting..."; client.publish(myTobic, sendThis.c_str()); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying delay(5000); } } } void setup() { Wire.begin(); writeRegister(MPU6050_PWR_MGT_1, 0); setInterrupt(1); // set Wake on Motion Interrupt / Sensitivity; 1(highest sensitivity) - 255 clientId += String(random(0xffff), HEX); pinMode(D3, INPUT_PULLUP); // Hier den Taster anschließen pinMode(ledPin, OUTPUT); pinMode(interruptPin, INPUT); attachInterrupt(digitalPinToInterrupt(interruptPin), motion, RISING); pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output Serial.begin(9600); setup_wifi(); client.setServer(mqtt_server, 1883); client.setCallback(callback); } void loop() { if (!client.connected()) { reconnect(); } client.loop(); // Das ist der Teil des Codes, wo beim Festellen einer // Erschütterung eine Nachricht abgesetzt wirde if(accEvent){ Serial.print("Publishing message: acc"); client.publish(myTobic, "ICH HABE EINE BEWEGUNG BEMERKT"); digitalWrite(ledPin, HIGH); delay(1000); digitalWrite(ledPin, LOW); accEvent = false; attachInterrupt(digitalPinToInterrupt(interruptPin), motion, RISING); } } void setInterrupt(byte threshold){ //writeRegister(MPU6050_SIGNAL_PATH_RESET, 0b00000111); // not(?) needed //writeRegister(MPU6050_INT_PIN_CFG, 1<<MPU6050_ACTL); // 1<<MPU6050_LATCH_INT_EN writeRegister(MPU6050_ACCEL_CONFIG, 0b00000001); writeRegister(MPU6050_WOM_THR, threshold); writeRegister(MPU6050_MOT_DUR, 0b00000001); // set duration (LSB = 1 ms) //writeRegister(MPU6050_ACCEL_INTEL_CTRL, 0x15); // not needed (?) writeRegister(MPU6050_INT_ENABLE, 1<<MPU6050_WOM_EN); } void writeRegister(uint16_t reg, byte value){ Wire.beginTransmission(MPU6050_ADDR); Wire.write(reg); Wire.write(value); Wire.endTransmission(); } void motion(){ accEvent = true; detachInterrupt(interruptPin); }