|
本帖最后由 futuremeng 于 2014-5-31 23:00 编辑
数据地址:http://www.machtalk.net/device/dataview/b0af42ac5acc4b639d28fdcaad25640e
设备:Arduino Yun
传感器:
1.甲醛/有机气体:MS1100
http://item.taobao.com/item.htm?spm=a1z09.2.9.79.caH6XU&id=18451419906
取值有两个,一个是根据读数算出来的ppm,一个是直接的读数,因为前者变化不大,为了显示的更清晰,所有把
2. DH11数字温湿度
http://item.taobao.com/item.htm?spm=a1z10.3.w4002-3141887416.10.pY0Ps0&id=6031644481
这个比较简单,不解释了,直接用库中的例子就行。但我去掉了不相干的一些代码。
- #include <Process.h>
- #include <dht.h>
- dht DHT;
- #define GAS_PIN 0
- #define DHT11_PIN 8
- #define ledPin 13
- void setup() {
- // Initialize Bridge
- pinMode(GAS_PIN,INPUT);
- pinMode(DHT11_PIN,INPUT);
- pinMode(ledPin, OUTPUT);
- Bridge.begin();
- // Initialize Serial
- //Serial.begin(9600);
- // Wait until a Serial Monitor is connected.
- //while (!Serial);
- }
- void loop() {
- digitalWrite(ledPin, HIGH);
-
- int GAS_Valt = analogRead(GAS_PIN);
- int GAS_PPM=Valt2PPM(GAS_Valt);
-
-
- //String cmdValt = "curl --request POST -F params={"value":"+String(GAS_Valt)+"} --header "APIKey:aee3bff8c9ad40db926c5be12a0a21b4" http://api.machtalk.net/v1.0/device/b0af42ac5acc4b639d38fdcaad15640e/2/1/datapoints/add";
- runProcess(String(GAS_Valt),2);
- //String cmdPPM = "curl --request POST -F params={"value":"+String(GAS_PPM)+"} --header "APIKey:aee3bff8c9ad40db926c1be12a0a11b4" http://api.machtalk.net/v1.0/device/b0af42ac5acc4b639d28fdcaad15640e/1/1/datapoints/add";
- runProcess(String(GAS_PPM),1);
- int chk = DHT.read11(DHT11_PIN);
- switch (chk)
- {
- case DHTLIB_OK:
- //Serial.print("OK,\t");
- break;
- case DHTLIB_ERROR_CHECKSUM:
- //Serial.print("Checksum error,\t");
- break;
- case DHTLIB_ERROR_TIMEOUT:
- //Serial.print("Time out error,\t");
- break;
- default:
- //Serial.print("Unknown error,\t");
- break;
- }
- //String cmdTemperature="curl --request POST -F params={"value":"+String(DHT.temperature)+"} --header "APIKey:aee3bff8c9ad40db926c5be13a0a11b4" http://api.machtalk.net/v1.0/device/b0af42ac5acc4b639d28fdcaad35640e/4/1/datapoints/add";
- runProcess(String(DHT.temperature),3);
- //String cmdHumidity="curl --request POST -F params={"value":"+String(DHT.humidity)+"} --header "APIKey:aee3bff8c9ad40db926c5be13a0a11b4" http://api.machtalk.net/v1.0/device/b0af42ac5acc4b639d38fdcaad25640e/3/1/datapoints/add";
- runProcess(String(DHT.humidity),4);
-
- digitalWrite(ledPin, LOW);
- delay(5000);
- }
- int Valt2PPM(int valt)
- {
- double ya=valt/204.8;
- int value=0;
- if(ya>0.64)
- {
- value=1;
- }
- if(ya>2.10)
- {
- value=2;
- }
- if(ya>2.38)
- {
- value=3;
- }
- if(ya>2.57)
- {
- value=4;
- }
- if(ya>2.71)
- {
- value=5;
- }
- if(ya>2.82)
- {
- value=6;
- }
- if(ya>2.92)
- {
- value=7;
- }
- if(ya>3.01)
- {
- value=8;
- }
- if(ya>3.09)
- {
- value=9;
- }
- if(ya>3.16)
- {
- value=10;
- }
-
- if(ya>3.22)
- {
- value=15;
- }
- if(ya>3.49)
- {
- value=20;
- }
- if(ya>3.69)
- {
- value=25;
- }
- if(ya>3.87)
- {
- value=30;
- }
- if(ya>4.02)
- {
- value=35;
- }
- if(ya>4.15)
- {
- value=40;
- }
- if(ya>4.27)
- {
- value=45;
- }
- if(ya>4.38)
- {
- value=50;
- }
- if(ya>4.48)
- {
- value=55;
- }
- if(ya>4.57)
- {
- value=60;
- }
- if(ya>4.66)
- {
- value=65;
- }
- if(ya>4.74)
- {
- value=70;
- }
- if(ya>4.82)
- {
- value=75;
- }
-
- return value;
- }
- void runProcess(String value,int type)
- {
- String cmd = "curl --request POST -F params={"value":"+value+"} --header "APIKey:aee3bff8c9ad40db926c3be12a0a11b5" http://api.machtalk.net/v1.0/device/b0af42ac5acc4b639d28fdcaad25630f/"+String(type)+"/1/datapoints/add";
-
- Process p;
- p.runShellCommand(cmd);
-
- while(p.running());
-
- // while (p.available()>0) {
- //char c = p.read();
- //Serial.print(c);
- //}
- // Ensure the last bit of data is sent.
- //Serial.flush();
- }
复制代码
说一下调试中遇到的问题:
1. 一开始想用Progress的例程,但添加参数的方式没有成功,貌似分开加可以,但后来在网上找到了用runShellCommand,如此一来一句就搞定了;
2. 还有就是一开始我把cmd分别用String cmd1 ,String cmd2这样的方式赋值,后来发现到第四个就传不上去,猜测可能是占用内存太多,改成了现在这个方式,已经可以用了,但不知道是否还有优化的方式。
|
|