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)
?>