logo elektroda
logo elektroda
X
logo elektroda

How to modify the code on the D1 mini Pro for DS18B20 with names?

maroo78 786 0
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 18314338
    maroo78
    Level 9  
    Welcome.

    I'm basically completely in the dark - I'm piecing together what I can find around the web and something works. I would like to modify the following program so that I can add specific sensors with their addresses and display their reading under a specific name - e.g. Temp.Salon: .... I have the sensor addresses because the attached program displays each sensor in the monitor. I plan to display the readings from specific sensors under a defined name sequentially on a 64x48 OLED - but I hope I can manage that.

    In general, I would throw away the detection of what chip is in the sensor, I have all DS18B20 with address 28 ...

    Another issue after dealing with the above, how to read specific sensors in Blynk - at the moment I can read a sensor from any board by just selecting the board. How about several on 1 board?

    Ps. I use a delay so I don't send temperatures up my sleeve - probably not a good solution, by the way it determines the response time in communication between D1 and Blynk - so what I send is also realised with such a delay - how to handle it, probably with some kind of counter?

    In the suggestions, please comment on each line, it makes it easier to grasp and understand.

    Programme:
    #define BLYNK_PRINT Serial // Enables Serial Monitor
    #include <SPI.h>
    #include <ESP8266WiFi.h>
    #include <BlynkSimpleEsp8266.h>
    #include <OneWire.h>

    OneWire ds(D4); // on wemos pin D2 (a 4.7K resistor is necessary) make sure you change this from the original pin 10 to an unused pin.

    char auth[] = "blah blah blah"; // Thermometer test Auth Token here. (see Step 3 above)
    // char auth[] = "blah blah blah"; // Thermometer2 Put your Auth Token here. (see Step 3 above)
    // char auth[] = "blah blah blah"; // D1 Mini Pro Put your Auth Token here. (see Step 3 above)

    // Your WiFi credentials.
    // Set password to "" for open networks.
    char ssid[] = "bla bla bla";
    char pass[] = "blah blah blah";


    void setup()
    {
    // Debug console
    Serial.begin(9600);

    Blynk.begin(auth, ssid, pass);
    }

    void loop()
    {
    Blynk.run(); // All the Blynk Magic happens here....

    // You can inject your own code or combine it with other sketches.
    // Check other examples on how to communicate with Blynk. Remember
    // to avoid delay() function!

    delay(10000); // this should not be here ;) .
    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(" ========================================== ");
    Serial.println();
    ds.reset_search();
    delay(250);
    return;
    }

    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;
    }
    Serial.println();

    // the first ROM byte indicates which chip
    switch (addr[0]) {
    case 0x28:
    Serial.println(" Chip = DS18B20");
    type_s = 0;
    break;
    default:
    Serial.println("Device is not a DS18x20 family device.");
    return;
    }

    ds.reset();
    ds.select(addr);
    ds.write(0x44, 1); // start conversion, with parasite power on at the end

    delay(1000); // maybe 750ms is enough, maybe not
    // we might do a ds.depower() here, but the reset will take care of it.

    present = ds.reset();
    ds.select(addr);
    ds.write(0xBE); // Read Scratchpad

    Serial.print(" Data = ");
    Serial.print(present, HEX);
    Serial.print(" ");
    for ( i = 0; i < 9; i++) { // we need 9 bytes
    data[i] = ds.read();
    Serial.print(data[i], HEX);
    Serial.print(" ");
    }
    Serial.print(" CRC=");
    Serial.print(OneWire::crc8(data, 8), HEX);
    Serial.println();

    // Convert the data to actual temperature
    // because the result is a 16 bit signed integer, it should
    // be stored to an "int16_t" type, which is always 16 bits
    // even when compiled on a 32 bit processor.
    int16_t raw = (data[1] << 8) | data[0];
    if (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x20) {
    // "count remain" gives full 12 bit resolution
    raw = (raw & 0xFFF0) + 12 - data[6];
    }
    } else {
    byte cfg = (data[4] & 0x60);
    // at lower res, the low bits are undefined, so let's zero them
    if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
    //// default is 12 bit resolution, 750 ms conversion time
    }
    celsius = (float)raw / 16.0;
    fahrenheit = celsius * 1.8 + 32.0;
    Serial.print(" Temperature = ");
    Serial.print(celsius);
    Serial.print(" °C, ");
    Serial.print(fahrenheit);
    Serial.println(" Fahrenheit");
    Blynk.virtualWrite(V6, celsius);



    I would very much appreciate help with the changes with full explanations of what/how/why.

    Regards
  • ADVERTISEMENT
ADVERTISEMENT