// Libraries we need #include #include // Define server MAC and IP // ID of Ethernet module: byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0xFE, 0x70 }; // IP address assigned to the Arduino Ethernet Shield: IPAddress ip(192,168,1,191); // Starts the server on port 80 EthernetServer server(80); // Static variables int liquidDetection= 2; // Pin for the water sensor int CO2Sensor = A0; // Pin for the CO2 sensor // Dynamic variables int liquidState = 0; // We'll assume there is no water or CO2 around to start with int CO2Value = 0; void setup() { // Initializes the input pin for the liquid sensor pinMode(liquidDetection, INPUT); // Initiate the Ethernet connection and the server Ethernet.begin(mac, ip); server.begin(); } void loop() { // Read in the digital value for the presence of liquid liquidState = digitalRead(liquidDetection); // Read in the analogical value of CO2 and store it as % CO2Value = analogRead(CO2Sensor); CO2Value = map(CO2Value,0,1023,0,100); // Listen for potential incoming clients EthernetClient client = server.available(); if (client) { // An HTTP petition ends with a blank line boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); // Check if the HTTP request has finished or not if (c == '\n' && currentLineIsBlank) { // Send standard HTTP header client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println(); client.print("KITCHEN MONITOR"); client.print(" => "); // Print out value from the liquid sensor client.print("[Presence of liquid"); client.print(" = "); if (liquidState == 0){ client.print("No"); } else { client.print("Yes"); } client.print("]"); // Print the amount of CO2 client.print(" ; [CO2"); client.print(" = "); client.print(CO2Value); client.print(" %] "); break; } if (c == '\n') { // Start a new line currentLineIsBlank = true; } else if (c != '\r') { // Get a character currentLineIsBlank = false; } } } // Give the server time to receive the data delay(1); // Close connection client.stop(); } }