1.5.8. fejezet, Akvárium vízhőmérséklet mérés, étel adagolás
Beküldte pzoli - 2018, március 31 - 8:54du
Felhasznált alkatrészek
- NodeMCU-ESP8266-CH (WiFi)
- DS18B20 1M (vízálló)
- 1 db 4,7K ohm ellenállás (pull up resistor - felhúzó ellenállás)
- DHT22
- infúziós szerelékek (tápanyag adagoláshoz)
Feladat: egy akvárium vízhőmérsékletének mérése, webes leolvasása, valamint egy szervomotorral növényi tápoldat adagolása. Extra funkcióként a DHT22-vel környezeti hőmérséklet és páratartalom mérésére is alkalmassá tehető (lásd itt).
#include <ESP8266WiFi.h> #include <OneWire.h> #include <DallasTemperature.h> #include <Servo.h> #define ONE_WIRE_BUS 13 OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); int servoPin = 5; Servo servo; const char* ssid = "ssid"; //Change it for yours const char* password = "password"; //Change it for yours WiFiServer server(80); String header; void setup() { Serial.begin(9600); Serial.setTimeout(2000); // Wait for serial to initialize. WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("Device Started"); Serial.println("-------------------------------------"); Serial.println("Running Temperature sensor server!"); Serial.println("-------------------------------------"); Serial.print("Use this URL to connect: "); Serial.print("http://"); Serial.print(WiFi.localIP()); Serial.println("/"); server.begin(); sensors.begin(); Serial.println("Server started"); } void loop() { WiFiClient client = server.available(); if (client) { Serial.println("new client"); String currentLine = ""; while(client.connected()){ if (client.available()) { char c = client.read(); // read a byte, then Serial.write(c); // print it out the serial monitor header += c; if (c == '\n') { // if the byte is a newline character // if the current line is blank, you got two newline characters in a row. // that's the end of the client HTTP request, so send a response: if (currentLine.length() == 0) { // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) // and a content-type so the client knows what's coming, then a blank line: client.println("HTTP/1.1 200 OK"); client.println("Content-type:text/html"); client.println("Connection: close"); client.println(); processRequest(client,header); // The HTTP response ends with another blank line client.println(); // Break out of the while loop delay(10); client.stop(); break; } else { // if you got a newline, then clear currentLine currentLine = ""; } } else if (c != '\r') { // if you got anything else but a carriage return character, currentLine += c; // add it to the end of the currentLine } } } header = ""; // Close the connection client.stop(); Serial.println("Client disconnected."); Serial.println(""); } } boolean isValidInt(String val) { boolean result = true; if (val == "") { result = false; } else { int i=0; if (val[0]=='-') { i++; } for(; i<val.length() && result;i++) { if (!isDigit(val[i])) { result = false; } } } return result; } void processRequest(WiFiClient client, String request) { boolean isServoAngleSetRequest = false; int paramIdx = request.indexOf("/servo="); String angleStr; if (paramIdx != -1) { String paramStr = request.substring(paramIdx); angleStr = paramStr.substring(7,paramStr.indexOf(" ")); isServoAngleSetRequest = isValidInt(angleStr); } if (isServoAngleSetRequest) { adjustServo(client, angleStr); } else { readTemperatureFromSensors(client); } } void readTemperatureFromSensors(WiFiClient client){ sensors.requestTemperatures(); client.println("<content>"); client.print("<dallas-celsius>"); client.print(sensors.getTempCByIndex(0)); client.println("</dallas-celsius>"); client.println("</content>"); delay(10); } void adjustServo(WiFiClient client, String angleStr) { client.println("<content>"); client.print("<angle>"); client.print(angleStr); client.println("</angle>"); client.println("</content>"); if(!servo.attached()) { servo.attach(servoPin); } servo.write(angleStr.toInt()); delay(2000); servo.detach(); }
- A hozzászóláshoz be kell jelentkezni