5329| 1
|
[已解决] 传感器收集数据通过以太网上传到客户端,遇到些困难。 |
各位大神好,麻烦请教个问题,我把红外定位探测头与Ethernet W5200 和UNO板连接在一起,想实现:探测到测到数据之后将数据后将数据通过以太网,将W5200作为服务器,电脑作为客户端,传输到客户端,可以通过WEB网页来查看传感器数据,这样的功能。但是程序写好之后,访问浏览器,浏览器迟迟打不开,串口能显示部分数据。 #include <SPI.h> #include <EthernetV2_0.h> #include <Wire.h> // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress ip(192,168,1, 177); #define SS 10 //Gadgeteer PIN 6 #define nRST 8 //Gadgeteer PIN 4 #define nPWDN 9 //Gadgeteer PIN 5 #define nINT 3 //Gadgeteer PIN 3 //I2C// //int IRsensorAddress = 0xB0; int IRsensorAddress = 0x58; int slaveAddress; int ledPin = 13; boolean ledState = false; byte data_buf[16]; int i; int Ix[4]; int Iy[4]; int s; //I2C// // Initialize the Ethernet server library // with the IP address and port you want to use // (port 80 is default for HTTP): EthernetServer server(80); #define W5200_CS 10 #define SDCARD_CS 4 //I2C// void Write_2bytes(byte d1, byte d2) { Wire.beginTransmission(slaveAddress); Wire.write(d1); Wire.write(d2); Wire.endTransmission(); } //I2C// void setup() { pinMode(SS,OUTPUT); //端口定义Dreamer MEGA X2 PORT Gadgeteer PIN 6 use SS pinMode(nRST,OUTPUT); pinMode(nPWDN,OUTPUT); pinMode(nINT,INPUT); digitalWrite(nPWDN,LOW); //enable power digitalWrite(nRST,LOW); //Reset W5200 delay(10); digitalWrite(nRST,HIGH); delay(200); // wait W5200 work // Open serial communications and wait for port to open: Serial.begin(9600); // pinMode(SDCARD_CS,OUTPUT); // digitalWrite(SDCARD_CS,HIGH);//Deselect the SD card // start the Ethernet connection and the server: Ethernet.begin(mac, ip); server.begin(); Serial.print("server is at "); Serial.println(Ethernet.localIP()); //I2C// slaveAddress = IRsensorAddress >> 1; // This results in 0x21 as the address to pass to TWI // Serial.begin(19200); pinMode(ledPin, OUTPUT); // Set the LED pin as output Wire.begin(); // IR sensor initialize Write_2bytes(0x30,0x01); delay(10); Write_2bytes(0x30,0x08); delay(10); Write_2bytes(0x06,0x90); delay(10); Write_2bytes(0x08,0xC0); delay(10); Write_2bytes(0x1A,0x40); delay(10); Write_2bytes(0x33,0x33); delay(10); delay(100); //I2C// } void loop() { // listen for incoming clients EthernetClient client = server.available(); if (client) { Serial.println("new client"); // an http request ends with a blank line boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); Serial.write(c); //I2C// ledState = !ledState; if (ledState) { digitalWrite(ledPin,HIGH); } else { digitalWrite(ledPin,LOW); } //IR sensor read Wire.beginTransmission(slaveAddress); Wire.write(0x36); Wire.endTransmission(); Wire.requestFrom(slaveAddress, 16); // Request the 2 byte heading (MSB comes first) for (i=0;i<16;i++) { data_buf[i]=0; } i=0; while(Wire.available() && i < 16) { data_buf[i] = Wire.read(); i++; } Ix[0] = data_buf[1]; Iy[0] = data_buf[2]; s = data_buf[3]; Ix[0] += (s & 0x30) <<4; Iy[0] += (s & 0xC0) <<2; Ix[1] = data_buf[4]; Iy[1] = data_buf[5]; s = data_buf[6]; Ix[1] += (s & 0x30) <<4; Iy[1] += (s & 0xC0) <<2; Ix[2] = data_buf[7]; Iy[2] = data_buf[8]; s = data_buf[9]; Ix[2] += (s & 0x30) <<4; Iy[2] += (s & 0xC0) <<2; Ix[3] = data_buf[10]; Iy[3] = data_buf[11]; s = data_buf[12]; Ix[3] += (s & 0x30) <<4; Iy[3] += (s & 0xC0) <<2; //I2C // if you've zgotten 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("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println("Connnection: close"); client.println(); client.println("<!DOCTYPE HTML>"); client.println("<html>"); // add a meta refresh tag, so the browser pulls again every 5 seconds: client.println("<meta http-equiv=\"refresh\" content=\"5\">"); // output the value of each analog input pin /* for (int analogChannel = 0; analogChannel < 6; analogChannel++) { int sensorReading = analogRead(analogChannel); client.print("analog input "); client.print(analogChannel); client.print(" is "); client.print(sensorReading); client.println("<br />"); } */ //I2C// for(i=0; i<4; i++) { if (Ix[i] < 1000) Serial.print(""); client.print(""); if (Ix[i] < 100) Serial.print(""); client.print(""); if (Ix[i] < 10) Serial.print(""); client.print(""); Serial.print( int(Ix[i]) ); client.print("int(Ix[i])"); Serial.print(","); client.print(","); if (Iy[i] < 1000) Serial.print(""); client.print(""); if (Iy[i] < 100) Serial.print(""); client.print(""); if (Iy[i] < 10) Serial.print(""); client.print(""); Serial.print( int(Iy[i]) ); client.print("int(Iy[i])"); if (i<3) Serial.print(","); client.print(","); } //I2C client.println("</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(); Serial.println("client disonnected"); } } 这个是程序,请各位大神帮忙看看是什么问题? |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed