-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSender
More file actions
113 lines (91 loc) · 2.8 KB
/
Copy pathSender
File metadata and controls
113 lines (91 loc) · 2.8 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
#include <ArduinoMqttClient.h>
#include <WiFiNINA.h>
#include "arduino_secrets.h"
#define echoPin 2
#define trigPin 4
#define led A5
long duration;
int distance;
//////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);
const char broker[] = "broker.hivemq.com";
int port = 1883;
const char topic[] = "arduino/simple/wave";
//set interval for sending messages (milliseconds)
const long interval = 8000;
unsigned long previousMillis = 0;
int count = 0;
void setup() {
pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);
pinMode(led ,OUTPUT);
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// attempt to connect to Wifi network:
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
// failed, retry
Serial.print(".");
delay(500);
}
Serial.println("You're connected to the network");
Serial.println();
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("You're connected to the MQTT broker!");
Serial.println();
}
void loop() {
digitalWrite(trigPin,LOW);
delay(20);
digitalWrite(trigPin, HIGH);
delay(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
// call poll() regularly to allow the library to send MQTT keep alive which
// avoids being disconnected by the broker
mqttClient.poll();
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time a message was sent
previousMillis = currentMillis;
if(distance <= 250)
{
Serial.print("Sending message to topic: ");
Serial.println(topic);
Serial.print("Hello World! ");
Serial.print(distance);
Serial.println(" cm");
digitalWrite (led, HIGH);
delay(100);
digitalWrite (led, LOW);
//delay(300);
digitalWrite (led, HIGH);
delay(100);
digitalWrite (led, LOW);
digitalWrite (led, HIGH);
delay(100);
digitalWrite (led, LOW);
// send message, the Print interface can be used to set the message contents
mqttClient.beginMessage(topic);
mqttClient.print("Hello World!: ");
mqttClient.print(distance);
mqttClient.println(" cm");
mqttClient.endMessage();
}
Serial.println();
}
}