Can't get my Uno + Ethernet shield to display a JPG on the webpage. I think I am missing some vital part?
It displays a box on the webpage, but no image. Tried different image files and SD cards.
What am I missing?
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
#include <EEPROM.h>
File myFile;
#define Output1 3
#define Output2 5
#define Output3 6
#define Output4 7
#define Output5 8
#define Output6 9
#define Piezo A2
#define Reset A3
#define Local A4
//#define Confirm A5
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDA, 0x02 };
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
int hash1;
int data1;
int data2;
int data3;
int count;
int readposition;
String readString;
byte ip_1; //Ip address pulled from SD card
byte ip_2;
byte ip_3;
byte ip_4;
//-----------------------------------------------------------------------------------------------------------------------------
void setup()
{
pinMode(Reset, INPUT);
pinMode(Output1, OUTPUT);
pinMode(Output2, OUTPUT);
pinMode(Output3, OUTPUT);
pinMode(Output4, OUTPUT);
pinMode(Output5, OUTPUT);
pinMode(Output6, OUTPUT);
pinMode(Piezo, OUTPUT);
pinMode(Local, OUTPUT);
//pinMode(Confirm, INPUT);
Serial.begin(9600);
//Factory_reset ();
data1=EEPROM.read(551); //Check to see if this is the first time this program has ever run
if (data1==0) {First_run ();}
//-----------------------------------------------------------------------------------------------------------------------------
while (!Serial) { // Open serial communications and wait for port to open:
}
Serial.println();
Serial.print(F("Checking for SD card... "));
Serial.println();
if (SD.begin(4)) {
Serial.println(F("Found SD card"));
delay(50);
// Open the file for reading:
myFile = SD.open("SiteData.txt");
if (myFile) {
Serial.println(F("SiteData.txt:"));
Serial.println(F(" "));
for (count=0;count<=500;count++) { //Clear all EEPROM text memory allocations
EEPROM.write(count,32);
}
Serial.println(F("Existing data erased. New data being loaded from SD card"));
Serial.println(F(" "));
// read from the file until there's nothing else in it:
while (myFile.available()) {
hash1=myFile.read(); //Search for a hash
if (hash1==35) { //If a hash is found, get the line number of the data
Serial.print(char(hash1)); //Display the hash+line number
data1=(myFile.read()-48);
data2=(myFile.read()-48);
data3=((data1*10)+data2); //Calculate which line number we are reading
Serial.print(data3);
EEPROM.write((data3*30)-30,data3); //Store the line number at its own line number address *30 then -30 (because there is no zero line number)
for (count=0;count<=12;count++) { //Skip the next 12 spaces/letters as they are the reference text. Just print them to the serial monitor
Serial.print(char(myFile.read()));
}
for (count=1;count<30;count++) { //Read the next 30 letters or numbers and store them incrementally from the known line number start point
readposition=(myFile.position()); //Get the current read position
data1=(myFile.read());
if (data1==35) { //If it finds another hash at the beginning of the next line, then process ready for next line data
myFile.seek(readposition-1); //Move the read position back one byte
break; //Exit this line read
}
Serial.print(char(data1));
EEPROM.write(((data3*30)-30)+count,data1);
if (count==29) {Serial.print('\n');} //If it doesn't move to the next line due to finding the next hash, then force a new line
}
if (data3>=16) {break;} //If all 16 files are found then exit
}
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println(F("error opening SiteData.txt"));
}
}else {Serial.print(F("No SD card present, using memory stored data"));}
IPAddress ip(192, 168, 1, 75);
IPAddress gateway(192, 168, 1, 254);
IPAddress subnet(255, 255, 255, 0);
//start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// 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(F("HTTP/1.1 200 OK")); //Send new page
client.println(F("Content-Type: text/html"));
client.println();
client.println(F("<html>"));
client.println(F("<body>"));
client.println(F("<body style='background-color:black'>"));
//Box around screen here
//client.println(F("<p style='color:yellow; text-align:center> <form action=''>User name:
<input type='text' name='userid'>
User password:
<input type='password' name='psw'></form>"));
client.println(F("<h1 style='color:white; strong; text-align:center'; >SMART</h1>"));
client.println(F("</p>"));
client.println(F("<center><svg width='500' height='8'> <rect width='500' height='8' style='fill:rgb(255,0,0);stroke-width:10;stroke:rgb(255,0,0)' /> Sorry, your browser does not support inline SVG.</svg>"));
client.println(F("</p>"));
client.println(F("</p>"));
client.println(F("<h2 style='color:yellow; strong; text-align:center'; >Remote Power Controller v1.0</h2>"));
client.println(F("</p>"));
client.println(F("<form action='/action_page.php'>Quantity:<input type='number' name='1st range'min='0' max='255' step='1' value='192'><input type='submit'></form>"));
client.println(F("<form action='/action_page.php'>Quantity (between 0 and 255):<input type='1st range' name='quantity' min='0' max='255'><input type='submit'></form>"));
//client.println(F("<input type='number' placeholder='ip address' autocomplete='off' name='firstname'>
"));
client.println(F("</p>"));
client.println("Content-Type: image/jpeg");
File myFile = SD.open("Smart.jpg");
client.print(F("<img src='Smart.jpg' alt='PPHE' style='width:304px;height:228px;'>"));
client.println(F("</body>"));
client.println(F("</html>"));
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();
}
}