【分享】自制环境参数检测装置接入 oneNet 平台(二)-Arduino中文社区 - Powered by Discuz! Archiver

xiebinqiang 发表于 2016-1-27 09:39

【分享】自制环境参数检测装置接入 oneNet 平台(二)

本帖最后由 xiebinqiang 于 2016-1-27 09:42 编辑

第二部分 传感器接入和数据采集 通信的问题解决了,下面在ardiuno板子上接入传感器,采集数据,三个传感器接线方式如下:

噪音传感器         ardiuno
GND-------------------GND
VCC-------------------5V
AO--------------------A0
DO-------------------悬空

温湿度传感器         ardiuno
GND------------------GND
VCC-------------------5V
AO-----10K上拉电阻----A1

关照传感器传感器         ardiuno
GND------------------------GND
VCC------------------------5V
SCL------------------------SCL
SDA------------------------SDA
ADD------------------------GND


添加了读取传感器数据的函数后,代码如下:

#include //温湿度
#include//IIC
#include

#define RET_OK   0
#define RET_ERR1

int BH1750address = 0x23;
byte buff;

#define DHTPIN A1
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

void setup() {
// put your setup code here, to run once:
pinMode(8, OUTPUT);
Serial.begin(115200);
dht.begin();
Wire.begin();//初始化IIC接口

while (!Serial) //
{
    ; // wait for serial port to connect. Needed for Leonardo only
}

//init WIFI module
WifiInit();
}

void WifiInit(void)
{
SendCmd("AT+CWMODE=3\r");
SendCmd("AT+RST\r");
//无线网络的SSID和密码
SendCmd("AT+CWJAP=\"hello-simon-30\",\"guajiguajiSIMONSEN2048\"\r");
SendCmd("AT+CIFSR\r");
SendCmd("AT+CIPSTART=\"TCP\",\"192.168.206.12\",8234\r");
//SendCmd("AT+CIPSTART=\"TCP\",\"183.230.40.33\",80\r");
SendCmd("AT+CIPMODE=1\r");
Serial.println("AT+CIPSEND\r");
}

void loop() {
float temp, hum;
int noise;
uint16_t light;

// put your main code here, to run repeatedly:
ReadTempHum(&hum, &temp);
noise = ReadVoice();
light = LightStrength();

//数据输出到服务器,此处为192.168.206.12的PC
Serial.print("temp:");
Serial.print(temp);
Serial.print("\thum:");
Serial.print(hum);
Serial.print("\tnoise:");
Serial.print(noise);
Serial.print("\tlight:");
Serial.println(light);
delay(2000);
//LedFlash();
}

void LedFlash(void)
{
digitalWrite(8, HIGH);
delay(500);
digitalWrite(8, LOW);
delay(500);
}

char SendCmd(String data)
{
Serial.println(data);//发送AT指令
while (1)
{
      if (Serial.find("OK") == true)
      {
          LedFlash();
          break;
      }
      Serial.println(data);
}
return RET_OK;
}

void ReadTempHum(float *h, float *t) //读取温湿度数据
{
*h = dht.readHumidity();
// Read temperature as Celsius
*t = dht.readTemperature();
// Read temperature as Fahrenheit
float f = dht.readTemperature(true);

// Check if any reads failed and exit early (to try again).
if (isnan(*h) || isnan(*t) || isnan(f))
{
    Serial.println("Failed to read from DHT sensor!");
    return;
}
}

int ReadVoice(void)//读取噪声数据
{
int temp;
temp = analogRead(A0);
temp = (long)100*temp/1024; //换算成分贝
//delay(1000);

return temp;
}

uint16_t LightStrength(void)//读取光照强度数据
{
   int i;
   uint16_t val=0;
   BH1750_Init(BH1750address);
   delay(200);
   //Serial.println("");
   if(2 == BH1750_Read(BH1750address))
   {
   val=((buff<<8)|buff)/1.2;
   //Serial.print(val,DEC);   
   //Serial.println("");
   }
delay(150);

return val;
}

int BH1750_Read(int address) //
{
int i=0;
Wire.beginTransmission(address);
Wire.requestFrom(address, 2);
while(Wire.available()) //
{
    buff = Wire.read();// receive one byte
    i++;
}
Wire.endTransmission();
return i;
}

void BH1750_Init(int address)
{
Wire.beginTransmission(address);
Wire.write(0x10);//1lx reolution 120ms
Wire.endTransmission();
}
编译后烧写到ardiuno板,重启,在PC端用网络调试工具查看数据传输结果,如下图所示:


表明各个传感器数据采集成功。
页: [1]
查看完整版本: 【分享】自制环境参数检测装置接入 oneNet 平台(二)