Arduino code for API

Hi, I’m trying to convert my ESP32 code which used to pull AirQual from my sensor every 30 min with the old JSON pull. Arduino code is a little different than Python and I’m having difficulties. Would appreciate seeing code from anyone that is successfully pulling data from their sensor.

Hi @Bart, we have an article that may help here: Making API Calls in Other Languages. That page includes an example API call written in Python.

Thanks. The Arduino code is just a little different from Python and that has made this a bit more difficult.

I’ve seen examples with the following after the sensor code -H “X-API-Key: readkey” and other examples where quotes aren’t used. I’ve tried it without double quotes and with - neither works and I get a code 400 response.

Ethan, I transmitted my GET request from my Arduino, exactly as you described in the reference. I will try to upload an example of what I sent. I get an error of 400. When I try the same exact URL in a browser, it comes back saying the key is not there. Yet, when I do the sample on the purpleair api page, it works fine. I’m just at a loss on what to do.
Bart

Having problems uploading. Browser locks up when I try the upload button. Will see if Edge does the same.

Okay, got the header passed using an http.addHeader command and I successfully downloaded the response. Now I just need to figure out how to extract humidity, temperature, and pm2.5.

The response from the previous was too large for the Arduino to handle, so I changed to request specific fields (temperature, humidity, pm2.5_30minute using the following:

http.begin("https://api.purpleair.com/v1/sensors/:37403

with header

http.addHeader(“X-API-Key”, “myapireadkey, temperature, humidity, pm2.5_30minute”
with myapireadkey the actual read key as a string

I get an http error 403 in response. What am I doing wrong here?

I see you have included a colon before the sensor ID in your URL, which should not be there. Also, the fields for the data are not sent as headers and instead should be added to the URL. Please refer to the documentation at api.purpleair.com and our getting started guide to create your requests.

Thanks. I finally got the download to work after studying your API - PurpleAir (A RESTful API for PurpleAir sensors, v 1.0.10) and using the built-in test calls. That colon which was part of the problem was there because your documentation showed it - look under Sensors - Get Sensor Data. In any case, I am now wrestling with JSON data extraction because the Arduino documentation is all over the place. Thanks for helping me get this far.

P.S. It would help somewhere in your documentation if you showed the syntax of the fieldnames in the URL. Commas only, no spaces, and the leading pieces, such as what I had to used:
https://api.purpleair.com/v1/sensors/37403?sensor_index=37403&fields=pm2.5_30minute%2Ctemperature%2Chumidity

Can you kindly post the code that you used for Arduino using github? Would be a great help if you did thanks in advance.

Thanks for the interest. Also posted a pic of my monitor.

1 Like