SHOW:
|
|
- or go back to the newest paste.
| 1 | #include <ESP8266WiFi.h> | |
| 2 | #include <DHT11.h> | |
| 3 | - | const char* ssid = "ptaszek"; |
| 3 | + | const char* ssid = "xxxx"; |
| 4 | - | const char* password = "krzysiuu";; |
| 4 | + | const char* password = "xxxx";; |
| 5 | bool light = false; | |
| 6 | const int pinLight = 0; | |
| 7 | const int pinDHT11 = 2; | |
| 8 | - | String apiKey = "ZAKAMYCV3JWFX4HT"; |
| 8 | + | String apiKey = "xxxx"; |
| 9 | const char* thingspeak = "api.thingspeak.com"; | |
| 10 | unsigned long time = 0; | |
| 11 | int sendSensorTime = 30000; | |
| 12 | ||
| 13 | WiFiServer server(80); | |
| 14 | DHT11 dht11(pinDHT11); | |
| 15 | ||
| 16 | void setup() {
| |
| 17 | Serial.begin(115200); | |
| 18 | delay(10); | |
| 19 | ||
| 20 | pinMode(pinLight, OUTPUT); | |
| 21 | digitalWrite(pinLight, 0); | |
| 22 | ||
| 23 | // Connect to WiFi network | |
| 24 | Serial.println(); | |
| 25 | Serial.println(); | |
| 26 | Serial.print("Connecting to ");
| |
| 27 | Serial.println(ssid); | |
| 28 | ||
| 29 | WiFi.begin(ssid, password); | |
| 30 | ||
| 31 | while (WiFi.status() != WL_CONNECTED) {
| |
| 32 | delay(500); | |
| 33 | Serial.print(".");
| |
| 34 | } | |
| 35 | Serial.println("");
| |
| 36 | Serial.println("WiFi connected");
| |
| 37 | ||
| 38 | // Start the server | |
| 39 | server.begin(); | |
| 40 | Serial.println("Server started");
| |
| 41 | ||
| 42 | // Print the IP address | |
| 43 | Serial.println(WiFi.localIP()); | |
| 44 | } | |
| 45 | ||
| 46 | void loop() {
| |
| 47 | if (time < millis()) {
| |
| 48 | sendSensor(); | |
| 49 | time = millis() + sendSensorTime; | |
| 50 | } | |
| 51 | WiFiClient client = server.available(); | |
| 52 | if (!client) {
| |
| 53 | return; | |
| 54 | } | |
| 55 | // Wait until the client sends some data | |
| 56 | Serial.println("new client");
| |
| 57 | int client_delay = 0; | |
| 58 | while (!client.available()) {
| |
| 59 | if (client_delay > 10000) {
| |
| 60 | return; | |
| 61 | } | |
| 62 | client_delay++; | |
| 63 | delay(1); | |
| 64 | } | |
| 65 | String req = client.readStringUntil('\r');
| |
| 66 | Serial.println(req); | |
| 67 | client.flush(); | |
| 68 | if (req.indexOf("/light/0") != -1) {
| |
| 69 | light = false; | |
| 70 | digitalWrite(pinLight, light); | |
| 71 | } | |
| 72 | else if (req.indexOf("/light/1") != -1) {
| |
| 73 | light = true; | |
| 74 | digitalWrite(pinLight, light); | |
| 75 | } | |
| 76 | client.flush(); | |
| 77 | client.print(generateSite(light)); | |
| 78 | delay(1); | |
| 79 | Serial.println("Client disonnected");
| |
| 80 | ||
| 81 | // The client will actually be disconnected | |
| 82 | // when the function returns and 'client' object is detroyed | |
| 83 | } | |
| 84 | ||
| 85 | String generateSite(bool light) {
| |
| 86 | int err; | |
| 87 | float temp, humi; | |
| 88 | ||
| 89 | String site = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>"; | |
| 90 | 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>"; | |
| 91 | ||
| 92 | ||
| 93 | if ((err = dht11.read(humi, temp)) == 0) | |
| 94 | {
| |
| 95 | site += "Temperatura: <b>"; | |
| 96 | site += temp; | |
| 97 | site += "°C </b><br>"; | |
| 98 | site += "Wilgotnosc: <b>"; | |
| 99 | site += humi; | |
| 100 | site += "%</b><br>"; | |
| 101 | ||
| 102 | } | |
| 103 | else | |
| 104 | {
| |
| 105 | ||
| 106 | site += "Error No :"; | |
| 107 | site += err; | |
| 108 | ||
| 109 | } | |
| 110 | ||
| 111 | 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>"; | |
| 112 | site += "UPTIME: "; | |
| 113 | site += millisToHrMinSec(millis()); | |
| 114 | ||
| 115 | site += "</div></body></html>"; | |
| 116 | return site; | |
| 117 | } | |
| 118 | ||
| 119 | void sendSensor() {
| |
| 120 | int err; | |
| 121 | float temp, humi; | |
| 122 | WiFiClient clientThing; | |
| 123 | delay(100); | |
| 124 | if ((err = dht11.read(humi, temp)) == 0) | |
| 125 | {
| |
| 126 | if (clientThing.connect(thingspeak, 80)) {
| |
| 127 | String postStr = apiKey; | |
| 128 | postStr += "&field1="; | |
| 129 | postStr += String(temp); | |
| 130 | postStr += "&field2="; | |
| 131 | postStr += String(humi); | |
| 132 | postStr += "&field3="; | |
| 133 | postStr += String(light); | |
| 134 | postStr += "\r\n\r\n"; | |
| 135 | ||
| 136 | clientThing.print("POST /update HTTP/1.1\n");
| |
| 137 | clientThing.print("Host: api.thingspeak.com\n");
| |
| 138 | clientThing.print("Connection: close\n");
| |
| 139 | clientThing.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
| |
| 140 | clientThing.print("Content-Type: application/x-www-form-urlencoded\n");
| |
| 141 | clientThing.print("Content-Length: ");
| |
| 142 | clientThing.print(postStr.length()); | |
| 143 | clientThing.print("\n\n");
| |
| 144 | clientThing.print(postStr); | |
| 145 | ||
| 146 | Serial.println("Temperature: ");
| |
| 147 | Serial.print(temp); | |
| 148 | Serial.print(" degrees Celcius Humidity: ");
| |
| 149 | Serial.print(humi); | |
| 150 | Serial.println("% send to Thingspeak");
| |
| 151 | } | |
| 152 | } | |
| 153 | else | |
| 154 | {
| |
| 155 | Serial.println("Error No :");
| |
| 156 | Serial.print(err); | |
| 157 | } | |
| 158 | clientThing.stop(); | |
| 159 | delay(100); | |
| 160 | } | |
| 161 | ||
| 162 | String millisToHrMinSec(unsigned long time){
| |
| 163 | int hr = time/1000/60/60; | |
| 164 | int min = time/1000/60 - (hr*60); | |
| 165 | int sec = time/1000 - (hr*60*60)-(min*60); | |
| 166 | String result; | |
| 167 | if(hr<10){ result += "0";}
| |
| 168 | result += String(hr); | |
| 169 | result += ":"; | |
| 170 | if(min<10){ result += "0";}
| |
| 171 | result += String(min); | |
| 172 | result += ":"; | |
| 173 | if(sec<10){ result += "0";}
| |
| 174 | result += String(sec); | |
| 175 | return result; | |
| 176 | } |