View unanswered posts | View active topics It is currently Fri Dec 19, 2014 7:30 pm






Reply to topic  [ 14 posts ] 
DiWiFi get command 
Author Message
Novice

Joined: Sun Jun 13, 2010 9:26 am
Posts: 58
Post DiWiFi get command
Hello:

After fiddling with my network setup, see this posting -- http://www.robotc.net/forums/viewtopic.php?f=1&t=4393&p=19388#p19388 I can now connect to the Internet using the Dexter Industries WiFi sensor. How I need some more help.... I am trying to figure out how to form a get request. I have an IP address to a website, and the data.txt file I want to read. How do I send the get request, and store the information, a single numerical character, so that I can use it to control the robot? I have looked at Xander's driver suite but I am not sure what I am looking for or what changes I need to make for my situation. Any help would be greatly appreciated.

Mike,


Mon Jun 11, 2012 11:46 am
Profile
Novice

Joined: Sun Jun 13, 2010 9:26 am
Posts: 58
Post Re: DiWiFi get command
I have been looking at the weather sample code from Dexter Industries and started altering some of the code. I changed the first line in the DIWIFI-Pachube.h code to reflect my IP address. Do I need to change the next couple of lines from:
ubyte BytesRead[8];
char weather_data[2000];
to
ubyte BytesRead[1]; // the text file will only have a single character in it
char weather_data[1];

Next I would need to alter this piece of code:
Code:
////////////////////////////////////////////////////////////////////////////////////////////////////////
// Send information to Google to get weather.
////////////////////////////////////////////////////////////////////////////////////////////////////////
void GetWeather(int CID, string location) {
  wait1Msec(1000);
 
  nxtWriteRawHS(byteStart, 3);
  writeDebugStream("%c", byteStart, 2);
  writeDebugStreamLine("%c", (CID+48));
 
  wait1Msec(200);
 
  writeStr("GET /ig/api?");
  //writeStr("weather=");     // do I need this?
  //writeStr(location);       // do I need this?
  writeStr(" HTTP/1.1");
  crln();
 
  //writeStr("User-Agent: "); // do I need this?
  //writeStr("DIWifi");       // do I need this?
  crln();
 
  writeStr("Host: ");
  writeStr("www.google.com"); //Do I change this to my website address?????
  crln();
  crln();
 
  wait1Msec(10);
  nxtWriteRawHS(byteEnd, 2);
}


I remarked out a few lines but I am not sure if this is necessary or good to do? Also, how do I tell the program that it needs to open this text file -- data.txt?

Once that is set how do I get and display the single character from the text file?

Mike


Thu Jun 14, 2012 11:29 am
Profile
Expert

Joined: Thu Sep 29, 2011 11:09 pm
Posts: 184
Location: Michigan USA
Post Re: DiWiFi get command
The weather_data array needs to be close to 2000 elements long (I think the data is usually between 1600 and 1800 characters long, and that array holds all of it).

I would leave BytesRead alone. It's only a matter of 7 bytes of ram.

I would also uncomment out at least the first 2 lines you commented out (after-all, it seems pretty important that Google know where you want the weather info from).

I'm not sure if it's necessary to tell it the User-Agent, but why not?

The function GetWeather sends a request to Google, asking the Google servers to send back the weather information for the location you specified. GetWeather does not take care of receiving the weather information when the servers return it (in my modified program, this is done with the read_weather_data function).

Edit: in my program, the read_weather_data function is what receives the info from Google, and loads it into the weather_data array.

_________________
Matt


Thu Jun 14, 2012 5:30 pm
Profile WWW
Novice

Joined: Sun Jun 13, 2010 9:26 am
Posts: 58
Post Re: DiWiFi get command
Hello Matt:

I realize that commenting out the line about location would be important for Google Weather to retrieve the proper weather information. What I want to do is read a text file (data.txt) on a remote web site that contains a single character (1, 2, 3, 4, or 5) depending on what button is pressed. I then need to put that character into a variable that would be used to control a robot. 1 -- drive forward, 2 -- drive backward, 3 -- turn left, 4 -- turn right, and 5 -- stop. So how do I alter the above code to open and read that text file, and what code changes need to be made to store that value for use else where in the code?

Mike,


Thu Jun 14, 2012 9:00 pm
Profile
Expert

Joined: Thu Sep 29, 2011 11:09 pm
Posts: 184
Location: Michigan USA
Post Re: DiWiFi get command
So you want to serve a .txt file for the NXT to be able to read? Awesome idea! but unfortunately I don't know how to do it.

_________________
Matt


Thu Jun 14, 2012 9:03 pm
Profile WWW
Novice

Joined: Sun Jun 13, 2010 9:26 am
Posts: 58
Post Re: DiWiFi get command
Hello Matt:

Maybe you can help me in another way. I changed the line:
writeStr("ww.google.com");
to
writeStr("mikesrobots.webege.com");
But I get a warning message that the line is longer than 19 characters. Other than getting another website and creating a shorter username is there a way around this problem?

If you go to this web address:
http://mikesrobots.webege.com/remote.php
you should see the 5 buttons set to control the robot. This file then sends data to another file that creates the data.txt file. This is all stuff shown at the Humabotoics web site.

Mike


Thu Jun 14, 2012 10:32 pm
Profile
Expert

Joined: Thu Sep 29, 2011 11:09 pm
Posts: 184
Location: Michigan USA
Post Re: DiWiFi get command
I think I heard that ROBOTC now supports constant strings of more than 19 chars. If not, then you could try separating it to something like this:
Code:
writeStr("mikesrobots");
writeStr(".webege.com");


I think that would be functionally the same.

BTW, you will need to receive more than just the 1 byte of the text file. There is a TCP header and trailer IIRC, and you will probably get some other server info. You will need to parse the incoming data for the info you want.

_________________
Matt


Thu Jun 14, 2012 10:54 pm
Profile WWW
Novice

Joined: Sun Jun 13, 2010 9:26 am
Posts: 58
Post Re: DiWiFi get command
Splitting up the URL as suggested worked. As for the rest of what you write about I do not know how to deal with that. I will look further though the code. I feel like I am slowly getting closer to the goal.

Thank you for all your help.
Mike


Thu Jun 14, 2012 11:46 pm
Profile
Novice

Joined: Sun Jun 13, 2010 9:26 am
Posts: 58
Post Re: DiWiFi get command
I was wondering if the HumaRobotics demo sample is set up incorrectly??? If you go to my web address shown above there are 4 files listed (I am still working on what the web site will finally look like. For now I just want to get remote control working.) The default.php file is the web page shown when accessing the web site without a specific file in mind. If you double click on the remote.php file it will display the 5 robot movement control buttons. This file takes the button press, asigns a value from 1 to 5 and passes it onto the m.php file. The m.php file than takes the value from the remote.php file and writes it into the data.txt file. In the HumaRobotics sample codes/tutorial you need to access the data.txt file. What if the file the robot should call is the m.php file? There is a section in the m.php file for reading the data.txt file. I am off to work but will try this a try tonight first with the NXT-G sample from HumaRobotics, and then in the Robotc code. I still need help on how to extract the information once I get it from the website.

Here is my altered Robotc code for trying to access the button presses from the web site.

Code:
////////////////////////////////////////////////////////////////////////////////////////////////////////
// Send information to Google to get weather.
////////////////////////////////////////////////////////////////////////////////////////////////////////
void GetWeather(int CID, string location) {
  wait1Msec(1000);

  nxtWriteRawHS(byteStart, 3);
  writeDebugStream("%c", byteStart, 2);
  writeDebugStreamLine("%c", (CID+48));

  wait1Msec(200);

  writeStr("GET /ig/api?");
  //writeStr("weather=");     // do I need this?
  //writeStr(location);       // do I need this?
  writeStr(" HTTP/1.1");
  crln();

  writeStr("User-Agent: "); // do I need this?
  writeStr("DIWifi");       // do I need this?
  crln();

  writeStr("Host: ");
  writeStr("mikesrobots"); //my website address?
  writeStr(".webege.com");
  writeStr(/m.php"); // changed this line from -- writeStr("/data.txt");
  writeStr("?role=robot");
  crln();
  crln();

  wait1Msec(10);
  nxtWriteRawHS(byteEnd, 2);
}


Mike,


Fri Jun 15, 2012 9:49 am
Profile
Novice

Joined: Sun Jun 13, 2010 9:26 am
Posts: 58
Post Re: DiWiFi get command
I think I am getting closer but still no cigar!

Based on the above code sample from the Dexter Industries Weather sample I changed this line:
writeStr("GET /ig/api?");
to
writeStr("GET");

Now I get the following debug output. The key being I am getting a Bad Request error. DO I need to send some code along with the get command? Or is the error related more that this sample code is writen for dealing with Google Weather? If so, what parts do I need to alter? Does someone have some sample code for reading a text file from a web page?

Mike,

closeAllCons
at+ncloseall
ver could not understand.<br />
</p>
<hr>
<address>Apache Server at * Port 80</address>
</body></html>
E
DISCONNECT 0

OK
AT+NCTCP=50.116.98.58,80

CONNECT 0

OK
0
GET HTTP/1.1
User-Agent: DIWifi
Host: laurensvalk.com/w/d.txt

OOS0<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<hr>
<address>Apache Server at * Port 80</address>
</body></html>
E
DISCONNECT 0


Sat Jun 16, 2012 10:55 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3297
Location: Rotterdam, The Netherlands
Post Re: DiWiFi get command
That doesn't look like a good HTTP request to me. A few points:
You don't need the agent line, it is used for logging purposes and also to perhaps show a different page, to say, a mobile device or something.
You can practice a HTTP connection by simply using telnet to port 80 of the server you're trying to connect with. This is what I usually do :) Simply open telnet (I tend to use PuTTY), connect to your host on port 80 and type the following:
(I did this on a Linux VM, but you get the idea) The green text is what I sent to the server. The red text is what it sent back.
xander@ubuntu:~$ telnet http://www.laurensvalk.com 80
Trying 50.116.98.58...
Connected to laurensvalk.com.
Escape character is '^]'.

GET /w/d.txt HTTP/1.1
Host: http://www.laurensvalk.com <--- followed by two newlines!


HTTP/1.1 200 OK
Date: Sat, 16 Jun 2012 19:45:24 GMT
Server: Apache
Last-Modified: Sat, 16 Jun 2012 16:20:10 GMT
Accept-Ranges: bytes
Content-Length: 10
Content-Type: text/plain

1222Hello!


So why do you need to send the "Host" field? That has to do with the fact there is more than one website that is served by the Apache server listening on IP address 50.116.98.58. So when you connect to that server on port 80, it doesn't know what website you really want until you pass it the "Host" field. Then it goes "ah, so it's for Laurens' site, let me grab that for you real quick, one moment." However, if a webserver only hosts ONE site, then this is optional. So if you want to make sure you'll always get the site you want, I would fill it in. If you want to learn more about that, read up about Virtual Hosts.

If you have more questions about HTTP, don't hesitate to ask!

- Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sat Jun 16, 2012 3:50 pm
Profile WWW
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3297
Location: Rotterdam, The Netherlands
Post Re: DiWiFi get command
As a side note, the WiFiBlock does not currently support the Host: field, that means you can only connect to servers that don't use Virtual Hosts -or- unless you know the path on the webserver where the website is hosted. Sometimes that's a path like:
19.182.182.1/websites/laurensvalkcom/w/d.txt (no, this is not the path, I made it up as an example), it's not always easy to track down, though and requires fairly intimate knowledge of the layout of the server itself. That said, on the WiFiBlock, you don't need to fabricate your own HTTP requests :)

- Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sat Jun 16, 2012 4:07 pm
Profile WWW
Novice

Joined: Sun Jun 13, 2010 9:26 am
Posts: 58
Post Re: DiWiFi get command
Hello Xander:

Thank you for your replies. I did not realize that the situation was a bit more complex than what appears on the surface. I did try the TCP and webserver examples and was successful in getting them to work. One thing I did experience is that I was usually only able to communicate with the NXT 1 or 2 times before I had to restart the TCP or webserver programs. One time I had to start over and get a new the connection and get a new IP address before I could communicate again with the NXT. If you have any ideas why the system is hanging????, and what I can do to prevent it, please let me know.

There is a lot to learn and understand on how to program and parse the response the WiFi sensor creates. Each time I play I learn a little bit more.

Mike,


Mon Jun 18, 2012 3:15 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3297
Location: Rotterdam, The Netherlands
Post Re: DiWiFi get command
Lego Geek wrote:
One thing I did experience is that I was usually only able to communicate with the NXT 1 or 2 times before I had to restart the TCP or webserver programs.

You need to put timeouts on all your send and receive functions. 100ms is a pretty good guideline. If you haven't got a response in that time, you should consider it lost.

Quote:
One time I had to start over and get a new the connection and get a new IP address before I could communicate again with the NXT. If you have any ideas why the system is hanging????, and what I can do to prevent it, please let me know.

That sounds a little extreme. Try modifying the code like a described above.

Quote:
There is a lot to learn and understand on how to program and parse the response the WiFi sensor creates. Each time I play I learn a little bit more.

There is sooooo much to learn! It's what keeps this hobby interesting :)

- Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Tue Jun 19, 2012 5:30 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 14 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.