How would I create a simple Arduino script that allows me to turn on and off LEDBuitIn remotely? - arduino

I'm working on a project that includes working with arduino hardware remotely. I would like to learn how to create a simple script that allows me to turn on and off the Led builtin to my MKR1000 wirelessly. I could then use this knowledge in more complicated projects. After doing some research and looking at the arduino library's sample webserver program, I came up with this Frankenstein of code. After working for hours I just keep on making it worse, I could really use some guidance on what I'm doing wrong, why and how to fix it.
My Frankenstein code:
#include <WiFi101.h>
#include <SPI.h>
char ssid[] = "ARROW_015D80";
char pass[] = "KRR3K47XZXM3NYRHV7GX";
int status = WL_IDLE_STATUS;
int LED = LED_BUILTIN;
int LEDState = digitalRead(LED);
WiFiServer server(80);
void setup() {
while (!Serial) {
}
Serial.begin(9600);
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("Yo, where the wifi shield at?");
while(true);
}
while (status !=WL_CONNECTED) {
Serial.print("Connecting to ssid: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(10000);
}
server.begin();
printWiFiStatus();
}
void loop() {
WiFiClient client = server.available();
if (client) {
Serial.println("+1 Client");
String currentLine = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
if (c == '\n') {
if(currentLine.length() == 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("Refresh: 1"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.print("State of button:");
if (LEDState == HIGH){
client.print("ON");
}
if (LEDState == LOW){
client.print("OFF");
}
client.println("<br>");
client.println("</html>");
client.println();
break;
}
else {
currentLine = "";
}
}
else if (c !='\r') {
currentLine += c;
}
if (currentLine.endsWith("GET /H")) {
digitalWrite(LED, HIGH);
}
if (currentLine.endsWith("Get /L")) {
digitalWrite(LED, LOW);
}
if (currentLine.endsWith("Get /stop")){
client.stop();
Serial.println ("Client disconnected");
}
}
}
}
}
void printWiFiStatus() {
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
According to the videos and the mismatch of information, this program should connect the arduino to the internet, print the arduino's ip address in the serial monitor and I should be able to change the state of the built in LED by changing the end of the ip address search.
Instead, after showing the ip address and displaying the page, with the button state on it. When I try to change the url to change the button state it bugs out. It takes me to the "This page can't be reached" and the serialmonitor bug out.

Never mind, I found a video online that explain clearly how to write the code I'm looking for. It is extremely helpful : https://www.youtube.com/watch?v=H0p7GVPdlyU
It also link to a page with all the code for it.

Related

ESP32 using BLE and WiFi alternately

I have an ESP32 which should receive data over BLE and then send the data over WiFi to a webserver. If I code both tasks separately in Arduino, then everything works but as soon as I merge both tasks, sending over WiFi breaks down.
From what I understand, BLE and WiFi are sharing the same radio on the ESP32, thus the tasks need to be done alternately to avoid interferences. I've tried to implement this by adding delays between the two tasks but was unsuccessful.
This is the code I have so far:
#include <HTTPClient.h>
#include <BLEDevice.h>
#include <BLEScan.h>
const char* ssid = "xx";
const char* password = "xx";
int scanTime = 2; //In seconds
BLEScan* pBLEScan;
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
}
};
void setup()
{
Serial.begin(115200);
Serial.setDebugOutput(1);
Serial.setDebugOutput(0); //turn off debut output
WiFi.begin(ssid, password);
int retrycon = 50;
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
if (--retrycon == 0)
{
Serial.println("RESTART");
ESP.restart();
}
Serial.print(".");
}
Serial.print("WiFi connected with IP: ");
Serial.println(WiFi.localIP());
BLEDevice::init("");
pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true);
pBLEScan->setInterval(100);
pBLEScan->setWindow(99);
}
void loop()
{
BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
Serial.print("Devices found: ");
Serial.println(foundDevices.getCount());
pBLEScan->clearResults();
delay(3000);
int tryconnect = 20;
while (--tryconnect != 0) {
if (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("...");
} else {
break;
}
}
if (WiFi.status() != WL_CONNECTED) {
WiFi.reconnect();
Serial.println("reconnect");
}
else {
Serial.println("connected to WiFi");
HTTPClient http;
http.begin("http://httpbin.org/ip");
int httpCode = http.GET();
if (httpCode > 0) {
Serial.print("HTTP code ");
Serial.println(httpCode);
} else {
Serial.println("Error on HTTP request");
}
http.end();
delay(10000);
}
}
Can anybody tell me how to implement the two tasks (receiving over BLE, sending over WiFi) to avoid interferences?
I use commands like this to turn off BLE and WiFi:
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
btStop();
adc_power_off();
//esp_wifi_stop(); // Doesn't work for me!
esp_bt_controller_disable();
In my code I BLE advertize/scan, then do the stuff above, then connect to WiFi. No problem. Oh and remember to BLEDevice::deinit when you finished with BLE, otherwise you can't get it to fit in a 4Mb ESP32. I do BLE, WiFi, HTTPS/SSL, OTA and use the SPIFFS to store data, all on a standard 4Mb ESP32 (ESP-WROOM-32) without PSRAM.
All above is in a function to lower the power usage, so I call it all every time I change 'mode'. In deep sleep I only use 5uA.
Also:
pBLEScan->setInterval(100);
pBLEScan->setWindow(99);
... don't work for me, so I commented them out, and scan for 3 sec.

ESP8266 EEPROM or FLASH Error: Succesfully connecting to Wifi with false credentials

I am building a small sensor based on the ESP8266. For setting up wifi I host a small webserver, where the user can enter the credentials.
In the setup routine I load the credentials and try to connect to wifi. If the connection fails, the esp creates an AP and the user can enter new credentials.
I got some strange behaviour. Because when I remove the credentials, the esp nevertheless connects successfully to the wifi. But the 'serial.print()' don't show any credentials.
Maybe its a problem with the EEPROM or FLASH I have no idea. I could reproduce this behaviour on several ESPs.
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <EEPROM.h>
ESP8266WebServer server(80);
const char *soft_ap_ssid = "Connect Sensor to MCU";
const char *soft_ap_password = "MySensor";
char ssid[32] = "";
char password[32] = "";
bool wifi_status;
void setup()
{
Serial.begin(115200);
Serial.println();
delay(10);
load_credentials();
wifi_status = setup_wifi(5);
if (!wifi_status)
{
setup_soft_ap();
setup_server();
}
}
void loop()
{
if (!wifi_status)
{
server.handleClient();
}
else
{
Serial.println("Doing Sensor stuff");
delay(2000);
// Remove exsting credentials
for( int i = 0; i < sizeof(ssid); i++ ){
ssid[i] = (char)0;
password[i] = (char)0;
}
save_credentials();
}
}
/*
############## Gernerische Code ########### TODO LIB
*/
bool setup_wifi(int timeout)
{
int timeout_ctr = 0;
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password); //Connect to the WiFi network
while ((WiFi.status() != WL_CONNECTED) && (timeout_ctr < timeout))
{
delay(500);
Serial.println("...");
timeout_ctr++;
}
if (WiFi.status())
{
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //Print the local IP
}
else
{
Serial.println("Connection to MCU failed, setting up AP.");
}
return (WiFi.status() == WL_CONNECTED);
}
void setup_soft_ap()
{
Serial.println();
Serial.println("Creating WiFi AP...");
WiFi.softAP(soft_ap_ssid, soft_ap_password);
Serial.print("AP \"");
Serial.print(soft_ap_ssid);
Serial.println("\" is online.");
Serial.print("IP address: ");
Serial.println(WiFi.softAPIP());
Serial.println();
}
void setup_server()
{
server.on("/", handle_root); //Associate the landingpage function to the path
server.on("/submit", handle_submit); //Associate the handler function to the path
server.begin(); //Start the server
Serial.println("Server listening");
}
void handle_root()
{
const char *landingpage = "<html><head> <title>ESP8266 Demo</title> <style></style></head><body> <h1>Sensor einrichten</h1> <p> Eingabe der Wifi Daten </p><form action='/submit' method='post'> <input type='text' name='ssid' placeholder='Wifi SSID'> <input type='text' name='pw' placeholder='Wifi Password'> <input type='submit' value='Submit'> </form></body></html>";
server.send(200, "text/html", landingpage);
}
void handle_submit()
{
if (server.args() > 0)
{
Serial.println("POST:");
Serial.println(server.arg("ssid"));
Serial.println(server.arg("pw"));
server.arg("ssid").toCharArray(ssid, sizeof(ssid) - 1);
server.arg("pw").toCharArray(password, sizeof(password) - 1);
Serial.println("credentials:");
Serial.println(ssid);
Serial.println(password);
save_credentials();
}
server.sendHeader("Location", String("/"), true);
server.send(302, "text/plain", "");
}
void load_credentials()
{
EEPROM.begin(512);
EEPROM.get(0, ssid);
EEPROM.get(0 + sizeof(ssid), password);
char ok[2 + 1];
EEPROM.get(0 + sizeof(ssid) + sizeof(password), ok);
EEPROM.end();
if (String(ok) != String("OK"))
{
ssid[0] = 0;
password[0] = 0;
}
Serial.println("Recovered credentials:");
Serial.println(ssid);
Serial.println(password);
}
/** Store WLAN credentials to EEPROM */
void save_credentials()
{
EEPROM.begin(512);
EEPROM.put(0, ssid);
EEPROM.put(0 + sizeof(ssid), password);
char ok[2 + 1] = "OK";
EEPROM.put(0 + sizeof(ssid) + sizeof(password), ok);
EEPROM.commit();
EEPROM.end();
}
By default, the ESP8266 Arduino SDK saves its wifi configuration in flash memory.
This is documented but not called out clearly.
You'll need to call Wifi.persistent() to get the ESP to not save the credentials. This doesn't seem to be called out clearly in a lot of ESP Arduino Core writeups that I've seen.
Try this:
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.persistent(false);
WiFi.begin(ssid, password); //Connect to the WiFi network
while ((WiFi.status() != WL_CONNECTED) && (timeout_ctr < timeout))
You'll probably want to write a bad set of credentials to flash before you update your code to tell the SDK to stop saving them, otherwise I think it will keep using the last saved set. The alternative is to wipe the sector of flash on your ESP where the credentials are stored, which is a lot more work.
There's some documentation on WiFi.persistent() here - which appears to be incorrect - I believe this call operates the way I described (passing false just doesn't store the credentials at all).
If your project is going to be restarting frequently (like going in and out of deep sleep often) I'd definitely use WiFi.persistent(false) to avoid wear on the flash.

NodeMCU gets strange IP address

Today I got my NodeMCU and I instantly started with coding. I wanted to connect to my WiFi and to my MQTT server.
I used the PubSub example for this.
In the serial monitor I get the message that I connected successfully with the WiFi, but I get the IP 172.20.10.6. However we have a 192.168... network.
Then when I try to reach my MQTT server it doesn't find it. When I try to give the NodeMCU a static IP it also says connected successfully and shows up the static IP I gave it, but I still can't connect to my MQTT server.
I can't ping the NodeMCU and don't find it in my Smartphone app "Fing".
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* ssid = "myssid";
const char* password = "mypw";
const char* mqtt_server = "192.168.42.131";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
void setup() {
pinMode(BUILTIN_LED, OUTPUT);
// Initialize the BUILTIN_LED pin as an output
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
// Switch on the LED if an 1 was received as first character
if ((char)payload[0] == '1') {
digitalWrite(BUILTIN_LED, LOW);
// Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
// it is active low on the ESP-01)
} else {
digitalWrite(BUILTIN_LED, HIGH);
// Turn the LED off by making the voltage HIGH
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client")) {
Serial.println("connected");
// Once connected, publish an announcement...
//client.publish("outTopic", "hello world");
// ... and resubscribe
client.subscribe("mathistest");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 2000) {
lastMsg = now;
++value;
snprintf (msg, 75, "hello world #%ld", value);
}
}
You are most likely having a DHCP failure.
A 172.20.x.x address is a non-routable IP address (see: https://www.lifewire.com/what-is-a-private-ip-address-2625970) and the DHCP code is (likely) using that address when the address assignment fails.
Stepping back, DHCP is most likely failing because you are failing to connect to the Wifi network with the correct SSID and password.

ESP 8266 Program Issue

Description
I am writing a web server on ESP-8266(01), that will control 3 locks using Arduino Mega.
The locks will be controlled by Bluetooth ESP 8266 (get method from phone) and using my laptops browser.
I can do it all with bluetooth HC-05 (easily).
The issue I am facing is with my ESP-8266 wifi module.
I need to send 3 different strings from ESP for Arduino to read it.
If it's "AAAA" Arduino will read it and unlock door one, if it BBBB then door 2 and etc etc.....
So I used this code:
#include <ESP8266WiFi.h>
const char* ssid = "Do-Not-Ask-For-Password";
const char* password = "ITISMYPASSWORD";
//const char* ssid = "`STAR-NET-Azhar-32210352";
//const char* password = "ITISMYPASSWORD";
int ledPin = 2; // GPIO2
WiFiServer server(80);
// Update these with values suitable for your network.
IPAddress ip(192,168,8,128); //Node static IP
IPAddress gateway(192,168,8,1);
IPAddress subnet(255,255,255,0);
void setup() {
Serial.begin(115200);
delay(10);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
WiFi.config(ip, gateway, subnet);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
}
void loop() {
ESP.wdtDisable();
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
// Serial.println("new client");
// while(!client.available()){
// delay(1);
// }
if(!client.available()){
delay(1);
client.flush();
}
// Read the first line of the request
String request = client.readStringUntil('\r');
//Serial.println(request);
client.flush();
// Match the request
int value1 = LOW;
int value2 = LOW;
int value3 = LOW;
if (request.indexOf("/LOCK=ON1") != -1) {
//digitalWrite(ledPin, HIGH);
value1 = HIGH;
Serial.println("AAAA");
}
if(request.indexOf("/LOCK=ON2") != -1){
value2 = HIGH;
Serial.println("BBBB");
}
if(request.indexOf("/LOCK=ON3") != -1){
value3 = HIGH;
Serial.println("CCCC");
}
// Set ledPin according to the request
//digitalWrite(ledPin, value);
// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<br><br>");
client.print("LOCK ONE IS NOW: ");
if(value1 == HIGH) {
client.print("UNLOCKED");
// Serial.println("A");
} else {
client.print("LOCKED");
}
client.println("<br><br>");
client.print("LOCK TWO IS NOW: ");
if(value2 == HIGH) {
client.print("UNLOCKED");
// Serial.println("B");
} else {
client.print("LOCKED");
}
client.println("<br><br>");
client.print("LOCK THREE IS NOW: ");
if(value3 == HIGH) {
client.print("UNLOCKED");
//Serial.println("C");
} else {
client.print("LOCKED");
}
client.println("<br><br>");
client.println("CLICK HERE TO UNLOCK THE LOCK ONE<br>");
client.println("CLICK HERE TO UNLOCK THE LOCK TWO<br>");
client.println("CLICK HERE TO UNLOCK THE LOCK THREE<br>");
client.println("</html>");
delay(100);
//Serial.println("client disconnected");
// Serial.println("");
}
The Result I Get on Browser is ( Which is What i need):
enter image description here
ISSUE:
Now The Issue Is Some Times it misses the serial data i send, For Example if i click unlock door 1, It sends data and i click unlock door 2 it does nothn and when i reset it sends data from all 3 but for two to 3 times and then module resets it self ....
This Is What I got From Serial Monitor:
enter image description here
Now i have searched alot and its watchdre out what i did wrong in the code.
this is how my ESP is connected:
vcc & CHPD to 3.3V ragulator
gnd to gnd of powersuply and to arduino gnd...
GP 0 ------> 3.3v with 2.2k Resistor ( in non flashing state).
Rst ------> 3.3v with resistor
RX to TX
TX to RX
Any Ideas how to fix it?
Is There any other way to control Arduino pins (6 different pins) from ESP 8266 module??
ive been trying from many days please help!!
Ok I am Posting It For those who are facing the same problem!! i took the Hello ESP example And Modified it to Print Serial!!!
On Mega My Serial Was Started at 9600 and i started Serial On ESP at 9600 aswell :| So This is the Code .... Slow Server and WDT issue is Almost Gone....
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
//const int RELAY_PIN = 2; //RELAY
const char* ssid = "Do-Not-Ask-For-Password";
const char* password = "AeDrki$32ILA!$#2";
MDNSResponder mdns;
ESP8266WebServer server(80);
void handleRoot() {
server.send(200, "text/plain", "hWelcome To Door Unlock Project By Haziq Sheikh");
}
void handleNotFound(){
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET)?"GET":"POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i=0; i<server.args(); i++){
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
void setup(void){
// pinMode(RELAY_PIN, OUTPUT);
Serial.begin(9600);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
//digitalWrite(RELAY_PIN, 1);
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// digitalWrite(RELAY_PIN, 0);
if (mdns.begin("esp8266", WiFi.localIP())) {
Serial.println("MDNS responder started");
}
server.on("/", handleRoot);
server.on("/lock1", [](){
server.send(200, "text/plain", "Okay Door One Unlocked");
Serial.println("AAAA");
});
server.on("/lock2", [](){
server.send(200, "text/plain", "Okay -- Door 2 Unlocked");
Serial.println("BBBB");
});
server.on("/lock3", [](){
server.send(200, "text/plain", "Okay -- Door 3 is Unlocked");
Serial.println("CCCC");
});
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop(void){
server.handleClient();
}

Arduino - No Delay loop with wifi client failing

Anyone who feels the title is incorrect or misleading, please feel free to update it.
The issue I'm having is with my Uno R3 and WiFi Shield. I really can't share the exact source right now, but this will explain the issue pretty well:
#include <WiFi.h>
#include <SPI.h>
#include <Ethernet.h>
char httpHost[] = "my.local.site.com";
IPAddress server(192,168,1,10);
int serverPort = 80;
char ssid[] = "myssid";
char password[] = "abcd123456";
int thingy = 0;
float lastTick = 0;
float tickInterval = 30000;
WiFiClient client;
boolean lastConnected = false;
void setup()
{
Serial.begin(9600);
connectWifi();
pinMode(2, INPUT);
}
void loop()
{
float currentMillis = millis();
if (currentMillis >= (lastTick + tickInterval) || currentMillis < lastTick) {
pollThingy();
logUpdate();
lastTick = currentMillis;
}
while (client.available()) {
char c = client.read();
Serial.print(c);
}
if (client.connected() == false && lastConnected == true) {
Serial.println("-- Disconnected.");
client.stop();
}
lastConnected = client.connected();
if (lastConnected == true) {
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
}
void connectWifi()
{
int status = WL_IDLE_STATUS;
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("No WiFi Shield Found! Aborting!");
while(true) { }
}
int i = 0;
while(true) {
Serial.print("Attempting to connect to network: ");
Serial.print(ssid);
Serial.print(" Attempt #");
Serial.println(++i);
status = WiFi.begin(ssid, password);
if (status == WL_CONNECTED) break;
delay(10000);
}
if (status == WL_CONNECTED) {
Serial.println("Connected!");
} else {
Serial.println("Failed connecting! Aborting!");
while(true) { }
}
}
void pollThingy()
{
thingy = digitalRead(2);
}
void logUpdate()
{
Serial.println("Logging update: ");
if (client.connected() == false) {
Serial.print("-- Connecting to ");
Serial.print(server);
Serial.print(":");
Serial.println(serverPort);
if (client.connect(server, serverPort)) {
Serial.println(" Connected!");
client.print("GET /some/Uri/with/arguments?output=json HTTP/1.1");
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(httpHost);
client.println("Connection: close");
client.println();
} else {
Serial.println(" Connection failed!");
}
} else {
Serial.println("-- Already connected... Skipping, this time...");
}
}
Now the issue I'm having seems to be kinda crazy. This will work fine and log an update to the given endpoint every 30 seconds as expected. However, after about 2 hours it just stalls and fails to do anything again. I've confirmed by watching the serial monitor, which returns nothing other than the last expected Serial.println() statements, but there are no errors to be displayed or anything.
I haven't updated the firmware on the WiFi shield since I got it, and I honestly couldn't tell you what revision it's running. However, I feel like this is something else entirely and I just can't put my finger on it.
Can anyone offer any advice?
Please don't beat me up too much about my code style. This is just a first draft.
You never close the connections you open. You show a connection to a web server, but no Keep-Alive header. The server should not be letting people sit on open connections just in case there is additional transactions to follow. I would expect the server to drop the connections.
The Arduino code never closes a connection, it just keeps opening another every time it sees the server has disconnected. At some point, the network stack runs out of handles to keep track of the debris.
For 30 s intervals, there is no reason to try to keep a connection open to a web server. Add a
client.stop()
after the last println().
You could confirm what is going on with two tools:
- netstat (or similar) would tell you if the server is accumulating connections in CLOSE_WAIT or TIME_WAIT
- Wireshark would let you see if the server is trying to close the connection and not being acknowledged by the Arduino.

Resources