Advertisement
pleasedontcode

**WiFi MQTT** rev_01

Jan 27th, 2025
59
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**
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-01-27 15:11:27
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The project aims to develop a responsive Arduino */
  21.     /* application utilizing connected components for */
  22.     /* real-time data processing and user interaction. It */
  23.     /* should support modular library integration for */
  24.     /* enhanced functionality. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /* START CODE */
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <WiFi.h>
  31. #include <HTTPClient.h>
  32. #include <ArduinoJson.h>
  33. #include <PubSubClient.h>
  34.  
  35. /****** FUNCTION PROTOTYPES *****/
  36. void setup(void);
  37. void loop(void);
  38.  
  39. // WiFi credentials
  40. const char* ssid = "your_SSID"; // Replace with your network SSID
  41. const char* password = "your_PASSWORD"; // Replace with your network password
  42.  
  43. // MQTT broker details
  44. const char* mqtt_server = "broker.hivemq.com"; // Replace with your MQTT broker address
  45. WiFiClient espClient;
  46. PubSubClient client(espClient);
  47.  
  48. /****** FUNCTION IMPLEMENTATIONS *****/
  49. void setup(void)
  50. {
  51.     // Initialize Serial Monitor
  52.     Serial.begin(115200);
  53.    
  54.     // Connect to WiFi
  55.     WiFi.begin(ssid, password);
  56.     while (WiFi.status() != WL_CONNECTED) {
  57.         delay(500);
  58.         Serial.print(".");
  59.     }
  60.     Serial.println("Connected to WiFi");
  61.  
  62.     // Setup MQTT client
  63.     client.setServer(mqtt_server, 1883);
  64. }
  65.  
  66. void loop(void)
  67. {
  68.     // Ensure the MQTT client is connected
  69.     if (!client.connected()) {
  70.         // Attempt to reconnect
  71.         if (client.connect("ESP32Client")) {
  72.             Serial.println("Connected to MQTT broker");
  73.         } else {
  74.             Serial.print("Failed to connect to MQTT broker, state: ");
  75.             Serial.println(client.state());
  76.             delay(2000);
  77.         }
  78.     }
  79.    
  80.     // Handle MQTT client loop
  81.     client.loop();
  82.    
  83.     // Example: Publish a message
  84.     StaticJsonDocument<200> doc;
  85.     doc["message"] = "Hello from ESP32!";
  86.     char jsonBuffer[512];
  87.     serializeJson(doc, jsonBuffer);
  88.    
  89.     client.publish("test/topic", jsonBuffer);
  90.    
  91.     delay(5000); // Publish every 5 seconds
  92. }
  93.  
  94. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement