Good afternoon,
I have two Arduino Uno Rev2 WiFi boards with one as the server, while the other a client. The client uses a ultrasonic sensor to detect if an object goes in front of it. Then sends a GET request to the static IP of the server indicating that someone is blocking its view.
Both the server and client can work, and have worked several times with one another. The problem is that if the client doesn't find the server within 10 minutes, it likely will never find it.
On the other end of this problem is that, if there is connectivity it lasts only an hour or two before freezing up on both ends. I believe that it's client.connect()'s issue, because it takes at least 10 seconds to try and connect before timing out. But also the server freezes as well, so I'm not too sure what the issue is.
I just need a bit of help with making sure that both connect within at least a minute of being reset/powered-on externally, and lasting at minimum 24 hours.
If you can help in any way, thank you and I appreciate your input.
This is the Server code:
#include <SPI.h>
#include "WiFiNINA.h"
#include "arduino_secrets.h"
#define greenLED 10
//Network specific variables
char ssid[] = SECRET_SSID; // WiFi SSID
char pass[] = SECRET_PASS; // WiFi Password
int status = WL_IDLE_STATUS;
IPAddress ip(192, 168, 46, 244);
IPAddress dns(8, 8, 8, 8);
IPAddress gateway(192, 168, 46, 1);
IPAddress subnet(255, 255, 255, 0);
WiFiServer server(80);
String readString;
//Setup Function
void setup() {
Serial.begin(115200);
pinMode(greenLED, OUTPUT);
while (!Serial) {;}
digitalWrite(LED_BUILTIN, LOW);
WiFi.config(ip, dns, gateway, subnet);
while (status != WL_CONNECTED) {status = WiFi.begin(ssid, pass);}
digitalWrite(LED_BUILTIN, HIGH);
IPAddress ip = WiFi.localIP();
Serial.print("http://");
Serial.println(ip); //Prints to the newly generated IP Address
server.begin();
}
void loop(){
WiFiClient client = server.available();
if (client) {
boolean currentLineIsBlank = true;
while (client.connected()){
if (client.available()){
char c = client.read();
if (readString.length() < 50){
readString += c;
if (c == '\n') {
currentLineIsBlank = true;
if (readString.indexOf("one") > 0){
analogWrite(greenLED, 15);
}
else{
analogWrite(greenLED, 0);
}
readString = "";
} else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
}
}
delay(1);
//Serial.print(millis()/1000);
//Serial.println(" seconds elapsed");
}
This is the Client code:
#include <SPI.h>
#include "WiFiNINA.h"
#include "arduino_secrets.h"
#define bluePin 10
#define trigPin 8
#define echoPin 9
//Network specific variables
char ssid[] = SECRET_SSID; // WiFi SSID
char pass[] = SECRET_PASS; // WiFi Password
int status = WL_IDLE_STATUS;
char setIP[] = "192.168.46.244"; //IP Address trying to connect to
int port = 80; //The port for HTTP transmission
WiFiClient client;
//Setup Function
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
while (!Serial) {;}
digitalWrite(LED_BUILTIN, LOW);
while (status != WL_CONNECTED) {
status = WiFi.begin(ssid, pass);
}
digitalWrite(LED_BUILTIN, HIGH);
}
int sensorDist() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
if (pulseIn(echoPin, HIGH) < 500){return 1;}
else{return 0;};
}
void loop(){
if (sensorDist() == 0) {
if (client.connect(setIP, port)) {
client.write("GET /zro HTTP/1.1\r\n");
analogWrite(bluePin, 0);
}
} else {
if (client.connect(setIP, port)) {
client.write("GET /one HTTP/1.1\r\n");
analogWrite(bluePin, 15);
}
}
if (client.connected()) {client.stop();}
delay(1000);
//Serial.print(millis()/1000);
//Serial.println(" seconds elapsed");
}