- Above image link
- One wire protocol is serial communication protocol
- Its developed by Dallas Semiconductor.
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
#include <OneWire.h> #include <DallasTemperature.h> // Data wire is plugged into GPIO 15 on the ESP32 #define ONE_WIRE_BUS 22 #define TEMPERATURE_PRECISION 12 // Lower resolution // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) OneWire oneWire(ONE_WIRE_BUS); // Pass our oneWire reference to Dallas Temperature. DallasTemperature sensors(&oneWire); int numberOfDevices; // Number of temperature devices found DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address void setup(void) { // start serial port Serial.begin(115200); Serial.println("Dallas Temperature IC Control Library Demo"); // Start up the library sensors.begin(); // Grab a count of devices on the wire numberOfDevices = sensors.getDeviceCount(); // locate devices on the bus Serial.print("Locating devices..."); Serial.print("Found "); Serial.print(numberOfDevices, DEC); Serial.println(" devices."); // report parasite power requirements Serial.print("Parasite power is: "); if (sensors.isParasitePowerMode()) Serial.println("ON"); else Serial.println("OFF"); // Loop through each device, print out address for(int i=0;i<numberOfDevices; i++) { // Search the wire for address if(sensors.getAddress(tempDeviceAddress, i)) { Serial.print("Found device "); Serial.print(i, DEC); Serial.print(" with address: "); printAddress(tempDeviceAddress); Serial.println(); Serial.print("Setting resolution to "); Serial.println(TEMPERATURE_PRECISION, DEC); // set the resolution to TEMPERATURE_PRECISION bit (Each Dallas/Maxim device is capable of several different resolutions) sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION); Serial.print("Resolution actually set to: "); Serial.print(sensors.getResolution(tempDeviceAddress), DEC); Serial.println(); }else{ Serial.print("Found ghost device at "); Serial.print(i, DEC); Serial.print(" but could not detect address. Check power and cabling"); } } delay(5000); } // function to print the temperature for a device void printTemperature(DeviceAddress deviceAddress) { float tempC = sensors.getTempC(deviceAddress); Serial.print("Temp C: "); Serial.println(tempC); Serial.print("Temp F: "); Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit } void loop(void) { // call sensors.requestTemperatures() to issue a global temperature request to all devices on the bus Serial.print("Requesting temperatures..."); sensors.requestTemperatures(); // Send the command to get temperatures Serial.println("DONE"); // Loop through each device, print out temperature data for(int i=0;i<numberOfDevices; i++) { // Search the wire for address if(sensors.getAddress(tempDeviceAddress, i)) { // Output the device ID Serial.print("Temperature for device: "); Serial.println(i,DEC); // It responds almost immediately. Let's print out the data printTemperature(tempDeviceAddress); // Use a simple function to print out the data delay(3000); } //else ghost device! Check your power requirements and cabling } } // function to print a device address void printAddress(DeviceAddress deviceAddress) { for (uint8_t i = 0; i < 8; i++) { if (deviceAddress[i] < 16) Serial.print("0"); Serial.print(deviceAddress[i], HEX); } } |
if you have any queries call us +918300026060 or WhatsApp Dismiss