safwan092

ESP_1_PIR_MQTT_RPi_Server

Dec 3rd, 2024
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. #include <WiFi.h>
  2. #include <PubSubClient.h>
  3.  
  4. const char* ssid = "BS Home1";
  5. const char* password = "0554418546";
  6. const char* mqtt_server = "raspberrypi.local";
  7.  
  8. WiFiClient espClient;
  9. PubSubClient client(espClient);
  10.  
  11. int sensorVal = 0;
  12. long lastMsg = 0;
  13. #define ledPin 2
  14. #define pirPin 22
  15. void blink_led(unsigned int times, unsigned int duration) {
  16. for (int i = 0; i < times; i++) {
  17. digitalWrite(ledPin, HIGH);
  18. delay(duration);
  19. digitalWrite(ledPin, LOW);
  20. delay(200);
  21. }
  22. }
  23.  
  24. void setup_wifi() {
  25. delay(50);
  26. pinMode(pirPin, INPUT);
  27. Serial.println();
  28. Serial.print("Connecting to ");
  29. Serial.println(ssid);
  30. WiFi.begin(ssid, password);
  31. int c = 0;
  32. while (WiFi.status() != WL_CONNECTED) {
  33. blink_led(2, 200); //blink LED twice (for 200ms ON time) to indicate that wifi not connected
  34. delay(1000); //
  35. Serial.print(".");
  36. c = c + 1;
  37. if (c > 10) {
  38. ESP.restart(); //restart ESP after 10 seconds
  39. }
  40. }
  41. Serial.println("");
  42. Serial.println("WiFi connected");
  43. Serial.println("IP address: ");
  44. Serial.println(WiFi.localIP());
  45. }
  46.  
  47. void connect_mqttServer() {
  48. // Loop until we're reconnected
  49. while (!client.connected()) {
  50. //first check if connected to wifi
  51. if (WiFi.status() != WL_CONNECTED) {
  52. //if not connected, then first connect to wifi
  53. setup_wifi();
  54. }
  55. //now attemt to connect to MQTT server
  56. Serial.print("Attempting MQTT connection...");
  57. // Attempt to connect
  58. if (client.connect("ESP32_client1")) { // Change the name of client here if multiple ESP32 are connected
  59. //attempt successful
  60. Serial.println("connected");
  61. // Subscribe to topics here
  62. client.subscribe("rpi/broadcast");
  63. //client.subscribe("rpi/xyz"); //subscribe more topics here
  64. }
  65. else {
  66. //attempt not successful
  67. Serial.print("failed, rc=");
  68. Serial.print(client.state());
  69. Serial.println(" trying again in 2 seconds");
  70. blink_led(3, 200); //blink LED three times (200ms on duration) to show that MQTT server connection attempt failed
  71. // Wait 2 seconds before retrying
  72. delay(2000);
  73. }
  74. }
  75. }
  76.  
  77. //this function will be executed whenever there is data available on subscribed topics
  78. void callback(char* topic, byte* message, unsigned int length) {
  79. Serial.print("Message arrived on topic: ");
  80. Serial.print(topic);
  81. Serial.print(". Message: ");
  82. String messageTemp;
  83. for (int i = 0; i < length; i++) {
  84. Serial.print((char)message[i]);
  85. messageTemp += (char)message[i];
  86. }
  87. Serial.println();
  88. // Check if a message is received on the topic "rpi/broadcast"
  89. if (String(topic) == "rpi/broadcast") {
  90. if (messageTemp == "10") {
  91. Serial.println("Action: blink LED");
  92. blink_led(1, 1250); //blink LED once (for 1250ms ON time)
  93. }
  94. }
  95. //Similarly add more if statements to check for other subscribed topics
  96. }
  97.  
  98. void setup() {
  99. pinMode(ledPin, OUTPUT);
  100. pinMode(pirPin, INPUT);
  101. Serial.begin(115200);
  102. setup_wifi();
  103. client.setServer(mqtt_server, 1883); //1883 is the default port for MQTT server
  104. client.setCallback(callback);
  105. }
  106.  
  107. void loop() {
  108. sensorVal = digitalRead(pirPin);
  109. String sensorVal_string = String(sensorVal);
  110. Serial.println(sensorVal);
  111. if (sensorVal > 0) {
  112. Serial.println("High Value Motion Detected");
  113. client.publish("esp32/sensor1", sensorVal_string.c_str());
  114. delay(3000);
  115. }
  116. if (!client.connected()) {
  117. connect_mqttServer();
  118. }
  119. client.loop();
  120. long now = millis();
  121. if (now - lastMsg > 500) {
  122. lastMsg = now;
  123. client.publish("esp32/sensor1", sensorVal_string.c_str()); //topic name (to which this ESP32 publishes its data). 88 is the dummy value.
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment