ArduinoServer

Arduino MEGA2560

传感器: DHT11,GP2Y10(Sharp)
网络: HLK-M03(透明串口,连接Arduino的Com3)
其他:VoltageChecker电压检测

/*
Sharp pin 1 (V-LED) => 5V (with a resistance of 150 Ohm)
 Sharp pin 2 (LED-GND) => Arduino GND pin
 Sharp pin 3 (LED) => Arduino pin 2
 Sharp pin 4 (S-GND) => Arduino GND pin
 Sharp pin 5 (Vo) => Arduino A0 pin
 Sharp pin 6 (Vcc) => 5V
 pin1 & pin2 binglian 220dianrong
 */

#include <stdio.h>
// DHT11
#define DHT11_PIN 4 // Pin:D4  
#define TIMEOUT 10000

// PM2.5
int dustPin=0;  //Pin:A0
float dustVal=0;
int ledPower=2;
int delayTime=280;
int delayTime2=40;
float offTime=9680;

// bettery voltage pin
int batteryPin = 1;  //Pin:A1

// comm data
String dataIn = "";
String dataOut = "";

//
int g_humidity = 0;
int g_temperature = 0;
float g_dust = 0.0;
float g_voltage = 0.0;

void setup() {
  // initialize both serial ports:
  Serial.begin(9600);
  Serial3.begin(9600);
  //must set GP2Y10,else data will be set 0
  pinMode(ledPower,OUTPUT);
}

void loop() {
  while(Serial3.available()) {
    dataIn += char(Serial3.read());
    delay(2);
  }
  if(dataIn.length()>0)
  {
    Serial.println(dataIn);
    //Serial3.println(dataIn);
    if( dataIn.equals("GetDHT11") )
    {
      int h = 0;
      int t = 0;
      GetDHT11(h, t);
      char data[128] = {
      };
      sprintf(data, "[{\"key\":\"H\", \"value\":\"%d\"}, {\"key\":\"T\", \"value\":\"%d\"}]", h, t);
      Serial.println(data);
      Serial3.println(data);
    }
    else if( dataIn.equals("GetGP2Y10") )
    {
      float gp = 0.0;
      GetGP2Y10(gp);
      char val[7]={
      };
      dtostrf(gp, 7, 2, val);
      char data[128] = {
      };
      sprintf(data, "{\"key\":\"GP2Y10\", \"value\":\"%s\"}", val);
      Serial.println(data);
      //Serial3.println(val);
      Serial3.println(data);
    }
    else if( dataIn.equals("GetVoltage") )
    {
      float v = 0.0;
      GetVoltage(v);
      char val[5]={
      };
      dtostrf(v, 5, 2, val);
      char data[64] = {
      };
      sprintf(data, "{\"key\":\"Voltage\", \"value\":\"%s\"}", val);
      Serial.println(data);
      Serial3.println(data);
    }
    else if( dataIn.equals("") )
    {
    }
    else
    {
    }
    dataIn = "";
  }
}
void GetGP2Y10(float& val)
{
  // ledPower is any digital pin on the arduino connected to Pin 3 on the sensor
  digitalWrite(ledPower,LOW); // power on the LED
  delayMicroseconds(delayTime);
  dustVal=analogRead(dustPin); // read the dust value via pin 5 on the sensor
  delayMicroseconds(delayTime2);
  digitalWrite(ledPower,HIGH); // turn the LED off
  delayMicroseconds(offTime);
  //delay(1000);
  //Serial.println(dustVal);
  val = (float(dustVal/1024)-0.0356)*120000*0.035;
  //Serial.println((float(dustVal/1024)-0.0356)*120000*0.035);
  Serial.println(val);
}

void GetDHT11(int& h, int& t)  
{
  int pin = DHT11_PIN;
  int bits[5];
  // INIT BUFFERVAR TO RECEIVE DATA  
  int cnt = 7;  
  int idx = 0;  

  // EMPTY BUFFER  
  for (int i=0; i< 5; i++)
    bits[i] = 0;  

  // REQUEST SAMPLE  
  pinMode(pin, OUTPUT);  
  digitalWrite(pin, LOW);  
  delay(20);  
  digitalWrite(pin, HIGH);  
  delayMicroseconds(40);  
  pinMode(pin, INPUT);  

  // GET ACKNOWLEDGE or TIMEOUT  
  unsigned int loopCnt = TIMEOUT;  
  while(digitalRead(pin) == LOW)  
    if (loopCnt-- == 0) return;  

  loopCnt = TIMEOUT;  
  while(digitalRead(pin) == HIGH)  
    if (loopCnt-- == 0) return ;  

  // READ THE OUTPUT - 40 BITS => 5 BYTES  
  for (int i=0; i<40; i++)  
  {  
    loopCnt = TIMEOUT;  
    while(digitalRead(pin) == LOW)  
      if (loopCnt-- == 0) return ;  

    unsigned long t = micros();  

    loopCnt = TIMEOUT;  
    while(digitalRead(pin) == HIGH)  
      if (loopCnt-- == 0) return ;  

    if ((micros() - t) > 40) bits[idx] |= (1 << cnt);  
    if (cnt == 0)   // next byte?  
    {  
      cnt = 7;     
      idx++;        
    }  
    else cnt--;  
  }  

  Serial.print(bits[0],1);  
  Serial.print(",");
  Serial.println(bits[2],1);  
  h = bits[0];
  t = bits[2];
  //sprintf(value, "%d|%d", bits[0], bits[1]);
  return;  
}  

void GetVoltage(float& volts)
{
  int referenceVolts = 5;
  int R1 = 1000;
  int R2 = 1000;
  float  resistorFactor = 1023.0 *  R2 / (R2+R1);  //[not use] float  resistorFactor = 1023.0 *  (R2 / (R2+R1));  
  int val = analogRead(batteryPin);
  volts = (val/resistorFactor) * referenceVolts;
  Serial.println(volts);
}

 

添加新评论