Hello Everyone,
I am trying to get an old Arduino project working on the Photon. The old project broadcast a HTML document from a SD card on the local network under the Arduino’s IP address. All what this did was bring up a simple page on the web browser where you can click onto a link on the site and the Arduino would react to the input. For the example below I have simplified the Arduino code so it will just turn pin 13 on and off. I am trying to achieve the same on a Photon but I am a little stuck where to start as the Photon is already internet enabled and does not require an Ethernet shield to function.
If someone could assist me to get the following code working on the Photon wirelessly I would greatly appreciate it. Thank-you in advanced!
The old Arduino code: (This code works fine on the Arduino Mega with a Ethernet Shield)
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 0, 100 }; // ip in lan
byte gateway[] = { 192, 168, 0, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port
File webFile;
String HTTP_req = "";
int LED = 13;
void setup () {
Serial.begin(9600);
pinMode(LED, OUTPUT);
// initialize SD card
Serial.println("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("ERROR - SD card initialization failed!");
return; // init failed
}
Serial.println("SUCCESS - SD card initialized.");
// check for index.htm file
if (!SD.exists("index.htm")) {
Serial.println("ERROR - Can't find index.htm file!");
return; // can't find index file
}
Serial.println("SUCCESS - Found index.htm file.");
// initialize SD card
Ethernet.begin(mac, ip);
server.begin();
}
void loop () {
EthernetClient client = server.available();
if (client) {
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
HTTP_req += c;
Serial.print(c);
if (c == '\n' && currentLineIsBlank) {
if ((HTTP_req.indexOf("GET / ") > -1) ||
(HTTP_req.indexOf("GET /index.htm") > -1)) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
webFile = SD.open("index.htm");
}
//HTML Request Settings
else if (HTTP_req.indexOf("GET /?off") > -1) {
Serial.println("Ethernet: LED Off");
digitalWrite(LED, LOW);
}
else if (HTTP_req.indexOf("GET /?on") > -1) {
Serial.println("Ethernet: LED On");
digitalWrite(LED, HIGH);
}
else if (HTTP_req.indexOf("GET /bg.jpg") > -1) {
webFile = SD.open("bg.jpg");
if (webFile) {
client.println("HTTP/1.1 200 OK");
client.println();
}
}
//HTML Request Settings
if (webFile) {
while(webFile.available()) {
client.write(webFile.read());
}
webFile.close();
}
HTTP_req = "";
break;
}
if (c == '\n') {
currentLineIsBlank = true;
}
else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
delay(1);
client.stop();
}
}
HTML (index.htm file) Code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>LED Control</title>
<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="format-detection" content="telephone=no">
<style type="text/css">
body {
font: 100%/1.4 Verdana, Arial, Helvetica, sans-serif;
color: black;
}
h1 {
font-size: 150%;
text-align: center;
}
FORM {
text-align: center;
}
</style>
</head>
<body>
<h1>LED Control</h1>
<br>
<FORM>
<a href="/?off" target="inlineframe"><INPUT TYPE="button" style="font-size:12px;color:red;height:40px;width:40%" value="LED: OFF"></a>
<a href="/?on" target="inlineframe"><INPUT TYPE="button" style="font-size:12px;color:green;height:40px;width:40%" value="LED: ON"></a>
</FORM>
<IFRAME name=inlineframe style="display:none" >
</IFRAME>
</body>
</html>