Author Topic: Purple Air Cron Job No Go (RESOLVED)  (Read 697 times)

0 Members and 1 Guest are viewing this topic.

Offline CNYWeather

  • Forecaster
  • *****
  • Posts: 2298
    • CNYWeather
Purple Air Cron Job No Go (RESOLVED)
« on: February 20, 2024, 07:31:20 PM »
Got my Purple Air Sensor today. It's inside for now until I can get it mounted outside.
https://map.purpleair.com/1/m/i/mAQI/a0/p1800/cC5?select=211619#11.97/43.14073/-75.4146

I visited this thread https://www.wxforum.net/index.php?topic=45730.0 and am using that code.

So, trying to get wxpurpleair.php working without success. 211619 is my sensor ID, correct?
I got an API key from develop.purpleair.com and its type is READ and Status is ENABLED.

I created a cron job to get the data. (https://cnyweather.com/purple.php) Is the cron job below.  I'm using IONOS also if that mean anything different.
That gets a HTTP 500 error/

Code: [Select]
<?php

# cron task to get PurpleAir data and cache it

$sensorid "xxxxxx";
$PurpleAirAPI "xxxxxxxxxxxxxxxx";

$STRopts = array(
"http" => array(
"method" => "GET",
"protocol_version" => 1.1,
"proxy" => "tcp://proxy.example.com:5100",
"header" => "Cache-Control: no-cache, must-revalidate\r\n" .
"Cache-control: max-age=0\r\n" .
"Connection: close\r\n" .
"User-agent: Mozilla/5.0 (purpleair-getjson.php - saratoga-weather.org)\r\n" .
"Accept: application/json\r\n"
) ,
"ssl" => array(
"method" => "GET",
"protocol_version" => 1.1,
"verify_peer" => false,
"proxy" => "tcp://proxy.example.com:5100",
"header" => "Cache-Control: no-cache, must-revalidate\r\n" .
"Cache-control: max-age=0\r\n" .
"Connection: close\r\n" .
"User-agent: Mozilla/5.0 (purpleair-getjson.php - saratoga-weather.org)\r\n" .
"Accept: application/json\r\n"
)
);
$STRcontext stream_context_create($STRopts);
$pajson file_get_contents("https://api.purpleair.com/v1/sensors/$sensorid?api_key=$PurpleAirAPI",0,$STRcontext);
$fname "./purpleair-".$sensorid.".json";
if (
strlen($pajson) > 50)
{
file_put_contents($fname,$pajson);
echo "..saved $fname with ".strlen($pajson)." bytes.\n";
} else {
echo "..error.  Only ".strlen($pajson)." bytes returned.  Not saved to $fname\n";

https://www.cnyweather.com/purpleair-inc.php returns with No data from Purpleair - check Sensor ID/API keys.

I've checked and rechecked. Not sure what i'm missing.
« Last Edit: February 22, 2024, 07:26:11 AM by CNYWeather »
Tony




Offline saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 9298
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
Re: Purple Air Cron Job No Go
« Reply #1 on: February 20, 2024, 07:43:59 PM »
Take out both the

"proxy" => "tcp://proxy.example.com:5100",

lines and try again.
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis VP1+ FARS, Blitzortung RED, GRLevel3, WD, WL, VWS, Cumulus, Meteobridge
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

Offline CNYWeather

  • Forecaster
  • *****
  • Posts: 2298
    • CNYWeather
Re: Purple Air Cron Job No Go
« Reply #2 on: February 20, 2024, 08:05:46 PM »
I got a HTTP 500 error on https://www.cnyweather.com/purple.php

https://www.cnyweather.com/purpleair-inc.php shows
No data from Purpleair - check Sensor ID/API keys

Is the API key active once you create it?
Tony




Offline saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 9298
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
Re: Purple Air Cron Job No Go
« Reply #3 on: February 20, 2024, 09:28:24 PM »
I suggest you try the PurpleAir API access url directly in your browser (adding your Sensor ID/API key to the URL) and see what the JSON return says.
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis VP1+ FARS, Blitzortung RED, GRLevel3, WD, WL, VWS, Cumulus, Meteobridge
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

Offline CNYWeather

  • Forecaster
  • *****
  • Posts: 2298
    • CNYWeather
Re: Purple Air Cron Job No Go
« Reply #4 on: February 21, 2024, 05:25:26 AM »
Here is the result Ken

Code: [Select]
{
  "api_version" : "V1.0.11-0.0.49",
  "time_stamp" : 1708511046,
  "error" : "PaymentRequiredError",
  "description" : "Payment is required to make this api call."
}

I have 1,000,000 points I guess with the developer account where I created my API key.
« Last Edit: February 21, 2024, 05:31:09 AM by CNYWeather »
Tony




Offline wvdkuil

  • Wim van der kuil
  • Forecaster
  • *****
  • Posts: 1986
    • My PWS at Leuven Belgium Europe
Re: Purple Air Cron Job No Go
« Reply #5 on: February 21, 2024, 05:44:23 AM »
Here is the result Ken

Code: [Select]
{
  "api_version" : "V1.0.11-0.0.49",
  "time_stamp" : 1708511046,
  "error" : "PaymentRequiredError",
  "description" : "Payment is required to make this api call."
}

I have 1,000,000 points I guess with the developer account where I created my API key.

You have to contact PurpleAir that you  are an owner of a purpleair sensor.

They will add the necessary points to your account FREE.

There are multiple discussions at the weather-watch forum. This is the latest one
https://discourse.weather-watch.com/t/purpleair-sensors/73342
Quote
If you want to query the data from your own sensor, we want you to be able to do that.
We are working on a system that will automatically allocate API points to sensor owners, but it has not been released yet.
For the time being, please email contact@purpleair.com, and we will manually grant you API points.
They can also check if you have the correct type of account a.s.o.
Wim
« Last Edit: February 21, 2024, 06:01:19 AM by wvdkuil »

Offline CNYWeather

  • Forecaster
  • *****
  • Posts: 2298
    • CNYWeather
Re: Purple Air Cron Job No Go
« Reply #6 on: February 21, 2024, 06:08:38 AM »
Thank you for the info Wim.
Tony




Offline drrehak

  • Senior Member
  • **
  • Posts: 72
Re: Purple Air Cron Job No Go
« Reply #7 on: February 21, 2024, 05:47:12 PM »
Alternative Approach. 
Have you tried to pull the data directly from your sensor?
No need for API keys, no rate limits.  Sensor just has to be accessible from the process grabbing the data.  Easier if you are self hosting; sensor has to be exposed to the outside world if you are running the cron job and site on a host -- so that may be a nonstarter for you.

Local data is just a simple call:
Code: [Select]
<local sensor ip address>/json?live=true
Code: [Select]
live=true gives you 10 second average readings
Code: [Select]
live=false gives you 120 second average readings updated every 10 seconds
Since this is just raw data, you would need to average the two sensors if you have two, add code if you wanted something other than EPA2.5

I've been running HomeAssistant pulling the readings every 2 minutes successfully for a month.  Getting the data directly into the Saratoga template is on my list.

API documentation:
https://community.purpleair.com/t/local-json-documentation/6917
Dan Rehak | Pittsburgh PA
Davis Vantage Pro 2 Plus | Meteobridge Pro
Local Site | Raspberry Pi 4B

Offline CNYWeather

  • Forecaster
  • *****
  • Posts: 2298
    • CNYWeather
Re: Purple Air Cron Job No Go
« Reply #8 on: February 21, 2024, 06:14:07 PM »
Thats another approach I may take a look at too.

I did get things all figured out. Got some coins moved over to the API account and things are working good now.
Tony




Offline Vasco

  • Forecaster
  • *****
  • Posts: 343
    • PWS
Re: Purple Air Cron Job No Go
« Reply #9 on: February 21, 2024, 06:51:12 PM »
I did get things all figured out. Got some coins moved over to the API account and things are working good now.


How often are you running the cron?

Ecowitt GW1102 (with GW2000) + Weather Display in Windows 10

Offline CNYWeather

  • Forecaster
  • *****
  • Posts: 2298
    • CNYWeather
Re: Purple Air Cron Job No Go
« Reply #10 on: February 21, 2024, 09:30:28 PM »
I was going to run it every 15 minutes to start
Tony




Offline Vasco

  • Forecaster
  • *****
  • Posts: 343
    • PWS
Re: Purple Air Cron Job No Go (RESOLVED)
« Reply #11 on: February 22, 2024, 07:29:39 AM »
AFAIK each call to the API costs 155 points, unless you just select the fields you need. At that rate, a million points last 67.2 days.

Ecowitt GW1102 (with GW2000) + Weather Display in Windows 10

Offline CNYWeather

  • Forecaster
  • *****
  • Posts: 2298
    • CNYWeather
Re: Purple Air Cron Job No Go (RESOLVED)
« Reply #12 on: February 22, 2024, 07:34:00 AM »
I'm using Ken's script which does it's best to limit the amount of information from the API to just what is needed.
Tony




Offline Vasco

  • Forecaster
  • *****
  • Posts: 343
    • PWS
Re: Purple Air Cron Job No Go (RESOLVED)
« Reply #13 on: February 22, 2024, 07:58:41 AM »
I'm using Ken's script

Which one? I'm still using purpleair-inc.php which gets all the fields, I think?
« Last Edit: February 22, 2024, 08:03:27 AM by Vasco »
Ecowitt GW1102 (with GW2000) + Weather Display in Windows 10

Offline CNYWeather

  • Forecaster
  • *****
  • Posts: 2298
    • CNYWeather
Re: Purple Air Cron Job No Go (RESOLVED)
« Reply #14 on: February 22, 2024, 09:01:19 AM »
Here you go. I see you posted in this thread also.

https://www.wxforum.net/index.php?topic=45730.0
Tony




Offline Vasco

  • Forecaster
  • *****
  • Posts: 343
    • PWS
Re: Purple Air Cron Job No Go (RESOLVED)
« Reply #15 on: February 22, 2024, 09:20:40 AM »
Here you go. I see you posted in this thread also.

Yes, see especially the last post in that thread. I may be wrong, but I think Ken is still using an old free API so it doesn't matter how many fields he gets? See PurpleAir Develop for current field "prices".

Wim amended the API call in PWS-dashboard (https://discourse.weather-watch.com/t/charges-for-purpleair-api-calls/72160/22) for users of the paid API to get only the fields necessary. I don't know if Ken has done that yet?

EDIT: how many fields in the cached json?
« Last Edit: February 22, 2024, 09:43:34 AM by Vasco »
Ecowitt GW1102 (with GW2000) + Weather Display in Windows 10

Offline CNYWeather

  • Forecaster
  • *****
  • Posts: 2298
    • CNYWeather
Re: Purple Air Cron Job No Go (RESOLVED)
« Reply #16 on: February 22, 2024, 09:57:06 AM »
Looks like this is what returns from purple-air-inc.php

Code: [Select]
<!-- stA=0.4 stB=0.3 avg=0.35 -->
<!-- aqA=2 aqB=1 avg=1 -->
Tony




Offline Vasco

  • Forecaster
  • *****
  • Posts: 343
    • PWS
Re: Purple Air Cron Job No Go (RESOLVED)
« Reply #17 on: February 22, 2024, 10:44:00 AM »
When I tried the cron with purpleair-inc.php I got purpleair-190xxx.json (attached, .txt added) cached.

Now I don't run the cron I don't get a cached file at all, but I do know it costs me 155 points a go  :)
« Last Edit: February 22, 2024, 11:06:58 AM by Vasco »
Ecowitt GW1102 (with GW2000) + Weather Display in Windows 10

 

anything