TCP_Client SEMI-AUTOMATIC mode

Hi
I started a project that needs to connect to a local webserver. The use of Spark Cloud is not possible, as my intranet will not have cloud access.

Saying this, after struggling with other issues, I am able to have the demo working, EXCEPT the case when I don’t connect to Spark Cloud.

Some have asked my code


, which I published partially. App is OK from other config perspective I guess, but I keep being asked on other details not related to TCP_Client, so I cleaned up code and generated new app for testing only, and if able to solve it, will share it on git as a base example for others not to struggle as I am now.

ISSUE:
With 1 second timer to GET from local webserver, after a few cycles Photon crashes with red led blinks and restarts.
I have D7 to signal the time from start to end of GET. It seems that when webserver takes longer to reply, (I can see from led being lit longer) it will just take 3-4 tries and it dies.
Now I am tempted to say that there is an issue with re-entrant functions for TCP/WiFi class implementation timeouts…?

Here my php code (really just a query to take some time on db execute and do not return anything)

<?php
/*code just to test failure on Photon on SEMI-AUTO config*/

  	//error_reporting(E_ALL);
	//ini_set('display_errors', true);
	try
	{
		//open the database
		$db = new PDO('sqlite:test.db');
		//run query
		$result = $db->query('SELECT * FROM test2');
		// close the database connection
		$db = NULL;
	}
	catch(PDOException $e)
	{
		print 'Exception : '.$e->getMessage();
	}
?>

Now my ino app: (with WiFi use instead of Spark)

#include "SparkIntervalTimer/SparkIntervalTimer.h"

#include "HttpClient/HttpClient.h"

HttpClient http;

http_request_t request;
http_response_t response;
// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
    //  { "Content-Type", "application/json" },
    //  { "Accept" , "application/json" },
    //{ "Accept" , "* /*"},
    { NULL, NULL } // NOTE: Always terminate headers will NULL
};



int aa = 0; //dummy counter to check failure count


SYSTEM_MODE(SEMI_AUTOMATIC);    //not to enable Spark Cloud connection

// Creat IntervalTimer objects
IntervalTimer myTimer;

bool update_db_ready = false;            //flag update time

void update_db_tmr()
{
    update_db_ready = true;
}

void update_db()
{
    digitalWrite(D7,1);
    if (WiFi.ready() == true) 
//    if (Spark.connected() == true) 
    {
        //request.hostname = "192.168.1.61";
        request.ip = IPAddress(192,168,1,61);
        request.port = 80;
        request.path = "/testdb.php?write=" + String(aa++);
        http.get(request, response, headers);
    }
    else
    {
    }
    digitalWrite(D7,0);
 }


void setup()   { 
    pinMode(D7,OUTPUT);
    connect();    //connect to WiFi or Spark
    myTimer.begin(update_db_tmr, 2*1000, hmSec);   //init my update timer flag event in half milliseconds
}


void loop() {
    if (update_db_ready)
    {
        update_db();
        update_db_ready = false;
    }
}


void connect() {
/*  if (Spark.connected() == false) {
    Spark.connect();
  }
  while (Spark.connected() == false) delay(1000);
*/

    WiFi.on();
    if (WiFi.ready() == false)
        WiFi.connect();
    while (WiFi.ready() == false) 
        delay(1000);

}

update: if my php code echo anything… It improves but not solve


.

<?php
/*code just to test failure on Photon on SEMI-AUTO config*/

  	//error_reporting(E_ALL);
	//ini_set('display_errors', true);
	try
	{
		//open the database
		$db = new PDO('sqlite:test.db');
		//run query
		$db->exec("INSERT INTO test2 (Reading) VALUES (".$_GET["write"]. ");");
		//$result = $db->query('SELECT * FROM test2');
		// close the database connection
		$db = NULL;
		//usleep(100000); //ms delay worked better (was:fine also).... maybe Photon needs some time connection on or some reply?
	}
	catch(PDOException $e)
	{
		print 'Exception : '.$e->getMessage();
	}
	echo "OK"; //works better( was: works fine)
?>

I’d love to explain why… but I can’t.
It works IF my php code looks like this:

<?php
/*code just to test failure on Photon on SEMI-AUTO config*/

  	//error_reporting(E_ALL);
	//ini_set('display_errors', true);
	try
	{
		//open the database
		$db = new PDO('sqlite:test.db');
		//run query
		$db->exec("INSERT INTO test2 (Reading) VALUES (".$_GET["write"]. ");");
		//$result = $db->query('SELECT * FROM test2');
		// close the database connection
		$db = NULL;
		usleep(1000); //1mS  this seems to be really needed combined with sending echo back
	}
	catch(PDOException $e)
	{
		print 'Exception : '.$e->getMessage();
	}
	echo "OK"; //seems mandatory together with delay
?>

not yet… :frowning: : (WAS: LOOKS SOLVED)

Still fails after long time…
It seems that after first failure… it keeps failing quite often… so really a loose end…
Hitting reset button seems to restart the complete process…

I have one of my photons which works out the temperature, sends it to my local server every minute , which posts that to thinspeak.
I followed the example.at https://docs.particle.io/reference/firmware/photon/#tcpclient , and just use the basics … connect to the server , if connected send data , wait for incoming data , act on data , than wait for the next minute etc.
So far 10 days without any problems !!!

Just upgraded to 0.4.6 , and having problems myself , not connecting to a server or just locking

1 Like

Another one…