Hi,
I will dump my code here tonight, but I think your advice worked.
I used 'Host: xxxxx.com' and the request go through.
But it goes through only when it wants ... the connect() fails for time to time and the issue may be linked to what you suggested. I need to do more testing, and I will give you the results later.
Update: so putting a myserial.disconnected() around the connect() statement helps a lot indeed !! Now I have a connection every time.
I still have some failure on the reset sometimes ... may be I need a flush somewhere.
But now my concern is that when I post a POST request I get as an answer:
*** NodeSet 2.0 ***
And not my "HTTP/1.1 200 OK" answer .... which means I entered into the Monitor Mode at some point ... not sure how !!
If I do the same POST using curl it works fine.
Here is some code pde and php (that may help others). Any help is welcome:
#include <NewSoftSerial.h>
#include <AF_XPort.h>
#include <avr/io.h>
#include <string.h>
#define HTTPPATH "/test.php"
#define WEB_HOST "secretweb.free.fr"
#define IPADDR "2x2.yx.63.zzz"
#define PORT 80
char linebuffer[256];
#define XPORT_RXPIN 2
#define XPORT_TXPIN 3
#define XPORT_RESETPIN 4
#define XPORT_DTRPIN 0
#define XPORT_CTSPIN 6
#define XPORT_RTSPIN 0
//*** Channel 1
//Baudrate 9600, I/F Mode 4C, Flow 00
//Port 10001
//Connect Mode : D4
//Send '+++' in Modem Mode enabled
//Show IP addr after 'RING' enabled
//Auto increment source port disabled
//Remote IP Adr: --- none ---, Port 00000
//Disconn Mode : 00
//Flush Mode : 77
AF_XPort xPortSerial = AF_XPort(XPORT_RXPIN, XPORT_TXPIN, XPORT_RESETPIN, XPORT_DTRPIN, XPORT_RTSPIN, XPORT_CTSPIN);
uint8_t errno;
void setup()
{
Serial.begin(9600);
Serial.println("Ready?");
// set the data rate for the NewSoftSerial port
xPortSerial.begin(9600);
int ver = NewSoftSerial::library_version();
Serial.println(ver);
}
void loop() // run over and over again
{
Serial.println("Saving...");
save_data();
Serial.println("Done");
delay(10000);
}
void save_data() {
int i=0;
char json[328];
sprintf(json,"{\"log\":{\"AC_relays\":[{\"id\":0,\"value\":%d},{\"id\":1,\"value\":%d},{\"id\":2,\"value\":%d},{\"id\":3,\"value\":%d}],\"DC_relays\":[{\"id\":0,\"value\":%d},{\"id\":1,\"value\":%d},{\"id\":2,\"value\":%d},{\"id\":3,\"value\":%d}],\"ds18b20\":[{\"id\":0,\"value\":%2d},{\"id\":1,\"value\":%2d},{\"id\":2,\"value\":%2d}],\"water_level\":[{\"id\":0,\"value\":%4d},{\"id\":1,\"value\":%4d}]}}",0,1,0,1,1,2,1,0,25,22,27,1003,2000);
httpPost(json);
//httpGet();
}
int httpPost(char* json)
{
uint8_t ret;
char *found=0;
ret = xPortSerial.reset();
switch (ret) {
case ERROR_TIMEDOUT: {
Serial.println("Timed out on reset!");
return 0;
}
case ERROR_BADRESP: {
Serial.println("Bad respons on reset!");
return 0;
}
case ERROR_NONE: {
Serial.println("Reset OK!");
break;
}
default:
Serial.println("Unknown error");
return 0;
}
// time to connect...
if (xPortSerial.disconnected()) {
ret = xPortSerial.connect(IPADDR, PORT);
switch (ret) {
case ERROR_TIMEDOUT: {
Serial.println("Timed out on connect");
return 0;
}
case ERROR_BADRESP: {
Serial.println("Failed to connect");
return 0;
}
case ERROR_NONE: {
Serial.println("Connected..."); break;
}
default:
Serial.println("Unknown error");
return 0;
}
} else {
Serial.println("Already connected");
}
//xPortSerial.flush(250);
//xPortSerial.print("GET "); xPortSerial.print("/"); xPortSerial.println(" HTTP/1.1");
//base64encode(USERNAMEPASS, linebuffer);
xPortSerial.print("POST "); xPortSerial.print(HTTPPATH); xPortSerial.println(" HTTP/1.1");
Serial.print("POST "); Serial.print(HTTPPATH); Serial.println(" HTTP/1.1");
// next, the authentication
xPortSerial.print("Host: "); xPortSerial.println(WEB_HOST);
Serial.print("Host: "); Serial.println(WEB_HOST);
//xPortSerial.print("Authorization: Basic ");
//Serial.print("Authorization: Basic ");
//xPortSerial.println(linebuffer);
//Serial.println(linebuffer);
//xPortSerial.println("User-Agent: arduino");
//Serial.println("User-Agent: arduino");
xPortSerial.print("Content-Length: "); xPortSerial.println(1+strlen(json), DEC);
Serial.print("Content-Length: "); Serial.println(1+strlen(json), DEC);
xPortSerial.print("Content-Type: application/x-www-form-urlencoded\n");
Serial.print("Content-Type: application/x-www-form-urlencoded\n");
//xPortSerial.print("\nsubmit=");
xPortSerial.println(json);
//Serial.print("\nsubmit=");
Serial.println(json);
xPortSerial.println("");
Serial.println("");
// while (1) {
// read one line from the xport at a time
ret = xPortSerial.readline_timeout(linebuffer, 255, 3000); // 6s timeout
Serial.println(linebuffer);
if (strstr(linebuffer, "HTTP/1.1 200 OK") == linebuffer) {
Serial.println("Done Success...");
ret = 1;
} else if (strstr(linebuffer, "HTTP/1.1 400 Bad Request") == linebuffer) {
Serial.println("Done Bad Request..");
ret = 0;
}
if (((errno == ERROR_TIMEDOUT) && xPortSerial.disconnected()) ||
((XPORT_DTRPIN == 0) &&
(linebuffer[0] == 'D') && (linebuffer[1] == 0))) {
Serial.println("Disconnected...");
return ret;
}
// }
}
Here is the test.php file:
<?php
if (!function_exists('json_decode')) {
function json_decode($json) {
$comment = false;
$out = '$x=';
for ($i=0; $i<strlen($json); $i++)
{
if (!$comment)
{
if ($json[$i] == '{') $out .= ' array(';
else if ($json[$i] == '}') $out .= ')';
else if ($json[$i] == ':') $out .= '=>';
else if ($json[$i] == '[') $out .= ' array('; else if ($json[$i] == ']') $out .= ')';
else $out .= $json[$i];
}
else $out .= $json[$i];
if ($json[$i] == '"') $comment = !$comment;
}
eval($out . ';');
return $x;
}
}
$handle = fopen('php://input','r');
$jsonInput = fgets($handle);
$decoded = json_decode($jsonInput);
var_dump($decoded);
fclose($handle);
?>
Here is the result I get:
Ready?
9
Saving...
Reset OK!
Connected
POST /test.php HTTP/1.1
Host: mysecretweb.com
Content-Length: 22
Content-Application: application/x-www-form-urlencoded
<json string>
*** NodeSet 2.0 ***
Done
Any idea how can I get an html header as expected.