I think Xbee to Xbee across networks would work fine, but my goal is to have 1 HTML / PHP web interface on a remote server and 1 Xbee at home connected to my network. The biggest thing I was trying to accomplish was a direct socket connection from PHP to the Xbee which I don't think is possible. Maybe it has something to do with the way PHP connects and waits for acceptance to connect to the socket?
In your example, you show Xbee to Xbee. If you can get PHP to connect directly to an Xbee without a middle socket server running, please let me know how
Okay, you're right. I seem to be missing your point. But I have actually done this from PC to XBee within the same network. I wrote a TCP client in VB.NET and used it to test communication long before I did all the other stuff. I do not recall experiencing any packet-loss with the app. The only thing that did happen was that when the TCP connection timed out, I had to "reconnect". I didn't seem to have any way to determine that the connection did timeout.
It would be easy enough to test for packet-loss by writing a program that sends serialized packets and receives an acknowledgement from the remote side. This would be similar to how the one project worked sending packets over the internet, except with a PC on the other end.
What do you mean my "serialized" packets? I am checking for a beginning character and and end character which works, but the middle characters sometimes come through incorrectly. Like "R:C:3:#" may send fine 10 times in a row, then it will come through as "R:
When I am testing any kind of radio system for errors or missing packets I send out two or three sets of values all combined into a packet and using a simple checksum. The first value is a serial number which starts at 0 and continues incrementing by one for each packet sent. Each time the receiver gets a valid packet it stores the serial number and when the next valid packet comes in it is checked to see if it the next value. If not it calculates how many packets were missed and adds that the the missed packet count. This way I can tell exactly how many packets have been missed over a period of time. The checksum is a simple byte, word or long (depending on what your MCU stores) that is the sum of all the bytes being sent. This value is then added to the end of the packet. I also prefix all packets with a preamble character string. For example, my current project with XBee Wi-Fi prefixes each command with, "!XB" and the next character is the command. You can always generate a checksum and compare it on the receiving end to tell you if the command has been corrupted.
So what do you do in the case of missing packets? This seems to be quite a difficult task to send and receive data via a socket. Watching the output of my socket server, the problem seems to be at the Xbee to the Prop.
When I send data to the socket server, the data appears correctly in the "output view" of what the socket is getting. What is received on the XBee to the Prop is right about 80% of the time. The data sent out of the Xbee to the socket server is correct about 30% of the time.
Data being sent from the circuit to the socket server is done via a line like : xb.str(string("Moving to Park Position", 13)) The "output view" of the socket server shows this string a number of ways and 70% of the time, it is only part of the string.
As you can see, the HTML / Jquery / PHP code sends fine. As soon as I try to send from the Prop to the server, the string is all messed up and half missing....
So, after hours of trying to get this to work, I think I got it!
So to collaborate on what works and what does not work...
What does NOT work....
PHP socket connecting directly to an XBee. Apparently the protocol is different? No matter what I tried (TCP and UDP), the connection could not be made. Maybe there is a way, but I cannot figure it out.
TCP socket server using socket_read() as PHP_NORMAL_READ. Even with proper escapes "\r" and/or "\n", data will be lost. Not sure what causes this.
TCP socket client using socket_read() as PHP_BINARY_READ to read from the socket causes the server script to hang or timeout since it is trying to "stream" the data. Checking for a "start" and "end" character may help but decided to use PHP_NORMAL_READ and end each string with chr(10) and chr(13)
What does work...
Communicating with an XBee in Transparent Mode via PHP TCP socket server!
Streaming output of Xbee to socket server without "\r" and "\n" : string("blah", 13). Use "start" and "end" characters to parse on socket server script to output to other clients (Thank you Chris for this idea). Socket Client will receive what the server parses and adds the "\r\n" to.
In my case, I used "<" and ">" symbols as Chris did in his project. The client (HTML Form) would send a string such as <R:C:5:#> to the socket server. The server script would check for both start and end symbols and if there, grab the string between the delimiters and send to other connected clients. The Xbee will read this data and check for an "R" for the beginning character and an # symbol before processing the string to get the command.
In order to keep the Xbee connected to the server, I set TM (TCP Client Connection Timeout) to the value 1770 which is 6000 * 100ms or 600 seconds. When the program starts, it sends a string to the socket server which accepts the connection and creates the client. In the PROP code, the Xbee will "PING" the server every 20 seconds to keep the connection alive.
Now for the code!
socket.php - creates multiclient socket server. Cron runs this script every minute using nohup php socket.php
<?php
set_time_limit(0);
function extract_unit($string, $start, $end){
$pos = stripos($string, $start);
$str = substr($string, $pos);
$str_two = substr($str, strlen($start));
$second_pos = stripos($str_two, $end);
$str_three = substr($str_two, 0, $second_pos);
$unit = trim($str_three); // remove whitespaces
return $unit;
}
$address = 'SERVER_IP_ADDRESS';
$port = SERVER_PORT;
echo "Creating socket.\n";
$sock = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Could not create socket.");
@socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1);
$timeout = array('sec'=>2,'usec'=>500000);
@socket_set_option($socket,SOL_SOCKET,SO_RCVTIMEO,$timeout);
echo "Binding to socket.\n";
$result = @socket_bind($sock, 0, $port) or die("Could not Bind to socket");
echo "Listening....\n";
$result = @socket_listen($sock) or die("Could not listen to socket.");
$clients = array($sock);
$string = array();
while (true) {
$read = $clients;
if (@socket_select($read, $write = NULL, $except = NULL, 0) < 1)
continue;
if (in_array($sock, $read)) {
$clients[] = $newsock = @socket_accept($sock);
@socket_write($newsock, "Connected:");
@socket_getpeername($newsock, $ip);
echo "New client connected: {$ip}\n";
$key = array_search($sock, $read);
unset($read[$key]);
}
foreach ($read as $read_sock) {
$key = array_search($read_sock, $clients);
$data = @socket_read($read_sock, 512, PHP_BINARY_READ);
// check if the client is disconnected
if ($data == false) {
// remove client for $clients array
unset($clients[$key]);
echo "client disconnected.\n";
continue;
}
if (!empty($data)) {
// REMOVE ALL NON-PRINTABLE CHARACTERS
$string[$key] = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $data);
// IF BEGINNING CHARACTER AND END CHARACTER EXIST, OUTPUT TO ALL OTHER CLIENTS
if(strpos($string[$key], "<") !== false && strpos($string[$key], ">") !== false){
$tosend = extract_unit($string[$key], "<", ">");
foreach ($clients as $send_sock) {
if ($send_sock == $sock || $send_sock == $read_sock)
continue;
@socket_write($send_sock, $tosend.chr(10).chr(13));
}
echo $tosend."\n";
unset($string[$key]);
} else {
// IF START AND END CHARACTERS DO NOT EXIST, OUTPUT EXISTING DATA TO SCREEN
echo "More : ".$string[$key];
}
}
}
}
@socket_close($sock);
?>
client.php - Sends data from HTML form and returns data sent back from PROP
control_panel.html - Web interface. Yes, I used Tables :P Was in a hurry. Images were just clipart found on Google. Also well visible on a smart phone or tablet.
Glad to hear you got it working. I'm not much for PHP. I have modded my website heavily (which uses PHP), but only within the realm of what is already capable on the various pages.
Yes, I am quite happy to get it working reliably. I am continually changing the script to add more functions / redo some of the functions so I can use this thread as a "restart" point if needed. PHP is my strong point and SPIN is like a 6th language for me so I am still attempting to learn it while keeping up to date in all the other languages I know.... Pretty tough!
is it possible to have a example of a program in spin that just :
- configure the xbee S6B
- send a http get request to http://www.google.com
- print the output to the serial.
i would like to retrieve information from the weather channel but i don't know how to start
I am not sure about getting the Xbee communicating directly with the web and requesting the output of a page. The Xbee setup I have is connecting to a port that I have a PHP socket server running on. The Xbee can send out a "Ping" command and request data every X seconds and the socket server will process the data and send back whatever information you want.
To parse weather data, you will have to find an XML feed which will be the easiest to work with to parse using the PHP socket server. Have the socket server wait for the command from the Xbee, when the command comes, pull the data from the website or XML file, parse the data, and send back the response to the XBee to use in your SPIN program. If you need help with the PHP socket server or the spin, let me know
Apologies for that. When my wife passed away I kind of lost motivation to fix the site issues caused by vBulletin. I have new software and eventually I will have the content back up. I appreciate your patience.
Chris, I have created scripts that can export the Vbulletin scripts to other scripts if you need help with that. Sorry to hear about your wife! Hope you are doing well.
I have stopped using vBulletin and am now just running a Joomla site at the same address. I just need to take some time to get the content back up. I backed everything up from my old website.
I've got "GET HTTP" to work . My issue was that I did not supply the complete escape sequence for the GET HTTP string which is "\r\n\r\n" or 0d, 0a,0d,0a. so the total request in the API frame payload is "GET / HTTP/1.1\r\n\r\n". Hope this helps someone. Took me quite a few hours to nail it.
now i still have to code in spin to send that from propeller.
Comments
At that point the only real difference between internal and external is the port forwarding on the router.
In your example, you show Xbee to Xbee. If you can get PHP to connect directly to an Xbee without a middle socket server running, please let me know how
It would be easy enough to test for packet-loss by writing a program that sends serialized packets and receives an acknowledgement from the remote side. This would be similar to how the one project worked sending packets over the internet, except with a PC on the other end.
When I send data to the socket server, the data appears correctly in the "output view" of what the socket is getting. What is received on the XBee to the Prop is right about 80% of the time. The data sent out of the Xbee to the socket server is correct about 30% of the time.
Data being sent from the circuit to the socket server is done via a line like : xb.str(string("Moving to Park Position", 13)) The "output view" of the socket server shows this string a number of ways and 70% of the time, it is only part of the string.
SERVER.PHP output. Comments are in "( )".
As you can see, the HTML / Jquery / PHP code sends fine. As soon as I try to send from the Prop to the server, the string is all messed up and half missing....
I am using this to send through the XB :
SERVER.PHP content
So to collaborate on what works and what does not work...
What does NOT work....
PHP socket connecting directly to an XBee. Apparently the protocol is different? No matter what I tried (TCP and UDP), the connection could not be made. Maybe there is a way, but I cannot figure it out.
TCP socket server using socket_read() as PHP_NORMAL_READ. Even with proper escapes "\r" and/or "\n", data will be lost. Not sure what causes this.
TCP socket client using socket_read() as PHP_BINARY_READ to read from the socket causes the server script to hang or timeout since it is trying to "stream" the data. Checking for a "start" and "end" character may help but decided to use PHP_NORMAL_READ and end each string with chr(10) and chr(13)
What does work...
Communicating with an XBee in Transparent Mode via PHP TCP socket server!
Streaming output of Xbee to socket server without "\r" and "\n" : string("blah", 13). Use "start" and "end" characters to parse on socket server script to output to other clients (Thank you Chris for this idea). Socket Client will receive what the server parses and adds the "\r\n" to.
In my case, I used "<" and ">" symbols as Chris did in his project. The client (HTML Form) would send a string such as <R:C:5:#> to the socket server. The server script would check for both start and end symbols and if there, grab the string between the delimiters and send to other connected clients. The Xbee will read this data and check for an "R" for the beginning character and an # symbol before processing the string to get the command.
In order to keep the Xbee connected to the server, I set TM (TCP Client Connection Timeout) to the value 1770 which is 6000 * 100ms or 600 seconds. When the program starts, it sends a string to the socket server which accepts the connection and creates the client. In the PROP code, the Xbee will "PING" the server every 20 seconds to keep the connection alive.
Now for the code!
socket.php - creates multiclient socket server. Cron runs this script every minute using nohup php socket.php
client.php - Sends data from HTML form and returns data sent back from PROP
control_panel.html - Web interface. Yes, I used Tables :P Was in a hurry. Images were just clipart found on Google. Also well visible on a smart phone or tablet.
myproject.spin - The XBee part of my project. I am just going to attach this. You can strip out whatever you don't need
Thanks for the help / suggestions!
is it possible to have a example of a program in spin that just :
- configure the xbee S6B
- send a http get request to http://www.google.com
- print the output to the serial.
i would like to retrieve information from the weather channel but i don't know how to start
To parse weather data, you will have to find an XML feed which will be the easiest to work with to parse using the PHP socket server. Have the socket server wait for the command from the Xbee, when the command comes, pull the data from the website or XML file, parse the data, and send back the response to the XBee to use in your SPIN program. If you need help with the PHP socket server or the spin, let me know
Apologies for that. When my wife passed away I kind of lost motivation to fix the site issues caused by vBulletin. I have new software and eventually I will have the content back up. I appreciate your patience.
I'm sorry too for your wife. I hope you have taken confort with your familly or your friends.
[Edit] after 2 days hitting a wall, i found at last how to send a http get request from the xctu console.
i 'spend the whole day lurking every forum until i found that comment here
http://www.digi.com/support/forum/34364/xbee-is-not-receiving-tcp-data?show=34368#a34368
now i still have to code in spin to send that from propeller.