Page 1 of 1

Feed AQICN

Posted: Sat Dec 19, 2020 6:34 pm
by josecmorales
Hi, i want to feed AQICN.org, but i dont know how, i think is possible trhough weatherlink API, but really dont know how to handle, either know if i can use any function of the MB? some ideas?

Re: Feed AQICN

Posted: Sat Dec 19, 2020 7:56 pm
by jasonmfarrow
You need to sign-up and get a UserToken and then you're going to have to write some code. I'm assuming, given the nature of this forum, that you're using some kind of meteobridge product (Pro, Nano, SD, Stick) etc. with some AQI measuring device attached (PurpleAir, Airlink etc.) and probably using the weather34 template as well. If this is not then case then you'll have to elaborate on your setup for further help.

Here's my snippet of python code which I run on a RaspberryPi which has a Nova SDS011 sensor attached. It shows you the quite simple code required to upload the pm2.5 and pm10 current/live data:

Code: Select all

#!/usr/bin/python -u
from __future__ import print_function
import struct, sys, time, json, subprocess
import requests

# This is the format of the AQI data required. I have PM2.5 and PM10 in an array called Values[]. 
sensorReadings = [  {'specie':'pm25', 'value': values[0]}, {'specie':'pm10', 'value': values[1]} ]

# Station parameter. This describes, and gives info, about your weather station  
stationID = "your_StationID"
cityName = "your_City"
stationName = "your_StationName"
stationWebsite = "your_Website.com"
location = {'latitude': your_decimal_Latitude, 'longitude': your_decimal_Longitude}
station = {'id':stationID, 'city':cityName, 'station':stationName, 'location':location, 'website':stationWebsite}

# User parameter - get yours from https://aqicn.org/data-platform/token/
userToken = "your_stringofhexadecimalcharacters"

# Then Upload the data 
params = {'station':station,'readings':sensorReadings,'token':userToken} 
request = requests.post( url = "https://aqicn.org/sensor/upload/",  json = params)
data = request.json() 
if data["status"]!="ok":
      print("AQICN.org Something went wrong: %s" % data)
else:
      print("AQICN.org data successfully posted: %s, values: %s" % (data, sensorReadings))
quit()
If you are trying to get this to work within the Weather34 template then I suggest you do the following:
  • Firstly, re-write the above Python code into PHP code and test it until it works.
  • Secondly, you need to hook this in to regularly upload. I think you can do this in two ways:
    1. Execute your code as an additional item in /mdbfiles/stationcron
    2. Execute your code directly from meteobridge in the same way that /mdbfiles/stationcron is executed by meteobridge.
The second is preferable as when you update the weather34 template you may lose your modifications.

In your code you'll have to add an include("livedata.php") php line to get the AQI pm2.5 and pm10 data that's being held by the weather34 template.

If you understood none of what I said, then you're into a bit of an uphill struggle. Let us know if any of this made sense.