c++arduinosparkcore

How to access the Spark core via we browser?


I want the core to act like a web server to show text when access via webrowser with this command >>>(client.println("Sorry");) it should show "sorry" in browser right? but when I type the core's IP in browser. It said "This webpage is not available"

Thank you for reading.

TCPServer server = TCPServer(80);
    TCPClient client;

    void setup()
    {
      // start listening for clients
      server.begin();

      // Make sure your Serial Terminal app is closed before powering your Core
      Serial.begin(9600);
      // Now open your Serial Terminal, and hit any key to continue!
      while(!Serial.available()) SPARK_WLAN_Loop();

      Serial.println(WiFi.localIP());
      Serial.println(WiFi.subnetMask());
      Serial.println(WiFi.gatewayIP());
      Serial.println(WiFi.SSID());
    }

    void loop()
    {
      if (client.connected()) {
        // echo all available bytes back to the client
        while (client.available()) {
          server.write(client.read());
          server.write("Hello world!");
          client.println("Sorry"); //<<<<<<<<<<<<<<should show this
        }
      } else {
        // if no client is yet connected, check for a new connection
        client = server.available();
      }
    }

here is the code I use in arduino and ethernet sheild and it's work fine

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

//Enter a new MAC address from your computer MAC plus one.
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
EthernetServer server(80); // Create new server object at selected port

void setup() {

  Serial.begin(9600);
  Serial.println("Requesting ip address ...");
  Ethernet.begin(mac,ip,gateway,subnet);
  Serial.print("My IP address: ");
  Serial.print(Ethernet.localIP());
  server.begin(); // begin listening
}

void loop() {
  EthernetClient client = server.available(); // Get connection
  if(server.available()){ // check if there are connection
    Serial.println("new client connection");
    while (client.connected()) { // check if the client still alive
      if (client.available()) { // check if the connection are still alive
        char c = client.read(); // read data from Ethernet buffer
        if(c=='='){ p = client.read(); }
        Serial.write(c);
        if(c == '\n'){ // check if there are terminate character
          client.println("sorry");
          break;
        }
      }
    }
    client.stop(); // disconnect from client
    Serial.println("client disconnected");
  }
  delay(1000);
}///// credit P'O ITE

Solution

  • The browser is not going to be very happy with what you've done. Nothing you are returning to the browser obeys the HTTP protocol specs. You should use a telnet client to open a connection on port 80 to the IP Address of the Core. You will then see something closer to what you expected.

    Using the SparkCore and it's modest facilities to return a web page is possible but you must be tight on sizes. To return a web page, a browser will be happy with, you can ignore the request and always return this:

    HTTP/1.1 200 OK
    Date: Tue, 31 Mar 2015 22:38:34 GMT
    Server: SparkCore
    Content-Type: text/html; charset=UTF-8
    Content-Length: 138
    Accept-Ranges: bytes
    Connection: close
    
    <html>
    <head>
      <title>An Example Page</title>
    </head>
    <body>
      Hello World, this is a very simple HTML document.
    </body>
    </html>
    

    This is a minimal HTTP Response. I have not tested it but am pretty confident it will work. To learn more about HTTP and what you will need to do, check out wikipedia:

    http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol

    I also found a good thread on community.spark.io:

    http://community.spark.io/t/spark-core-web-server-solved/10110/4

    Good Luck!