Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <DHT11.h>
- const char* ssid = "ptaszek";
- const char* password = "krzysiuu";;
- bool light = false;
- const int pinLight = 0;
- const int pinDHT11 = 2;
- String apiKey = "ZAKAMYCV3JWFX4HT";
- const char* thingspeak = "api.thingspeak.com";
- unsigned long time = 0;
- int sendSensorTime = 30000;
- WiFiServer server(80);
- DHT11 dht11(pinDHT11);
- void setup() {
- Serial.begin(115200);
- delay(10);
- pinMode(pinLight, OUTPUT);
- digitalWrite(pinLight, 0);
- // Connect to WiFi network
- Serial.println();
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- // Start the server
- server.begin();
- Serial.println("Server started");
- // Print the IP address
- Serial.println(WiFi.localIP());
- }
- void loop() {
- if (time < millis()) {
- sendSensor();
- time = millis() + sendSensorTime;
- }
- WiFiClient client = server.available();
- if (!client) {
- return;
- }
- // Wait until the client sends some data
- Serial.println("new client");
- int client_delay = 0;
- while (!client.available()) {
- if (client_delay > 10000) {
- return;
- }
- client_delay++;
- delay(1);
- }
- String req = client.readStringUntil('\r');
- Serial.println(req);
- client.flush();
- if (req.indexOf("/light/0") != -1) {
- light = false;
- digitalWrite(pinLight, light);
- }
- else if (req.indexOf("/light/1") != -1) {
- light = true;
- digitalWrite(pinLight, light);
- }
- client.flush();
- client.print(generateSite(light));
- delay(1);
- Serial.println("Client disonnected");
- // The client will actually be disconnected
- // when the function returns and 'client' object is detroyed
- }
- String generateSite(bool light) {
- int err;
- float temp, humi;
- String site = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>";
- site += "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-2\"><title>Szklarnia</title></head><body style=\"background: #26270D; font-family:Cursive;\"><div id=\"header\" style=\"background: #9B824E; border-radius:10px; margin: 0px 10px 10px 10px; text-align:center;\"><span style=\"font-size:36px;font-weight:bold; color: #ffffff;\">Mini szklarnia</span></div><div id=\"body\" style=\"background: #C6AE87; border-radius:10px; text-align: center; padding: 10px; margin: 10px 10px 10px 10px;\"><span style=\"font-size:18px;color:#ffffff;\">ODCZYTY</span><br><br>";
- if ((err = dht11.read(humi, temp)) == 0)
- {
- site += "Temperatura: <b>";
- site += temp;
- site += "°C </b><br>";
- site += "Wilgotnosc: <b>";
- site += humi;
- site += "%</b><br>";
- }
- else
- {
- site += "Error No :";
- site += err;
- }
- site += (light) ? "<a href=\"/light/0/\"><img src=\"http://icons.iconarchive.com/icons/paomedia/small-n-flat/128/switch-off-icon.png\"></a><br>" : "<a href=\"/light/1/\"><img src=\"http://icons.iconarchive.com/icons/paomedia/small-n-flat/128/switch-on-icon.png\"></a><br>";
- site += "UPTIME: ";
- site += millisToHrMinSec(millis());
- site += "</div></body></html>";
- return site;
- }
- void sendSensor() {
- int err;
- float temp, humi;
- WiFiClient clientThing;
- delay(100);
- if ((err = dht11.read(humi, temp)) == 0)
- {
- if (clientThing.connect(thingspeak, 80)) {
- String postStr = apiKey;
- postStr += "&field1=";
- postStr += String(temp);
- postStr += "&field2=";
- postStr += String(humi);
- postStr += "&field3=";
- postStr += String(light);
- postStr += "\r\n\r\n";
- clientThing.print("POST /update HTTP/1.1\n");
- clientThing.print("Host: api.thingspeak.com\n");
- clientThing.print("Connection: close\n");
- clientThing.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
- clientThing.print("Content-Type: application/x-www-form-urlencoded\n");
- clientThing.print("Content-Length: ");
- clientThing.print(postStr.length());
- clientThing.print("\n\n");
- clientThing.print(postStr);
- Serial.println("Temperature: ");
- Serial.print(temp);
- Serial.print(" degrees Celcius Humidity: ");
- Serial.print(humi);
- Serial.println("% send to Thingspeak");
- }
- }
- else
- {
- Serial.println("Error No :");
- Serial.print(err);
- }
- clientThing.stop();
- delay(100);
- }
- String millisToHrMinSec(unsigned long time){
- int hr = time/1000/60/60;
- int min = time/1000/60 - (hr*60);
- int sec = time/1000 - (hr*60*60)-(min*60);
- String result;
- if(hr<10){ result += "0";}
- result += String(hr);
- result += ":";
- if(min<10){ result += "0";}
- result += String(min);
- result += ":";
- if(sec<10){ result += "0";}
- result += String(sec);
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment