techposts

SpanPoint-Transmitter-AHT10

Mar 1st, 2025
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.01 KB | Source Code | 0 0
  1. #include "HomeSpan.h"
  2. #include <Adafruit_AHTX0.h>
  3. #include <Wire.h>
  4.  
  5. #define AHTPIN 1       // I2C SDA pin (adjust based on your specific board)
  6. #define SLEEP_TIME 20  // Deep sleep time in seconds
  7.  
  8. // Structure for sensor data - must match receiver's structure
  9. struct sensor_data {
  10.   float temperature;
  11.   float humidity;
  12.   int batteryLevel;
  13. };
  14.  
  15. // Create AHT10 sensor object
  16. Adafruit_AHTX0 aht;
  17.  
  18. void setup() {
  19.   // Set CPU to lower frequency to save power
  20.   setCpuFrequencyMhz(80);
  21.  
  22.   Serial.begin(115200);
  23.   delay(1000);
  24.   Serial.println("\nAHT10 Remote Sensor Starting...");
  25.  
  26.   // Initialize I2C communication
  27.   Wire.begin(3, 1);  // SDA pin 3, SCL pin 1
  28.  
  29.   // Check if AHT10 sensor is found
  30.   if (!aht.begin()) {
  31.     Serial.println("Failed to find AHT10 sensor!");
  32.     // Handle sensor initialization failure
  33.     esp_deep_sleep_start();
  34.   }
  35.  
  36.   // Set device as a Wi-Fi Station
  37.   WiFi.mode(WIFI_STA);
  38.  
  39.   // Print MAC address for configuration
  40.   Serial.print("Remote Sensor MAC Address: ");
  41.   Serial.println(WiFi.macAddress());
  42.  
  43.   // Initialize SpanPoint with the receiver's MAC address
  44.   // sendSize=sizeof(sensor_data), recvSize=0, channel=1
  45.   SpanPoint *mainDevice = new SpanPoint("D8:3B:DA:34:B0:90", sizeof(sensor_data), 0, 1);
  46.  
  47.   // Allow sensor time to stabilize\
  48.   delay(2000);
  49.  
  50.   // Read sensor data
  51.   sensors_event_t humidity, temp;
  52.   aht.getEvent(&humidity, &temp);
  53.  
  54.   sensor_data sensorData;
  55.   sensorData.temperature = temp.temperature;
  56.   sensorData.humidity = humidity.relative_humidity;
  57.  
  58.   // Read battery level (example - adjust for your hardware)
  59.   int batteryRaw = analogRead(A0);
  60.   sensorData.batteryLevel = map(batteryRaw, 0, 4095, 0, 100);
  61.  
  62.   // Print readings for debugging
  63.   Serial.print("Temperature: ");
  64.   Serial.print(sensorData.temperature);
  65.   Serial.println(" °C");
  66.  
  67.   Serial.print("Humidity: ");
  68.   Serial.print(sensorData.humidity);
  69.   Serial.println(" %");
  70.  
  71.   Serial.print("Battery: ");
  72.   Serial.print(sensorData.batteryLevel);
  73.   Serial.println(" %");
  74.  
  75.   // Check if readings are valid
  76.   if (isnan(sensorData.temperature) || isnan(sensorData.humidity)) {
  77.     Serial.println("Failed to read from AHT10 sensor!");
  78.   } else {
  79.     // Send data to main device
  80.     boolean success = mainDevice->send(&sensorData);
  81.     Serial.print("Send data update: ");
  82.     Serial.println(success ? "Success" : "Failed");
  83.    
  84.     // Try again if failed the first time
  85.     if (!success) {
  86.       delay(500);
  87.       success = mainDevice->send(&sensorData);
  88.       Serial.print("Second attempt: ");
  89.       Serial.println(success ? "Success" : "Failed");
  90.     }
  91.   }
  92.  
  93.   // Wait a moment to ensure transmission completes
  94.   delay(200);
  95.  
  96.   // Go to deep sleep
  97.   Serial.print("Going to deep sleep for ");
  98.   Serial.print(SLEEP_TIME);
  99.   Serial.println(" seconds");
  100.   Serial.flush();
  101.   esp_sleep_enable_timer_wakeup(SLEEP_TIME * 1000000); // Convert to microseconds
  102.   esp_deep_sleep_start();
  103. }
  104.  
  105. void loop() {
  106.   // This won't run due to deep sleep
  107. }
Advertisement
Add Comment
Please, Sign In to add comment