|
|
@ -18,11 +18,14 @@ |
|
|
|
// Debugging over telnet
|
|
|
|
#include <WiFi.h>
|
|
|
|
#define MAX_SRV_CLIENTS 2
|
|
|
|
const char* ssid = "Pi3_2G"; |
|
|
|
const char* password = "trustno1"; |
|
|
|
// const char* ssid = "Pi3_2G";
|
|
|
|
// const char* password = "trustno1";
|
|
|
|
const char* ssid = "M5LoRa"; |
|
|
|
const char* password = "LetMeIn"; |
|
|
|
|
|
|
|
WiFiServer server(23); |
|
|
|
WiFiClient serverClients[MAX_SRV_CLIENTS]; |
|
|
|
WiFiClient wifiClient; // Socket Handler
|
|
|
|
|
|
|
|
|
|
|
|
// Temporary hardcode Address...
|
|
|
|
#define ADDR_H 0x00
|
|
|
@ -51,6 +54,7 @@ void printParameters(struct Configuration configuration); |
|
|
|
void printModuleInformation(struct ModuleInformation moduleInformation); |
|
|
|
void setup_lora(void); |
|
|
|
void telnet_server(void); |
|
|
|
void socket_client(void); |
|
|
|
void lora_recv(void); |
|
|
|
|
|
|
|
int lora_received_count_raw = 0; |
|
|
@ -60,7 +64,7 @@ uint32_t last_seconds = 0; |
|
|
|
uint32_t last_telnet_seconds = 0; |
|
|
|
|
|
|
|
bool ip_shown = false; |
|
|
|
|
|
|
|
bool socket_connected = true; |
|
|
|
|
|
|
|
void setup() { |
|
|
|
Serial.begin(115200); |
|
|
@ -69,11 +73,12 @@ void setup() { |
|
|
|
setup_lora(); |
|
|
|
|
|
|
|
// WiFi Setup
|
|
|
|
WiFi.begin(ssid, password); |
|
|
|
// WiFi.begin(ssid, password);
|
|
|
|
WiFi.begin(ssid); |
|
|
|
|
|
|
|
// Telnet
|
|
|
|
server.begin(); |
|
|
|
server.setNoDelay(true); |
|
|
|
// server.begin();
|
|
|
|
// server.setNoDelay(true);
|
|
|
|
} |
|
|
|
|
|
|
|
void loop() { |
|
|
@ -95,7 +100,53 @@ void loop() { |
|
|
|
|
|
|
|
lora_recv(); |
|
|
|
|
|
|
|
telnet_server(); |
|
|
|
socket_client(); |
|
|
|
// telnet_server();
|
|
|
|
} |
|
|
|
|
|
|
|
uint32_t last_reconnect = 0; |
|
|
|
uint32_t last_sent = 0; |
|
|
|
const char * host = "192.168.4.1"; |
|
|
|
const uint16_t port = 1001; |
|
|
|
|
|
|
|
void socket_client(void) { |
|
|
|
if(WiFi.status() == WL_CONNECTED) { |
|
|
|
if(millis() > last_sent) { |
|
|
|
if(wifiClient.connected()) { |
|
|
|
Serial.println("Sent Data to Screen"); |
|
|
|
char post_body[16]; |
|
|
|
sprintf(post_body, "RATE:%d", lora_received_count); |
|
|
|
wifiClient.write(post_body); |
|
|
|
} else { |
|
|
|
if(!wifiClient.connect(host, port, 2000)){ |
|
|
|
Serial.println("Unable to connect..."); |
|
|
|
} else { |
|
|
|
Serial.println("connected.."); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
last_sent = millis() + 1000; |
|
|
|
|
|
|
|
/*** Temporary disable for now
|
|
|
|
// For command from the server back to client
|
|
|
|
if(wifiClient.available()) { |
|
|
|
line = wifiClient.readStringUntil('\n'); |
|
|
|
Serial.println(line); |
|
|
|
} |
|
|
|
*/ |
|
|
|
} |
|
|
|
} else { |
|
|
|
if(millis() > last_reconnect) { |
|
|
|
// Try to reconnect...
|
|
|
|
if(wifiClient.connected()) |
|
|
|
wifiClient.stop(); |
|
|
|
|
|
|
|
WiFi.disconnect(); |
|
|
|
WiFi.begin(ssid); |
|
|
|
last_reconnect = millis() + 1000; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void lora_recv(void) { |
|
|
@ -123,72 +174,72 @@ void lora_recv(void) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void telnet_server(void) { |
|
|
|
uint8_t i; |
|
|
|
|
|
|
|
if (WiFi.status() == WL_CONNECTED) { |
|
|
|
//check if there are any new clients
|
|
|
|
if (server.hasClient()){ |
|
|
|
for(i = 0; i < MAX_SRV_CLIENTS; i++){ |
|
|
|
//find free/disconnected spot
|
|
|
|
if (!serverClients[i] || !serverClients[i].connected()){ |
|
|
|
if(serverClients[i]) serverClients[i].stop(); |
|
|
|
serverClients[i] = server.available(); |
|
|
|
if (!serverClients[i]) Serial.println("available broken"); |
|
|
|
Serial.print("New client: "); |
|
|
|
Serial.print(i); Serial.print(' '); |
|
|
|
Serial.println(serverClients[i].remoteIP()); |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
if (i >= MAX_SRV_CLIENTS) { |
|
|
|
//no free/disconnected spot so reject
|
|
|
|
server.available().stop(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//check clients for data
|
|
|
|
/*
|
|
|
|
for(i = 0; i < MAX_SRV_CLIENTS; i++){ |
|
|
|
if (serverClients[i] && serverClients[i].connected()){ |
|
|
|
if(serverClients[i].available()){ |
|
|
|
// get data from the telnet client and push it to the UART
|
|
|
|
// TODO: CLI STARTS HERE!!!
|
|
|
|
while(serverClients[i].available()) Serial.write(serverClients[i].read()); |
|
|
|
} |
|
|
|
} else { |
|
|
|
if (serverClients[i]) { |
|
|
|
serverClients[i].stop(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
*/ |
|
|
|
|
|
|
|
// TODO.... convert the LoRa...packets to here...so can able to telnet in...
|
|
|
|
if(millis() > last_telnet_seconds) { |
|
|
|
char sbuf[8]; |
|
|
|
sprintf(sbuf, "RATE:%i\n", lora_received_count); |
|
|
|
int len = strlen(sbuf); |
|
|
|
for(i = 0; i < MAX_SRV_CLIENTS; i++){ |
|
|
|
if (serverClients[i] && serverClients[i].connected()){ |
|
|
|
serverClients[i].write(sbuf, len); |
|
|
|
delay(1); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
last_telnet_seconds = millis() + 1000; |
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
// Serial.println("WiFi not connected!");
|
|
|
|
/*
|
|
|
|
for(i = 0; i < MAX_SRV_CLIENTS; i++) { |
|
|
|
if (serverClients[i]) serverClients[i].stop(); |
|
|
|
} |
|
|
|
delay(10); |
|
|
|
*/ |
|
|
|
} |
|
|
|
} |
|
|
|
//void telnet_server(void) {
|
|
|
|
// uint8_t i;
|
|
|
|
//
|
|
|
|
// if (WiFi.status() == WL_CONNECTED) {
|
|
|
|
// //check if there are any new clients
|
|
|
|
// if (server.hasClient()){
|
|
|
|
// for(i = 0; i < MAX_SRV_CLIENTS; i++){
|
|
|
|
// //find free/disconnected spot
|
|
|
|
// if (!serverClients[i] || !serverClients[i].connected()){
|
|
|
|
// if(serverClients[i]) serverClients[i].stop();
|
|
|
|
// serverClients[i] = server.available();
|
|
|
|
// if (!serverClients[i]) Serial.println("available broken");
|
|
|
|
// Serial.print("New client: ");
|
|
|
|
// Serial.print(i); Serial.print(' ');
|
|
|
|
// Serial.println(serverClients[i].remoteIP());
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// if (i >= MAX_SRV_CLIENTS) {
|
|
|
|
// //no free/disconnected spot so reject
|
|
|
|
// server.available().stop();
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// //check clients for data
|
|
|
|
// /*
|
|
|
|
// for(i = 0; i < MAX_SRV_CLIENTS; i++){
|
|
|
|
// if (serverClients[i] && serverClients[i].connected()){
|
|
|
|
// if(serverClients[i].available()){
|
|
|
|
// // get data from the telnet client and push it to the UART
|
|
|
|
// // TODO: CLI STARTS HERE!!!
|
|
|
|
// while(serverClients[i].available()) Serial.write(serverClients[i].read());
|
|
|
|
// }
|
|
|
|
// } else {
|
|
|
|
// if (serverClients[i]) {
|
|
|
|
// serverClients[i].stop();
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// */
|
|
|
|
//
|
|
|
|
// // TODO.... convert the LoRa...packets to here...so can able to telnet in...
|
|
|
|
// if(millis() > last_telnet_seconds) {
|
|
|
|
// char sbuf[32];
|
|
|
|
// sprintf(sbuf, "RATE:%i\n", lora_received_count);
|
|
|
|
// int len = strlen(sbuf);
|
|
|
|
// for(i = 0; i < MAX_SRV_CLIENTS; i++){
|
|
|
|
// if (serverClients[i] && serverClients[i].connected()){
|
|
|
|
// serverClients[i].write(sbuf, len);
|
|
|
|
// delay(1);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// last_telnet_seconds = millis() + 1000;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// } else {
|
|
|
|
// // Serial.println("WiFi not connected!");
|
|
|
|
// /*
|
|
|
|
// for(i = 0; i < MAX_SRV_CLIENTS; i++) {
|
|
|
|
// if (serverClients[i]) serverClients[i].stop();
|
|
|
|
// }
|
|
|
|
// delay(10);
|
|
|
|
// */
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
|
|
|
|
void setup_lora() { |
|
|
|
// Startup all pins and UART
|
|
|
@ -260,12 +311,14 @@ void setup_lora() { |
|
|
|
//
|
|
|
|
} |
|
|
|
|
|
|
|
ResponseStructContainer rsc; |
|
|
|
|
|
|
|
void RecvLoRaSignals(void *pvParameters) { |
|
|
|
// Forever loop in this loop :P
|
|
|
|
for(;;) { |
|
|
|
/***** LORA ***/ |
|
|
|
if(e32ttl100.available() > 1){ |
|
|
|
ResponseStructContainer rsc = e32ttl100.receiveMessage(sizeof(LoraMessage)); |
|
|
|
rsc = e32ttl100.receiveMessage(sizeof(LoraMessage)); |
|
|
|
struct LoraMessage loramessage = *(LoraMessage*) rsc.data; |
|
|
|
|
|
|
|
/*
|
|
|
|