|
|
本帖最后由 laoliu1982 于 2012-11-18 12:27 编辑
前言:
参考内容是:
http://www.howmuchsnow.com/arduino/airquality/ (感谢@友善的蜜蜂)
http://www.geek-workshop.com/thread-297-1-1.html
硬件:
1 Arduino UNO(必须)约50RMB
2 W5100(可选) 约85RMB
3 PPD42NS(必须) 约70RMB
现在市面上有三种廉价颗粒传感器:
GP2Y1010AU0F(约40RMB),DSM501A(约30RMB),PPD42NS(约70RMB)
为什么选择PPD42NS?上面第一个连接用的是PPD42NS,第二个连接用的是DSM501A。DSM501A与PPD42NS原理完全一致,连引脚顺序也一致(模块引脚间距不一样而已)。但是实际我们用DSM501A测试,效果不是很好。用PPD测试,感觉和标定用的dolos设备一致性比较好。sharp的GP2Y1010AU0F 由于需要外接电阻电容,就没进行测试。
DYLOS 与 PPD42NS的数据对比 http://open.lewei50.com/home/gatewaystatus/69 选择“最近一周”看的比较明显。
平台:乐联网平台 open.lewei50.com
arduino:连线
PPD42NS Pin 1 => Arduino GND
PPD42NS Pin 3 => Arduino 5VDC
PPD42NS Pin 4 => Arduino Digital Pin 8
程序1 :通过W5100 上传
/*
open.lewei50.com sensor clinet
*/
#include <SPI.h>
#include <Ethernet.h>
#define USERKEY "xxxxxx8845829a4f348acb720ed3" // replace your key here
// assign a MAC address for the ethernet controller.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
// fill in your address here:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
// fill in an available IP address on your network here,
// for manual configuration:
// initialize the library instance:
EthernetClient client;
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(216,52,233,121); // numeric IP for api.cosm.com
char server[] = "open.lewei50.com"; // name address for cosm API
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 30*1000; //delay between updates to cosm.com
int pin = 8;
unsigned long duration;
unsigned long starttime;
unsigned long sampletime_ms = 30000;
unsigned long lowpulseoccupancy = 0;
float ratio = 0;
float concentration = 0;
void setup() {
// start serial port:
Serial.begin(9600);
pinMode(8,INPUT);
starttime = millis();
// start the Ethernet connection with DHCP:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
for(;;)
;
}
else {
Serial.println("Ethernet configuration OK");
}
starttime = millis();
}
int x=0; //simulated sensor output
int sampling=1;
int transfering=0;
void loop() {
// read the analog sensor:
//int sensorReading = analogRead(A0);
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
if(1==sampling)
{
duration = pulseIn(pin, LOW);
lowpulseoccupancy = lowpulseoccupancy+duration;
if ((millis()-starttime) > sampletime_ms)
{
ratio = lowpulseoccupancy/(sampletime_ms*10.0); // Integer percentage 0=>100
concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; // using spec sheet curve
//Serial.print(lowpulseoccupancy);
// Serial.print(",");
Serial.print(ratio);
Serial.print(",");
Serial.println(concentration);
lowpulseoccupancy = 0;
//initiate the http post
sampling=0;
transfering=1;
}
}
// http post begin
if(1==transfering)
{
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// if there's no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && lastConnected) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
//initiate the PPDS testing
transfering=0;
sampling=1;
starttime=millis();
}
// if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
Serial.print("http post:");
Serial.println(concentration);
sendData(concentration);
}
// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();
//Serial.println(lastConnected);
}
}
// this method makes a HTTP connection to the server:
void sendData(int thisData) {
// if there's a successful connection:
if (client.connect(server, 80)) {
Serial.println("connecting...");
// send the HTTP PUT request:
client.print("POST /api/V1/gateway/Updatesensors/01 "); // 01代表01网关,如果是02网关这里换成02
client.println("HTTP/1.1");
client.print("userkey: ");
client.println(USERKEY);
client.println("Host: open.lewei50.com ");
client.print("Content-Length: ");
// calculate the length of the sensor reading in bytes:
// 8 bytes for "sensor1," + number of digits of the data:
int thisLength = 24 + getLength(thisData);
client.println(thisLength);
// last pieces of the HTTP PUT request:
//client.println("Content-Type: application/x-www-form-urlencoded");
client.println("Connection: close");
client.println();
// here's the actual content of the PUT request:
// 这里的用p1,是因为用户在系统里面已经添加了一个传感器缩写叫p1的传感器 (在01网关下面)
client.print("[{\"Name\":\"p1\",\"Value\":");
client.print(thisData);
client.println("}]");
}
else {
// if you couldn't make a connection:
Serial.println("connection failed");
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
// note the time that the connection was made or attempted:
lastConnectionTime = millis();
}
// This method calculates the number of digits in the
// sensor reading. Since each digit of the ASCII decimal
// representation is a byte, the number of digits equals
// the number of bytes:
int getLength(int someValue) {
// there's at least one byte:
int digits = 1;
// continually divide the value by ten,
// adding one to the digit count for each
// time you divide, until you're at 0:
int dividend = someValue /10;
while (dividend > 0) {
dividend = dividend /10;
digits++;
}
// return the number of digits:
return digits;
}
程序2:通过串口打印到电脑,电脑通过乐联网转发软件 上传。
int pin = 8;
unsigned long duration;
unsigned long starttime;
unsigned long sampletime_ms = 30000;
unsigned long lowpulseoccupancy = 0;
float ratio = 0;
float concentration = 0;
void setup() {
Serial.begin(9600);
pinMode(8,INPUT);
starttime = millis();
}
void loop() {
duration = pulseIn(pin, LOW);
lowpulseoccupancy = lowpulseoccupancy+duration;
if ((millis()-starttime) > sampletime_ms)
{
ratio = lowpulseoccupancy/(sampletime_ms*10.0); // Integer percentage 0=>100
concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; // using spec sheet curve
//Serial.print(lowpulseoccupancy);
// Serial.print(",");
Serial.print(ratio);
Serial.print(",");
Serial.println(concentration);
lowpulseoccupancy = 0;
starttime = millis();
}
}
串口打印的数据通过“乐联网转发软件” 就可以完成数据的上传
乐联网转发软件
乐联网平台使用设置:
乐联网采用
用户-网关(具备地理属性)-传感器三级架构
每个用户可以有多个网关,每个网关可以带多个传感器。可以分别设置网关是否公开,传感器是否公开。首页的地图只显示公开的网关,点进去就可以看到该网关下面公开的传感器。
系统具体使用方法:http://open.lewei50.com/home/news/69
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|