使用樂為物聯GPRS模塊搭建遠程環境監測系統
06-24
本帖最後由 數字 於 2013-3-28 11:06 編輯 使用樂為物聯GPRS模塊搭建遠程環境監測系統1 目的 試驗基於Arduino硬體平台,採用樂為物聯GPRS模塊監控不具備有線網路接入條件的山區環境,可以用於農業、林業數據採集。2 實驗條件 硬體設備列表:1) Arduino Nano約50RMB, 2) 樂為物聯GPRS模塊為測試版(必要的GPRS資費SIM卡),3) DSM501A模塊約30RMB,顆粒物感測器,目的在於監測花粉濃度,PWM輸出,4) HTF3223LF模塊23RMB,用於監測空氣溫濕度,NTC溫度輸出(試驗採用10k上拉),濕度頻率輸出,5) BH1750模塊13RMB,監測光照強度,I2C輸出,6) 降雨感測器,大約10元,用於監測降雨情況,邏輯電平輸出,7) LCD1602,大約10元, 數據系統平台:樂聯網http://www.lewei50.com3 硬體連線1) 樂為物聯GPRS模塊為6腳,本次只用1、2、3、4腳,1腳為Vcc接電源5-12伏輸入,2腳為接地,3腳為TTL電平TX,4腳為TTL電平RX。 2) DSM501A,本次只使用2、3、5腳,2腳為Vout 2 output (PWM)接Arduino D8, 3腳為Vcc接電源5伏,5腳為GND接地。 3) HTF3223LF模塊,使用1、2、3、4腳,1腳為NTC輸出端,接Arduino A0,2腳為GND接地,3腳為濕度頻率輸出,接Arduino D2,4腳為Vcc接電源5伏。 4) BH1750模塊為5腳,I2C介面,本次不用接ADD腳,VCC接電源5伏,SCL接Arduino A5,SDA接Arduino A4,GND接地。 5) 降雨感測器為4腳,邏輯輸出接Arduino D10,VCC接電源5伏,GND接地。 6) LCD1602,本次採用4數據線連接法,RS 接Arduino D12,Enable接Arduino D11, LCD D4接Arduino D4,LCD D5接Arduino D5,LCD D6接Arduino D6,LCD D7接Arduino D7,LCD R/W 接地,LCD V0接10k可調電阻,LCD使用5伏供電,背景燈可設開關。4 程序ARDUINO 代碼複製列印
- #include<string.h>
- //#include <SoftwareSerial.h>
- #include <Wire.h>
- #include <math.h>
- //#include <MsTimer2.h>
- #include <LiquidCrystal.h>
- LiquidCrystal lcd(12, 11, 4, 5, 6, 7);
- //* LCD RS pin to digital pin 12
- // * LCD Enable pin to digital pin 11
- // * LCD D4 pin to digital pin 4
- // * LCD D5 pin to digital pin 5
- // * LCD D6 pin to digital pin 6
- // * LCD D7 pin to digital pin 7
- // * LCD R/W pin to ground
- // * 10K resistor:
- // * ends to +5V and ground
- // * wiper to LCD VO pin (pin 3)
- //SoftwareSerial mySerial(9, 10); // RX, TX
- //String inputString = "";
- const int buttonPin = 10; //the number of the jiangyu input pin
- int buttonState = 0; // variable for reading the jiangyu status
- int Rain = 1;
- int BH1750address = 0x23;//BH1750 I2C地址
- byte buff[2];
- float BH = 0;
- const int analogInPin = A0;// Analog input pin that the potentiometer is attached to//*//Freqinput pin D2
- int pin = 8;//DSM501A input D8
- float wuchaxiuzheng = 0;
- unsigned long duration;
- unsigned long starttime;
- unsigned long endtime;
- unsigned long sampletime_ms = 30000;
- unsigned long lowpulseoccupancy = 0;
- float ratio = 0;
- float concentration = 0;
- float tempconcentration=0;
- int sensorValue = 0; // value read from the NTC
- int TEMPRATURE=0;
- int i=0;
- float R_Data=0;
- float U_data [111]={4.67,4.65,4.64,4.62,4.60,4.57,4.55,4.53,4.51,4.48,4.46,4.43,4.40,4.38,4.35,4.32,4.28,4.25,4.22,4.19,4.15,4.11,4.08,4.04,4.00,3.96,3.92,3.88,3.83,3.79,3.75,3.70,3.65,3.61,3.56,3.51,3.47,3.42,3.37,3.32,3.27,3.22,3.17,3.11,3.06,3.01,2.96,2.91,2.86,2.81,2.75,2.70,2.65,2.60,2.54,2.50,2.44,2.39,2.34,2.29,2.24,2.20,2.15,2.10,2.06,2.01,1.96,1.92,1.87,1.83,1.78,1.74,1.70,1.66,1.62,1.58,1.54,1.50,1.47,1.43,1.39,1.36,1.32,1.29,1.25,1.22,1.19,1.15,1.12,1.09,1.06,1.04,1.01,0.98,0.96,0.93,0.91,0.88,0.86,0.84,0.81,0.79,0.77,0.75,0.73,0.713,0.69,0.67,0.66,0.64,0.62};//溫度對應NTC電錶格
- int divider[6] = {0, 1, 8, 64, 256, 1024};//read RH use interrupt get F
- int prescaler = 5;
- double count = 0;
- double middle = 0;
- char x = 0;
- int Fout=0;
- int RH=0;
- byte du[8]={
- B00111,
- B00101,
- B00111,
- B00000,
- B00000,
- B00000,
- B00000,
- };//define 1602 char
- ISR(TIMER1_OVF_vect)
- {
- if (prescaler < 4)
- {
- prescaler++;
- }
- }
- void interrupt()
- {
- if (!x)
- {
- count = TCNT1;
- TCNT1 = 0x000;
- TCCR1B = prescaler;
- attachInterrupt(0, interrupt, FALLING);
- }
- else
- {
- middle = TCNT1;
- attachInterrupt(0, interrupt, RISING);
- }
- x = ~x;
- }
- void setup()
- {
- Wire.begin();
- Serial.begin(9600);
- //mySerial.begin(9600);
- //inputString.reserve(200);
- //MsTimer2::set(2000, timer); //定時器設置,每2秒觸發一次timer函數操作
- //MsTimer2::start();
- //pinMode(buttonPin, INPUT);
- analogReference(DEFAULT);//INTERNAL
- lcd.createChar(1,du);
- lcd.begin(16, 2);
- lcd.clear();
- // lcd.setCursor(0, 0);
- // lcd.print("PM2.5=");
- // lcd.setCursor(0, 1);
- // lcd.print("T= ");
- // //lcd.write(1);
- // lcd.print(" RH=%");
- // Serial.begin(9600);
- pinMode(buttonPin, INPUT);
- pinMode(8,INPUT);
- TIMSK1 = 0x01;
- TCCR1A = 0x00;
- attachInterrupt(0, interrupt, RISING);
- starttime = millis();
- }
- void loop()
- {
- duration = pulseIn(pin, LOW);
- lowpulseoccupancy += duration;
- endtime = millis();
- if ((endtime-starttime) > sampletime_ms)
- {
- ratio = (lowpulseoccupancy-endtime+starttime + sampletime_ms)/(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("DSM501A:");
- // Serial.println(concentration);
- // Serial.print(";");
- //Serial.println(endtime-starttime);
- wuchaxiuzheng = (endtime-starttime-30000)/30000.0;
- //Serial.println(wuchaxiuzheng);
- concentration = (1.00-wuchaxiuzheng)*concentration;
- //Serial.println(concentration);
- lcd.setCursor(6, 0);
- lcd.print(concentration);
- lcd.print("pcs/0.01cf");
- // lowpulseoccupancy = 0;
- // starttime = millis();
- }
- sensorValue = analogRead(analogInPin); // read the analog in value:
- delay(10);
- R_Data=sensorValue*5/1024.0;
- //Serial.print("R_Data = " );
- //Serial.print(R_Data);
- for(i=0;i<111;i++){
- if(R_Data>4.67)
- {
- TEMPRATURE =-30;//對應-30℃
- }
- else if(R_Data<0.62)
- {
- TEMPRATURE=80; //對應80℃
- }
- else if(R_Data<U_data[i]&&R_Data>U_data[i+1])
- {
- TEMPRATURE=int(i-30-3);
- }
- }
- // Serial.print("T:" );
- // Serial.print(TEMPRATURE);
- // Serial.print(";" );
- delay(10);
- lcd.setCursor(0, 0);
- lcd.print("PM2.5=");
- lcd.setCursor(0, 1);
- lcd.print("T= ");
- //lcd.write(1);
- lcd.print(" RH=%");
- // lcd.setCursor(0, 1);
- // lcd.print("T= ");
- // //lcd.write(1);
- // lcd.print(" RH=%");
- lcd.setCursor(2, 1);
- lcd.print(TEMPRATURE);
- lcd.write(1);
- lcd.print("C");
- Fout=(16000000.0 / divider[prescaler] / count);
- //Serial.print("Vsensor=" );
- //Serial.print(sensorValue);
- //Serial.print(", " );
- //Serial.print("Freq=");
- //Serial.print(Fout);
- // Serial.print(" ");
- // Serial.print(int(divider));
- // Serial.print(" ");
- // Serial.print(prescaler);
- // Serial.print(" ");
- // Serial.println(count);
- if (Fout<8115)
- {
- RH=100;
- // Serial.print("RH:100");
- // Serial.println(";");
- lcd.setCursor(13, 1);
- lcd.print("H.");
- }
- else if(Fout>9595)
- {
- RH=0;
- // Serial.print("RH:0");
- // Serial.println(";");
- lcd.setCursor(13, 1);
- lcd.print("L.");
- }
- else
- {
- RH=(9595-Fout)/14.8;
- // Serial.print("RH:");
- // Serial.print(RH);
- // Serial.println(";");
- lcd.setCursor(13, 1);
- lcd.print(RH);
- }
- if (prescaler > 1)
- {
- prescaler--;
- delay(200);
- }
- //if(flag )//
- //{
- // Serial.print( BH1750() );
- // Serial.println("[lux]");
- //
- // flag = 0;//歸零,等著定時中斷重新賦值
- //}
- delay(2000);
- if(tempconcentration!=concentration)
- {
- tempconcentration=concentration;
- BH=BH1750();
- // read the state of the jiangyu value:
- buttonState = digitalRead(buttonPin);
- // check if the jiangyu is low.
- // if it is, the buttonState is LOW:
- if (buttonState == LOW) {
- Rain=0;
- }
- else {
- Rain=1;
- }
- //lewei ouput by computer tools
- // Serial.print("D:");
- // Serial.print(concentration);
- // Serial.print(";");
- // Serial.print("T:" );
- // Serial.print(TEMPRATURE);
- // Serial.print(";" );
- // Serial.print("R:");
- // Serial.print(RH);
- // Serial.print(";" );
- // Serial.print("B:" );
- // Serial.print(BH);
- // Serial.print(";" );
- // Serial.print("W:");
- // Serial.print(Rain);
- // Serial.println(";");
- //lewei ouput by GPRS
- lcd.setCursor(0, 1);
- lcd.print(" ");
- lcd.setCursor(0, 1);
- lcd.print(BH);
- lcd.print("LUX");
- //delay(5000);
- String tempstring="@uploading(01,D,";
- String tempstring1=")";
- Serial.print(tempstring);
- Serial.print(int(concentration));
- Serial.println(tempstring1);
- // delay(12000); min transtime
- delay(30000);
- String tempstring2="@uploading(01,T,";
- String tempstring3=")";
- Serial.print(tempstring2);
- Serial.print(TEMPRATURE);
- Serial.println(tempstring3);
- // delay(12000);
- delay(30000);
- String tempstring4="@uploading(01,R,";
- String tempstring5=")";
- Serial.print(tempstring4);
- Serial.print(RH);
- Serial.println(tempstring5);
- // delay(15000);
- delay(30000);
- String tempstring6="@uploading(01,B,";
- String tempstring7=")";
- Serial.print(tempstring6);
- Serial.print(int(BH));
- Serial.println(tempstring7);
- // delay(2000);
- delay(30000);
- String tempstring8="@uploading(01,W,";
- String tempstring9=")";
- Serial.print(tempstring8);
- Serial.print(Rain);
- Serial.println(tempstring9);
- delay(30000);
- lowpulseoccupancy = 0;
- starttime = millis();
- }
- }
- double BH1750() //BH1750設備操作
- {
- int i=0;
- doubleval=0;
- //開始I2C讀寫操作
- Wire.beginTransmission(BH1750address);
- Wire.write(0x10);//1lx reolution 120ms//發送命令
- Wire.endTransmission();
- delay(200);
- //讀取數據
- Wire.beginTransmission(BH1750address);
- Wire.requestFrom(BH1750address, 2);
- while(Wire.available()) //
- {
- buff[i] = Wire.read();// receive one byte
- i++;
- }
- Wire.endTransmission();
- if(2==i)
- {
- val=((buff[0]<<8)|buff[1])/1.2;
- }
- if(val<0) val=56134+val;
- return val;
- }
5 總結 樂為gprs模塊使用非常簡單,但是要注意供電問題,我用5v1.5a開關電源有時會出現初始化無法通過的問題,後來改用12v1.5a開關電源一切正常。樂為gprs命令非常簡單,通常可以先用串口連接PC設置key,使用串口工具發送@key=你的key值,模塊返回key=你的key值,然後就可以當作黑盒子使用了。因為數據操作通常只使用上傳命令@uploading(a,b,c),a為網關標識(2位數字),b 設備標識(1-8位字元),c 為上傳數值(如果是負數,請在數值前加『-』號),簡單情況下根本不需要考慮交互反饋的判斷條件,只需要等待適當的延時就可以了。希望今後可以考慮簡訊控制的輸出協議,便於同時使用簡訊操作Arduino,也希望能將GPRS模塊協議與樂聯網串口工具協議保持一致,便於程序調試的兼容。試驗結果可以訪問http://open.lewei50.com/home/gatewaystatus/361初學Arduino,程序中的問題還請大家批評指正。上幾張照片看看 2013-3-28 11:05 上傳下載附件 (53.49 KB)
推薦閱讀:
※建設霧霾監測系統實現霧霾實時監測隨筆
※環保部通報2015年典型環境違法案件
※DR-801D橋樑採樣器
※水質自動採樣器-自密封作用到底有多重要?
※水質自動採樣器簡介