Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <Ethernet.h>
- #include <OneWire.h> //1
- #include <DallasTemperature.h>
- //The data wire for the indoor sensor is plugged into pin d5 on the Arduino
- // on pin 5 & 6 (a 4.7K resistor is necessary) - the temperature probe has one built in.
- //2
- #define TEMPERATURE_INDOOR 5
- #define TEMPERATURE_OUTDOOR 6
- const int ledPin = 7;
- byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x7D, 0x54 };
- //3
- //setup an instance to communicate with your sensor
- OneWire oneWireIndoor(TEMPERATURE_INDOOR);
- DallasTemperature sensorIndoor(&oneWireIndoor);
- //4
- OneWire oneWireOutdoor(TEMPERATURE_OUTDOOR);
- DallasTemperature sensorOutdoor(&oneWireOutdoor);
- EthernetServer server(80);
- const int ledPin = 7;
- byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x7D, 0x54 }; //1
- EthernetServer server(80); //2
- void setup(void) {
- pinMode(ledPin, OUTPUT); //1
- Serial.begin(9600);
- Serial.println("LED Pin setup for output on pin ");
- digitalWrite(ledPin, HIGH);
- sensorIndoor.begin();
- sensorOutdoor.begin();
- // start the Ethernet connection and the server:
- Serial.println("Trying to get an IP address using DHCP");
- digitalWrite(ledPin, HIGH); //2
- // start the Ethernet connection and the server:
- Serial.println("Trying to get an IP address using DHCP");
- if (Ethernet.begin(mac) == 0) { //3
- Serial.println("Failed to configure Ethernet using DHCP");
- while (true) {
- digitalWrite(ledPin, HIGH);
- delay(200);
- digitalWrite(ledPin, LOW);
- delay(200);
- }
- }
- Serial.println();
- // start listening for clients
- Serial.print("server is at "); //4
- Serial.println(Ethernet.localIP());
- digitalWrite(ledPin, LOW);
- }
- void blinkLED(void)
- {
- digitalWrite(ledPin, HIGH);
- delay(500);
- digitalWrite(ledPin, LOW);
- delay(500);
- return;
- }
- void loop(void) {
- client.println();
- temperatureIndoor = readTemperatureCelsius(sensorIndoor);
- temperatureOutdoor = readTemperatureCelsius(sensorOutdoor);
- client.print("{\"arduino\":[{\"location\":\"indoor\",\"celsius\":\"");
- // 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><body>Aquaryoum</body></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");
- blinkLED();
- float readTemperatureCelsius(DallasTemperature sensor) {
- sensor.requestTemperatures();
- float temperature = sensor.getTempCByIndex(0);
- Serial.print("Celsius Temperature for device is: ");
- Serial.println(temperature); //zero is first sensor if we had multiple on bus
- return temperature;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement