XBee Wifi send and receive data from web

eagletalontimeagletalontim Posts: 1,384
edited 2015-02-09 - 17:34:16 in Accessories
As the title states, I am attempting to send data to my server and receive data as well. My first step I want to accomplish is receiving data from the web.

I have already set my router up and set my XBee in transparent mode. I am not really sure where to start :( On my remote server, I have written a PHP script that sends text to the specified IP address and port. I know this code works as it works on my spinneret. I have since changed the router to direct the same traffic to my XBee Module on a battery powered board.

The Xbee is connecting to the network as I can ping it. That is about as far as I have gotten. Any help is appreciated!

Processor I am using is the Prop.


  • $WMc%$WMc% Posts: 1,884
    edited 2015-01-24 - 21:41:11
    I take it your using the 802.11 XBee's. This is just a media converter with some I/O pins.
    What do you want to display the data on?
  • eagletalontimeagletalontim Posts: 1,384
    edited 2015-01-24 - 21:43:27
    I believe the 802.15? XBee S6B. That is what it is set to in the settings (Xbee 802.15.4 API Rx Indicator)

    EDIT *

    My plans are to be able to read the data that is calculated from my solar panel setup and also to move the panels / park them via my phone or standard web browser. All I need to do is send 1 letter or number from the browser to the module to activate each function (Move up, down, left, right, or Park). Data sent from the circuit is not necessary, but would be handy.
  • $WMc%$WMc% Posts: 1,884
    edited 2015-01-24 - 22:03:47
    I didn't think the 802.15 was compatible with a home WiFi 802.11 router, But I could be wrong. You stated that you could "ping" it, So I'm not sure.
    I Posted some stuff back in the day on WiFi in this forum. Mostly for the RN171-174 but some for the XBee 802.11. I'll do a search and see if I can find it.
  • $WMc%$WMc% Posts: 1,884
    edited 2015-01-24 - 22:29:38
    The stuff above is for the RN171/174 but should be of help with your project.
    Do you have X-CTU installed on you pc? This is the USB interface from the XBee.
  • eagletalontimeagletalontim Posts: 1,384
    edited 2015-01-24 - 22:35:47
    Yes, I have it installed and have been using it to try to change settings to see if I can get this to do anything.... Been online researching for hours with no luck :( Looks like most of the projects I am running across are 1 XBee to another across a network or the web. Not finding anything on talking with a remote server. Maybe I need to open a port (2616) on my server and see if that will work.

    I found this and maybe it will help? This is kind of similar to what I am trying to accomplish. : http://forum.arduino.cc/index.php?topic=252543.0

    It is late and I need sleep. Will attack this again when I wake up.
  • $WMc%$WMc% Posts: 1,884
    edited 2015-01-24 - 23:33:47
    When I started my first WiFi project, I was way over thinking it. Getting the rite IP addresses and Protocols are the tricky part. The factory manuals can be miss leading too.
  • eagletalontimeagletalontim Posts: 1,384
    edited 2015-01-25 - 08:38:36
    So... I opened port 2616 on my server and wrote a socket server listener PHP script. I have tried every setting i can think of in the XCTU program and still not able to get anything to work. Any ideas, tutorials, etc?
  • $WMc%$WMc% Posts: 1,884
    edited 2015-01-25 - 12:18:24
    Post your set-up. Don't forget to edit out your Passphrase and any other personal info!!!
    For those that aren't sure how to do this. Press ALT-Print screen "(Alt)-(PrtScr) on my keyboard". Next open up "Paint" and click edit, then paste. Edit the file, give it a name and save it as a jpeg.
  • eagletalontimeagletalontim Posts: 1,384
    edited 2015-01-25 - 12:51:08
    Ok, here we go...

    Using Parallax Xbee USB module to communicate / program XBee S6B wifi module.
    Newest version of XCTU V6.1.2
    Remote shared hosting Server port 2616 is open as TCP/UDP. Opened it for testing. Maybe I can just use port 80?
    Router set to forward requests of port 236 to port 2616 of IP address

    On my remote server, I have logged in through SSH and run "nc -l 2616" then run Telnet from my PC to communicate with that port. It worked perfectly.

    I am able to ping on my home network. This is about as far as I have gotten. I cannot seem to get the XBee module to communicate with my web server. Any output of the Xbee, I would think, would be sent to port 2616 of my server and it should have been displayed on the Putty screen when running the NC command.
    700 x 704 - 155K
    700 x 545 - 105K
  • $WMc%$WMc% Posts: 1,884
    edited 2015-01-25 - 13:22:54
    I see the passphrase is empty, Did you edit it out for the post or did you miss this setting?
    You can ping a device that does not have the passphrase.
  • eagletalontimeagletalontim Posts: 1,384
    edited 2015-01-25 - 13:29:44
    I have put it in and saved it then re read all settings. It never shows back up unless I manually put it back in.

    Edit * The xbee also shows up in the attached device list of the router. I would think this means it is connected.
  • $WMc%$WMc% Posts: 1,884
    edited 2015-01-25 - 13:48:55
    Ok, rule that out. If it shows up in the device list, I agree it's good to that point.
    I would stay clear of port 80. That was the hacker highway back in the day. Fire walls will start blocking any communications! This could be your internet provider looking out for you.
    Have you tried to talk to the XBee from your web sever?
  • eagletalontimeagletalontim Posts: 1,384
    edited 2015-01-25 - 13:52:46
    I have tried, but no success :(

    Maybe I am going about this the wrong way... I have been trying to either use telnet to connect directly to the Xbee Module or have tried sending data through the XCTU console and catch it through the server "NC" command. Telnet does not connect and I can't see the data going anywhere.
  • eagletalontimeagletalontim Posts: 1,384
    edited 2015-01-25 - 14:10:17
    Hmm, I just changed my PHP socket code and it appears it is finally connecting!

    So, now that I have it working on the USB board, I need to get it to work with the Prop. I found the Xbee_Object.spin on the forums and will see if I can get it to send data and receive data from the socket.
  • $WMc%$WMc% Posts: 1,884
    edited 2015-01-25 - 14:27:11
    With telnet you need to use the little "o" to open the device. " o 2616", just in case you forgot.
  • eagletalontimeagletalontim Posts: 1,384
    edited 2015-01-25 - 14:32:07
    I tried multiple times but was not successful for some reason until now. I guess it was something simple? Looks like I will be able to make some head way now. About to implement this into my existing code and see if it will work. I am using the Packet code by Chris which works great so far. On packet 36 right now.
  • $WMc%$WMc% Posts: 1,884
    edited 2015-01-25 - 14:50:49
    Post a little bit of your code and trouble shooting when you get it all working.
  • eagletalontimeagletalontim Posts: 1,384
    edited 2015-01-25 - 15:12:12
    Removed and started new thread for relevance.
  • $WMc%$WMc% Posts: 1,884
    edited 2015-01-25 - 18:05:54
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2015-01-26 - 09:44:16
    So... I opened port 2616 on my server and wrote a socket server listener PHP script. I have tried every setting i can think of in the XCTU program and still not able to get anything to work. Any ideas, tutorials, etc?

    That is port 2616 in HEX, or decimal 9750 in pretty much everything else. Have a look at the following links:

    http://www.savagecircuits.com/showthread.php?662-XBee-Wi-Fi-Setup-X-CTU (setting up the XBee Wi-Fi for an existing wireless network).

    http://www.savagecircuits.com/content.php?201-XBee-Wi-Fi-via-WAN-IP (Demonstration of sending packets over the internet from CA to Belgium)
  • eagletalontimeagletalontim Posts: 1,384
    edited 2015-01-30 - 19:43:30
    So after hours and hours of testing, I have finally gotten somewhere :) The socket server code was only accepting connections and not echoing it to the other clients. Got that fixed and now I am having a few strange issues....

    I have attached my project to show my code, but testing it would probably be pretty difficult without the same circuit setup I have.

    The problem I am having is the move_EW_flag and move_UD_flag not working correctly 100% of the time. I believe this problem lies somewhere in the controlMovement section or ProcessData section. What is happening is I press "RIGHT", "STOP", or "LEFT" on my HTML / PHP script and it sends text to the socket server which the XBee receives and sends it to the Prop for parsing. This is done in the ProcessData section. This appears to work correctly as the "last_data" variable is displayed in the PST which shows the data was received correctly even with it glitches. Since controlMovement is running in it's own COG, I set flags in the ProcessData section which should be picked up in the controlMovement section and processed accordingly. This works most of the time, but sometimes it does not....even when the "last_data" shows correct information and the returned string to the socket server is correct according to the command that should have been run.

    For example : Flow process =

    1: Click "RIGHT" on HTML form
    2: String "Received:COMMAND:2:END" gets sent to socket server
    3: Socket Server echos data to XBee
    4: XBee receives data and the PROP receives then processes it through the PUB ProcessData.
    5: If the string contains "COMMAND:", the next characters are stored as the "command" variable in the Prop.
    6: Since "RIGHT" is command 2, case #2 is selected and the flag "move_EW_flag" is set to 2.
    7: The PROP sends data to the XBee to send to the socket verifying data was received successfully. In this case "Moving Right.".
    8: The controlMovement COG should pick up the change to the flag "move_EW_flag" and process the change (Right here is where it fails 50% of the time)

    Hope this is not too confusing!
  • eagletalontimeagletalontim Posts: 1,384
    edited 2015-01-30 - 20:49:06
    I change the Receiving code on the PROP to look for character 35 and the ":END" to ":#" in the output from the PHP and everything seems to work :)
  • eagletalontimeagletalontim Posts: 1,384
    edited 2015-01-31 - 19:04:04
    So after several hours of testing, I have concluded that the XBee does not send and receive packets very well.... I am getting packet losses about 20% of the time and can't seem to figure out how to make this number much lower. The inbound from the socket is usually not the problem... It is mostly the outbound back to the socket.

    From what I have read in researching this packet loss issue is to send and receive something other than "strings" like I am now : "RX:CMD:3:#". So in researching more, I can use the PHP function "unpack" to convert each character to it's ascii code and send it to my socket which I am doing now :
    $text = $_REQUEST['text_message'];
    	$byte_array = unpack('C*', $text);
    	foreach($byte_array as $byte){
    		socket_write($socket, $byte, strlen($byte));
    	socket_write($socket, "\r", strlen("\r"));
    // $text = RX:CMD:5:#
    // OUTPUT : 82885867776858535835

    What I am trying to do now is convert this on the Prop back to a string.
    Here is my receiving code on the Prop as of right now :
      xb            : "FullDuplexSerial"
    PUB Handle_Wifi | char, i
      xb.Start(XB_OUT, XB_IN, %0000, 9_600)
      xb.str(string("Solar Connected!", 13))
      index := 0
      i := 0
        if i => 1000
          xb.str(string("CHECK", 13))
          i := 0
        char := xb.rxtime(20)
        if char > 31
          repeat until char == 13 or char == -1
            buffer[index++] := char
            char := xb.rxtime(100)
          i := 0
          index := 0
          bytemove(@last_data, @buffer, 255)
          if(char == 35)
            bytefill(@buffer, 0, 256)
        char := -1

    This code properly receives data from the socket and I have not found any missing packets yet.

    Now for the sending to the server is where most of the glitches happen. I use the xb.str(string("Something", 13)) method but it seems to be kind of glitchy. If I put that line of code in a repeat loop and a 1 second pause between each send, the data received on the socket will vary (Viewing with Telnet). It will either be missing a letter, or only 1/2 the string will be send / received. Any ideas?
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2015-02-02 - 11:09:47

    I have not experienced any packet loss, even when using UDP which does not provide any acknowledgement that the packet made it to the destination. In the demo below you can see how many packets I sent from California to Belgium with zero packet loss.


    There has to be something else going on or you have a very noisy environment/connection.
  • eagletalontimeagletalontim Posts: 1,384
    edited 2015-02-02 - 17:00:57
    Did you have anything connected to the RX and TX of the Xbee besides the prop? Pull up / down resistors? I also noticed that you were doing a "direct" connection between 2 XBee's instead of having a socket server between them. I have tried to get PHP to directly communicate with my PHP form, but I just can't seem to figure it out. I though you could connect to the XBee via connecting directly to the Source Port via TCP / UDP connection. Maybe I am doing something wrong?
  • TumblerTumbler Posts: 323
    edited 2015-02-02 - 21:33:01

    Can i take a look at the PHP code ur using?
  • eagletalontimeagletalontim Posts: 1,384
    edited 2015-02-02 - 22:01:07
    Here is all the PHP code :

    server.php : run as a cron. Server runs this script every minute. If socket is still alive, the second request to run socket server fails and first continues to run.
    	$address = 'MY_SERVER_IP';
        $port = 2616;
    	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'=>1,'usec'=>500000);
    	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);
        while (true) {
            $read = $clients;
            if (@socket_select($read, $write = NULL, $except = NULL, 0) < 1)
            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);
            foreach ($read as $read_sock) {
                $data = @socket_read($read_sock, 1024, PHP_BINARY_READ);
                // check if the client is disconnected
                if ($data === false) {
                    $key = array_search($read_sock, $clients);
                    echo "client disconnected.\n";
                $data = trim($data);
                if (!empty($data)) {
                    foreach ($clients as $send_sock) {
                        if ($send_sock == $sock || $send_sock == $read_sock)
                        @socket_write($send_sock, $data."\n");

    index.html : Front user interface. Could have used better coding, but I just threw this together.
    <!DOCTYPE html>
    	<title>Solar Panel Control</title>
    	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    	<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.js"></script>;
    	<script language="JavaScript">
    	function submit_text(what){
    		formid = document.getElementById("form_1");
    			do: "sendmessage",
    			text_message: what
    			function(data) {
    				data = '';
    		table td{
    	<div class="form_body" id="form_body">
    		<div class="post_message">
    			<table width="100%" cellpadding="0px" cellspacing="0px" style="border:1px solid;">
    					<td style="text-align:left;">
    						<a href="javascript:void(0);" onClick="submit_text('R:C:9:#');"><img src="images/park.jpg" height="100px" border="0"></a>
    					<td style="text-align:center;">
    						<a href="javascript:void(0);" onClick="submit_text('R:C:3:#');"><img src="images/arrow-up.jpg" height="100px" border="0"></a>
    					<td style="text-align:center;">
    						<a href="javascript:void(0);" onClick="submit_text('R:C:1:#');"><img src="images/arrow-left.jpg" height="100px" border="0"></a>
    					<td style="text-align:center;">
    						<a href="javascript:void(0);" onClick="submit_text('R:C:5:#');"><img src="images/stop-sign.jpg" height="100px" border="0"></a>
    					<td style="text-align:center;">
    						<a href="javascript:void(0);" onClick="submit_text('R:C:2:#');"><img src="images/arrow-right.jpg" height="100px" border="0"></a>
    					<td style="text-align:center;">
    						<a href="javascript:void(0);" onClick="submit_text('R:C:4:#');"><img src="images/arrow-down.jpg" height="100px" border="0"></a>
    					<td style="text-align:center;">
    						<a href="javascript:void(0);" onClick="submit_text('R:C:7:#');"><img src="images/lock.jpg" height="100px" border="0"></a>
    					<td style="text-align:center;">
    						<a href="javascript:void(0);" onClick="submit_text('R:C:8:#');"><img src="images/unlock.jpg" height="100px" border="0"></a>
    					<td style="text-align:center;">
    						<a href="javascript:void(0);" onClick="submit_text('R:C:6:#');"><img src="images/info.jpg" height="100px" border="0"></a>
    		<div style="clear:both;"></div>
    	<div class="text_body" id="text_body" style="width:100%">

    actions.php : Generates the connection to the socket and puts requested text to the socket server to be echoed to all clients. Called by Ajax / jQuery.
    function after($this, $inthat){
    	if (!is_bool(strpos($inthat, $this)))
    	return substr($inthat, strpos($inthat,$this)+strlen($this));
    $do = $_REQUEST['do'];
    $text = $_REQUEST['text_message']."\r";
    if($do == "sendmessage"){
    	$service_port = 2616;
    	$address = 'MY_SERVER_IP';
    	/* Create a TCP/IP socket. */
    	$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    	if ($socket === false) {
    		echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
    	$timeout = array('sec'=>1,'usec'=>500000);
    	$result = @socket_connect($socket, $address, $service_port);
    	@socket_write($socket, $text, strlen($text));
    	//socket_write($socket, "\r", strlen("\r"));
    	$str = '';
    	while ($out = @socket_read($socket, 1024)) {
    		$str .= $out;
    		if (strpos($str, "\n") !== false) break;
    	$str = after("Connected:", $str);
    	$data_array = explode("#", $str);
    	$string = '';
    	if(is_array($data_array) && isset($data_array[1])){                // CHECK IF MESSAGE IS ARRAY BEFORE OUTPUTTING TO BROWSER
    		foreach($data_array as $data){
    			$value = explode(":", $data);
    			$string .= $value[0]." = ".$value[1]."</br>\n";
    		echo $string;
    	} else {
    		echo $str;
    } else {
    	echo "empty";

    Attached is my SPIN
  • TumblerTumbler Posts: 323
    edited 2015-02-02 - 22:23:59
    Thx m8, will take a look at it later when i'm home
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2015-02-03 - 07:47:37
    Did you have anything connected to the RX and TX of the Xbee besides the prop? Pull up / down resistors? I also noticed that you were doing a "direct" connection between 2 XBee's instead of having a socket server between them. I have tried to get PHP to directly communicate with my PHP form, but I just can't seem to figure it out. I though you could connect to the XBee via connecting directly to the Source Port via TCP / UDP connection. Maybe I am doing something wrong?

    Nothing else connected to those pins. Resistors would not typically be used on the serial I/O unless you are using an open baud mode (which I am not). And while it is a direct connection, you can be sure those packets are making many hops between here and Belgium. I hadn't realized you were going through a server.

    You can connect directly on the specified port. But over the internet your target IP becomes the WAN address of the remote and the router on that end is responsible for packet forwarding to the device inside. When the remote XBee replies the packets are returned to the origin without having to be port forwarded.

    On a similar note I am working on a remote Telepresence Robot using an RB5X shell with a Propeller inside. It will be able to be controlled over the internet via an Android app and display video on a web page interface to the operator. This will be using the XBee Wi-Fi module.
  • eagletalontimeagletalontim Posts: 1,384
    edited 2015-02-03 - 14:09:51
    I have tried connecting directly to the XB using my home IP address from whatismyip.com and the port number I have chosen to forward requests to the xbee. TCP was unsuccessful and so was UDP. Every bit of code I can find online connecting to a socket has been tried and non have worked to connect to the Xbee. I can however use telnet to connect via my home IP address and the same port. Maybe I am using the wrong code / connection type?
Sign In or Register to comment.