|
// zoomkat's meta refresh data frame test page 5/25/13
// use http://192.168.1.102:84 in your brouser for main page
// http://192.168.1.102:84/data static data page
// http://192.168.1.102:84/datastart meta refresh data page
// for use with W5100 based ethernet shields
// set the refresh rate to 0 for fastest update
// use STOP for single data updates
#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
const int analogInPin0 = A0;
const int analogInPin1 = A1;
const int analogInPin2 = A2;
const int analogInPin3 = A3;
const int analogInPin4 = A4;
const int analogInPin5 = A5;
int X0, X1, Y0, Y1, Z1, Z0;
float X,Y,Z;
int sensorRead = 0;
int newdata = 0;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // arduino ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port
unsigned long int x=0; //set refresh counter to 0
String readString;
//////////////////////
void setup(){
Serial.begin(9600);
// disable SD SPI if memory card in the uSD slot
pinMode(A2,INPUT);
Wire.begin(); //初始化 I2C
setReg(0x2D, 0xA); // (打開電源, 設定輸出資料速度為 100 Hz)
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
Ethernet.begin(mac, ip, gateway, gateway, subnet);
server.begin();
Serial.println("meta refresh data frame test 5/25/13"); // so I can keep track of what is loaded
}
void setReg(int reg, int data){
Wire.beginTransmission(I2C_Address);
Wire.write(reg); // 指定佔存器
Wire.write(data); // 寫入資料
Wire.endTransmission();
}
/* setReg(reg,data):寫入佔存器
* 參數:
* reg → 指定佔存器位址
* data → 要寫入的資料
*/
int getData(int reg){
Wire.beginTransmission(I2C_Address);
Wire.write(reg);
Wire.endTransmission();
Wire.requestFrom(I2C_Address,1);
if(Wire.available()<=1){
return Wire.read();
}
}
/* getData(reg):取得佔存器裡的資料
* 參數:reg → 佔存器位址
*/
void loop(){
X0 = getData(0x32); // 取得 X 軸 低位元資料
X1 = getData(0x33); // 取得 X 軸 高位元資料
X = ((X1 << 8) + X0) / 256.0;
Y0 = getData(0x34); // 取得 Y 軸 低位元資料
Y1 = getData(0x35); // 取得 Y 軸 高位元資料
Y = ((Y1 << 8) + Y0) / 256.0;
Z0 = getData(0x36); // 取得 Z 軸 低位元資料
Z1 = getData(0x37); // 取得 Y 軸 高位元資料
Z = ((Z1 << 8) + Z0) / 256.0;
sensorRead=analogRead(A2);
newdata = map(sensorRead, 0, 1023, 0,255);
Serial.print(newdata);
Serial.print("\t");
Serial.print(X);
Serial.print("\t");
Serial.print(Y);
Serial.print("\t");
Serial.println(Z);
delay(2000);
}
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (readString.length() < 100) {
readString += c;
}
//check if HTTP request has ended
if (c == '\n') {
//check get atring received
Serial.println(readString);
//output HTML data header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
//generate data page
if(readString.indexOf("data") >0) { //checks for "data" page
x=x+1; //page upload counter
client.print("<HTML><HEAD>");
//meta-refresh page every 1 seconds if "datastart" page
if(readString.indexOf("datastart") >0) client.print("<meta http-equiv='refresh' content='1'>");
//meta-refresh 0 for fast data
if(readString.indexOf("datafast") >0) client.print("<meta http-equiv='refresh' content='0'>");
client.print("<title>Zoomkat's meta-refresh test</title></head><BODY><br>");
client.print("page refresh number: ");
client.print(x); //current refresh count
client.print("<br><br>");
//output the value of each analog input pin
client.print("analog input0 is: ");
client.print(analogRead(analogInPin0));
client.print("<br>analog input1 is: ");
client.print(analogRead(analogInPin1));
client.print("<br>analog input2 is: ");
client.print(analogRead(analogInPin2));
client.print("<br>analog input3 is: ");
client.print(analogRead(analogInPin3));
client.print("<br>analog input4 is: ");
client.print(analogRead(analogInPin4));
client.print("<br>analog input5 is: ");
client.print(analogRead(analogInPin5));
client.println("<br></BODY></HTML>");
}
//generate main page with iframe
else
{
client.print("<HTML><HEAD><TITLE>Zoomkat's frame refresh test</TITLE></HEAD>");
client.print("Zoomkat's Arduino frame meta refresh test 5/25/13");
client.print("<BR><BR>Arduino analog input data frame:<BR>");
client.print(" <a href='http://192.168.1.102:84/datastart' target='DataBox' title=''yy''>META-REFRESH</a>");
client.print(" <a href='http://192.168.1.102:84/data' target='DataBox' title=''xx''>SINGLE-STOP</a>");
client.print(" <a href='http://192.168.1.102:84/datafast' target='DataBox' title=''zz''>FAST-DATA</a><BR>");
client.print("<iframe src='http://192.168.1.102:84/data' width='350' height='250' name='DataBox'>");
client.print("</iframe><BR></HTML>");
}
delay(1);
//stopping client
client.stop();
//clearing string for next read
readString="";
}
}
}
}
}
是哪裡出問題了? |
|