Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <RCSwitch.h>
- #include <LowPower.h>
- #include <DHT.h> // DHT11 Temperature and Humidity Sensors Example include DHT library
- #define DHTPIN 10 //define as DHTPIN the Pin 3 used to connect the Sensor
- #define DHTTYPE DHT11 //define the sensor used(DHT11)
- DHT dht(DHTPIN, DHTTYPE); //create an instance of DHT
- int led = 13; // the pin that the LED is atteched to
- int sensor = 12; // the pin that the sensor is atteched to
- int state = LOW; // by default, no motion detected
- int val = 0; // variable to store the sensor status (value)
- int device = 00;
- int counter= 0;
- RCSwitch mySwitch = RCSwitch();
- void readPir();
- void readSensors();
- long readVoltage();
- void setup() {
- Serial.begin(9600); // initialize serial
- mySwitch.enableTransmit(10);
- pinMode(led, OUTPUT); // initalize LED as an output
- pinMode(sensor, INPUT); // initialize sensor as an input
- dht.begin(); //initialize the Serial communication
- delay(500);
- }
- void loop(){
- readPir();
- if(counter == 900){
- readSensors();
- counter = 0;
- }
- counter++;
- LowPower.powerDown(SLEEP_1S, ADC_OFF, BOD_OFF); // sleepmode 1 seconds
- }
- void readPir(){
- val = digitalRead(sensor); // read sensor value
- if (val == HIGH) { // check if the sensor is HIGH
- digitalWrite(led, HIGH); // turn LED ON
- delay(100); // delay 100 milliseconds
- if (state == LOW) {
- digitalWrite(led, HIGH);
- mySwitch.send(device, 0);
- state = HIGH; // update variable state to HIGH
- }
- }
- else {
- digitalWrite(led, LOW); // turn LED OFF
- if (state == HIGH){
- state = LOW; // update variable state to LOW
- }
- }
- }
- void readSensors(){
- int h = average(0); // reading Humidity
- int t = average(1); // read Temperature as Celsius (the default)
- int l = average(2);
- int v = average(3);
- mySwitch.send(device << h << t << l << v, 24);
- std::cout << to_string(device) << to_string(h) << to_string(t) << to_string(l) << to_string(v) << std::endl;
- }
- long readVoltage(){
- long result;
- // Read 1.1V reference against AVcc
- ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
- delay(2); // Wait for Vref to settle
- ADCSRA |= _BV(ADSC); // Convert
- while (bit_is_set(ADCSRA,ADSC));
- result = ADCL;
- result |= ADCH<<8;
- result = 1125300L / result; // Back-calculate AVcc in mV
- return result;
- }
- int average(int value){
- int averArray[5];
- int sum;
- for (int i = 0; i < 5; i = i + 1 ){
- if(value == 0)
- averArray[i] = dht.readHumidity();
- if (value = 1)
- averArray[i] = dht.readTemperature();
- if (value = 2)
- averArray[i] = analogRead(A1);
- if (value = 3)
- averArray[i] = readVoltage();
- delay(250);
- sum = sum + averArray[i];
- }
- return sum = sum / 5;
- }
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <thread>
- int couter =0;
- int main(void)
- {
- // this code sets up timer1 for a 1s @ 16Mhz Clock (mode 4)
- OCR1A = 0x3D08;
- TCCR1B |= (1 << WGM12);// Mode 4, CTC on OCR1A
- TIMSK1 |= (1 << OCIE1A);//Set interrupt on compare match
- TCCR1B |= (1 << CS12) | (1 << CS10);// set prescaler to 1024 and start the timer
- sei();// enable interrupts
- while (1)
- {
- // we have a working Timer
- if(couter == 1800){
- setup();
- readSensors();
- delay(2000);
- readVoltage();
- counter = 0;
- }
- }
- }
- ISR (TIMER1_COMPA_vect)
- {
- couter++;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement