数据上传 API 和示例脚本

第一步是从数据平台页面获取令牌。

拥有自己的令牌后,您可以使用以下脚本上传数据。上传第一个站点数据后,请前往 aqicn.org/data-feed/verification/ 配置站点并验证上传的数据。

支持的软件平台:

我们为这 3 个平台提供即用型软件:

  • Arduino:如果您有 Arduino CPU,请使用 github.com 上提供的即用型软件,网址为 aqicn/gaia-a08-arduino
  • Python:使用下面的代码片段
  • 命令行 (CURL):使用下面的代码片段

如果您没有任何监测站,但想拥有一个,请查看我们的GAIA空气质量监测站。

如果您更喜欢 DIY 电台,请查看 GAIA A08


--

示例代码(Python)

import requests
# Sensor parameter
sensorReadings = [
{'specie':'pm25', 'value': 393.3},
{'specie':'pm10', 'value': 109.3}
]
# Station parameter
station = {
'id': "station-001",
'location': {
'latitude': 28.7501,
'longitude': 77.1177
}
}
# User parameter - get yours from https://aqicn.org/data-platform/token/
userToken = "dummy-token-for-test-purpose-only"
# Then Upload the data
params = {'station':station,'readings':sensorReadings,'token':userToken}
request = requests.post( url = "https://aqicn.org/sensor/upload/", json = params)
#print(request.text)
data = request.json()
if data["status"]!="ok":
print("Something went wrong: %s" % data)
else:
print("Data successfully posted: %s"%data)

示例代码(卷曲)

curl -X POST https://aqicn.org/sensor/upload -H 'Content-Type: application/json' --data '{\
"token": "dummy-token-for-test-purpose-only",\
"station": { "id": "station-001" },\
"readings": [{"specie":"pm2.5", "value": 393.3}]\
}'

示例代码(arduino)

Check github.com/aqicn/gaia-a08-arduino for the full code.
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#define LATITUDE 28.7501
#define LONGITUDE 77.1177
void upload(float pm25_concentration, float pm10_concentration, const char * token)
{
static char stationID[32];
uint64_t efuseMac = ESP.getEfuseMac();
uint16_t chip = (uint16_t)(efuseMac >> 32);
snprintf(stationID, 32, "station-%x", chip);
doc["token"] = token;
doc["station"]["id"] = stationID;
doc["station"]["location"]["latitude"] = LATITUDE;
doc["station"]["location"]["longitude"] = LONGITUDE;
doc["readings"][0]["specie"] = "pm25";
doc["readings"][0]["value"] = pm25_concentration;
doc["readings"][0]["unit"] = "µg/m3";
doc["readings"][1]["specie"] = "pm10";
doc["readings"][1]["value"] = pm10_concentration;
doc["readings"][1]["unit"] = "µg/m3";
static char json_body[1024];
serializeJson(doc, json_body);
HTTPClient http;
http.begin("https://aqicn.org/sensor/upload");
http.addHeader("Content-Type", "application/json");
int httpResponseCode = http.POST(json_body);
if (httpResponseCode > 0)
{
String response = http.getString();
Serial.println(httpResponseCode);
Serial.println(response);
}
else
{
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
}
http.end();
}

API 选项

Parameter Type Optional/Mandatory Explanations
token string mandatory

aqicn.org/data-platform/token 获取您自己的令牌。

station
station.id string mandatory

唯一的电台 ID - 您可以选择最多 128 个字符的任何名称。
此名称仅供您内部使用。其他人不会看到此 ID

station.name string optional

车站名称 - 例如可以是您的建筑物名称、街道名称、大学系名称、您个人气象站的代码。
此名称将用作您的电台 URL 的后缀。

station.latitude float optional

您所在站的经度

station.longitude float optional

您所在站的经度

organization
org.website string optional

如果您有一个网站,其中包含有关您的站/传感器的更多信息,当您使用查看您的站时,我们会在地图上添加此链接

org.name string optional

如果您指定网站,则此“组织名称”将与该网站关联。

readings
readings[*].specie string mandatory

您报告的污染物名称。对于气体传感器,使用:“pm2.5”、“pm10”、“pm1.0”,... 对于气体传感器,使用:“co2”、“no2”、“o3”,... 对于天气传感器,使用:“温度”,“湿度”,“压力”,“风速”,“阵风”,“风向”,..
您实际上可以使用您想要的任何物种名称。当您的电台经过验证后,名称将在我们的系统中标准化。

readings[*].value float mandatory

如果您的传感器每秒生成值,并且您每分钟才上传一次,则该值应该是过去一分钟内读取的所有值的平均值。

readings[*].unit string optional

值的单位。例如,“mg/m3”表示灰尘传感器,ppb 表示气体传感器,C 表示温度传感器。

readings[*].time string optional

ISO 8601 格式的读数日期和时间

readings[*].min float optional

如果读数值基于多个值的平均值,则这对应于用于求平均值的所有值的最小值。

readings[*].max float optional

如果读数值基于多个值的平均值,则这对应于用于求平均值的所有值的最大值。

readings[*].median float optional

如果读数值基于多个值的平均值,则这对应于用于求平均值的所有值的中值。

readings[*].stddev float optional

如果读数值基于多个值的平均值,则这对应于用于求平均值的所有值的标准偏差。

readings[*].averaging float optional

如果上述值基于多个值的平均值,则这对应于平均周期的持续时间(以秒为单位)。
例如,使用 60 表示一分钟的平均数据,使用 3600 表示每小时的平均数据。

实施例1

{
"token": "......",
"station": {
"id": "station-001",
"name": "HCPA Santa Cecília",
"latitude": 103.37893,
"longitude": 43.17108,
},
"org": {
"website":"https://pacto.upsensor.com/",
"name":"Porto Ar Alegre",
},
"readings": [
{"time":"2025-03-08T11:59:01+09:00","specie":"pm2.5", "value": 393.3, "unit":"mg/m3", "min":390.3, "max": 402.3, "stddev": 0.332},
{"time":"2025-03-08T11:59:01+09:00","specie":"pm10", "value": 109.3, "unit":"mg/m3"},
{"time":"2025-03-08T11:59:01+09:00","specie":"co2", "value": 459.3, "unit":"ppb"},
{"time":"2025-03-08T11:59:01+09:00","specie":"temp", "value": 26.8, "unit":"C"},
]
}

实施例2

{
"token": "......",
"station": {
"id": "station-001",
},
"readings": [
{"specie":"pm2.5", "value": 393.3}
]
}

完整代码示例

您可以使用此代码从 SDS 传感器连续读取,并每分钟上传一次:(脚本也可从 https://github .com/aqicn/sds-sensor-reader)。

import requests
import random
import time
import math
import json
import sys
from serial import Serial
LOCATION = {'latitude': 28.7501, 'longitude': 77.1177}
TOKEN = "dummy-token-for-test-purpose-only"
SENSORID = "station-001"
USBPORT = "/dev/ttyUSB0"
class SensorDataUploader:
def __init__(self, station, token):
self.token = token
self.station = station
def send(self,readings):
params = {'station':self.station,'readings':readings,'token':self.token}
print("Uploading: %s"%json.dumps(params, indent=4))
request = requests.post( url = "https://aqicn.org/sensor/upload/", json = params)
data = request.json()
if data["status"]!="ok":
print("Something went wrong: %s" % data)
else:
print("Data successfully posted: %s"%data)
class Accumulator:
def __init__(self, name):
self.name = name
self.values = []
def add(self,val):
self.values.append(val)
def count(self):
return len(self.values)
def reset(self):
self.values=[]
def min(self):
return self.values[0]
def max(self):
return self.values[len(self.values)-1]
def median(self):
return self.values[len(self.values)/2]
def mean(self):
return float(sum(self.values)) / len(self.values)
def stddev(self):
l = len(self.values)
mean = self.mean()
return math.sqrt(float(reduce(lambda x, y: x + y, map(lambda x: (x - mean) ** 2, self.values))) / l)
def summary(self):
self.values.sort()
return {"specie":self.name,'value':self.mean(),'min':self.min(),'max':self.max(),'median':self.median(), 'stddev':self.stddev()}
class DummyReader:
def read( self ):
time.sleep(1.1)
return {"pm2.5":random.random()*10,"pm10":random.random()*10}
class SDS011Reader:
def __init__(self, inport):
self.serial = Serial(port=inport,baudrate=9600)
self.values = []
self.step = 0
def read( self ):
# time.sleep(1)
# return {"pm2.5":random.random()*100,"pm10":random.random()*100}
while self.serial.inWaiting()!=0:
v=ord(self.serial.read())
if self.step ==0:
if v==170:
self.step=1
elif self.step==1:
if v==192:
self.values = [0,0,0,0,0,0,0]
self.step=2
else:
self.step=0
elif self.step>8:
self.step =0
pm25 = (self.values[0]+self.values[1]*256)/10
pm10 = (self.values[2]+self.values[3]*256)/10
return {"pm2.5":pm25,"pm10":pm10}
elif self.step>=2:
self.values[self.step-2]=v
self.step= self.step+1
return None
def readAndUpload(sensor, uploader):
try:
while True:
accumulators = {}
startTime = time.time()
while time.time() < startTime+60:
values = sensor.read()
if values==None:
continue
print("Reading [%2d]: %s"%(int(time.time()-startTime),values))
for specie, value in values.items():
if not (specie in accumulators):
accumulators[specie]=Accumulator(specie)
accumulators[specie].add(value)
readings = []
for specie, accumulator in accumulators.items():
readings.append(accumulator.summary())
if len(readings)>0:
uploader.send(readings)
else:
print("No value read from the sensor...")
except KeyboardInterrupt:
print "Bye"
sys.exit()
print("Starting reading sensor "+SENSORID+" on port "+USBPORT)
# Station parameter
station = {'id':SENSORID, 'location':LOCATION}
uploader = SensorDataUploader(station, TOKEN)
sensor = SDS011Reader(USBPORT)
# sensor = DummyReader()
readAndUpload(sensor,uploader)

您知道您所在地区的任何空气质量站吗?
为什么不将自己的空气质量站纳入地图呢?

我们的 GAIA 空气质量监测器设置非常容易:您只需要一个 WIFI 接入点和一个 USB 兼容电源。

一旦连接,您的实时空气污染水平将立即在地图上和通过 API 显示。

该站配备 10 米防水电源线、USB 电源、安装设备和可选太阳能电池板。

关于空气质量与空气污染指数

本网站采用的污染指数和颜色与EPA是完全相同的。 EPA的指数可以从 AirNow上查到

空气质量指数 空气质量指数级别(状况)及表示颜色 对健康影响情况 建议采取的措施
0 - 50 一级(优) 空气质量令人满意,基本无空气污染 各类人群可正常活动
51 -100 二级(良) 空气质量可接受,但某些污染物可能对极少数异常敏感人群健康有较弱影响 极少数异常敏感人群应减少户外活动
101-150 三级(轻度污染) 易感人群症状有轻度加剧,健康人群出现刺激症状 儿童、老年人及心脏病、呼吸系统疾病患者应减少长时间、高强度的户外锻炼
151-200 四级(中度污染) 进一步加剧易感人群症状,可能对健康人群心脏、呼吸系统有影响 儿童、老年人及心脏病、呼吸系统疾病患者避免长时间、高强度的户外锻炼,一般人群适量减少户外运动
201-300 五级(重度污染) 心脏病和肺病患者症状显著加剧,运动耐受力降低,健康人群普遍出现症状 儿童、老年人及心脏病、肺病患者应停留在室内,停止户外运动,一般人群减少户外运动
300+ 六级(严重污染) 健康人群运动耐受力降低,有明显强烈症状,提前出现某些疾病 儿童、老年人和病人应停留在室内,避免体力消耗,一般人群避免户外活动
(参考详见 http://zh.wikipedia.org/wiki/空气质量指数)

如果你想了解更多有关空气质量与污染,详见维基百科或者 AirNow

有关健康建议详见北京的Richard Saint Cyr MD医生的博客:www.myhealthbeijing.com


注意事项: 所有空气质量数据在发布时均未经验证,并且为了保证数据质量,这些数据可能随时被修改,恕不另行通知。 世界空气质量指数项目在编制此信息的内容时已经运用了所有合理的技能和谨慎,在任何情况下世界空气质量指数项目团队或其代理人将不会为由于提供此数据而直接或间接引起的伤害、合同损失、侵权及其他任何损失负责。



设置


选择语言:


Temperature unit:
Celcius