1

I'm quite new to Arduino and have looked at many tutorials featuring an Arduino requesting data from a web server through a Wifi Shield, but haven't seen one featuring the sending of data from a website/server to an Arduino. I'm attempting to use a JavaScript then PHP script to send a string variable to the Arduino, which would then of course run its code at this point. Is this possible if I have a Wifi Shield and the Arduino's IP address?

  • Can you clarify if you want the Arduino to be a server or client? – Nick Gammon Aug 11 '15 at 0:45
  • send a string variable to the Arduino, which would then of course run its code - server – Matt Clark Aug 11 '15 at 1:46
  • As @MattClark said, Arduino would be the server – cholden Aug 11 '15 at 16:52
  • However the question read: looked at many tutorials featuring an Arduino requesting data from a web server through a Wifi Shield - the Arduino requesting data from a web server makes it sound like a client to me. – Nick Gammon Aug 12 '15 at 2:24
1

On the yun, this functionality is built-in via the bridge. Here's an example.

Via the shield, you now end up writing your own webserver and responding appropriately.

I'm guessing it makes more sense to use the client (the web browser) to make the calls than the backend php, but in either case, once the webserver is up and going on the arduino, you're just using it like any other server.

1

If you really want the Arduino to be a web server (that is, you connect to it with your web browser) then I made a library a little while back to simplify processing the get/post/pathname/cookie arguments.

See Tiny web server for Arduino or similar.

Example code (that comes with the library) turns on various LEDs (presumably connected to pins on the Arduino) upon getting a "get" request like this:

http://10.0.0.241/?on=4&off=5

That would turn on pin 4 and turn off pin 5. Obviously you change the IP address to suit your network, and use a suitable MAC address.

// Tiny web server demo
// Author: Nick Gammon
// Date:  20 July 2015

#include <SPI.h>
#include <Ethernet.h>
#include <HTTPserver.h>

const int LOW_PIN = 3;
const int HIGH_PIN = 5;

// Enter a MAC address and IP address for your controller below.
byte mac[] = {  0x90, 0xA2, 0xDA, 0x00, 0x2D, 0xA1 };

// The IP address will be dependent on your local network:
byte ip[] = { 10, 0, 0, 241 };

// the router's gateway address:
byte gateway[] = { 10, 0, 0, 1 };

// the subnet mask
byte subnet[] = { 255, 255, 255, 0 };

// Initialize the Ethernet server library
EthernetServer server(80);

// derive an instance of the HTTPserver class with custom handlers
class myServerClass : public HTTPserver
  {
  virtual void processPostType        (const char * key, const byte flags);
  virtual void processGetArgument     (const char * key, const char * value, const byte flags);
  };  // end of myServerClass

myServerClass myServer;

// -----------------------------------------------
//  User handlers
// -----------------------------------------------

void myServerClass::processPostType (const char * key, const byte flags)
  {
  println(F("HTTP/1.1 200 OK"));
  println(F("Content-Type: text/html\n"
            "Connection: close\n"
            "Server: HTTPserver/1.0.0 (Arduino)"));
  println();   // end of headers
  println (F("<!DOCTYPE html>\n"
             "<html>\n"
             "<head>\n"
             "<title>Arduino test</title>\n"
             "</head>\n"
             "<body>\n"));

  } // end of processPostType

void myServerClass::processGetArgument (const char * key, const char * value, const byte flags)
  {

  int which = atoi (value);
  if (which >= LOW_PIN && which <= HIGH_PIN)
    {
    if (strcmp (key, "on") == 0)
      {
      digitalWrite (which, HIGH);
      print (F("<p>Turning on pin "));
      println (which);
      }
    else  if (strcmp (key, "off") == 0)
      {
      digitalWrite (which, LOW);
      print (F("<p>Turning OFF pin "));
      println (which);
      }
    else
      {
      print (F("<p><b>Bad action: "));
      fixHTML (value);
      println (F("</b>"));
      }
    }
  else
    {
    print (F("<p><b>Bad pin number: "));
    fixHTML (value);
    println (F("</b>"));
    }

  }  // end of processGetArgument

// -----------------------------------------------
//  End of user handlers
// -----------------------------------------------

void setup ()
  {
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip, gateway, subnet);

  for (int i = LOW_PIN; i <= HIGH_PIN; i++)
    pinMode (i, OUTPUT);
  }  // end of setup

void loop ()
  {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (!client)
    {
    // do other processing here
    return;
    }

  myServer.begin (&client);
  while (client.connected() && !myServer.done)
    {
    while (client.available () > 0 && !myServer.done)
      myServer.processIncomingByte (client.read ());

    // do other stuff here

    }  // end of while client connected

  client.println(F("<hr>OK, done."));

  client.println (F("</body>\n"
                    "</html>"));

  // give the web browser time to receive the data
  delay(1);
  // close the connection:
  client.stop();

  }  // end of loop

Library source on GitHub at nickgammon/HTTPserver.

The library is designed to allow you to receive cookies, POST data, pathnames (on the URL) and GET parameters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.