using "http:// " type prompt in an Arduino program?

I am making a project with an Arduino and an Arduino-based Smart Citizen Kit (sck). The sck measures environment data such as temp, humidity, light level, and even pollution and uploads it to the internet for monitoring by its own web-based and android apps. The sck can also has an API which will reply to a request with temperature and other requested data. A request looks something like this:

http://api.smartcitizen.me/v0.0.1/6e0428e19cf2bff1a9c05d14d0400bf4/me.json

You can try entering that right into your address bar and get a response (that is not my sck, that seems to belong to the developer and is the example on the sck website.)

So, my question is, how to enter a command such as that above prompt (http://api.smart.......) into the Arduino program? And how to get the reply back? I think I can sift out my required information if I knew how to enter that prompts and accept the reply. I will have an Arduino wifi shield (though I just ordered it and waiting for delivery).

If my question is too broad, please just point me towards the right terms I need to search for.

Thanks,
Drew

You need to use the Arduino's WiFi Client class.

The reference pages are here. In particular, you should have a look at this example sketch. You can adapt that sketch to access any URL you like by modifying following lines to your needs.

char server[] = "www.google.com";    // name address for Google (using DNS)

and

client.println("GET /search?q=arduino HTTP/1.1");
client.println("Host: www.google.com");

For your example URL above, you would need to replace "www.google.com" with "api.smartcitizen.me", and "/search?q=arduino" with "/v0.0.1/6e0428e19cf2bff1a9c05d14d0400bf4/me.json".

It would probably also be a good idea to do some research into the HTTP protocol itself, since you need to work at that level (e.g. understanding what the "GET" part means, and what other options there are). The Wikipedia article gives a good overview, and has links to the official HTTP standards.


Hope that helps, and best of luck with your project. :slight_smile:

Thank you so much! Your status says "newbie" but I think you are a pro!. I will look over that material in detail (an still actually awaiting the arrival of my wifi shield...). But a couple of quick follow-up questions:

In you post you say "you should have a look at this example sketch.", but the link under "this example" seems to have gotten corrupted. If you could send me that link again, I would so much appreciate it.

I feel confident I can make the "get" request now. But not sure what to do with the text I get back (maybe I should see the whole example mentioned above. I think what I want is to have the response to my "get" stored in one long character string that I can then sort through to get what I need. Is there a way to put the response into a character string?

Thank you again so much!
drew

There are some other ways to do that. If you are monitoring wheather for pollution, maybe you are using MQ sensors and you need to place your arduino project to outside(open-air). So, it may not be available to connect your project to a internet cable or use wifi to connect a modem. You can use GPRS shield called SIM900, dont buy sim900A, it is available for Assian communication(datasheet says). Than you can use ATcommands function to send your parameters, than you can take the parameters from your website as GET() command.

Look here, there is example for php website getting parameters.
http://forum.arduino.cc/index.php?topic=155218.0

drew345:
Thank you so much! Your status says "newbie" but I think you are a pro!.

You're very welcome, I am pleased to be of assistance. As you said, I am indeed a professional website, networking and embedded systems engineer, so I have a fair bit of experience with these sorts of things - A talent most people in my life do not appreciate as I bore them with the complexities of this here interweb!

drew345:
In you post you say "you should have a look at this example sketch.", but the link under "this example" seems to have gotten corrupted. If you could send me that link again, I would so much appreciate it.

My apologies! I have now fixed the link, so it now correctly points to here.

drew345:
I feel confident I can make the "get" request now. But not sure what to do with the text I get back (maybe I should see the whole example mentioned above. I think what I want is to have the response to my "get" stored in one long character string that I can then sort through to get what I need. Is there a way to put the response into a character string?

Indeed, the example sketch gives you an idea what you need to do. The relevant code is

  while (client.available()) {
    char c = client.read();
    Serial.write(c);
  }

As you can see, currently it is reading the characters from the webserver's response in one by one then immediately spitting them back out on Serial, but you could just as easily append them into a String object and then do some later processing on them, as per my modified code below.

  String response = "";
  while (client.available()) {
    response += client.read();
  }
  Serial.write(response);  //Or whatever processing you need to do to your response string

Note however that using the String object can be hazardous as it can quickly (if you're not careful) suck up all your Arduino's memory - that example link you gave in your first post returns 1,756 bytes of text, an an Arduino Uno only has 2048 bytes in total. So as I said, beware.

Thank you again!

Wow, great word of warning about the size of the received character string. I'll be careful about that, probably just dump bits as they come in until I get to the part I am interested in.

I'm sure I will have more questions later. I am thinking about another function where it seems the arduino would have to be a "server", so not sure if it can be both server and client in the same program. But I will have to formulate that question more exactly later.

Thanks, Drew!