-
Notifications
You must be signed in to change notification settings - Fork 0
/
IOTsmartPlug.ino
105 lines (76 loc) · 2.36 KB
/
IOTsmartPlug.ino
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
#include <Wire.h>
#include <UnoWiFiDevEd.h>
#include <dht.h>
//ThingSpeak Defines
#define CONNECTOR "rest"
#define SERVER_ADDR "api.thingspeak.com"
#define APIKEY_THINGSPEAK "****************" //Your API Key
//Pins Defines
#define DHT11PIN 3 //temp sensor
#define ldrPin A1 //light sensor
#define relayPin 8 //5V relay
#define pirPin 13 //motion sensor
dht DHT; //For the DHT11 sensor
void setup() {
Ciao.begin(); // CIAO INIT
Serial.begin(9600);
//Pins Mode
pinMode(DHT11PIN, INPUT);
pinMode(ldrPin, INPUT);
pinMode(pirPin, INPUT);
pinMode(relayPin, OUTPUT);
}
void loop() {
int chk = DHT.read11(DHT11PIN);
int Temp = DHT.temperature;
int Humi = DHT.humidity;
int Light = analogRead(ldrPin);
int Motion = digitalRead(pirPin);
Ciao.println(Temp);
Ciao.println(Humi);
Ciao.println(Light);
Ciao.println(Motion);
//Sending The Data to ThingSpeak
String uri = "/update?api_key=";
uri += APIKEY_THINGSPEAK;
uri += "&field1=";
uri += String(Temp);
uri += "&field2=";
uri += String(Humi);
uri += "&field3=";
uri += String(Light);
uri += "&field4=";
uri += String(Motion);
//Ciao data is limited to 64 character
Ciao.println("Send data on ThingSpeak Channel");
CiaoData data = Ciao.write(CONNECTOR, SERVER_ADDR, uri);
if (!data.isEmpty()){
Ciao.println( "State: " + String (data.get(1)) );
Ciao.println( "Response: " + String (data.get(2)) );
}
else{
Ciao.println("Write Error");
}
//Power Automation Controller
if (Temp > 25){
if(Light > 800){ //still the light is on
if(Motion == 0){ //no movment
digitalWrite(relayPin, LOW); //disable the power socket
}
}
}
else
digitalWrite(relayPin, HIGH);
delay(10000); // Thinkspeak policy for sending data
}
/* Extra Diteals
* Arduino uno Wifi
*
* Conected thro the web interface to the HotSpot created by my phone.
*
* Using ThingSpeak for the cloud.
* Public ThingSpeak Cloud View:
* https://thingspeak.com/channels/453554
*
*
* */