1
5
6#define CAYENNE_PRINT Serial
7#include <CayenneMQTTEthernet.h>
8#include <Servo.h>
9#include <DHT.h>
10#define DHTTYPE DHT22
11#define DHTPIN 2
12DHT dht(DHTPIN,DHTTYPE);
13
14
15char username[]="498d2d00-afe2-11ea-883c-638d8ce4c23d";
16char password[]="ab4a8f92d94033c01f6e18ce1d8a84d8c304c9c4";
17char clientID[]="17798a40-b968-11ea-93bf-d33a96695544";
18
19Servo servo_x;
20int servoh = 0;
21int servohLimitHigh = 170;
22int servohLimitLow = 10;
23
24Servo servo_z;
25int servov = 0;
26int servovLimitHigh = 170;
27int servovLimitLow = 10;
28
29int topl,topr,botl,botr;
30int threshold_value=10;
31float Vout;
32
33void setup()
34{ Serial.begin(9600);
35 Cayenne.begin(username, password, clientID);
36 servo_x.attach(5);
37 servo_z.attach(6);
38 dht.begin();
39 pinMode(3,OUTPUT);
40 digitalWrite(3,LOW);
41}
42
43void loop()
44{ topr= analogRead(A2);
45 topl= analogRead(A3);
46 botl= analogRead(A4);
47 botr= analogRead(A5);
48 Vout=(analogRead(A1) * 5.0) / 1023;
49 Serial.println(" Manual-mode");
50 Cayenne.loop();
51
52 if (digitalRead(3)==HIGH){
53 Serial.println(" Automatic-mode");
54 servoh = servo_x.read();
55 servov = servo_z.read();
56 int avgtop = (topr + topl) / 2;
57 int avgbot = (botr + botl) / 2;
58 int avgright = (topr + botr) / 2;
59 int avgleft = (topl + botl) / 2;
60 int diffhori= avgtop - avgbot;
61 int diffverti= avgleft - avgright;
62
63
64 if (abs(diffhori) <= threshold_value)
65 {
66 servo_x.write(servoh);
67 }else {
68 if (diffhori > threshold_value)
69 { Serial.println(" x - 2 ");
70 servo_x.write(servoh -2);
71 if (servoh > servohLimitHigh)
72 {
73 servoh = servohLimitHigh;
74 }
75 delay(10);
76 }else {
77 servo_x.write(servoh +2);
78 if (servoh < servohLimitLow)
79 {
80 servoh = servohLimitLow;
81 }
82 delay(10);
83 }
84 }
85
86 if (abs(diffverti) <= threshold_value)
87 {
88 servo_z.write(servov);
89 }else{
90 if (diffverti > threshold_value)
91 {
92 servo_z.write(servov -2);
93 if (servov > servovLimitHigh)
94 {
95 servov = servovLimitHigh;
96 }
97 delay(10);
98 }else{
99 servo_z.write(servov +2);
100 if (servov < servovLimitLow)
101 {
102 servov = servovLimitLow;
103 }
104 delay(10);
105 }
106 }
107 }
108}
109
110CAYENNE_IN(8){
111 int value = getValue.asInt();
112 CAYENNE_LOG("Channel %d, pin %d, value %d", 8, 3, value);
113 digitalWrite(3,value);
114}
115CAYENNE_IN(7){
116 if (digitalRead(3)==HIGH){
117 }
118 else{
119 servo_x.write(getValue.asDouble() * 180);
120 }
121}
122CAYENNE_IN(6){
123 if (digitalRead(3)==HIGH){
124 }
125 else{
126 servo_z.write(getValue.asDouble() * 180);
127 }
128}
129
130CAYENNE_OUT(0) {
131 float current = Vout/10;
132 Cayenne.virtualWrite(0, current);
133 Serial.print("Current: ");
134 Serial.println(current);
135}
136CAYENNE_OUT(1) {
137 float voltage = Vout * 2;
138 Cayenne.virtualWrite(1, voltage);
139 Serial.print("Voltage: ");
140 Serial.println(voltage);
141}
142CAYENNE_OUT(2){
143 Cayenne.virtualWrite(2, topr);
144}
145CAYENNE_OUT(3){
146 Cayenne.virtualWrite(3,topl);
147}
148CAYENNE_OUT(4){
149 Cayenne.virtualWrite(4,botl);
150}
151CAYENNE_OUT(5){
152 Cayenne.virtualWrite(5,botr);
153}
154CAYENNE_OUT(10) {
155 float power = (Vout * 2 * Vout)/10 ;
156 Cayenne.virtualWrite(10, power);
157 Serial.print("Power: ");
158 Serial.println(power);
159}
160CAYENNE_OUT(11){
161 float t = dht.readTemperature();
162
163 Cayenne.virtualWrite(11, t, TYPE_TEMPERATURE, UNIT_CELSIUS);
164 Serial.print("temperature: ");
165 Serial.println(t);
166}
167CAYENNE_OUT(12){
168 float h = dht.readHumidity();
169
170 Cayenne.virtualWrite(12, h);
171 Serial.print(" humidity: ");
172 Serial.println(h);
173}
174
175