Photon WIFI Client

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>

You could have a look at the contributed WebServer library, if this suites you.

+1 to the WebServer library that @ScruffR mentioned. I’ve been using it to display and manage settings for word clocks on a local network. It’s very stable and pretty quick too. I think the Web_Buzzer.ino example is pretty close to what you need for your project. Play around with it and let us know if you have any questions. I’ve only been using it personally for about 2 weeks now, but it’s performed very well for me!

1 Like

Thank-you for the reply. This library does seem to work but the code for the website is hard coded into the Particle it’s self. Does anyone know how to adapt the code so it can read a HTML document from a SD card to then broadcast that?

I am new myself to all this but as i understand the web library is a port from arduino that do exactly what you are asking for (Webduino). So im pretty sure the library can do what you ask for but someone else have to tell how :slight_smile:

the web library as it is now use the 1000k byte (i think this was the size) free internal space on the photon so i guess you just have to tell that it should store/look at a sd card instead. I guess you could use the SD card library for that.

I hope this help you get going untill some more experienced user can tell you more.

BR
Dimi

thank-you for your reply @emilkl - I will just have to have a play around with it and see what I can achieve until someone knows or has done it themself comes forward :smile:

SDFat is the library you want to look at.
Bringing those two together should not be too hard and figuring it out will definetly be most beneficial for your future projects, but if you get stuck, we’ll try to help.

Thank-you for your reply. I have seen this library on the Particle Build website but I never went into it more as it looked too complicated. All you seem to have to do with the Arduino was initialize the SD card then use the SD.open feature. If anyone could give any examples I would greatly appreciate it. It is a shame in one way that the librarys for the Arduino won’t work the same way on it :expressionless:

The SdFat library is quite easy to use. Look in the TryMeFirst.cpp sample file. You basically just call sd.begin() and use standard C file read/write calls.