techposts

SpanPoint-Receiver-Temp_Hum

Mar 1st, 2025
566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.81 KB | Source Code | 0 0
  1. #include "HomeSpan.h"
  2.  
  3. // Structure for sensor data that will be received
  4. struct sensor_data {
  5.   float temperature;
  6.   float humidity;
  7.   int batteryLevel;
  8. };
  9.  
  10. // Remote sensor implementation following official example
  11. struct RemoteSensor : Service::TemperatureSensor {
  12.  
  13.   SpanCharacteristic *temp;           // Temperature characteristic
  14.   SpanCharacteristic *tempFault;      // Temperature fault status
  15.  
  16.   SpanService *humService;            // Humidity service
  17.   SpanCharacteristic *hum;            // Humidity characteristic
  18.   SpanCharacteristic *humFault;       // Humidity fault status
  19.  
  20.   SpanService *batService;            // Battery service
  21.   SpanCharacteristic *batLevel;       // Battery level
  22.   SpanCharacteristic *batLow;         // Low battery status
  23.  
  24.   SpanPoint *sensorPoint;             // SpanPoint for communication
  25.   const char *sensorName;             // Name of this sensor
  26.   sensor_data sensorData;             // Data structure for incoming data
  27.  
  28.   // Constructor
  29.   RemoteSensor(const char *name, const char *macAddress) : Service::TemperatureSensor() {
  30.     sensorName = name;
  31.    
  32.     // Configure Temperature Service (this service)
  33.     temp = new Characteristic::CurrentTemperature(20.0);
  34.     temp->setRange(-50, 100);
  35.     tempFault = new Characteristic::StatusFault(1);  // Start with fault
  36.     new Characteristic::Name(name);
  37.    
  38.     // Create Humidity Service
  39.     humService = new Service::HumiditySensor();
  40.       hum = (new Characteristic::CurrentRelativeHumidity(50.0))->setRange(0, 100);
  41.       humFault = new Characteristic::StatusFault(1);
  42.      
  43.     // Create Battery Service  
  44.     batService = new Service::BatteryService();
  45.       batLevel = new Characteristic::BatteryLevel(100);
  46.       batLow = new Characteristic::StatusLowBattery(0);
  47.       new Characteristic::ChargingState(0);
  48.      
  49.     // Initialize SpanPoint with the sender's MAC address
  50.     // sendSize=0 (we don't send), recvSize=sizeof(sensor_data), channel=1
  51.     sensorPoint = new SpanPoint(macAddress, 0, sizeof(sensor_data), 1);
  52.    
  53.     LOG1("RemoteSensor '%s' created. Waiting for data from %s\n", name, macAddress);
  54.   }
  55.  
  56.   // The loop method will be called repeatedly by HomeSpan
  57.   void loop() {
  58.     // Check if there is data from the remote sensor
  59.     if (sensorPoint->get(&sensorData)) {
  60.       // Update Temperature
  61.       temp->setVal(sensorData.temperature);
  62.       tempFault->setVal(0);  // Clear fault
  63.      
  64.       // Update Humidity
  65.       hum->setVal(sensorData.humidity);
  66.       humFault->setVal(0);  // Clear fault
  67.      
  68.       // Update Battery
  69.       batLevel->setVal(sensorData.batteryLevel);
  70.      
  71.       // Set Low Battery status if needed
  72.       if (sensorData.batteryLevel <= 20) {
  73.         batLow->setVal(1);  // Low battery
  74.       } else {
  75.         batLow->setVal(0);  // Normal battery
  76.       }
  77.      
  78.       LOG1("Sensor '%s' update: Temp=%0.1f°C, Humidity=%0.1f%%, Battery=%d%%\n",
  79.            sensorName, sensorData.temperature, sensorData.humidity, sensorData.batteryLevel);
  80.     }
  81.     else if (sensorPoint->time() > 300000 && !tempFault->getVal()) {
  82.       // If no data received for 5 minutes and not already in fault state
  83.       tempFault->setVal(1);  // Set fault states
  84.       humFault->setVal(1);
  85.       LOG1("Sensor '%s' fault: No data received for 5+ minutes\n", sensorName);
  86.     }
  87.   }
  88. };
  89.  
  90. void setup() {
  91.   Serial.begin(115200);
  92.   delay(1000);
  93.  
  94.   // Set logging level and configure HomeSpan
  95.   homeSpan.setLogLevel(2);  // More detailed logging
  96.   homeSpan.setStatusPin(2);
  97.   homeSpan.setControlPin(0);
  98.   homeSpan.begin(Category::Bridges, "AHT10 Sensor Hub");
  99.  
  100.   // Print MAC Address for reference
  101.   Serial.print("Main Device MAC Address: ");
  102.   Serial.println(WiFi.macAddress());
  103.  
  104.   // Create bridge accessory
  105.   new SpanAccessory();  
  106.     new Service::AccessoryInformation();
  107.       new Characteristic::Identify();
  108.       new Characteristic::Manufacturer("DIY - TechPosts");
  109.       new Characteristic::SerialNumber("HS123456");
  110.       new Characteristic::Model("ESP32 HomeSpan Hub");
  111.       new Characteristic::FirmwareRevision("1.0");
  112.       new Characteristic::Name("Sensor Hub");
  113.  
  114.   // Create sensor accessory with the remote device's MAC address
  115.   new SpanAccessory();
  116.     new Service::AccessoryInformation();
  117.       new Characteristic::Identify();
  118.       new Characteristic::Manufacturer("DIY");
  119.       new Characteristic::SerialNumber("AHT10-001");
  120.       new Characteristic::Model("ESP32 AHT10 Remote Sensor");
  121.       new Characteristic::FirmwareRevision("1.0");
  122.       new Characteristic::Name("Remote AHT10");
  123.    
  124.     // Create the remote sensor and specify the sender's MAC address
  125.     new RemoteSensor("AHT10 Sensor", "D8:3B:DA:34:CD:C4");  // Use your actual sender MAC
  126. }
  127.  
  128. void loop() {
  129.   homeSpan.poll();  // This will call all service loop() methods
  130. }
Advertisement
Add Comment
Please, Sign In to add comment