【Arduino】 搭建颗粒物监测系统(初级 10分钟)

2014-03-12 10:26 来源:乐联网

 

 

1 目的

 
演示如何使用Arduino硬件+乐联网平台 快速搭建 廉价室内颗粒物测试系统。
 
 
2 实验条件
硬件设备:
Arduino UNO:约50RMB,
W5100 :约50 RMB,
PPD42NS:约70RMB
系统平台:乐联网
现在市面上有三种廉价颗粒传感器:
GP2Y1010AU0F(约40RMB)
DSM501A(约30RMB)
PPD42NS(约70RMB)
 
连线方式
PPD42NS Pin 1 => Arduino GND
PPD42NS Pin 3 => Arduino 5VDC
PPD42NS Pin 4 => Arduino Digital Pin 8
DSM501A 连线方式类似,可以参考相关datasheet
 
 
C++代码
  1. /* 
  2.    open.lewei50.com  sensor  clinet 
  3.  */  
  4.   
  5.  #include <SPI.h>  
  6.  #include <Ethernet.h>  
  7.   
  8.  #define USERKEY         "xxxxxx8845829a4f348acb720ed3" // replace your key here  
  9.   
  10.   
  11.  // assign a MAC address for the ethernet controller.  
  12.  // Newer Ethernet shields have a MAC address printed on a sticker on the shield  
  13.  // fill in your address here:  
  14.  byte mac[] = {   
  15.    0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};  
  16.   
  17.  // fill in an available IP address on your network here,  
  18.  // for manual configuration:  
  19.   
  20.   
  21.  // initialize the library instance:  
  22.  EthernetClient client;  
  23.   
  24.  // if you don't want to use DNS (and reduce your sketch size)  
  25.  // use the numeric IP instead of the name for the server:  
  26.  //IPAddress server(216,52,233,121);      // numeric IP for api.cosm.com  
  27.  char server[] = "open.lewei50.com";   // name address for cosm API  
  28.   
  29.  unsigned long lastConnectionTime = 0;          // last time you connected to the server, in milliseconds  
  30.  boolean lastConnected = false;                 // state of the connection last time through the main loop  
  31.  const unsigned long postingInterval = 30*1000; //delay between updates to cosm.com  
  32.   
  33.   
  34.  int pin = 8;  
  35.  unsigned long duration;  
  36.  unsigned long starttime;  
  37.  unsigned long sampletime_ms = 30000;  
  38.  unsigned long lowpulseoccupancy = 0;  
  39.  float ratio = 0;  
  40.  float concentration = 0;  
  41.   
  42.  void setup() {  
  43.   
  44.    // start serial port:  
  45.    Serial.begin(9600);  
  46.    pinMode(8,INPUT);  
  47.    starttime = millis();  
  48.    // start the Ethernet connection with DHCP:  
  49.    if (Ethernet.begin(mac) == 0) {  
  50.      Serial.println("Failed to configure Ethernet using DHCP");  
  51.      for(;;)  
  52.        ;  
  53.    }  
  54.    else {  
  55.      Serial.println("Ethernet configuration OK");  
  56.    }  
  57.     starttime = millis();  
  58.  }  
  59.   
  60.  int x=0; //simulated sensor output  
  61.  int sampling=1;  
  62.  int transfering=0;  
  63.  void loop() {  
  64.    // read the analog sensor:  
  65.    //int sensorReading = analogRead(A0);     
  66.   
  67.    // if there's incoming data from the net connection.  
  68.    // send it out the serial port.  This is for debugging  
  69.    // purposes only:  
  70.    if(1==sampling)  
  71.    {  
  72.      duration = pulseIn(pin, LOW);  
  73.      lowpulseoccupancy = lowpulseoccupancy+duration;  
  74.   
  75.      if ((millis()-starttime) > sampletime_ms)  
  76.      {  
  77.        ratio = lowpulseoccupancy/(sampletime_ms*10.0);  // Integer percentage 0=>100  
  78.        concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; // using spec sheet curve  
  79.        //Serial.print(lowpulseoccupancy);  
  80.        // Serial.print(",");  
  81.        Serial.print(ratio);  
  82.        Serial.print(",");  
  83.        Serial.println(concentration);  
  84.        lowpulseoccupancy = 0;  
  85.        //initiate the http post  
  86.        sampling=0;  
  87.        transfering=1;  
  88.      }  
  89.    }  
  90.    // http post begin   
  91.  if(1==transfering)  
  92.  {  
  93.      if (client.available()) {  
  94.       char c = client.read();  
  95.       Serial.print(c);  
  96.     }  
  97.   
  98.     // if there's no net connection, but there was one last time  
  99.     // through the loop, then stop the client:  
  100.     if (!client.connected() && lastConnected) {  
  101.       Serial.println();  
  102.       Serial.println("disconnecting.");  
  103.       client.stop();  
  104.       //initiate the PPDS testing  
  105.       transfering=0;  
  106.       sampling=1;  
  107.       starttime=millis();  
  108.         
  109.     }  
  110.   
  111.     // if you're not connected, and ten seconds have passed since  
  112.     // your last connection, then connect again and send data:  
  113.     if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {  
  114.       Serial.print("http post:");  
  115.       Serial.println(concentration);  
  116.       sendData(concentration);  
  117.     }  
  118.     // store the state of the connection for next time through  
  119.     // the loop:  
  120.     lastConnected = client.connected();  
  121.     //Serial.println(lastConnected);  
  122.         
  123.  }  
  124.      
  125.  }  
  126.   
  127.  // this method makes a HTTP connection to the server:  
  128.  void sendData(int thisData) {  
  129.    // if there's a successful connection:  
  130.    if (client.connect(server, 80)) {  
  131.      Serial.println("connecting...");  
  132.      // send the HTTP PUT request:  
  133.      client.print("POST /api/V1/gateway/Updatesensors/01 "); // 01代表01网关,如果是02网关这里换成02  
  134.     client.println("HTTP/1.1");  
  135.          client.print("userkey: ");  
  136.      client.println(USERKEY);  
  137.      client.println("Host: open.lewei50.com ");  
  138.   
  139.   
  140.   
  141.      client.print("Content-Length: ");  
  142.   
  143.      // calculate the length of the sensor reading in bytes:  
  144.      // 8 bytes for "sensor1," + number of digits of the data:  
  145.      int thisLength = 24 + getLength(thisData);  
  146.      client.println(thisLength);  
  147.   
  148.      // last pieces of the HTTP PUT request:  
  149.      //client.println("Content-Type: application/x-www-form-urlencoded");  
  150.      client.println("Connection: close");  
  151.      client.println();  
  152.   
  153.      // here's the actual content of the PUT request:  
  154. // 这里的用p1,是因为用户在系统里面已经添加了一个传感器缩写叫p1的传感器 (在01网关下面)  
  155.   
  156.      client.print("[{"Name":"p1","Value":");       
  157.      client.print(thisData);  
  158.      client.println("}]");  
  159.      
  160.    }   
  161.    else {  
  162.      // if you couldn't make a connection:  
  163.      Serial.println("connection failed");  
  164.      Serial.println();  
  165.      Serial.println("disconnecting.");  
  166.      client.stop();  
  167.    }  
  168.     // note the time that the connection was made or attempted:  
  169.    lastConnectionTime = millis();  
  170.  }  
  171.   
  172.   
  173.  // This method calculates the number of digits in the  
  174.  // sensor reading.  Since each digit of the ASCII decimal  
  175.  // representation is a byte, the number of digits equals  
  176.  // the number of bytes:  
  177.   
  178.  int getLength(int someValue) {  
  179.    // there's at least one byte:  
  180.    int digits = 1;  
  181.    // continually divide the value by ten,   
  182.    // adding one to the digit count for each  
  183.    // time you divide, until you're at 0:  
  184.    int dividend = someValue /10;  
  185.    while (dividend > 0) {  
  186.      dividend = dividend /10;  
  187.      digits++;  
  188.    }  
  189.    // return the number of digits:  
  190.    return digits;  
  191.  }  
 
 

更多案例可以参考:

【推荐教程2】W5100+arduino+乐联网平台实现传感器数据上传  www.lewei50.com/home/news/94

【推荐教程1】W5100+arduino+乐联网平台 实现反向控制  www.lewei50.com/home/news/92

 

 

 

3 最后

 以上介绍了乐联网的反向控制Arduino的一个应用,希望能够让大家对乐联网的物联网应用有更进一步的了解,并能借鉴这个应用启发您的思路,一起在乐联网上实现自己的各类创意。后续,我们会推出通过数值的变化来控制设备实现不同的变化的更为复杂的控制方案,尽请期待!

如果你需要更详细的技术交流或者疑问咨询,可以加入乐为物联技术支持群:59162154;或关注乐为物联新浪微博@乐为物联

关注乐联网微信:搜索公众账号乐联网。或者扫描下面的二维码来添加关注乐联网