-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAR_CM.ino
48 lines (39 loc) · 1.36 KB
/
AR_CM.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
#include <SPI.h>
#include <WiFiNINA.h>
// #include <WiFiUdp.h>
// WiFi credentials
const char* ssid = "GJK";
const char* password = "jhu123456";
WiFiUDP udp;
unsigned int localPort = 2390; // Any local port that isn't in use
unsigned int broadcastPort = 50000; // The port listeners will be monitoring
void setup() {
Serial.begin(9600); // Start serial communication
while (!Serial); // Wait for serial port to connect
// Connect to WiFi
Serial.print("Connecting to ");
Serial.println(ssid);
while (WiFi.begin(ssid, password) != WL_CONNECTED) {
Serial.println("Connecting to WiFi...");
delay(1000);
}
Serial.println("Connected to WiFi");
udp.begin(localPort);
}
void loop() {
// int packetSize = udp.parsePacket();
broadcastA0Reading();
delay(10); // Wait for 2 seconds between broadcasts
}
void broadcastA0Reading() {
int sensorValue = analogRead(A0); // Read the analog value from pin A0
char valueStr[20]= "CMSignal ";
sprintf(valueStr,"CMSignal %d",sensorValue);
// itoa(sensorValue, valueStr, 10); // Convert integer to string
udp.beginPacket(IPAddress(255, 255, 255, 255), broadcastPort);// Begin a packet to the broadcast address
udp.write(valueStr); // Add the sensor value to the packet
udp.endPacket(); // Send the packet
Serial.print("Broadcasted: ");
Serial.println(valueStr);
// Serial.println(WiFi.localIP());
}