Advertisement
Guest User

Untitled

a guest
Jul 31st, 2024
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. #define BLYNK_TEMPLATE_ID "******"
  2. #define BLYNK_TEMPLATE_NAME "Smart Agricole PFE"
  3. #define BLYNK_AUTH_TOKEN "**********"
  4.  
  5. #include <ESP8266WiFi.h>
  6. #include <BlynkSimpleEsp8266.h>
  7. #include <SPI.h>
  8. #include <LoRa.h>
  9.  
  10. // Wi-Fi credentials
  11. char ssid[] = "******"; // Your Wi-Fi network name (SSID)
  12. char pass[] = "*****"; // Your Wi-Fi network password
  13.  
  14. // LoRa configuration
  15. #define SCK D5
  16. #define MISO D6
  17. #define MOSI D7
  18. #define SS D8
  19. #define RST D0
  20. #define BAND 433E6
  21.  
  22. // Relay pin configuration
  23. #define RELAY_PIN D1
  24.  
  25. // Virtual Pin for Blynk
  26. #define BLYNK_VIRTUAL_PIN_SWITCH V0
  27. #define BLYNK_VIRTUAL_PIN_MOISTURE V1
  28. #define BLYNK_VIRTUAL_PIN_MODE V2
  29.  
  30. const String expectedNetworkID = "MedSalhi"; // Ensure this matches the sender's network ID
  31.  
  32. bool manualMode = false;
  33.  
  34. void setup() {
  35. pinMode(LED_BUILTIN, OUTPUT);
  36. digitalWrite(LED_BUILTIN, HIGH);
  37. pinMode(RELAY_PIN, OUTPUT);
  38. digitalWrite(RELAY_PIN, LOW); // Ensure the relay is initially off
  39.  
  40. Serial.begin(115200);
  41.  
  42. WiFi.begin(ssid, pass);
  43. while (WiFi.status() != WL_CONNECTED) {
  44. delay(500);
  45. Serial.print(".");
  46. }
  47. Serial.println("WiFi connected");
  48.  
  49. // Start Blynk connection (using template ID and name)
  50. Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  51.  
  52. SPI.pins(SCK, MISO, MOSI, SS);
  53. LoRa.setPins(SS, RST, D0);
  54.  
  55. if (!LoRa.begin(BAND)) {
  56. Serial.println("Starting LoRa failed!");
  57. while (1);
  58. }
  59. Serial.println("LoRa initialized successfully");
  60.  
  61. // Ensure relay is off initially
  62. digitalWrite(RELAY_PIN, LOW);
  63. }
  64.  
  65. // Blynk button widget handler
  66. BLYNK_WRITE(BLYNK_VIRTUAL_PIN_SWITCH) {
  67. int pinValue = param.asInt(); // Get the switch state
  68. Serial.print("Blynk button control received: ");
  69. Serial.println(pinValue);
  70. digitalWrite(RELAY_PIN, pinValue); // Control the relay directly
  71. Serial.print("Relay state: ");
  72. Serial.println(digitalRead(RELAY_PIN));
  73. }
  74.  
  75. // Blynk mode switch handler
  76. BLYNK_WRITE(BLYNK_VIRTUAL_PIN_MODE) {
  77. int pinValue = param.asInt(); // Get the switch state
  78. manualMode = (pinValue == 1);
  79. Serial.print("Manual mode: ");
  80. Serial.println(manualMode ? "ON" : "OFF");
  81. }
  82.  
  83. void loop() {
  84. Blynk.run(); // Keep Blynk connection alive
  85.  
  86. int packetSize = LoRa.parsePacket();
  87. if (packetSize) {
  88. String receivedMessage = "";
  89. while (LoRa.available()) {
  90. receivedMessage += (char)LoRa.read();
  91. }
  92.  
  93. Serial.print("Received packet: ");
  94. Serial.println(receivedMessage);
  95.  
  96. // Split the message to get network ID and moisture level
  97. int separatorIndex = receivedMessage.indexOf(':');
  98. if (separatorIndex == -1) {
  99. Serial.println("Invalid packet format");
  100. return;
  101. }
  102.  
  103. String receivedNetworkID = receivedMessage.substring(0, separatorIndex);
  104. float moisturePercent = receivedMessage.substring(separatorIndex + 1).toFloat();
  105.  
  106. if (receivedNetworkID == expectedNetworkID) {
  107. Blynk.virtualWrite(BLYNK_VIRTUAL_PIN_MOISTURE, moisturePercent);
  108.  
  109. // Blink the built-in LED for 1 ms to indicate packet reception
  110. digitalWrite(LED_BUILTIN, LOW);
  111. delay(100);
  112. digitalWrite(LED_BUILTIN, HIGH);
  113.  
  114. // Automatic relay control based on moisture value
  115. if (!manualMode) {
  116. if (moisturePercent < 30) {
  117. Serial.println("Moisture < 30%, turning relay ON");
  118. digitalWrite(RELAY_PIN, LOW);
  119. } else if (moisturePercent >= 80) {
  120. Serial.println("Moisture >= 80%, turning relay OFF");
  121. digitalWrite(RELAY_PIN, HIGH);
  122. }
  123. Serial.print("Relay state: ");
  124. Serial.println(digitalRead(RELAY_PIN));
  125. }
  126. } else {
  127. Serial.println("Received packet from unknown network ID.");
  128. }
  129. }
  130. }
  131.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement