// Sensor data on demand // Via web REST interface // Note: It can take a while for the Yun to connect to WiFi after a cold boot. // I have configured my Yun to light the WLAN light on the board when connected as a reminder // Mailbox handles REST request from a web URL #include // Servers up files and data from web #include // Handles web client requests and delivery of data to browser page #include // Library for DHT11 Temperature and Weather Sensor #include // DHT11 Connected to pin 2 on the Arduino int pinDHT11 = 2; // Initialize the DHT11 library SimpleDHT11 dht11; // Photoresistor connected to Analog Pin A0 int sensorPin = A0; int lightLevel = 0; // variable to store the value coming from the sensor // Start YUN Web server YunServer server; void setup() { // Set Built-in LED as output and make sure it is off pinMode(13, OUTPUT); digitalWrite(13, LOW); // Initialize Bridge and Mailbox -- Takes a few seconds Bridge.begin(); Mailbox.begin(); // Light LED 13 when Bridge is running and ready to interact with user // This can take a few seconds, hence the notification light digitalWrite(13, HIGH); // Initialize Serial for debugging purposes, if needed Serial.begin(9600); // Tell server to start listening for connections server.listenOnLocalhost(); server.begin(); } void loop() { // Check for web browser accessing server -- if client request is made, process it YunClient client = server.accept(); // If a client is asking for data, do this if (client) { // Read in the command - i.e. string following arduino.local/arduino/ String command = client.readString(); // Trim Carriage Return - Required as ReadString includes carriage return on end of string command.trim(); // ---------- Gather data from sensors ---------- // Get Date and Time from Yun Linux Side String dataString; dataString += getTimeStamp(); // read with raw Temperature and Humidity data. byte temperature = 0; byte humidity = 0; byte data[40] = {0}; if (dht11.read(pinDHT11, &temperature, &humidity, data)) { client.print("Read DHT11 failed"); return; } // Read Light Level Data from Photoresistor lightLevel = analogRead(sensorPin); // Convert to 1023 point low to high scale lightLevel = map(lightLevel, 0, 1023, 1023, 0); // ---------- End Gather data from sensors ---------- /* Debug Print Out of Raw Data client.println("Command Received: " + command); client.println(temperature); client.println(humidity); client.println(lightLevel); client.println("------------------------"); */ // Respond to Web REST Commands // Define each command you wish to respond to and also failure case if (command == "temp") { client.print(dataString+"\n"); client.print("Temperature: "); client.print(temperature); } else if (command == "humid") { client.print(dataString+"\n"); client.print("Humidity: "); client.print(humidity); } else if (command == "light") { client.print(dataString+"\n"); client.print("Light Level: "); client.print(lightLevel); } else if (command == "all") { client.print(dataString+"\n"); client.print("Temperature: "); client.print(temperature); client.print("\n"); client.print("Humidity: "); client.print(humidity); client.print("\n"); client.print("Light Level: "); client.print(lightLevel); client.print("\n"); } else { // If command isn't in list, return command listing client.print("Command Not Recognized\n"); client.print("Avaiable Commands: temp humid light all\n"); } // Close client session and pause client.stop(); delay(50); } } // *********** FUNCTIONS ************ // Functon to get Time Stamp from Yun Linux Side String getTimeStamp() { String result; Process time; time.begin("date"); //time.addParameter("+%D-%T"); time.run(); while(time.available()>0) { char c = time.read(); if(c != '\n') result += c; } return result; }