-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.ino
121 lines (104 loc) · 3.25 KB
/
Main.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <Servo.h>
#include <DHT.h>
#include <CavyIoTdevelopmentBoard.h>
#define SERVO 9
Servo myservo;
DHT dht(8, DHT11);
//---------------For communication with DevBoard ----------------------------
#define rx 10
#define tx 11
#define rst 13
CavyIoT myIoTdevice;
int routerScheme = 1;
long int current_time;
long int start_time = millis();
long int Interval = 6 * 60 * 60 * 1000; // Timer Six hour
// Interval=60000;//Uncomment for checking servo functions TimerOne minute.
float temp;
float hum;
int angle;
void setup()
{
Serial.begin(9600);
myIoTdevice.SetPort(rx, tx, rst);
dht.begin();
myservo.write(45);
myservo.attach(9);
myservo.detach();
Serial.println("Setting device buttons!.");
myIoTdevice.DefineButtonLables("Heat", "on", "off",
"Humid", "on", "off",
"E-Fan", "on", " off",
"I-Fan", "on", "off");
Serial.println("Wait for a while to connect!");
myIoTdevice.StartDevice
/*Your Wi-Fi router->*/ ("WiFi-SSID", "WiFi-password",
/* CavyIoT------>*/ "Username", "password", "Device"); //
}
void loop()
{
current_time = millis();
myIoTdevice.loop();
Serial.println("Device Status:-" + myIoTdevice.Status);
//---------Read values from sensor and angle of servo---------------------
temp = dht.readTemperature();
hum = dht.readHumidity();
//----------------Sending data to server----------------------------------
myIoTdevice.UpdateSensorData("Temperature", String(temp), "C",
"humidity", String(hum), "Rh",
"Tray-Angle", String(angle), "deg");
//-------------------Check timer for router scheme-------------------------
if (current_time - start_time > Interval)
{
myservo.attach(9);
rotateTray(routerScheme);
start_time = current_time;
// Uncomment for debug
// Serial.print("Servo is working ,router scheme :");
// Serial.println(routerScheme);
}
}
void rotateTray(int scheme)
{
int current_position = myservo.read();
if (scheme == 0)
{
for (current_position; current_position <= 45; current_position += 1)
{
angle = current_position;
myservo.write(current_position);
delay(50);
}
}
if (scheme == 1)
{
for (current_position; current_position <= 90; current_position += 1)
{
angle = current_position;
myservo.write(current_position);
delay(50);
}
}
if (scheme == 2)
{
for (current_position; current_position >= 45; current_position -= 1)
{
angle = current_position;
myservo.write(current_position);
delay(50);
}
}
if (scheme == 3)
{
for (current_position; current_position >= 0; current_position -= 1)
{
angle = current_position;
myservo.write(current_position);
delay(50);
}
}
routerScheme = routerScheme + 1;
myservo.detach();
if (routerScheme > 3)
routerScheme = 0;
}