Building a Particulate Monitoring System

This article demonstrates how to use Arduino+W5100 and Lewei Website to build a cheap particulate monitoring system quickly.
ORIGINAL POST
By Unknown
components
Hardware Components
Arduino UNO
X 1
W5100
X 1
PPD42NS
X 1
Software Apps and online services
Lewei Website
details

This article demonstrates how to use Arduino+W5100 and Lewei Website to build a cheap particulate monitoring system quickly.

 

Hardware:

Arduino UNO

W5100

PPD42NS

System Platform:

Lewei Website

室内颗粒物设备

Linking method:

PPD42NS Pin 1 => Arduino GND
PPD42NS Pin 3 => Arduino 5VDC
PPD42NS Pin 4 => Arduino Digital Pin 8

 

Code :

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 ");
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.
155.     client.print("[{"Name":"p1","Value":");
156.     client.print(thisData);
157.     client.println("}]");
158.
159.   }
160.   else {
161.     // if you couldn't make a connection:
162.     Serial.println("connection failed");
163.     Serial.println();
164.     Serial.println("disconnecting.");
165.     client.stop();
166.   }
167.    // note the time that the connection was made or attempted:
168.   lastConnectionTime = millis();
169. }
170.
171.
172. // This method calculates the number of digits in the
173. // sensor reading.  Since each digit of the ASCII decimal
174. // representation is a byte, the number of digits equals
175. // the number of bytes:
176.
177. int getLength(int someValue) {
178.   // there's at least one byte:
179.   int digits = 1;
180.   // continually divide the value by ten, 
181.   // adding one to the digit count for each
182.   // time you divide, until you're at 0:
183.   int dividend = someValue /10;
184.   while (dividend > 0) {
185.     dividend = dividend /10;
186.     digits++;
187.   }
188.   // return the number of digits:
189.   return digits;
190. }

 

Source:http://www.lewei50.com/dev/doc/167(in Chinese)

This article demonstrates how to use Arduino+W5100 and Lewei Website to build a cheap particulate monitoring system quickly.

 

Hardware:

Arduino UNO

W5100

PPD42NS

System Platform:

Lewei Website

室内颗粒物设备

Linking method:

PPD42NS Pin 1 => Arduino GND
PPD42NS Pin 3 => Arduino 5VDC
PPD42NS Pin 4 => Arduino Digital Pin 8

 

Code :

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 ");
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.
155.     client.print("[{"Name":"p1","Value":");
156.     client.print(thisData);
157.     client.println("}]");
158.
159.   }
160.   else {
161.     // if you couldn't make a connection:
162.     Serial.println("connection failed");
163.     Serial.println();
164.     Serial.println("disconnecting.");
165.     client.stop();
166.   }
167.    // note the time that the connection was made or attempted:
168.   lastConnectionTime = millis();
169. }
170.
171.
172. // This method calculates the number of digits in the
173. // sensor reading.  Since each digit of the ASCII decimal
174. // representation is a byte, the number of digits equals
175. // the number of bytes:
176.
177. int getLength(int someValue) {
178.   // there's at least one byte:
179.   int digits = 1;
180.   // continually divide the value by ten, 
181.   // adding one to the digit count for each
182.   // time you divide, until you're at 0:
183.   int dividend = someValue /10;
184.   while (dividend > 0) {
185.     dividend = dividend /10;
186.     digits++;
187.   }
188.   // return the number of digits:
189.   return digits;
190. }

 

Source:http://www.lewei50.com/dev/doc/167(in Chinese)

COMMENTS

Please Login to comment
  Subscribe  
Notify of