|
| 1 | +# ESP Daylight Example |
| 2 | + |
| 3 | +This example demonstrates how to use the ESP Daylight component to calculate sunrise and sunset times for various locations around the world. |
| 4 | + |
| 5 | +## What This Example Does |
| 6 | + |
| 7 | +The example showcases several key features of the ESP Daylight component: |
| 8 | + |
| 9 | +1. **Basic Calculations**: Calculate sunrise/sunset for multiple cities worldwide |
| 10 | +2. **Seasonal Variations**: Show how daylight hours change throughout the year |
| 11 | +3. **Time Offsets**: Demonstrate scheduling events relative to sunrise/sunset |
| 12 | +4. **Polar Regions**: Handle special cases like midnight sun and polar night |
| 13 | +5. **Practical Scheduling**: Real-world smart home lighting automation example |
| 14 | + |
| 15 | +## How to Use |
| 16 | + |
| 17 | +### Build and Flash |
| 18 | + |
| 19 | +Create the example project: |
| 20 | + |
| 21 | +```bash |
| 22 | +idf.py create-project-from-example "espressif/esp_daylight:get_started" |
| 23 | +cd get_started |
| 24 | +idf.py set-target esp32 |
| 25 | +idf.py build flash monitor |
| 26 | +``` |
| 27 | + |
| 28 | +Alternatively, if you have the component source locally: |
| 29 | + |
| 30 | +```bash |
| 31 | +cd examples/get_started |
| 32 | +idf.py set-target esp32 |
| 33 | +idf.py build flash monitor |
| 34 | +``` |
| 35 | + |
| 36 | +### Expected Output |
| 37 | + |
| 38 | +The example will display sunrise and sunset times for various locations and demonstrate different use cases: |
| 39 | + |
| 40 | +``` |
| 41 | +ESP Daylight Component Example |
| 42 | +============================ |
| 43 | +
|
| 44 | +=== Basic Sunrise/Sunset Calculation === |
| 45 | +Calculating sunrise/sunset for 2025-08-29: |
| 46 | +
|
| 47 | +New York, USA : Sunrise 10:12:34 UTC, Sunset 23:45:12 UTC (Daylight: 13:32) |
| 48 | +London, UK : Sunrise 05:23:45 UTC, Sunset 19:34:56 UTC (Daylight: 14:11) |
| 49 | +Pune, India : Sunrise 01:15:23 UTC, Sunset 13:02:45 UTC (Daylight: 11:47) |
| 50 | +... |
| 51 | +``` |
| 52 | + |
| 53 | +## Key Locations Tested |
| 54 | + |
| 55 | +The example includes calculations for these major cities: |
| 56 | +- New York, USA (40.7128°N, 74.0060°W) |
| 57 | +- London, UK (51.5074°N, 0.1278°W) |
| 58 | +- Pune, India (18.5204°N, 73.8567°E) |
| 59 | +- Shanghai, China (31.2304°N, 121.4737°E) |
| 60 | +- Sydney, Australia (33.8688°S, 151.2093°E) |
| 61 | +- Moscow, Russia (55.7558°N, 37.6173°E) |
| 62 | +- Tokyo, Japan (35.6762°N, 139.6503°E) |
| 63 | +- Rio de Janeiro, Brazil (22.9068°S, 43.1729°W) |
| 64 | + |
| 65 | +## Customization |
| 66 | + |
| 67 | +### Change Location |
| 68 | + |
| 69 | +Modify the coordinates in the code to match your location: |
| 70 | + |
| 71 | +```c |
| 72 | +esp_daylight_location_t my_location = { |
| 73 | + .latitude = YOUR_LATITUDE, |
| 74 | + .longitude = YOUR_LONGITUDE, |
| 75 | + .name = "My Location" |
| 76 | +}; |
| 77 | +``` |
| 78 | + |
| 79 | +### Change Date |
| 80 | + |
| 81 | +Update the date parameters in the calculation functions: |
| 82 | + |
| 83 | +```c |
| 84 | +int year = 2025, month = 8, day = 29; // Change to your desired date |
| 85 | +``` |
| 86 | + |
| 87 | +### Add Time Zone Support |
| 88 | + |
| 89 | +The component returns UTC timestamps. To display local time, you can convert using standard C library functions or ESP-IDF timezone support. |
| 90 | + |
| 91 | +## Integration with Scheduling |
| 92 | + |
| 93 | +The example shows how to integrate with scheduling systems: |
| 94 | + |
| 95 | +```c |
| 96 | +// Calculate sunset time |
| 97 | +time_t sunset_utc; |
| 98 | +esp_daylight_calc_sunrise_sunset_utc(2025, 8, 29, lat, lon, NULL, &sunset_utc); |
| 99 | + |
| 100 | +// Schedule event 30 minutes before sunset |
| 101 | +time_t light_on_time = esp_daylight_apply_offset(sunset_utc, -30); |
| 102 | + |
| 103 | +// Use with ESP Schedule component (if available) |
| 104 | +esp_schedule_config_t config = { |
| 105 | + .trigger.type = ESP_SCHEDULE_TYPE_SUNSET, |
| 106 | + .trigger.solar.latitude = lat, |
| 107 | + .trigger.solar.longitude = lon, |
| 108 | + .trigger.solar.offset_minutes = -30, |
| 109 | + .callback = your_callback_function |
| 110 | +}; |
| 111 | +``` |
| 112 | +
|
| 113 | +## Troubleshooting |
| 114 | +
|
| 115 | +### No Output for Polar Regions |
| 116 | +
|
| 117 | +If you see "No sunrise/sunset" messages, this is normal for polar regions during certain times of year (midnight sun in summer, polar night in winter). |
| 118 | +
|
| 119 | +### Accuracy |
| 120 | +
|
| 121 | +The calculations use NOAA Solar Calculator equations and are typically accurate to within 1-2 minutes for most locations. |
| 122 | +
|
| 123 | +## Next Steps |
| 124 | +
|
| 125 | +- Modify coordinates for your specific location |
| 126 | +- Integrate with your IoT scheduling system |
| 127 | +- Add timezone conversion for local time display |
| 128 | +- Implement automated device control based on solar events |
| 129 | +- Create recurring schedules that automatically adjust throughout the year |
| 130 | +
|
0 commit comments