Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFi.h>
- #include <PubSubClient.h>
- const char* ssid = "BS Home1";
- const char* password = "0554418546";
- const char* mqtt_server = "raspberrypi.local";
- WiFiClient espClient;
- PubSubClient client(espClient);
- int sensorVal = 0;
- long lastMsg = 0;
- #define ledPin 2
- #define pirPin 22
- void blink_led(unsigned int times, unsigned int duration) {
- for (int i = 0; i < times; i++) {
- digitalWrite(ledPin, HIGH);
- delay(duration);
- digitalWrite(ledPin, LOW);
- delay(200);
- }
- }
- void setup_wifi() {
- delay(50);
- pinMode(pirPin, INPUT);
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- int c = 0;
- while (WiFi.status() != WL_CONNECTED) {
- blink_led(2, 200); //blink LED twice (for 200ms ON time) to indicate that wifi not connected
- delay(1000); //
- Serial.print(".");
- c = c + 1;
- if (c > 10) {
- ESP.restart(); //restart ESP after 10 seconds
- }
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- }
- void connect_mqttServer() {
- // Loop until we're reconnected
- while (!client.connected()) {
- //first check if connected to wifi
- if (WiFi.status() != WL_CONNECTED) {
- //if not connected, then first connect to wifi
- setup_wifi();
- }
- //now attemt to connect to MQTT server
- Serial.print("Attempting MQTT connection...");
- // Attempt to connect
- if (client.connect("ESP32_client1")) { // Change the name of client here if multiple ESP32 are connected
- //attempt successful
- Serial.println("connected");
- // Subscribe to topics here
- client.subscribe("rpi/broadcast");
- //client.subscribe("rpi/xyz"); //subscribe more topics here
- }
- else {
- //attempt not successful
- Serial.print("failed, rc=");
- Serial.print(client.state());
- Serial.println(" trying again in 2 seconds");
- blink_led(3, 200); //blink LED three times (200ms on duration) to show that MQTT server connection attempt failed
- // Wait 2 seconds before retrying
- delay(2000);
- }
- }
- }
- //this function will be executed whenever there is data available on subscribed topics
- void callback(char* topic, byte* message, unsigned int length) {
- Serial.print("Message arrived on topic: ");
- Serial.print(topic);
- Serial.print(". Message: ");
- String messageTemp;
- for (int i = 0; i < length; i++) {
- Serial.print((char)message[i]);
- messageTemp += (char)message[i];
- }
- Serial.println();
- // Check if a message is received on the topic "rpi/broadcast"
- if (String(topic) == "rpi/broadcast") {
- if (messageTemp == "10") {
- Serial.println("Action: blink LED");
- blink_led(1, 1250); //blink LED once (for 1250ms ON time)
- }
- }
- //Similarly add more if statements to check for other subscribed topics
- }
- void setup() {
- pinMode(ledPin, OUTPUT);
- pinMode(pirPin, INPUT);
- Serial.begin(115200);
- setup_wifi();
- client.setServer(mqtt_server, 1883); //1883 is the default port for MQTT server
- client.setCallback(callback);
- }
- void loop() {
- sensorVal = digitalRead(pirPin);
- String sensorVal_string = String(sensorVal);
- Serial.println(sensorVal);
- if (sensorVal > 0) {
- Serial.println("High Value Motion Detected");
- client.publish("esp32/sensor1", sensorVal_string.c_str());
- delay(3000);
- }
- if (!client.connected()) {
- connect_mqttServer();
- }
- client.loop();
- long now = millis();
- if (now - lastMsg > 500) {
- lastMsg = now;
- client.publish("esp32/sensor1", sensorVal_string.c_str()); //topic name (to which this ESP32 publishes its data). 88 is the dummy value.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment