• Witaj na Forum Arduino Polska! Zapraszamy do rejestracji!
  • Znajdziesz tutaj wiele informacji na temat hardware / software.
Witaj! Logowanie Rejestracja


Ocena wątku:
  • 0 głosów - średnia: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Pomiar temp. z czterech czujników za pomocą domowej sieci WiFi
#1
Photo 
Witam chciałbym zaprezentować układ pomiaru temperatury z wielu czujników Ds18b20 ,na razie czterech z jednego modułu ESP8266. 

[img]<a href=https://zapodaj.net/6d4c746f4c526.png.html>ESP8266.png</a>[/img]

Jak widać możemy podłączyć cztery czujniki ds18B20 w klasycznym układzie. Wyniki pomiarów wyświetlane są na telefonie czy tablecie z systemem Android, wszystko dzięki aplikacji która została stworzona na potrzeby tego projektu. Oczywiście możemy podłączyć dwa lub trzy czujniki i ustawić sobie odpowiedni widok w aplikacji. 
[img]<a href=https://zapodaj.net/9a55ddc3f23bb.png.html>Screenshot_2018-01-18-20-24-13.png</a>[/img]
Kod:
#include <DallasTemperature.h>#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <OneWire.h>

// tutaj musimy ustawić adres ip esp8266 oraz wprowadzić inne dane naszej sieci
// po tym zabiegu będziemy mieć na stałe przypisany adres ip w module ESP8266
// USTAWIENIA PONIŻEJ TO USTAWIENIA MOJEJ SIECI

IPAddress ip(192, 168, 8, 151);
IPAddress gateway(192, 168, 8, 1);
IPAddress subnet(255, 255, 255, 0);

const char* ssid = ".................";// tutaj wprowadzamy nazwę naszej sieci WiFI
const char* password = "................."; tutaj hasło sieci

void wifi_init(){
WiFi.config(ip, gateway, subnet);
delay(100);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println();
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(200);
}
Serial.println();
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Fail connecting");
delay(5000);
ESP.restart();
}
Serial.print(" OK ");
Serial.print("Module IP: ");
Serial.println(WiFi.localIP());
}

#define ONE_WIRE_BUS 0
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

// W ten sposób przypisujemy adresy czujników wcześniej odczytane,niestety tak to narazie trzeba wykonać
//
DeviceAddress t1 = { 0x28, 0x39, 0xbf, 0x22, 0x06, 0x00, 0x00, 0xe3 }; // - adres DS18B20 - 1
DeviceAddress t2 = { 0x28, 0xff, 0xbe, 0x6b, 0x62, 0x17, 0x04, 0xa8 }; // - adres DS18B20 - 2
DeviceAddress t3 = { 0x28, 0xff, 0xc5, 0x67, 0x62, 0x17, 0x04, 0xa7 }; // - adres DS18B20 - 3
DeviceAddress t4 = { 0x28, 0xff, 0x0d, 0x62, 0x62, 0x17, 0x04, 0x3a }; // - adres DS18B20 - 4

WiFiUDP Udp;
unsigned int localUdpPort = 12346; // local port to listen on
char incomingPacket[255]; // buffer for incoming packets
char replyPacekt[] = "Dokonano pomiaru temperatur :-)"; // a reply string to send back


void setup()

{
Serial.begin(115200);
Serial.println();
wifi_init();
{
delay(500);
Serial.print(".");
}
Serial.println(" connected");

Udp.begin(localUdpPort);
Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.localIP().toString().c_str(), localUdpPort);
}


void loop()
{
int packetSize = Udp.parsePacket();
if (packetSize)
{
// receive incoming UDP packets

sensors.requestTemperatures();
Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
int len = Udp.read(incomingPacket, 255);
if (len > 0)
{
incomingPacket[len] = 0;
sensors.requestTemperatures();
delay(750);
}
Serial.printf("UDP packet contents: %s\n", incomingPacket);

float temp1;
temp1 = sensors.getTempC(t1);
String Pomiar1;
Pomiar1=String(temp1, 1);
Serial.println(Pomiar1);

//float temp2;
temp1 = sensors.getTempC(t2);
String Pomiar2;
Pomiar2=String(temp1, 1);
Serial.println(Pomiar2);

//float temp3;
temp1 = sensors.getTempC(t3);
String Pomiar3;
Pomiar3=String(temp1, 1);
Serial.println(Pomiar3);

//float temp4;
temp1 = sensors.getTempC(t4);
String Pomiar4;
Pomiar4=String(temp1, 1);
Serial.println(Pomiar4);

Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());


String tempp1 ;

tempp1 += "n1=";
tempp1 += Pomiar1;
tempp1 += "x";
tempp1 += "n2=";
tempp1 += Pomiar2;
tempp1 += "x";
tempp1 += "n3=";
tempp1 += Pomiar3;
tempp1 += "x";
tempp1 += "n4=";
tempp1 += Pomiar4;
tempp1 += "x";

Udp.println(tempp1);

Udp.endPacket();
}
}

Jak to teraz odpalić ? 

1. Po zmontowaniu i podłączeniu zasialania należy odczytać seriale DS18B20 możemy załadować do modułu np. ten szkic 

Kod:
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>


//------------------------------------------
//DS18B20
#define ONE_WIRE_BUS 0 //Pin to which is attached a temperature sensor
#define ONE_WIRE_MAX_DEV 15 //The maximum number of devices

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
int numberOfDevices; //Number of temperature devices found
DeviceAddress devAddr[ONE_WIRE_MAX_DEV]; //An array device temperature sensors
float tempDev[ONE_WIRE_MAX_DEV]; //Saving the last measurement of temperature
float tempDevLast[ONE_WIRE_MAX_DEV]; //Previous temperature measurement
long lastTemp; //The last measurement
const int durationTemp = 5000; //The frequency of temperature measurement

//------------------------------------------
//WIFI
const char* ssid = "............"; // nazwa sieci WIFI
const char* password = "................"; //HASŁO

//------------------------------------------
//HTTP
ESP8266WebServer server(80);

//------------------------------------------
//Convert device id to String
String GetAddressToString(DeviceAddress deviceAddress){
String str = "";
for (uint8_t i = 0; i < 8; i++){
if( deviceAddress[i] < 16 ) str += String(0, HEX);
str += String(deviceAddress[i], HEX);
}
return str;
}

//Setting the temperature sensor
void SetupDS18B20(){
DS18B20.begin();

Serial.print("Parasite power is: ");
if( DS18B20.isParasitePowerMode() ){
Serial.println("ON");
}else{
Serial.println("OFF");
}

numberOfDevices = DS18B20.getDeviceCount();
Serial.print( "Device count: " );
Serial.println( numberOfDevices );

lastTemp = millis();
DS18B20.requestTemperatures();

// Loop through each device, print out address
for(int i=0;i<numberOfDevices; i++){
// Search the wire for address
if( DS18B20.getAddress(devAddr[i], i) ){
//devAddr[i] = tempDeviceAddress;
Serial.print("Found device ");
Serial.print(i, DEC);
Serial.print(" with address: " + GetAddressToString(devAddr[i]));
Serial.println();
}else{
Serial.print("Found ghost device at ");
Serial.print(i, DEC);
Serial.print(" but could not detect address. Check power and cabling");
}

//Get resolution of DS18b20
Serial.print("Resolution: ");
Serial.print(DS18B20.getResolution( devAddr[i] ));
Serial.println();

//Read temperature from DS18b20
float tempC = DS18B20.getTempC( devAddr[i] );
Serial.print("Temp C: ");
Serial.println(tempC);
}
}

//Loop measuring the temperature
void TempLoop(long now){
if( now - lastTemp > durationTemp ){ //Take a measurement at a fixed time (durationTemp = 5000ms, 5s)
for(int i=0; i<numberOfDevices; i++){
float tempC = DS18B20.getTempC( devAddr[i] ); //Measuring temperature in Celsius
tempDev[i] = tempC; //Save the measured value to the array
}
DS18B20.setWaitForConversion(false); //No waiting for measurement
DS18B20.requestTemperatures(); //Initiate the temperature measurement
lastTemp = millis(); //Remember the last time measurement
}
}

//------------------------------------------
void HandleRoot(){
String message = "Number of devices: ";
message += numberOfDevices;
message += "\r\n<br>";
char temperatureString[6];

message += "<table border='1'>\r\n";
message += "<tr><td>ID czujnika id</td><td>Temperatura</td></tr>\r\n";
for(int i=0;i<numberOfDevices;i++){
dtostrf(tempDev[i], 2, 2, temperatureString);
Serial.print( "Sending temperature: " );
Serial.println( temperatureString );

message += "<tr><td>";
message += GetAddressToString( devAddr[i] );
message += "</td>\r\n";
message += "<td>";
message += temperatureString;
message += "</td></tr>\r\n";
message += "\r\n";
}
message += "</table>\r\n";

server.send(200, "text/html", message );
}

void HandleNotFound(){
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET)?"GET":"POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i=0; i<server.args(); i++){
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/html", message);
}


//------------------------------------------
void setup() {
//Setup Serial port speed
Serial.begin(115200);

//Setup WIFI
WiFi.begin(ssid, password);
Serial.println("");

//Wait for WIFI connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());

server.on("/", HandleRoot);
server.onNotFound( HandleNotFound );
server.begin();
Serial.println("HTTP server started at ip " + WiFi.localIP().toString() );

//Setup DS18b20 temperature sensor
SetupDS18B20();

}

void loop() {
long t = millis();

server.handleClient();
TempLoop( t );
}

Po załadowaniu szkicu odpalamy przeglądarkę internetową na kompie i wpisujemy adres naszego modułu esp8266( musicie sami sobie odnaleźć adres waszego modułu jaki został mu przydzielony tymczasowo przez ruter. Najlepiej przeskanować sieć darmowym programem albo zalogować się na ruterze). Gdy już wszystko się uda, to waszym oczom ukaże się tabelka z temperaturami i adresami ds18b20 . 

Teraz należy wklepać te adresy do właściwego wsadu, opisy umieściłem w kodzie. 

Następnie musimy wprowadzić dane naszej sieci i przydzielić adres ip. taki który nie będzie kolidował z innymi adresami (tu odsyłam do innych poradników), wprowadzamy jeszcze numer portu i przygotowany w ten sposób szkic wgrywamy do modułu ESP8266. 



W aplikacji należy ustawić taki sam port i adres ( można też dokonać własnych ustawień i widoków) a następnie całkowicie wyłączyć apkę (również usunąć z rzutu) i odpalić jeszcze raz . 

Aplikacja jest do pobrania w załączniku, niestety z przyczyn technicznych nie udało mi się umieścić jej jeszcze w sklepie google dlatego też przy instalacji należy dopuścić możliwość instalacji z nieznanych źródeł. Na niektórych telefonach moich kolegów wyskoczył monit że aplikacja może być potencjalnie niebezpieczna (z braku weryfikacji przez sklep google) . Oczywiście za jakiś czas umieszczę aplikację w sklepie google i wtedy należy odinstalować tą wersję i zainstalować ze sklepu. 



APLIKACJA JEST WŁASNOŚCIĄ INTELEKTUALNĄ AUTORA TEGO POSTU I PODLEGA PEŁNEJ OCHRONIE PRAW AUTORSKICH. MOŻE BYĆ WYKORZYSTYWANA RÓWNIEŻ W CELACH KOMERCYJNYCH. 

Czekam na opinie i komentarze życzę wszystkim miłej zabawy, Wkrótce dalsze modyfikacje i rozbudowa o pomiar z wielu ESP, funkcje termostatu itp. 



P.S 
Aplikacja jest już dostępna w sklepie nazwa to : Termik4Temp . Aplikacja jest darmowa i nie wyświetla reklam.
 
Odpowiedź
  


Skocz do:


Przeglądający: 1 gości