Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <OneWire.h>
- #include <ESP8266WiFi.h>
- const char* ssid = "sadhu-home";
- const char* password = "dir3002003";
- // Create an instance of the server
- // specify the port to listen on as an argument
- WiFiServer server(80);
- // пример использования библиотеки OneWire DS18S20, DS18B20, DS1822
- OneWire ds(2); // на пине 10 (нужен резистор 4.7 КОм)
- void setup(void) {
- Serial.begin(9600);
- delay(10);
- // Connect to WiFi network
- Serial.println();
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.mode(WIFI_STA);
- 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());
- }
- const char* thermo_id(char code, byte& type_s) {
- switch (code) {
- case 0x10:
- type_s = 1;
- return " Chip = DS18S20"; // или более старый DS1820
- case 0x28:
- type_s = 0;
- return " Chip = DS18B20"; // или более старый DS1820
- case 0x22:
- type_s = 0;
- return " Chip = DS1822"; // или более старый DS1820
- default:
- type_s = 2;
- return "Device is not a DS18x20 family device.";
- }
- }
- bool thermo_probe(float& probe) {
- byte i;
- byte present = 0;
- byte type_s;
- byte data[12];
- byte addr[8];
- float celsius, fahrenheit;
- if ( !ds.search(addr)) {
- Serial.println("No more addresses.");
- Serial.println();
- ds.reset_search();
- delay(250);
- return false;
- }
- Serial.print("ROM =");
- for( i = 0; i < 8; i++) {
- Serial.write(' ');
- Serial.print(addr[i], HEX);
- }
- if (OneWire::crc8(addr, 7) != addr[7]) {
- Serial.println("CRC is not valid!");
- return false;
- }
- Serial.println();
- // первый байт определяет чип
- Serial.println(thermo_id(addr[0], type_s));
- if (type_s > 1) return false;
- ds.reset();
- ds.select(addr);
- ds.write(0x44); // начинаем преобразование, используя ds.write(0x44,1) с "паразитным" питанием
- delay(1000); // 750 может быть достаточно, а может быть и не хватит
- // мы могли бы использовать тут ds.depower(), но reset позаботится об этом
- present = ds.reset();
- ds.select(addr);
- ds.write(0xBE);
- Serial.print(" Data = ");
- Serial.print(present, HEX);
- Serial.print(" ");
- for ( i = 0; i < 9; i++) { // нам необходимо 9 байт
- data[i] = ds.read();
- Serial.print(data[i], HEX);
- Serial.print(" ");
- }
- Serial.print(" CRC=");
- Serial.print(OneWire::crc8(data, 8), HEX);
- Serial.println();
- // конвертируем данный в фактическую температуру
- // так как результат является 16 битным целым, его надо хранить в
- // переменной с типом данных "int16_t", которая всегда равна 16 битам,
- // даже если мы проводим компиляцию на 32-х битном процессоре
- int16_t raw = (data[1] << 8) | data[0];
- if (type_s) {
- raw = raw << 3; // разрешение 9 бит по умолчанию
- if (data[7] == 0x10) {
- raw = (raw & 0xFFF0) + 12 - data[6];
- }
- } else {
- byte cfg = (data[4] & 0x60);
- // при маленьких значениях, малые биты не определены, давайте их обнулим
- if (cfg == 0x00) raw = raw & ~7; // разрешение 9 бит, 93.75 мс
- else if (cfg == 0x20) raw = raw & ~3; // разрешение 10 бит, 187.5 мс
- else if (cfg == 0x40) raw = raw & ~1; // разрешение 11 бит, 375 мс
- //// разрешение по умолчанию равно 12 бит, время преобразования - 750 мс
- }
- celsius = (float)raw / 16.0;
- fahrenheit = celsius * 1.8 + 32.0;
- Serial.print(" Temperature = ");
- Serial.print(celsius);
- Serial.print(" Celsius, ");
- Serial.print(fahrenheit);
- Serial.println(" Fahrenheit");
- probe = celsius;
- return true;
- }
- void loop(void) {
- // Check if a client has connected
- WiFiClient client = server.available();
- if (!client) {
- return;
- }
- // Wait until the client sends some data
- Serial.println("new client");
- while(!client.available()){
- delay(1);
- }
- // Read the first line of the request
- String req = client.readStringUntil('\r');
- Serial.println(req);
- client.flush();
- client.flush();
- // String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n";
- String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
- float celsius;
- byte retry_count = 5;
- while(!thermo_probe(celsius) && retry_count > 0) {
- retry_count--;
- }
- if (retry_count > 0) {
- s += String(celsius);
- } else {
- s += "No data";
- }
- s += "\n";
- // s += "</html>\n";
- // Send the response to the client
- client.print(s);
- Serial.println(s);
- delay(1);
- Serial.println("Client disonnected");
- // The client will actually be disconnected
- // when the function returns and 'client' object is detroyed
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement