Source Code Bab 7
Source Code Bab 7
Program: eth_websrv_SD_Ajax_in_out
Description: Arduino web server untuk 1 analog input
web page disimpan di micro SD card.
Hardware: Arduino Uno and official Arduino Ethernet
shield.
2Gb micro SD card formatted FAT16.
A2 to A5 sebagai input analog
References:
- WebServer example by David A. Mellis and
modified by Tom Igoe
- SD card examples by David A. Mellis and
Tom Igoe
- Ethernet library documentation:
http://arduino.cc/en/Reference/Ethernet
- SD Card library documentation:
http://arduino.cc/en/Reference/SD
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
// size of buffer used to capture HTTP requests
#define REQ_BUF_SZ 60
// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// IP address untuk akses via web
IPAddress ip(192, 168, 0, 20);
// server di port 80
EthernetServer server(80);
// the web page file on the SD card
File webFile;
// buffered HTTP request stored as null terminated string
char HTTP_req[REQ_BUF_SZ] = {0};
// index into HTTP_req buffer
char req_index = 0;
// stores the states of the LEDs
boolean LED_state[4] = {0};
void setup()
{
// disable Ethernet chip
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
// untuk proses cek kondisi ARDUINO
Serial.begin(9600);
// inisialisasi SD card
Serial.println("Initializing SD card...");
if (!SD.begin(4)) {
// init failed
Serial.println("ERROR- initialization failed!");
return;
}
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.");
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // start to listen for clients
}
void loop()
{ // try to get client
EthernetClient client = server.available();
if (client) { // got client?
boolean currentLineIsBlank = true;
while (client.connected()) {
cl.print("</inputs>");
}
// sets every element of str to 0 (clears array)
void StrClear(char *str, char length)
{
for (int i = 0; i < length; i++) {
str[i] = 0;
}
}
// searches for the string sfind in the string str
// returns 1 if string found
// returns 0 if string not found
char StrContains(char *str, char *sfind)
{
char found = 0;
char index = 0;
char len;
len = strlen(str);