code for LoRaWan mkr 1310 with relay HW-307 and Arduino Pyroelectric Infrared IR PIR…
- User
code for LoRaWan mkr 1310 with relay HW-307 and Arduino Pyroelectric Infrared IR PIR Motion Sensor Detection Module and NORDIC RF24 2.4Ghz Arduino Long Range Wireless Transceiver Module with SMA Antenna - 1100m and MH-Sensor-Series Temperature Sensor ????
- Assistant
Here is an example code for LoRaWan communication using the MKR 1310, the relay HW-307, PIR motion sensor, RF24 wireless transceiver module, and the temperature sensor from the MH-Sensor-Series:
#include <Arduino.h> #include <SPI.h> #include <LoRaWan.h> #include <RH_RF24.h> #include <DHT.h> #define SS_PIN 10 #define RST_PIN 14 #define INTERRUPT_PIN 15 #define DHT_PIN A0 #define PIR_PIN 2 #define RELAY_PIN 3 #define RF24_CE 4 #define RF24_CS 5 // LoRaWan device address (DevAddr) static uint8_t devAddr[] = { 0x11, 0x22, 0x33, 0x44 }; // LoRaWan NwkSKey static uint8_t nwkSKey[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 }; // LoRaWan AppSKey static uint8_t appSKey[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 }; // LoRaWan end-point identifier static uint8_t endDeviceId[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; // RF24 radio instance RH_RF24 rf24(RF24_CE, RF24_CS); // PIR motion sensor instance volatile boolean pirMotionDetected = false; // Relay state variables volatile boolean relayState = LOW; unsigned long lastRelayStateChangeTime = 0; const unsigned long relayDelayTime = 10000; // Temperature and humidity sensor instance DHT dht(DHT_PIN, DHT11); void setup() { Serial.begin(115200); pinMode(PIR_PIN, INPUT); pinMode(RELAY_PIN, OUTPUT); dht.begin(); // Initialize the RF24 module if (!rf24.init()) { Serial.println("RF24 initialization failed"); while (1); } // Initialize LoRaWan LoRaWan.begin(SS_PIN, RST_PIN, INTERRUPT_PIN, devAddr, nwkSKey, appSKey, endDeviceId); // Register PIR interrupt attachInterrupt(digitalPinToInterrupt(PIR_PIN), pirInterrupt, CHANGE); } void loop() { if (pirMotionDetected) { sendMotionData(); delay(100); // Delay to ensure the motion data is sent before sending other data } readAndSendTemperatureHumidityData(); checkRelayState(); // Handle LoRaWan events LoRaWan.loop(); } void onReceive(int size, uint8_t* data) { // Handle received LoRaWan messages if needed } void pirInterrupt() { if (digitalRead(PIR_PIN) == HIGH) { pirMotionDetected = true; } } void sendMotionData() { unsigned long currentMillis = millis(); if (currentMillis - lastRelayStateChangeTime > relayDelayTime) { relayState = HIGH; digitalWrite(RELAY_PIN, relayState); lastRelayStateChangeTime = currentMillis; } // Prepare and send LoRaWan motion data static uint8_t motionData[1]; motionData[0] = 0x01; // Motion detected flag LoRaWan.send(motionData, sizeof(motionData)); pirMotionDetected = false; } void readAndSendTemperatureHumidityData() { float temperature = dht.readTemperature(true); float humidity = dht.readHumidity(); // Prepare and send LoRaWan temperature and humidity data uint8_t tempHumData[4]; tempHumData[0] = 'T'; // Temperature type memcpy(&tempHumData[1], &temperature, sizeof(temperature)); tempHumData[3] = 'H'; // Humidity type memcpy(&tempHumData[4], &humidity, sizeof(humidity)); LoRaWan.send(tempHumData, sizeof(tempHumData)); } void checkRelayState() { if (relayState == HIGH && millis() - lastRelayStateChangeTime > relayDelayTime) { relayState = LOW; digitalWrite(RELAY_PIN, relayState); } }Make sure to install the required libraries (LoRaWan, RH_RF24, and DHT) before using this code. Also, make sure to vary the pin numbers according to your specific wiring configuration.
This code sets up LoRaWan communication using the MKR 1310 and sends data from the PIR motion sensor (HW-307 relay will be triggered when motion is detected), the temperature sensor, and the humidity sensor. The RF24 module is not used for LoRaWan communication; it's separated and can communicate in 2.4 GHz for other purposes.