Skip to content

Instantly share code, notes, and snippets.

@harrisonhjones
Last active October 9, 2015 07:22
Show Gist options
  • Save harrisonhjones/354908d4aa7479b59e6d to your computer and use it in GitHub Desktop.
Save harrisonhjones/354908d4aa7479b59e6d to your computer and use it in GitHub Desktop.
Spark Community - kokoro - Web Browser Issue
// Original Spark Community Thread: http://community.spark.io/t/how-to-access-the-core-via-we-browser/9711
// Code adapted from: http://arduino.cc/en/Tutorial/WebServer
/* Includes ------------------------------------------------------------------*/
#include "application.h"
SYSTEM_MODE(AUTOMATIC);
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() {
// listen for incoming clients
client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
//client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<H1>Hello World.</h1>");
client.print("The current time is: <B>");
client.print(Time.timeStr());
client.println("</B>");
// output the value of each analog input pin
/*for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
int sensorReading = analogRead(analogChannel);
client.print("analog input ");
client.print(analogChannel);
client.print(" is ");
client.print(sensorReading);
client.println("<br />");
}*/
client.println("</html>");
client.println();
delay(5);
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment