TCPserver stops accepting requests

I’m using a browser to core-ip. It displays the buttons for about a few minutes then it gets stuck.

Here’s a sippet ofmy code below. Thanks for the help

/* Includes ------------------------------------------------------------------*/
#include "application.h"
#include "spark-dallas-temperature.h"
#include "OneWire.h"
#define ONE_WIRE_BUS 2
#define MISTER1_PIN 7
#define TEMPER1SZ 12
#define TEMPER1SZHR 24

int temperatureRead(int probe);
    
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensor(&oneWire);

float temperature = 1.0;

char curTemper = 0;

// Interval is how long we wait
unsigned long interval1;

SYSTEM_MODE(AUTOMATIC);

TCPServer server = TCPServer(80);
TCPClient client;

/* This function is called once at start up ----------------------------------*/
void setup()
{
	Time.zone(-8); // PST time zone

 sensor.begin();
 sensor.setResolution(12);

 pinMode(MISTER1_PIN, OUTPUT);
 digitalWrite(MISTER1_PIN, LOW);
 interval1 = mist1Ontime;

 // TCP start listening for clients
 server.begin();

 // Make sure your Serial Terminal app is closed before powering your Core
 Serial.begin(9600);
 // Now open your Serial Terminal, and hit any key to continue!
 // while(!Serial.available()) SPARK_WLAN_Loop();

 Serial.println(WiFi.localIP());
 Serial.println(WiFi.subnetMask());
 Serial.println(WiFi.gatewayIP());
 Serial.println(WiFi.SSID());

}

/* This function loops forever --------------------------------------------*/
void loop() {
    // listen for incoming clients
    client = server.available();
    if (client) {
        Serial.println("new client");
        // an http request ends with a blank line
        boolean currentLineIsBlank = true;
        while (client.connected()) {
            if (client.available()) {
                char c = client.read();
                Serial.write(c);
                // if you've gotten to the end of the line (received a newline
                // character) and the line is blank, the http request has ended,
                // so you can send a reply
                if (c == '\n' && currentLineIsBlank) {
                    // send a standard http response header
                    client.println("HTTP/1.1 200 OK");
                    client.println("Content-Type: text/html");
                    client.println("Connection: close");  // the connection will be closed after completion of the response
                    client.println("Refresh: 10");  // refresh the page automatically every 5 sec
                    client.println();
                    client.println("<!DOCTYPE HTML>");
                    client.println("<html>");
                    client.print("<button style=\"width:47\%\">Temperature<br><h2>");
										client.print(temperatureRead(1));
										client.println("</h2></button>");
										client.print("<button style=\"width:47\%\">Temperature<br><h2>");
										client.print("97\%");
										client.println("</h2></button>");
                  //  client.print("The current time is: <B>");
                  //  client.print(Time.timeStr());
                  //  client.println("</B>");

                    // output the value of each analog input pin
                    /*for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
                    int sensorReading = analogRead(analogChannel);
                    client.print("analog input ");
                    client.print(analogChannel);
                    client.print(" is ");
                    client.print(sensorReading);
                    client.println("<br />");
                    }*/
                    client.println("</html>");
                    client.println();
                    delay(5);
                    break;
                }
                if (c == '\n') {
                    // you're starting a new line
                    currentLineIsBlank = true;
                }
                else if (c != '\r') {
                    // you've gotten a character on the current line
                    currentLineIsBlank = false;
                }
            }
        }
        // gi
    b browser time to receive the data
        delay(1);
        // close the connection:
        client.stop();
        Serial.println("client disconnected");
    }
}

/*******************************************************************************
* Function Name  : temperatureRead
* Description    : Reads temperature
* Return         :
*******************************************************************************/
int temperatureRead(int probe)
{
	int retTemper;
	sensor.requestTemperatures();
  retTemper = sensor.getTempFByIndex(0);
  //sprintf(myStr,"%.3f",temperature);
	return retTemper;
}

Since you are using SYSTEM_MODE(AUTOMATC), could it be that your while(client.connected()) { ... } keeps the loop() too long without allowing for cloud communication and this trips up the server?

Given the docs statement

connected()
[...]
Note that a client is considered connected if the connection has been closed but there is still unread data.
[...]

heres my code. It works a few times using the core-ip then stops until I reset the core.

thanks for any help

int temperatureRead(int probe);
//int tinkerAnalogWrite(String command);
//int mistertimer1(int mTimer1);


OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensor(&oneWire);

float temperature = 1.0;

// Tracks the last time event fired
unsigned long previousMillis1 = 0;
unsigned long currentMillis;

// Interval is how long we wait
unsigned long interval1;

SYSTEM_MODE(AUTOMATIC);

TCPServer server = TCPServer(80);
TCPClient client;

/* This function is called once at start up ----------------------------------*/
void setup()
{
	Time.zone(-8); // PST time zone

 sensor.begin();
 sensor.setResolution(12);

 pinMode(MISTER1_PIN, OUTPUT);
 digitalWrite(MISTER1_PIN, LOW);
 interval1 = mist1Ontime;

 // TCP start listening for clients
 server.begin();

 // Make sure your Serial Terminal app is closed before powering your Core
 Serial.begin(9600);
 // Now open your Serial Terminal, and hit any key to continue!
 // while(!Serial.available()) SPARK_WLAN_Loop();

 Serial.println(WiFi.localIP());
 Serial.println(WiFi.subnetMask());
 Serial.println(WiFi.gatewayIP());
 Serial.println(WiFi.SSID());

}

/* This function loops forever --------------------------------------------*/
void loop() {
	currentMillis = millis(); // capture current mills for mist timers
	//check every 5 seconds
	if ((unsigned long)(currentMillis - prevTempMills1) >= (unsigned long)5000) {
    // listen for incoming clients
    client = server.available();
    if (client) {
        Serial.println("new client");
        // an http request ends with a blank line
        boolean currentLineIsBlank = true;
        while (client.connected()) {
            if (client.available()) {
                char c = client.read();
                Serial.write(c);
                // if you've gotten to the end of the line (received a newline
                // character) and the line is blank, the http request has ended,
                // so you can send a reply
                if (c == '\n' && currentLineIsBlank) {
                    // send a standard http response header
                    client.println("HTTP/1.1 200 OK");
                    client.println("Content-Type: text/html");
                    client.println("Connection: close");  // the connection will be closed after completion of the response
                    client.println("Refresh: 10");  // refresh the page automatically every 5 sec
                    client.println();
                    client.println("<!DOCTYPE HTML>");
                    client.println("<html>");
										client.print("<head><title>Aeroponics Dashboard</title><style>");
										client.print(" .sensor-blk {width:47\%; font-family: Arial; color: \#ffffff; font-size: 20px; background:\#3498db; padding: 10px 10px 10px 10px; text-decoration: none;}");
										client.println("</style></head><body>");

                    client.print("<button class=\"sensor-blk\">Temperature<br><h2>");
										client.print(temperatureRead(1));
										client.println("</h2></button>");
										client.print("<button class=\"sensor-blk\">Humidity<br><h2>");
										client.print("97\%");
										client.println("</h2></button>");
                  //  client.print("The current time is: <B>");
                  //  client.print(Time.timeStr());
                  //  client.println("</B>");

                    // output the value of each analog input pin
                    /*for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
                    int sensorReading = analogRead(analogChannel);
                    client.print("analog input ");
                    client.print(analogChannel);
                    client.print(" is ");
                    client.print(sensorReading);
                    client.println("<br />");
                    }*/
                    client.println("</body></html>");
                    client.println();
                    delay(5);
                    break;
                }
                if (c == '\n') {
                    // you're starting a new line
                    currentLineIsBlank = true;
                }
                else if (c != '\r') {
                    // you've gotten a character on the current line
                    currentLineIsBlank = false;
                }
            }
        }
        // give the web browser time to receive the data
        delay(5);
        // close the connection:
        client.stop();
        Serial.println("client disconnected");
    }
	}
}

/*******************************************************************************
* Function Name  : temperatureRead
* Description    : Reads temperature
* Return         :
*******************************************************************************/
int temperatureRead(int probe)
{
	int retTemper;
	sensor.requestTemperatures();
  retTemper = sensor.getTempFByIndex(0);
  //sprintf(myStr,"%.3f",temperature);
	return retTemper;
}

This looks like essentially the same code and problem, so I combined your threads.

Right now, every one of the those client.print/println is generating a huge number of packets–you would be better off using char array around 1000-1400 bytes in length and building up your response and sending with one client.write call. Eventually this will get fixed but making this change should help a lot.

Search here in the forum for the “out” function that shows how to do this.

1 Like

I need more help. I read that when its in Automatic mode the waits to connect to the cloud before progressing.I dont want that.So I put it in Semi-auto and now the Core does not respond to the core-ip. What am I missing in my code to run without the cloud connection?
Thanks again

I experience the same difficulty with TCPServer. It just stops listening to incoming requests…usually with a fouled connection open. However, serial debug prints indicate that the code has done TCPClient.stop. It’s almost like TCPServer is accepting a connection without permission…and then doesn’t do anything with it. If I forcefully close the connection (via TCPView), the backlog of requests on the browser gives the Core a barrage!
Yes, I am using the TCPClient.write(buf, len) for all outputs, generally sending 50-255 bytes/send. However, the routine that tends to fail is only sending 23 bytes (as one packet). It runs for a random length of time (@every 4 seconds) and then TCPServer just stops responding to incoming connections.

System mode is automatic. I added some Spark.process() lines to the code…a few too many, I guess, as my first revision resulted in a hard fault. Just one process() in the main connection loop seemed to do the trick, although it does sometimes cause a 2-3 second delay (and a fail on the other end). Well, it didn’t fix the problem, as the TCPServer eventually started refusing to accept incoming connection. Next, the Core went back to “connecting to cloud”, despite the Spark.process() calls in a dead loop (unless a connection is opened, for less than one second).

I can provide some code tomorrow. Admittedly, though, I just don’t understand the difficulties with the TCPServer/TCPClient. Maybe the Photon has all of the world’s problems solved?? Or perhaps I need to redefine the TCPServer each time I close a connection?