diff --git a/LoRa_E32.cpp b/LoRa_E32.cpp index bc0178a..ad5e0e0 100644 --- a/LoRa_E32.cpp +++ b/LoRa_E32.cpp @@ -62,7 +62,7 @@ Status LoRa_E32::waitCompleteResponse(unsigned long timeout, unsigned int waitNo } // per data sheet control after aux goes high is 2ms so delay for at least that long) - this->managedDelay(20); + this->managedDelay(10); // DEBUG_PRINTLN(F("Complete!")); return result; } @@ -219,7 +219,7 @@ Status LoRa_E32::receiveStruct(void *structureManaged, uint16_t size_) { Status LoRa_E32::setMode(MODE_TYPE mode) { // data sheet claims module needs some extra time after mode setting (2ms) // most of my projects uses 10 ms, but 40ms is safer - this->managedDelay(40); + this->managedDelay(20); if (this->m0Pin == -1 && this->m1Pin == -1) { DEBUG_PRINTLN(F("The M0 and M1 pins is not set, this mean that you are connect directly the pins as you need!")) @@ -254,7 +254,7 @@ Status LoRa_E32::setMode(MODE_TYPE mode) { } // data sheet says 2ms later control is returned, let's give just a bit more time // these modules can take time to activate pins - this->managedDelay(40); + this->managedDelay(3); // wait until aux pin goes back low Status res = this->waitCompleteResponse(1000); diff --git a/OpenFlightRX.ino b/OpenFlightRX.ino index 03be214..c30b1fb 100644 --- a/OpenFlightRX.ino +++ b/OpenFlightRX.ino @@ -18,11 +18,14 @@ // Debugging over telnet #include #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; /*