pleasedontcode

Sensor Publisher rev_02

Aug 4th, 2025
469
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: Sensor Publisher
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2025-08-04 13:33:56
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Publicar las variables de temperatura y humedad en */
  21.     /* formato JSON mediante MQTT, usando la librería DHT */
  22.     /* y el pin D2 del Arduino Nano ESP32. */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /* START CODE */
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <DHT.h>  // https://github.com/adafruit/DHT-sensor-library
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32. void publishSensorData(float temperature, float humidity);
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t TempHum_DHT22_DOUT_PIN_D2 = 2;
  36.  
  37. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  38. DHT dht(TempHum_DHT22_DOUT_PIN_D2, DHT22);
  39.  
  40. // Placeholder for MQTT client object
  41. // Include your MQTT client library and instantiate the client here
  42. // For example:
  43. // #include <PubSubClient.h>
  44. // WiFiClient espClient;
  45. // PubSubClient mqttClient(espClient);
  46.  
  47. void setup(void)
  48. {
  49.     // put your setup code here, to run once:
  50.     pinMode(TempHum_DHT22_DOUT_PIN_D2, INPUT_PULLUP);
  51.     dht.begin();
  52.  
  53.     // Initialize MQTT connection here
  54.     // For example:
  55.     // mqttClient.setServer(mqtt_server, mqtt_port);
  56.     // connectMQTT();
  57. }
  58.  
  59. void loop(void)
  60. {
  61.     // put your main code here, to run repeatedly:
  62.     // Read temperature and humidity
  63.     float temperature = dht.readTemperature(); // Celsius
  64.     float humidity = dht.readHumidity();
  65.  
  66.     // Check if any reads failed and handle errors
  67.     if (isnan(temperature) || isnan(humidity)) {
  68.         // Handle sensor read error
  69.         return;
  70.     }
  71.  
  72.     // Publish sensor data in JSON format
  73.     publishSensorData(temperature, humidity);
  74.  
  75.     // Delay between readings, e.g., 2 seconds
  76.     delay(2000);
  77. }
  78.  
  79. void publishSensorData(float temperature, float humidity)
  80. {
  81.     // Format JSON string
  82.     char jsonBuffer[128];
  83.     snprintf(jsonBuffer, sizeof(jsonBuffer), "{\"temperature\":%.2f,\"humidity\":%.2f}", temperature, humidity);
  84.  
  85.     // Publish via MQTT
  86.     // Replace 'mqttClient' with your MQTT client object
  87.     // Example:
  88.     // if (mqttClient.connected()) {
  89.     //     mqttClient.publish("sensor/data", jsonBuffer);
  90.     // }
  91.     // For this example, we'll just print to Serial
  92.     Serial.println(jsonBuffer);
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment