-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask 3.3D
More file actions
150 lines (133 loc) · 3.29 KB
/
Task 3.3D
File metadata and controls
150 lines (133 loc) · 3.29 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Required Libraries
#include <WiFiNINA.h>
#include <ArduinoMqttClient.h>
#include "NewPing.h"
// Retrieve SSID and PASS from Secrets tab
char ssid[] = SECRET_SSID;
char pass[] = SECRET_OPTIONAL_PASS;
// Declare pin identities
const int ledPin = 2;
const int trigPin = 3;
const int echoPin = 3;
// Create NewPing instance
NewPing sonar(trigPin, echoPin);
// Create WiFI and MQTT clients
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);
// Set values of MQTT
const char broker[] = "broker.emqx.io";
int port = 1883;
const char wave[] = "SIT210/wave";
const char pat[] = "SIT210/pat";
// Set variables
int distance = 0;
bool wave_detected = false;
bool pat_detected = false;
void setup()
{
// Initialize serial and wait for port to open
Serial.begin(9600);
while (!Serial);
delay(1500);
// Initialize WiFi connection
Serial.println("Attempting to connect to SSID: ");
Serial.println(ssid);
while (WiFi.begin(ssid, pass) != WL_CONNECTED)
{
Serial.print(".");
delay(5000);
}
Serial.println();
Serial.println("Connection successful");
// Initialize MQTT connection
Serial.print("Attempting to connect to the MQTT broker: ");
Serial.println(broker);
if (!mqttClient.connect(broker, port))
{
Serial.print("MQTT connection failed! Error code = ");
Serial.println(mqttClient.connectError());
while (1);
}
Serial.println();
Serial.println("Connection successful");
// Subscripe to desired topics
mqttClient.subscribe(pat);
mqttClient.subscribe(wave);
// Initialize pins
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop()
{
mqttClient.poll();
delay(500);
distance = sonar.ping_cm();
Serial.print("Distance = ");
Serial.print(distance);
Serial.println("cm");
if (distance <= 30 && distance > 5)
{
mqttClient.beginMessage(wave);
mqttClient.println(wave);
mqttClient.print("*waved*");
mqttClient.endMessage();
delay(500);
wave_detected = true;
}
else if (distance <= 5)
{
mqttClient.beginMessage(pat);
mqttClient.println(pat);
mqttClient.print("*patted*");
mqttClient.endMessage();
delay(500);
pat_detected = true;
}
if(wave_detected || pat_detected )
{
int Msize = mqttClient.parseMessage();
Serial.print(Msize);
if (Msize)
{
Serial.println();
Serial.print("Received message with the subject ");
Serial.print(mqttClient.messageTopic());
Serial.print(", length ");
Serial.print(Msize);
Serial.println(" bytes:");
while (mqttClient.available())
{
Serial.print((char)mqttClient.read());
}
Serial.println();
if (wave_detected)
{
for (int i = 0; i < 3; i++)
{
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(100);
}
wave_detected = false;
}
if (pat_detected)
{
for (int i = 0; i < 2; i++)
{
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(100);
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(100);
}
pat_detected = false;
}
Serial.println();
}
}
}