可以先体验再看文章:
物联网登陆地址:www.lewei50.com/home/login
登陆名:office-air:guest
密码:123456
添加微信公共账号 “乐联网” 发送 u office-air 会返回这个账号(office-air)所有公开的传感器(两个颗粒物传感器)信息。
1 目的
演示如何使用Arduino硬件+乐联网平台 快速搭建 廉价室内颗粒物测试系统。
2 实验条件
硬件设备:
Arduino UNO:约50RMB,
W5100 :约50 RMB,
PPD42NS:约70RMB
系统平台:乐联网
现在市面上有三种廉价颗粒传感器:
GP2Y1010AU0F(约40RMB)
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
PPD42NS Pin 1 => Arduino GND
PPD42NS Pin 3 => Arduino 5VDC
PPD42NS Pin 4 => Arduino Digital Pin 8
DSM501A 连线方式类似,可以参考相关datasheet
C++代码
- /*
- 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】W5100+arduino+乐联网平台实现传感器数据上传 www.lewei50.com/home/news/94
【推荐教程1】W5100+arduino+乐联网平台 实现反向控制 www.lewei50.com/home/news/92
3 最后
以上介绍了乐联网的反向控制Arduino的一个应用,希望能够让大家对乐联网的物联网应用有更进一步的了解,并能借鉴这个应用启发您的思路,一起在乐联网上实现自己的各类创意。后续,我们会推出通过数值的变化来控制设备实现不同的变化的更为复杂的控制方案,尽请期待!
如果你需要更详细的技术交流或者疑问咨询,可以加入乐为物联技术支持群:59162154;或关注乐为物联新浪微博@乐为物联;
关注乐联网微信:搜索公众账号“乐联网”。或者扫描下面的二维码来添加关注“乐联网”。