-
Notifications
You must be signed in to change notification settings - Fork 0
/
Receiver and TCP Server
121 lines (98 loc) · 3.23 KB
/
Receiver and TCP Server
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
// This is testing and may not work as expected
// To find router on mac, go to network and click on information.
// Paste the router ip into the code and the computers up + 1, the router can't always assign the ip and this will need more debugging until it is universal.
#include <SPI.h>
#include <RH_RF95.h>
#include <WiFi.h>
const char *ssid = "SSID_HERE";
const char *password = "PASSWORD_HERE";
const char *staticIP = "192.168.1.100";
const char *gateway = "192.168.1.1"; // Replace with your gateway/router IP address
const char *subnet = "255.255.255.0";
#define RFM95_CS 33
#define RFM95_INT 12
#define RFM95_RST 27
#define RF95_FREQ 904.0
RH_RF95 rf95(RFM95_CS, RFM95_INT);
char message[256];
WiFiServer server(80);
WiFiClient client;
void setup() {
pinMode(RFM95_RST, OUTPUT);
digitalWrite(RFM95_RST, HIGH);
Serial.begin(115200);
while (!Serial) delay(1);
delay(100);
IPAddress staticIPaddr;
staticIPaddr.fromString(staticIP);
IPAddress gatewayAddr;
gatewayAddr.fromString(gateway);
IPAddress subnetMask;
subnetMask.fromString(subnet);
WiFi.config(staticIPaddr, gatewayAddr, subnetMask);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.println("Feather LoRa RX Test!");
digitalWrite(RFM95_RST, LOW);
delay(10);
digitalWrite(RFM95_RST, HIGH);
delay(10);
while (!rf95.init()) {
Serial.println("LoRa radio init failed");
Serial.println("Uncomment '#define SERIAL_DEBUG' in RH_RF95.cpp for detailed debug info");
while (1);
}
Serial.println("LoRa radio init OK!");
if (!rf95.setFrequency(RF95_FREQ)) {
Serial.println("setFrequency failed");
while (1);
}
Serial.print("Set Freq to: "); Serial.println(RF95_FREQ);
rf95.setTxPower(20, true);
rf95.setSpreadingFactor(12);
server.begin();
}
void loop() {
if (rf95.available()) {
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
if (rf95.recv(buf, &len)) {
Serial.print("Received: ");
// Convert the received buffer to a string
buf[len] = '\0';
Serial.println((char*)buf);
Serial.print("RSSI: ");
Serial.println(rf95.lastRssi(), DEC);
// Call the TCP function with the message
TCP((char*)buf);
} else {
Serial.println("Receive failed");
}
}
delay(100);
}
void TCP(char* message) {
client = server.available();
if (client) {
Serial.println("Client connected");
while (client.connected()) {
static unsigned long lastMillis = 0;
if (millis() - lastMillis >= 1000) {
client.println(message);
Serial.println("Message sent to client");
lastMillis = millis();
}
if (client.available()) {
char c = client.read();
Serial.write(c);
// Process the received data from Python
// You can add your logic here
}
}
Serial.println("Client disconnected");
}
}