/* Created by JBM This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. */ #include WiFiServer server(80); #include #include //AP definitions #define AP_SSID "*******" // change * and write here your SSID #define AP_PASSWORD "*********"// change * and write here your password #define ONE_WIRE_BUS 2 // DS18B20 pin OneWire oneWire(ONE_WIRE_BUS); DallasTemperature DS18B20(&oneWire); float oldTemp; const float thresholdH = -17; // temp at actuator starts const float thresholdL = -18; // temp at actuator stops void setup() { WiFi.mode(WIFI_STA); server.begin(); Serial.begin(115200); delay(500); pinMode(0, OUTPUT); // Initialize the 0 pin as an output digitalWrite(0, HIGH); // Establish output 0 as HIGH (compressor OFF) in the start wifiConnect(); oldTemp = -1; } void loop() { float temp; do { DS18B20.requestTemperatures(); temp = DS18B20.getTempCByIndex(0); //Serial.print("Temperature: "); //Serial.println(temp); } while (temp == 85.0 || temp == (-127.0)); if (temp != oldTemp){ oldTemp = temp; } //temp = -1 * temp;// change to negative values for checking purposes *(-1) Serial.print("Temperature: "); Serial.println(temp); // if temperature value is high enough, turn on the compressor: if (temp > thresholdH){ digitalWrite(0, LOW);// Compressor ON } if (temp < thresholdL){ digitalWrite(0, HIGH);// Compressor OFF } delay(1000); // Check if a client has connected WiFiClient client = server.available(); if (!client) { return; } // Read the first line of the request (not used) String req = client.readStringUntil('\r'); Serial.println(req); client.flush(); // Match the request (not used) int val = -1; // We'll use 'val' to keep track of both the // request type (read/set) and value if set. if (req.indexOf("/led/0") != -1) val = 0; // Will write LED low else if (req.indexOf("/led/1") != -1) val = 1; // Will write LED high else if (req.indexOf("/read") != -1) val = -2; // Will print pin reads // Otherwise request will be invalid. We'll say as much in HTML // Set GPIO0 according to the request (not used here) if (val >= 0) digitalWrite(0, val); client.flush(); // Prepare the response. Start with the common header: String s = "HTTP/1.1 200 OK\r\n"; s += "Content-Type: text/html\r\n\r\n"; s += "\r\n\r\n"; // If we're setting the LED, print out a message saying we did if (val >= 0) { s += "LED is now "; s += (val)?"on":"off"; } else if (val == -2) { // If we're reading pins, print out those values: s += "Temperature is = "; s += String(temp); s += " deg C "; s += "
"; // Go to the next line. s += "Relay status is (0= ON; 1= OFF ) = "; s += String(digitalRead(0)); } else { s += "Invalid Request.
Try /led/1, /led/0, or /read."; } s += "\n"; // Send the response to the client client.print(s); delay(1); Serial.println("Client disonnected"); // The client will actually be disconnected // when the function returns and 'client' object is detroyed } void wifiConnect(){ delay(500); Serial.print("Connecting to AP"); WiFi.begin(AP_SSID, AP_PASSWORD); delay(500); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); delay(500); Serial.println("this is my IP address: "); Serial.println(WiFi.localIP()); delay(500); }