Advertisement
pleasedontcode

"WiFi MQTT Setup" rev_01

Mar 6th, 2024
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "WiFi MQTT Setup"
  13.     - Source Code compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-03-06 15:45:13
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* code for esp32 to control relay using manual */
  21.     /* switch and cloud usding mqtt */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /****** DEFINITION OF LIBRARIES *****/
  25.  
  26. // Include the required libraries
  27. #include <WiFi.h>
  28. #include <PubSubClient.h>
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33. void initializeWiFi(void);
  34. void connectToMQTTBroker(void);
  35. void mqttCallback(char* topic, byte* payload, unsigned int length);
  36.  
  37. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  38. const uint8_t relay_PIN_D4 = 4;
  39.  
  40. /***** WIFI CREDENTIALS *****/
  41. const char* ssid = "Your SSID";
  42. const char* password = "Your Password";
  43.  
  44. /***** MQTT BROKER DETAILS *****/
  45. const char* mqttBroker = "your-mqtt-broker";
  46. const char* mqttUsername = "mqtt-username";
  47. const char* mqttPassword = "mqtt-password";
  48. const int mqttPort = 1883;
  49.  
  50. // Create an instance of the WiFiClient class
  51. WiFiClient wifiClient;
  52.  
  53. // Create an instance of the PubSubClient class
  54. PubSubClient client(wifiClient);
  55.  
  56. void setup(void)
  57. {
  58.   // put your setup code here, to run once:
  59.  
  60.   // Initialize serial communication
  61.   Serial.begin(115200);
  62.  
  63.   // Initialize WiFi connection
  64.   initializeWiFi();
  65.  
  66.   // Connect to the MQTT broker
  67.   connectToMQTTBroker();
  68.  
  69.   // Set the relay pin as input
  70.   pinMode(relay_PIN_D4, INPUT);
  71. }
  72.  
  73. void loop(void)
  74. {
  75.   // put your main code here, to run repeatedly:
  76.  
  77.   // Check for any incoming messages
  78.   client.loop();
  79.  
  80.   // TODO: Add your relay control logic using manual switch here
  81.  
  82.   // TODO: Add your relay control logic using cloud (MQTT) here
  83. }
  84.  
  85. void initializeWiFi(void)
  86. {
  87.   // Connect to WiFi network
  88.   Serial.print("Connecting to WiFi...");
  89.   WiFi.begin(ssid, password);
  90.  
  91.   // Wait until connected to the WiFi network
  92.   while (WiFi.status() != WL_CONNECTED)
  93.   {
  94.     delay(1000);
  95.     Serial.print(".");
  96.   }
  97.  
  98.   Serial.println();
  99.   Serial.println("WiFi connected");
  100.   Serial.println("IP address: ");
  101.   Serial.println(WiFi.localIP());
  102. }
  103.  
  104. void connectToMQTTBroker(void)
  105. {
  106.   // Set the MQTT broker and port
  107.   client.setServer(mqttBroker, mqttPort);
  108.  
  109.   // Set the MQTT callback function
  110.   client.setCallback(mqttCallback);
  111.  
  112.   // Connect to the MQTT broker
  113.   while (!client.connected())
  114.   {
  115.     Serial.print("Connecting to MQTT broker...");
  116.     // Connect to the MQTT broker with username and password
  117.     if (client.connect("ESP32Client", mqttUsername, mqttPassword))
  118.     {
  119.       Serial.println("Connected to MQTT broker");
  120.     }
  121.     else
  122.     {
  123.       // Failed to connect to the MQTT broker, retry after 5 seconds
  124.       Serial.print("Failed to connect to MQTT broker, retrying in 5 seconds...");
  125.       delay(5000);
  126.     }
  127.   }
  128. }
  129.  
  130. void mqttCallback(char* topic, byte* payload, unsigned int length)
  131. {
  132.   // TODO: Add your MQTT callback logic here
  133. }
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement