Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define BLYNK_TEMPLATE_ID "******"
- #define BLYNK_TEMPLATE_NAME "Smart Agricole PFE"
- #define BLYNK_AUTH_TOKEN "**********"
- #include <ESP8266WiFi.h>
- #include <BlynkSimpleEsp8266.h>
- #include <SPI.h>
- #include <LoRa.h>
- // Wi-Fi credentials
- char ssid[] = "******"; // Your Wi-Fi network name (SSID)
- char pass[] = "*****"; // Your Wi-Fi network password
- // LoRa configuration
- #define SCK D5
- #define MISO D6
- #define MOSI D7
- #define SS D8
- #define RST D0
- #define BAND 433E6
- // Relay pin configuration
- #define RELAY_PIN D1
- // Virtual Pin for Blynk
- #define BLYNK_VIRTUAL_PIN_SWITCH V0
- #define BLYNK_VIRTUAL_PIN_MOISTURE V1
- #define BLYNK_VIRTUAL_PIN_MODE V2
- const String expectedNetworkID = "MedSalhi"; // Ensure this matches the sender's network ID
- bool manualMode = false;
- void setup() {
- pinMode(LED_BUILTIN, OUTPUT);
- digitalWrite(LED_BUILTIN, HIGH);
- pinMode(RELAY_PIN, OUTPUT);
- digitalWrite(RELAY_PIN, LOW); // Ensure the relay is initially off
- Serial.begin(115200);
- WiFi.begin(ssid, pass);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("WiFi connected");
- // Start Blynk connection (using template ID and name)
- Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
- SPI.pins(SCK, MISO, MOSI, SS);
- LoRa.setPins(SS, RST, D0);
- if (!LoRa.begin(BAND)) {
- Serial.println("Starting LoRa failed!");
- while (1);
- }
- Serial.println("LoRa initialized successfully");
- // Ensure relay is off initially
- digitalWrite(RELAY_PIN, LOW);
- }
- // Blynk button widget handler
- BLYNK_WRITE(BLYNK_VIRTUAL_PIN_SWITCH) {
- int pinValue = param.asInt(); // Get the switch state
- Serial.print("Blynk button control received: ");
- Serial.println(pinValue);
- digitalWrite(RELAY_PIN, pinValue); // Control the relay directly
- Serial.print("Relay state: ");
- Serial.println(digitalRead(RELAY_PIN));
- }
- // Blynk mode switch handler
- BLYNK_WRITE(BLYNK_VIRTUAL_PIN_MODE) {
- int pinValue = param.asInt(); // Get the switch state
- manualMode = (pinValue == 1);
- Serial.print("Manual mode: ");
- Serial.println(manualMode ? "ON" : "OFF");
- }
- void loop() {
- Blynk.run(); // Keep Blynk connection alive
- int packetSize = LoRa.parsePacket();
- if (packetSize) {
- String receivedMessage = "";
- while (LoRa.available()) {
- receivedMessage += (char)LoRa.read();
- }
- Serial.print("Received packet: ");
- Serial.println(receivedMessage);
- // Split the message to get network ID and moisture level
- int separatorIndex = receivedMessage.indexOf(':');
- if (separatorIndex == -1) {
- Serial.println("Invalid packet format");
- return;
- }
- String receivedNetworkID = receivedMessage.substring(0, separatorIndex);
- float moisturePercent = receivedMessage.substring(separatorIndex + 1).toFloat();
- if (receivedNetworkID == expectedNetworkID) {
- Blynk.virtualWrite(BLYNK_VIRTUAL_PIN_MOISTURE, moisturePercent);
- // Blink the built-in LED for 1 ms to indicate packet reception
- digitalWrite(LED_BUILTIN, LOW);
- delay(100);
- digitalWrite(LED_BUILTIN, HIGH);
- // Automatic relay control based on moisture value
- if (!manualMode) {
- if (moisturePercent < 30) {
- Serial.println("Moisture < 30%, turning relay ON");
- digitalWrite(RELAY_PIN, LOW);
- } else if (moisturePercent >= 80) {
- Serial.println("Moisture >= 80%, turning relay OFF");
- digitalWrite(RELAY_PIN, HIGH);
- }
- Serial.print("Relay state: ");
- Serial.println(digitalRead(RELAY_PIN));
- }
- } else {
- Serial.println("Received packet from unknown network ID.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement