I have an application where I want to keep the TCP connection with the server.
So, I am opening a TCP connection by making call to connect(). However, the connection gets closed immediately after opening. I am not making any HTTP request.
In my opinion, the server is not closing the connection. I tried using the ethernet shield and connecting to the server. In this case, the server does not close connection by itself.
I am using Arduino Uno and SparkFun WiFi Shield - ESP8266 . Following is the code and serial monitor output.
Any ideas on how to keep TCP connection open for a long time will be helpful.
/************************************************************
ESP8266_Shield_Demo.h
SparkFun ESP8266 AT library - Demo
Jim Lindblom @ SparkFun Electronics
Original Creation Date: July 16, 2015
https://github.com/sparkfun/SparkFun_ESP8266_AT_Arduino_Library
This example demonstrates the basics of the SparkFun ESP8266
AT library. It'll show you how to connect to a WiFi network,
get an IP address, connect over TCP to a server (as a client),
and set up a TCP server of our own.
Development environment specifics:
IDE: Arduino 1.6.5
Hardware Platform: Arduino Uno
ESP8266 WiFi Shield Version: 1.0
This code is released under the MIT license.
Distributed as-is; no warranty is given.
************************************************************/
//////////////////////
// Library Includes //
//////////////////////
// SoftwareSerial is required (even you don't intend on
// using it).
#include <SoftwareSerial.h>
#include <SparkFunESP8266WiFi.h>
//////////////////////////////
// WiFi Network Definitions //
//////////////////////////////
// Replace these two character strings with the name and
// password of your WiFi network.
const char mySSID[] = "BlueVulture_TP-Link";
const char myPSK[] = "password_removed";
// To use the ESP8266 as a TCP client, use the
// ESP8266Client class. First, create an object:
ESP8266Client client;
//////////////////
// HTTP Strings //
//////////////////
const String httpRequest = "GET / HTTP/1.1\n"
"Host: www.example.com\n"
"Connection: close\n\n";
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
//char server[] = "www.google.com"; // name address for Google (using DNS)
char server[] = "74.125.232.128"; // name address for Google (using DNS)
//char server[] = "174.129.224.73"; // echo.websocket.org
//IPAddress server(174,129,224,73); // numeric IP for echo.websocket.org
//const char server[] = "example.com";
// All functions called from setup() are defined below the
// loop() function. They modularized to make it easier to
// copy/paste into sketches of your own.
void setup()
{
// Serial Monitor is used to control the demo and view
// debug information.
Serial.begin(9600);
serialTrigger(F("Press any key to begin."));
// initializeESP8266() verifies communication with the WiFi
// shield, and sets it up.
initializeESP8266();
// connectESP8266() connects to the defined WiFi network.
connectESP8266();
// displayConnectInfo prints the Shield's local IP
// and the network it's connected to.
displayConnectInfo();
Serial.print("connecting to ");
Serial.print(server);
Serial.println("...");
// if you get a connection, report back via serial:
int retCode = client.connect(server, 80, 7200);
if (retCode > 0) {
Serial.print("connected to ");
Serial.println(server);
// Make a HTTP request:
// client.print(httpRequest);
} else {
Serial.print("Return code: ");
Serial.println(retCode);
Serial.print("return from status() :");
Serial.println(client.status());
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
}
void loop()
{
// available() will return the number of characters
// currently in the receive buffer.
while (client.available())
Serial.write(client.read()); // read() gets the FIFO char
// if the server's disconnected, stop the client:
// Serial.println("Checking if client is disconnected...");
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting...");
client.stop();
Serial.println("[Disconnected]");
// do nothing forevermore:
while (true) {
delay(1);
}
}
}
void initializeESP8266()
{
// esp8266.begin() verifies that the ESP8266 is operational
// and sets it up for the rest of the sketch.
// It returns either true or false -- indicating whether
// communication was successul or not.
// true
int test = esp8266.begin();
if (test != true)
{
Serial.println(F("Error talking to ESP8266."));
errorLoop(test);
}
Serial.println(F("ESP8266 Shield Present"));
}
void connectESP8266()
{
// The ESP8266 can be set to one of three modes:
// 1 - ESP8266_MODE_STA - Station only
// 2 - ESP8266_MODE_AP - Access point only
// 3 - ESP8266_MODE_STAAP - Station/AP combo
// Use esp8266.getMode() to check which mode it's in:
int retVal = esp8266.getMode();
if (retVal != ESP8266_MODE_STA)
{ // If it's not in station mode.
// Use esp8266.setMode([mode]) to set it to a specified
// mode.
retVal = esp8266.setMode(ESP8266_MODE_STA);
if (retVal < 0)
{
Serial.println(F("Error setting mode."));
errorLoop(retVal);
}
}
Serial.println(F("Mode set to station"));
// esp8266.status() indicates the ESP8266's WiFi connect
// status.
// A return value of 1 indicates the device is already
// connected. 0 indicates disconnected. (Negative values
// equate to communication errors.)
retVal = esp8266.status();
if (retVal <= 0)
{
Serial.print(F("Connecting to "));
Serial.println(mySSID);
// esp8266.connect([ssid], [psk]) connects the ESP8266
// to a network.
// On success the connect function returns a value >0
// On fail, the function will either return:
// -1: TIMEOUT - The library has a set 30s timeout
// -3: FAIL - Couldn't connect to network.
retVal = esp8266.connect(mySSID, myPSK);
if (retVal < 0)
{
Serial.println(F("Error connecting"));
errorLoop(retVal);
}
}
}
void displayConnectInfo()
{
char connectedSSID[24];
memset(connectedSSID, 0, 24);
// esp8266.getAP() can be used to check which AP the
// ESP8266 is connected to. It returns an error code.
// The connected AP is returned by reference as a parameter.
int retVal = esp8266.getAP(connectedSSID);
if (retVal > 0)
{
Serial.print(F("Connected to: "));
Serial.println(connectedSSID);
}
// esp8266.localIP returns an IPAddress variable with the
// ESP8266's current local IP address.
IPAddress myIP = esp8266.localIP();
Serial.print(F("My IP: ")); Serial.println(myIP);
}
// errorLoop prints an error code, then loops forever.
void errorLoop(int error)
{
Serial.print(F("Error: ")); Serial.println(error);
Serial.println(F("Looping forever."));
for (;;)
;
}
// serialTrigger prints a message, then waits for something
// to come in from the serial port.
void serialTrigger(String message)
{
Serial.println();
Serial.println(message);
Serial.println();
while (!Serial.available())
;
while (Serial.available())
Serial.read();
}