>>> Join Us in the Fight Against Air Pollution

AirGradient Forum

API Upload with ESPHome

I’ve been running a DIY kit for a long time now and I’ve been using ESPHome for most of that time. I had written my config so it would upload to the AirGradient API along with home HomeAssistant and I watch HA daily, but I don’t often check the AirGradient site.

Someone posted on my Github earlier today that the upload wasn’t working for them into the API, and when I checked mine, I was getting the same error. I haven’t changed my ESPHome config, but it looks like the last time my sensor talked to the API was Feb 5th. Were there any backend changes that may account for that? I could try flashing to an older ESPHome to try or just try the Arduino sketch, but the format of the json body and API address hasn’t changed, so not sure why it would have stopped.

This is my config in ESPHome
airgradient_esphome/airgradient.yml at main · MallocArray/airgradient_esphome (github.com)

Example errors in ESPHome logs:
[W][http_request:086]: HTTP Request failed; URL: http://hw.airgradient.com/sensors/airgradient:xxxxx/measures; Error: connection failed; Duration: 0 ms

or

[W][http_request:093]: HTTP Request failed; URL: http://hw.airgradient.com/sensors/airgradient:XXXXXX/measures; Code: 400; Duration: 276 ms

@MallocArray I am not aware of any changes. If you could PM me your serial number of your device, then we can check our server logs.

I did roll back my ESPHome to October 2022 and flashed the AirGradient DIY with it and I get the same message, so I don’t think it is an ESPHome change

We checked our server. We cannot see any Incoming requests with your ID. Maybe your firewall or router blocks it?

In my case, at some point I had commented out my DNS servers while keeping the static IP and that kept it from resolving the hostname. Adding the DNS server back in allowed mine to connect again.
If you have DHCP setup, that shouldn’t be the issue

I was able to confirm I could connect by using Postman on my PC to try to send a POST to http://hw.airgradient.com/sensors/airgradient:xxxxxx/measures with a json body of:

{
    "pm02": 1
}

This was successful which let me know my network wasn’t blocking connection, so I looked closer at the wifi config of my ESPHome device and found the culprit.

Now that I’m watching logs, I see some Code: 429 which looks to be rate limiting. How often should we be sending a API update? I tried reading the Arduino code, but I only saw 10000 miliseconds which is less than 1 minute and I’m trying to send updates every 1 minute and getting a 429, then success, then 429, so I’m thinking about extending it to 2 minutes.

We are still experimenting with rate limits to protect our server from too much load but if you go for around 90 seconds between requests you should be good.