This repository was archived by the owner on Nov 24, 2025. It is now read-only.
forked from ali2560/iot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode1
More file actions
136 lines (133 loc) · 3.26 KB
/
node1
File metadata and controls
136 lines (133 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <LowPower.h>
#include <elapsedMillis.h>
#include <SimpleTimer.h>
#define interval 100
#include "DHT.h"
#define DHTPIN 5
#define DHTTYPE DHT22 // DHT 22 (AM2302)
void mydelay(int x);
void read_sensor();
void TX(int t, int h, int l, int g, int w);
void wakeUp();
DHT dht(DHTPIN, DHTTYPE);
RF24 radio(9,10);
elapsedMillis stopwatch;
SimpleTimer timer;
boolean flag = false;
const byte rxAddr_p0[] = "0xB3B0";
const int light_pin = A2, Gass_pin = A0,wakeUpPin = 3;
float temp=25, humidity;
int node_address=1;
char out_char[32];
char charbuff[10];
int dataready=0;
String stringbuff = "";
int lightlevel=0, high = 0, low = 1023;
int interrupt_handled=0;
int index=0,motion=0;
int sensorvalue; // variable to store the value coming from the sensor
void read_sensor(){
temp=dht.readTemperature();
humidity=dht.readHumidity();
lightlevel = analogRead(light_pin);
sensorvalue = analogRead(Gass_pin);
TX(temp, humidity,lightlevel, sensorvalue, motion);
}
void TX(int t, int h, int l, int g, int m){
radio.stopListening();
radio.openWritingPipe(rxAddr_p0);
sprintf(out_char,"@%d,t1%d,h1%d,l1%d,g1%d,m1%d,\n",node_address,t,h,l,g,m);
for(int i=0;i<32;i++){
if(out_char[i]=='\n')
index=i+1;
}
radio.write(out_char,index);
radio.startListening();
radio.openReadingPipe(0, rxAddr_p0);
stopwatch=0; //stopwatch reset
flag=false;
while(interval>stopwatch){
if (radio.available()){
char text[32] = {0};
radio.read(&text, sizeof(text));
int result=strcmp(text, "ack1");
if(result==0){
Serial.println(text);
motion=0;
flag=true;
break;
}
}
}
if(flag){
for(int i=0;i<32;i++)
{
out_char[i]=0;
}
}
else{
radio.stopListening();
radio.openWritingPipe(rxAddr_p0);
radio.write(out_char,sizeof(out_char));
}
}
void mydelay(int x){
for(unsigned int i=0; i<=x; i++)
{
delay(100);
}
}
void wakeUp(){
interrupt_handled=1;
motion=1;
digitalWrite(4, 1);
dht.begin();
//Serial.println("motion detection");
TX(temp, humidity, lightlevel, sensorvalue, motion);
}
void setup()
{
//To reduce power, setup all pins as inputs with no pullups
for(int x = 1 ; x < 18 ; x++)
{
pinMode(x, INPUT);
digitalWrite(x, LOW);
}
while (!Serial);
Serial.begin(9600);
radio.begin();
radio.setRetries(10,5);
radio.openWritingPipe(rxAddr_p0);
radio.stopListening();
// Configure wake up pin as input.
// This will consumes few uA of current.
pinMode(wakeUpPin, INPUT);
pinMode(7,OUTPUT);
pinMode(5,OUTPUT);
pinMode(4,OUTPUT);
digitalWrite(4,1);
delay(200);
dht.begin();
radio.write("node 1 test",sizeof("node 1 test"));
}
void loop()
{
attachInterrupt(digitalPinToInterrupt(wakeUpPin), wakeUp, RISING);
if(flag){
digitalWrite(4, 0);
delay(1000);
LowPower.powerDown(SLEEP_4S, ADC_OFF, BOD_OFF);
attachInterrupt(digitalPinToInterrupt(wakeUpPin), wakeUp, RISING);
LowPower.powerDown(SLEEP_4S, ADC_OFF, BOD_OFF);
}
if(interrupt_handled==0){
digitalWrite(4, 1);
delay(500);
dht.begin();
read_sensor();
}
interrupt_handled=0;
}