Пример #1
0
/**
 * Main function
 *
 * Parse the command line arguments, then every 5 minutes reads the sensor data
 * If the data is valid, push it to IoT lab
 *
 */
int main( int argc, char ** argv ) {
	char * additional_tags;
	size_t len = 0;
	int i;
	sensor_data data;

	if ( wiringPiSetup() == -1 )
	exit(EXIT_FAILURE);

	// Parse arguments
	if (argc >= 3) {
		if (argc >= 4) {
			// Concat additional tags arguments in a single char*
			for (i=3; i<argc; i++) {
				len += strlen(argv[i]);
			}
			len += (argc-2);
			additional_tags = malloc(len*sizeof(char));
			additional_tags[0] = '\0';
			for (i=3; i<argc; i++) {
				strcat(additional_tags, argv[i]);
				strcat(additional_tags, " ");
			}
		} else {
			additional_tags = malloc(sizeof(char));
			additional_tags[0] = '\0';
		}
		while (1) {
			data = read_dht11_dat();
			if (data.status == valid) {
				printf("%ld Send data hum=%s temp=%s\n", time(NULL), data.hum, data.temp);
				sendByTelnet(data, argv[1], argv[2], additional_tags);
			} else {
				printf("%ld Error getting sensor data, abort push data\n", time(NULL));
			}
			sleep(5*60); // Restart every 5 minutes
		}
	} else {
		printf("Usage %s token-id:token-key metric [additional tags]\n", argv[0]);
	}

	free(additional_tags);
	return(EXIT_SUCCESS);
}
Пример #2
0
int main (int argc, char **argv)
{
	int wifiCount=0;
	int peakSound=0;
	int meanSound=0;
	int humAttempt=0;
	char disp1[30];
	char disp2[30];

	fd = wiringPiI2CSetup(LCDAddr);
	// Setup pcf8591 on base pin 120, and address 0x48
	pcf8591Setup (PCF, 0x48);
	init();

	if (wiringPiSetup () == -1)
		exit (1) ;

	// Set up wifiCount if the parameter was passed
	if (argc>1) {
		wifiCount=atoi(argv[1]);
	} else {
		wifiCount=getWifiCount();
	}
	peakSound=getPeakSound(1000);
	meanSound=getMeanSound(100);
	while((read_dht11_dat()!=1)&& (humAttempt !=5)) {
		delay(1000);
		humAttempt++;
	}
	clear();
	sprintf(disp1,"Mob %d Snd %d (%d)", wifiCount, meanSound, peakSound);  
	if((humAttempt==5)) {
		sprintf(disp2,"Hum ?? Tem ??"); 
	} else {
		sprintf(disp2,"Hum %d.%d Tem %d.%d", dht11_dat[0], dht11_dat[1], dht11_dat[2], dht11_dat[3]); 
	}
	write(0, 0, disp1);
	write(0, 1, disp2);
	return 0 ;
}
Пример #3
0
int main( void )
{
    int a[4]={0,0,0,0};
    int i;
    if ( wiringPiSetup() < 0 )
        return -1;
    /*修改灯的状态为输出状态*/
    pinMode(LED_1,OUTPUT);
    pinMode(LED_2,OUTPUT);
    pinMode(LED_3,OUTPUT);
    pinMode(LED_4,OUTPUT);
    /*传入4个参数分别控制4个灯的状态*/
    for(i=0;i<4;i++)
       scanf("%d",&a[i]);
   control_led_Open(a[0],a[1],a[2],a[3]);
    while ( 1 )
    {   
        /*读取dht11数据函数*/ 
        read_dht11_dat();
        delay( 1000 ); 
    }

    return(0);
}
Пример #4
0
bool DHT11::trans(void)
{
    if (millis() - _timestamp < 1000)
    {
        Serial.println("double read dht11");
        return true;
    }

    uint8_t dht11_in;
    bool res = false;
    uint8_t dht11_check_sum = 0;
    // start condition
    // 1. pull-down i/o pin from 18ms
    PORTC &= ~_BV(_pin);
    delay(18);
    PORTC |= _BV(_pin);
    delayMicroseconds(40);

    DDRC &= ~_BV(_pin);
    delayMicroseconds(40);

    dht11_in = PINC & _BV(_pin);

    if (dht11_in) {
        Serial.println("dht11 start condition 1 not met");
        goto _dht11_exit;
    }
    delayMicroseconds(80);

    dht11_in = PINC & _BV(_pin);

    if (!dht11_in) {
        Serial.println("dht11 start condition 2 not met");
        goto _dht11_exit;
    }
    delayMicroseconds(80);

    // now ready for data reception
    for (uint8_t i = 0; i < 5; i++)
    {
        data_buf[i] = read_dht11_dat();
        dht11_check_sum += data_buf[i];
    }

    DDRC |= _BV(_pin);
    PORTC |= _BV(_pin);

    // check check_sum
    if(data_buf[4] != dht11_check_sum - data_buf[4])
    {
        Serial.println("DHT11 checksum error");
        goto _dht11_exit;
    }

    _timestamp = millis();
    res = true;

_dht11_exit:
    // re-init
    init();
    return res;
}
Пример #5
0
void DHT11() {
	byte dht11_dat[5];
	byte dht11_in;
	byte i;
	
	// output
	DHT_SetDir(1);
	// pull-down i/o pin for 18ms
	DHT_ClrVal();
	WAIT1_Waitms(19);
	DHT_SetVal();
	//pull-up i/o pin for 30ms
	WAIT1_Waitus(30);

	//input
	DHT_SetDir(0);
	
	WAIT1_Waitus(30);

	while(!dht11_in)
	{
		dht11_in = DHT_GetVal();
	}
//	if (dht11_in) {
//		printf("dht11 start condition 1 not met\r\n");
//		return;
//	}
	WAIT1_Waitus(80);//delayMicroseconds(80);
//	dht11_in = DHT_GetVal();//dht11_in = PINC & _BV(DHT11_PIN);
//	if (!dht11_in) {
//		printf("dht11 start condition 2 not met\r\n");
//		return;
//	}
//	WAIT1_Waitus(80);//delayMicroseconds(80);
	// now ready for data reception
	for (i = 0; i < 5; i++)
		dht11_dat[i] = read_dht11_dat();
	DHT_SetDir(1);
	DHT_SetVal();
	byte dht11_check_sum = dht11_dat[0] + dht11_dat[1] + dht11_dat[2]
			+ dht11_dat[3];
	// check check_sum
	if (dht11_dat[4] != dht11_check_sum) {
		printf("DHT11 checksum error\r\n");
	}
	

	
	printf("Current humdity = ");
	//printf("%d",dht11_dat[0]);
	
	UTIL1_Num8uToStr(strTmp1, sizeof(strTmp1),dht11_dat[0]);
	UTIL1_Num8uToStr(strTmp2, sizeof(strTmp2),dht11_dat[1]);
	UTIL1_strcat(strTmp1, sizeof(strTmp1),(byte *)".");
	UTIL1_strcat(strTmp1, sizeof(strTmp1),strTmp2);
	UTIL1_strcat(strTmp1, sizeof(strTmp1),(byte *)"%%\r\n");
	
	//printf((char *)strTmp1);
	//printf(".");
	//printf((char *)strTmp2);
	//printf("%d",dht11_dat[1]);	
	//printf("%%\r\n");
	printf((char *)strTmp1);
	
	printf("temperature = ");
	printf("%d",dht11_dat[2]);
	printf(".");
	printf("%d",dht11_dat[3]);
	printf("C \r\n");
//	WAIT1_Waitms(2000);//delay(2000);
}
Пример #6
0
boolean DHT::read(void) {
  uint8_t laststate = HIGH;
  uint8_t counter = 0;
  uint8_t j = 0, i;
  unsigned long currenttime;
  digitalWrite(_pin, HIGH);
  delay(250);

  currenttime = millis();
  if (currenttime < _lastreadtime) {  
    _lastreadtime = 0;
  }
  if (!firstreading && ((currenttime - _lastreadtime) < 2000)) {
    return true; // return last correct measurement
  }
  firstreading = false;
  _lastreadtime = millis();

  data[0] = data[1] = data[2] = data[3] = data[4] = 0;
  
  
  pinMode(_pin, OUTPUT);
  digitalWrite(_pin, LOW);
  delay(10);
 // cli();
  digitalWrite(_pin, HIGH);
  delayMicroseconds(40);
  //delay(1);
  pinMode(_pin, INPUT);
  while(digitalRead(_pin)){
    Serial.println("dht11 start condition 1 not met");
  }
  delayMicroseconds(80);

  while(!digitalRead(_pin)){
    Serial.println("dht11 start condition 2 not met");
  }
  delayMicroseconds(80);
  
  for (i=0; i<5; i++){
    data[i] = read_dht11_dat();
	}
 
  // for ( i=0; i< MAXTIMINGS; i++) {
    // counter = 0;
    // while (digitalRead(_pin) == laststate) {
      // counter++;
      // delayMicroseconds(1);
	  // //delay(1);
      // if (counter == 255) {
        // break;
      // }
    // }
    // laststate = digitalRead(_pin);

    // if (counter == 255) break; // ignore first 3 transitions
    // if ((i >= 4) && (i%2 == 0)) {
      // data[j/8] <<= 1;
      // if (counter > _count)
        // data[j/8] |= 1;
      // j++;
    // }
	
  // }

  //sei();
  
  

  // check we read 40 bits and that the checksum matches
 // if ((j >= 40) && 
 if (
      (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) ) {
    return true;
  }
  

  return false;

}
Пример #7
0
void *ChangeTHRepresentation (void *param)
{
    (void)param;
    OCStackResult result = OC_STACK_ERROR;
    modCounter += 1;
    if (modCounter % 10 ==
        0) // Matching the timing that the Linux Sample Server App uses for the same functionality.
    {

        byte dht11_dat[5];
        byte i;// start condition

        digitalWrite(dht11_pin, LOW);
        delay(18);
        digitalWrite(dht11_pin, HIGH);
        delayMicroseconds(1);
        pinMode(dht11_pin, INPUT);
        delayMicroseconds(40);

        if (digitalRead(dht11_pin))
        {
            Serial.println("dht11 start condition 1 not met"); // wait for DHT response signal: LOW
            delay(1000);
            return NULL;
        }
        delayMicroseconds(80);
        if (!digitalRead(dht11_pin))
        {
            Serial.println("dht11 start condition 2 not met");  //wair for second response signal:HIGH
            return NULL;
        }

        delayMicroseconds(80);// now ready for data reception
        for (i = 0; i < 5; i++)
        {
            dht11_dat[i] = read_dht11_dat();
        }  //recieved 40 bits data. Details are described in datasheet

        pinMode(dht11_pin, OUTPUT);
        digitalWrite(dht11_pin, HIGH);
        byte dht11_check_sum = dht11_dat[0] + dht11_dat[2]; // check check_sum
        if (dht11_dat[4] != dht11_check_sum)
        {
            Serial.println("DHT11 checksum error");
        }
        Serial.print("Current humdity = ");
        Serial.print(dht11_dat[0], DEC);
        Serial.print("%  ");
        Serial.print("temperature = ");
        Serial.print(dht11_dat[2], DEC);
        Serial.println("C  ");

        TH.m_humid = dht11_dat[0];
        TH.m_temp = dht11_dat[2];

        if (g_THUnderObservation)
        {
            OC_LOG_V(INFO, TAG, " =====> Notifying stack of new humid level %d\n", TH.m_humid);
            OC_LOG_V(INFO, TAG, " =====> Notifying stack of new temp level %d\n", TH.m_temp);

            result = OCNotifyAllObservers (TH.m_handle, OC_NA_QOS);

            if (OC_STACK_NO_OBSERVERS == result)
            {
                g_THUnderObservation = 0;
            }
        }
    }
    return NULL;
}