#include byte mac[] = { 0x5C, 0xCF, 0x7F, 0xC1, 0x1A, 0x33 }; int photocellPin = 0; // the cell and 10K pulldown are connected to a0 int photocellReading; // the analog reading from the analog resistor divider #include #define DHT11Pin D6 ///////////////////////////// //VARS //the time we give the sensor to calibrate (10-60 secs according to the datasheet) int calibrationTime = 30; //the time when the sensor outputs a low impulse long unsigned int lowIn; //the amount of milliseconds the sensor has to be low //before we assume all motion has stopped long unsigned int pause = 5000; boolean lockLow = true; boolean takeLowTime; int pirPin = 4; //the digital pin connected to the PIR sensor's output int ledPin = 13; #include IPAddress ip(192, 168, 1, 147); IPAddress gateway(192, 168, 1, 1); IPAddress subnet(255, 255, 255, 0); const char* ssid = "IWILLSTEALYOURMONEY"; const char* password = "namnamnewnew"; // Create an instance of the server // specify the port to listen on as an argument WiFiServer server(80); DHT dht(DHT11Pin, DHT11); unsigned long previousMillis = 0; // will store last temp was read const long interval = 2000; ///////////////////////////// //SETUP void setup(){ Serial.begin(9600); pinMode(pirPin, INPUT); pinMode(ledPin, OUTPUT); digitalWrite(pirPin, LOW); WiFi.begin(ssid, password); //WiFi.config(ip, gateway, subnet); while (WiFi.status() != WL_CONNECTED) { delay(500); //WiFi.begin(ssid, password); //Serial.print("."); } //Serial.println(""); //Serial.println("WiFi connected"); // Start the server server.begin(); //Serial.println("Server started"); Serial.println(""); // Print the IP address Serial.println(WiFi.localIP()); //give the sensor some time to calibrate Serial.print("calibrating sensor "); for(int i = 0; i < calibrationTime; i++){ Serial.print("."); delay(1000); } Serial.println(" done"); Serial.println("SENSOR ACTIVE"); delay(50); } // get humidity float humidity = dht.readHumidity(); // get temperature as C float celsius = dht.readTemperature(); // get temperature as F float fahrenheit = dht.readTemperature(true); // prepare a web page to be send to a client (web browser) String prepareHtmlPage() { //gettemperature(); String htmlPage = String("HTTP/1.1 200 OK\r\n") + "Content-Type: application/json;charset=utf-8\r\n" + "Connection: close\r\n" + // the connection will be closed after completion of the response "Refresh: 5\r\n" + // refresh the page automatically every 5 sec "\r\n" + //"" + //"" + "{" + "\"lightval\":" + String(analogRead(A0)) + "," + "\"pirval\":" + String(digitalRead(D4)) + "," + "\"temperature\":" + float(dht.readTemperature()) + "," + "\"fahrenheit\":" + float(dht.readTemperature(true)) + "," + "\"humidity\":" + float(dht.readHumidity()) + "}" + //"" + "\r\n"; return htmlPage; } //////////////////////////// //LOOP void loop(){ readPhotocell(); //delay(10000); // get humidity float humidity = dht.readHumidity(); // get temperature as C float celsius = dht.readTemperature(); // get temperature as F float fahrenheit = dht.readTemperature(true); // print results Serial.print("Humidity: "); Serial.print(humidity); Serial.print(" Celsius: "); Serial.print(celsius); Serial.print(" Fahrenheit: "); Serial.println(fahrenheit); if(digitalRead(pirPin) == HIGH){ digitalWrite(ledPin, HIGH); //the led visualizes the sensors output pin state if(lockLow){ //makes sure we wait for a transition to LOW before any further output is made: lockLow = false; Serial.println("---"); Serial.print("motion detected at "); Serial.print(millis()/1000); Serial.println(" sec"); delay(50); } takeLowTime = true; } ////high if(digitalRead(pirPin) == LOW){ digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state if(takeLowTime){ lowIn = millis(); //save the time of the transition from high to LOW takeLowTime = false; //make sure this is only done at the start of a LOW phase } //if the sensor is low for more than the given pause, //we assume that no more motion is going to happen if(!lockLow && millis() - lowIn > pause){ //makes sure this block of code is only executed again after //a new motion sequence has been detected lockLow = true; Serial.print("motion ended at "); //output Serial.print((millis() - pause)/1000); Serial.println(" sec"); delay(50); } } //low WiFiClient client = server.available(); // wait for a client (web browser) to connect if (client) { Serial.println("\n[Client connected]"); while (client.connected()) { // read line by line what the client (web browser) is requesting if (client.available()) { String line = client.readStringUntil('\r'); Serial.print(line); // wait for end of client's request, that is marked with an empty line if (line.length() == 1 && line[0] == '\n') { client.println(prepareHtmlPage()); break; } } } delay(1); // give the web browser time to receive the data // close the connection: client.stop(); Serial.println("[Client disonnected]"); } } //loop void readPhotocell(){ photocellReading = analogRead(photocellPin); Serial.print(WiFi.localIP()); Serial.print(" Analog reading = "); Serial.print(photocellReading); // the raw analog reading // We'll have a few threshholds, qualitatively determined if (photocellReading < 10) { Serial.println(" - Dark"); } else if (photocellReading < 200) { Serial.println(" - Dim"); } else if (photocellReading < 500) { Serial.println(" - Light"); } else if (photocellReading < 800) { Serial.println(" - Bright"); } else { Serial.println(" - Very bright"); } } /* void gettemperature() { // Wait at least 2 seconds seconds between measurements. // if the difference between the current time and last time you read // the sensor is bigger than the interval you set, read the sensor // Works better than delay for things happening elsewhere also unsigned long currentMillis = millis(); if(currentMillis - previousMillis >= interval) { // save the last time you read the sensor previousMillis = currentMillis; // Reading temperature for humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor) humidity = dht.readHumidity(); // Read humidity (percent) celsius = dht.readTemperature(); // Read temperature as Fahrenheit fahrenheit = dht.readTemperature(true); // Read temperature as Fahrenheit // Check if any reads failed and exit early (to try again). if (isnan(humidity) || isnan(fahrenheit)) { Serial.println("Failed to read from DHT sensor!"); return; } } } */