Hallo,
ich habe vor die Temperatur im Keller mit einem LM75 zu messen und dessen über I²C auszulesen und, das dann mit einem Ethernetshield in mein Netzwerk zu übertragen. Ich habe schon den Beispiel Webserver getestet und dieser funktioniert auch. Mein Problem ist, dass wenn ich den LM75 auslesen möchte, sich der Webserver aufhängt.
Was mach ich falsch?
mfg
Michael
#include <Wire.h>
#include <SPI.h>
#include <Ethernet.h>
#define LM75_1 0b1001000
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0x90, 0xA2, 0xDA, 0x0D, 0xD6, 0x03 };
IPAddress ip(10,0,0,177);
float temp;
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
float LM75()
{
byte msb_1,lsb_1=0;
float Degrees_1=0;
// read LM75_1
Wire.beginTransmission(LM75_1);
Wire.write(0x00);
Wire.endTransmission();
Wire.requestFrom(LM75_1, 2);
while(Wire.available() < 2);
msb_1 = Wire.read(); lsb_1 = Wire.read();
if(msb_1 < 0x80)
{
Degrees_1=((msb_1*10)+(((lsb_1&0x80)>>7)*5));
}
else
{
Degrees_1=((msb_1*10)+(((lsb_1&0x80)>>7)*5));
Degrees_1=-(2555.0-Degrees_1);
}
Degrees_1=Degrees_1/10;
return Degrees_1;
}
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop()
{
temp=LM75();
// 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);
// 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("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 temperature
client.println("Arduino Webserver");
client.println("Temperatur im Keller:");
client.print("T= ");
client.print(temp);
client.print("°C");
client.println("
");
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");
}
}