├── src ├── DS18B20 │ ├── DS18B20.h │ └── Particle-OneWire.h ├── DS18B20.h └── DS18B20.cpp ├── verified ├── ds18b20.fzz ├── wireDiagram.png ├── project.properties ├── README.md └── libraryTest_DS18B20.ino ├── library.properties ├── .gitattributes ├── README.md ├── .gitignore ├── LICENSE └── examples ├── ds18b20_SingleDrop └── ds18b20_SingleDrop.ino └── ds18b20_test └── ds18b20_test.ino /src/DS18B20/DS18B20.h: -------------------------------------------------------------------------------- 1 | #include "../DS18B20.h" -------------------------------------------------------------------------------- /src/DS18B20/Particle-OneWire.h: -------------------------------------------------------------------------------- 1 | #include "../Particle-OneWire.h" -------------------------------------------------------------------------------- /verified/ds18b20.fzz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LFigg/ds18b20-photon/HEAD/verified/ds18b20.fzz -------------------------------------------------------------------------------- /verified/wireDiagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LFigg/ds18b20-photon/HEAD/verified/wireDiagram.png -------------------------------------------------------------------------------- /verified/project.properties: -------------------------------------------------------------------------------- 1 | dependencies.OneWire=1.4.1 2 | dependencies.DS18B20=0.1.3 3 | dependencies.unit-test=0.1.3 4 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=DS18B20 2 | 3 | version=0.1.04 4 | license=MIT 5 | 6 | author=Lucas Figg 8 | sentence=DSB18XX Lib on Particle Photon 9 | url=https://github.com/spark/ds18b20-photon 10 | 11 | repository=https://github.com/spark/ds18b20-photon.git 12 | dependencies.OneWire=2.0.1 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #DS18B20 Library for Particle Core, Photon and P1 2 | Modified DS18BXX Lib copied from 3 | https://github.com/krvarma/Dallas_DS18B20_SparkCore 4 | Sample application using Particle Photon and Dallas DS18B20 Digital Temperature 5 | Sensor. 6 | The OneWire source code is taken from this link by @tidwelltimj. 7 | I just separated this into two classes OneWire and DS18B20. 8 | The sample code publishes a variable named tmpinfo with temperature value. 9 | 10 | Requires Particle-OneWire (included) from 11 | https://github.com/balbano/OneWire-Particle/tree/master/firmware 12 | Wiring: 13 | Power to 3.3/5V 14 | GND to GND 15 | Signal to D2 (with 4.7k pullup resistor) 16 | 17 | Use crcCheck() to verify the sensor was successfully read. 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear on external disk 35 | .Spotlight-V100 36 | .Trashes 37 | 38 | # Directories potentially created on remote AFP share 39 | .AppleDB 40 | .AppleDesktop 41 | Network Trash Folder 42 | Temporary Items 43 | .apdisk 44 | -------------------------------------------------------------------------------- /src/DS18B20.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #define MAX_NAME 8 7 | #define MAX_RETRIES 3 8 | 9 | // Device resolution 10 | #define TEMP_9_BIT 0x1F // 9 bit 11 | #define TEMP_10_BIT 0x3F // 10 bit 12 | #define TEMP_11_BIT 0x5F // 11 bit 13 | #define TEMP_12_BIT 0x7F // 12 bit 14 | //Parasite Powered or Not 15 | #define READPOWERSUPPLY 0xB4 16 | 17 | class DS18B20{ 18 | private: 19 | OneWire* ds; 20 | byte data[12]; 21 | byte addr[8]; 22 | byte type_s; 23 | byte chiptype; 24 | byte _dataCRC; 25 | byte _readCRC; 26 | bool _singleDrop; 27 | char szName[MAX_NAME]; 28 | 29 | public: 30 | DS18B20(uint16_t pi, bool singleDrop = false); 31 | ~DS18B20(); 32 | boolean search(); 33 | void resetsearch(); 34 | void setResolution(uint8_t newResolution); 35 | bool readPowerSupply(); 36 | void getROM(char szROM[]); 37 | byte getChipType(); 38 | char* getChipName(); 39 | float getTemperature(bool forceSelect = false); 40 | float convertToFahrenheit(float celsius); 41 | bool crcCheck(); 42 | }; 43 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Joe Goggins 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /examples/ds18b20_SingleDrop/ds18b20_SingleDrop.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const int MAXRETRY = 4; 5 | const uint32_t msSAMPLE_INTERVAL = 2500; 6 | const uint32_t msMETRIC_PUBLISH = 30000; 7 | 8 | DS18B20 ds18b20(D2, true); //Sets Pin D2 for Water Temp Sensor and 9 | // this is the only sensor on bus 10 | char szInfo[64]; 11 | double celsius; 12 | double fahrenheit; 13 | uint32_t msLastMetric; 14 | uint32_t msLastSample; 15 | 16 | void setup() { 17 | Time.zone(-5); 18 | Particle.variable("tempHotWater", fahrenheit); 19 | Serial.begin(115200); 20 | } 21 | 22 | void loop() { 23 | if (millis() - msLastSample >= msSAMPLE_INTERVAL){ 24 | getTemp(); 25 | } 26 | 27 | if (millis() - msLastMetric >= msMETRIC_PUBLISH){ 28 | Serial.println("Publishing now."); 29 | publishData(); 30 | } 31 | } 32 | 33 | void publishData(){ 34 | if(!ds18b20.crcCheck()){ //make sure the value is correct 35 | return; 36 | } 37 | sprintf(szInfo, "%2.2f", fahrenheit); 38 | Particle.publish("dsTmp", szInfo, PRIVATE); 39 | msLastMetric = millis(); 40 | } 41 | 42 | void getTemp(){ 43 | float _temp; 44 | int i = 0; 45 | 46 | do { 47 | _temp = ds18b20.getTemperature(); 48 | } while (!ds18b20.crcCheck() && MAXRETRY > i++); 49 | 50 | if (i < MAXRETRY) { 51 | celsius = _temp; 52 | fahrenheit = ds18b20.convertToFahrenheit(_temp); 53 | Serial.println(fahrenheit); 54 | } 55 | else { 56 | celsius = fahrenheit = NAN; 57 | Serial.println("Invalid reading"); 58 | } 59 | msLastSample = millis(); 60 | } 61 | -------------------------------------------------------------------------------- /verified/README.md: -------------------------------------------------------------------------------- 1 | 2 | ## Library verification for DS18B20. 3 | The propuse of this test is to verfify the current DS18B20 temperature library from our community.To make sure all the others can feel 4 | confortable to use this library in their project. 5 | This verification code is performed under the unit-test system and also based the particle platform. 6 | 7 | ## preparation for running this firmware. 8 | * Electron/Photon/Core. 9 | * DS18b20 or DS1822 one-wire temperature sensor. 10 | * Breadboard. 11 | * Resistor.Value varies from 1K to 10K. 12 | * microUSB cable. 13 | * Particle WEB IDE or DEV 14 | * `Particle-cli` had been installed on computer. 15 | 16 | ## Library version list 17 | * unit-test library `0.1.3` 18 | * ds18b20 library `0.1.03` 19 | * onewrire library `1.4.1` 20 | 21 | ## Verify process 22 | 1. Setup the hardware system based on the `ds18b20.fzz` diagarm file. You may need to install Fritzing from [this website](http://fritzing.org/download/).You can also wire the hardware like the picture below. 23 | ![Image of wireDiagram](https://github.com/spark/ds18b20-photon/blob/verification/verified/wireDiagram.png) 24 | 2. Create a project from WEB IED or Particle DEV and then. Add the `libraryTest_DS18B20.ino` file into that project. 25 | 3. Add the `UNIT-TEST` and `DS18B20` library before compile that firmware. 26 | 4. Once compiled successfully.Flash the firmware to your particle device. 27 | 5. Make sure the device is connected to the computure and it can be recognized as a serial port on computer. 28 | 6. run `Particle serial monitor` from terminal. 29 | 7. After the serial port had been recognized, type in `t` to start the verification test. 30 | 8. Follw the test instruction and run the test. 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /examples/ds18b20_test/ds18b20_test.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | DS18B20 ds18b20(D2); //Sets Pin D2 for Water Temp Sensor 4 | int led = D7; 5 | char szInfo[64]; 6 | float pubTemp; 7 | double celsius; 8 | double fahrenheit; 9 | unsigned int Metric_Publish_Rate = 30000; 10 | unsigned int MetricnextPublishTime; 11 | int DS18B20nextSampleTime; 12 | int DS18B20_SAMPLE_INTERVAL = 2500; 13 | int dsAttempts = 0; 14 | 15 | void setup() { 16 | Time.zone(-5); 17 | Particle.syncTime(); 18 | pinMode(D2, INPUT); 19 | Particle.variable("tempHotWater", &fahrenheit, DOUBLE); 20 | Serial.begin(115200); 21 | } 22 | 23 | void loop() { 24 | 25 | if (millis() > DS18B20nextSampleTime){ 26 | getTemp(); 27 | } 28 | 29 | if (millis() > MetricnextPublishTime){ 30 | Serial.println("Publishing now."); 31 | publishData(); 32 | } 33 | } 34 | 35 | 36 | void publishData(){ 37 | if(!ds18b20.crcCheck()){ //make sure the value is correct 38 | return; 39 | } 40 | sprintf(szInfo, "%2.2f", fahrenheit); 41 | Particle.publish("dsTmp", szInfo, PRIVATE); 42 | MetricnextPublishTime = millis() + Metric_Publish_Rate; 43 | } 44 | 45 | void getTemp(){ 46 | if(!ds18b20.search()){ 47 | ds18b20.resetsearch(); 48 | celsius = ds18b20.getTemperature(); 49 | Serial.println(celsius); 50 | while (!ds18b20.crcCheck() && dsAttempts < 4){ 51 | Serial.println("Caught bad value."); 52 | dsAttempts++; 53 | Serial.print("Attempts to Read: "); 54 | Serial.println(dsAttempts); 55 | if (dsAttempts == 3){ 56 | delay(1000); 57 | } 58 | ds18b20.resetsearch(); 59 | celsius = ds18b20.getTemperature(); 60 | continue; 61 | } 62 | dsAttempts = 0; 63 | fahrenheit = ds18b20.convertToFahrenheit(celsius); 64 | DS18B20nextSampleTime = millis() + DS18B20_SAMPLE_INTERVAL; 65 | Serial.println(fahrenheit); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/DS18B20.cpp: -------------------------------------------------------------------------------- 1 | #include "DS18B20.h" 2 | 3 | 4 | DS18B20::DS18B20(uint16_t pin, bool singleDrop) 5 | : _singleDrop(singleDrop) { 6 | ds = new OneWire(pin); 7 | } 8 | 9 | boolean DS18B20::search() { 10 | boolean isSuccess = ds->search(addr); 11 | 12 | if(isSuccess){ 13 | chiptype = addr[0]; 14 | 15 | switch (addr[0]) { 16 | case 0x10: sprintf(szName, "DS18S20"); type_s = 1; break; 17 | case 0x28: sprintf(szName, "DS18B20"); type_s = 0; break; 18 | case 0x22: sprintf(szName, "DS1822" ); type_s = 0; break; 19 | default : sprintf(szName, "Unknown"); type_s = 0; break; 20 | } 21 | } 22 | return isSuccess; 23 | } 24 | 25 | DS18B20::~DS18B20() { 26 | delete(ds); 27 | } 28 | void DS18B20::resetsearch() { 29 | ds->reset_search(); 30 | } 31 | 32 | void DS18B20::setResolution(uint8_t newResolution) { 33 | ds->reset(); 34 | ds->select(addr); 35 | switch (newResolution){ 36 | case 12: 37 | ds->write(TEMP_12_BIT); 38 | break; 39 | case 11: 40 | ds->write(TEMP_11_BIT); 41 | break; 42 | case 10: 43 | ds->write(TEMP_10_BIT); 44 | break; 45 | case 9: 46 | default: 47 | ds->write(TEMP_9_BIT); 48 | break; 49 | } 50 | HAL_Delay_Milliseconds(20); 51 | ds->reset(); 52 | } 53 | 54 | bool DS18B20::readPowerSupply() { 55 | bool ret = false; 56 | ds->reset(); 57 | ds->select(addr); 58 | ds->write(READPOWERSUPPLY); 59 | if (ds->read_bit() == 0) ret = true; 60 | ds->reset(); 61 | return ret; 62 | } 63 | 64 | void DS18B20::getROM(char szROM[]) { 65 | sprintf(szROM, "%X %X %X %X %X %X %X %X", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7]); 66 | } 67 | 68 | byte DS18B20::getChipType() { 69 | return chiptype; 70 | } 71 | 72 | char* DS18B20::getChipName() { 73 | return szName; 74 | } 75 | 76 | float DS18B20::getTemperature(bool forceSelect) { 77 | ds->reset(); 78 | if (_singleDrop && !forceSelect) 79 | ds->skip(); 80 | else 81 | ds->select(addr); 82 | ds->write(0x44); // start conversion, with parasite power on at the end 83 | delay(750); // maybe 750ms is enough, maybe not 84 | // we might do a ds.depower() here, but the reset will take care of it. 85 | ds->reset(); 86 | if (_singleDrop && !forceSelect) 87 | ds->skip(); 88 | else 89 | ds->select(addr); 90 | ds->write(0xBE); // Read Scratchpad 91 | 92 | for (int i = 0; i < 9; i++) { // we need 9 bytes 93 | data[i] = ds->read(); 94 | //Serial.println("DS Read Data:"); 95 | //Serial.println(data[i], HEX); 96 | //Serial.println(" "); 97 | } 98 | _dataCRC = (OneWire::crc8(data, 8)); 99 | //Serial.println(_dataCRC, HEX); 100 | _readCRC = (data[8]); 101 | //Serial.println(_readCRC, HEX); 102 | 103 | // Convert the data to actual temperature 104 | // because the result is a 16 bit signed integer, it should 105 | // be stored to an "int16_t" type, which is always 16 bits 106 | // even when compiled on a 32 bit processor. 107 | int16_t raw = (data[1] << 8) | data[0]; 108 | 109 | if (type_s) { 110 | raw = raw << 3; // 9 bit resolution default 111 | if (data[7] == 0x10) { 112 | // "count remain" gives full 12 bit resolution 113 | raw = (raw & 0xFFF0) + 12 - data[6]; 114 | } 115 | } else { 116 | byte cfg = (data[4] & 0x60); 117 | // at lower res, the low bits are undefined, so let's zero them 118 | if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms 119 | else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms 120 | else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms 121 | //// default is 12 bit resolution, 750 ms conversion time 122 | } 123 | return (float)raw / 16.0; 124 | } 125 | 126 | float DS18B20::convertToFahrenheit(float celsius) { 127 | return celsius * 1.8 + 32.0; 128 | } 129 | 130 | bool DS18B20::crcCheck() { 131 | if (_dataCRC != _readCRC) { 132 | Serial.println("CRC Failed"); 133 | return false; 134 | } 135 | 136 | return true; 137 | } 138 | -------------------------------------------------------------------------------- /verified/libraryTest_DS18B20.ino: -------------------------------------------------------------------------------- 1 | 2 | 3 | // This #include statement was automatically added by the Particle IDE. 4 | #include "unit-test.h" 5 | // This #include statement was automatically added by the Particle IDE. 6 | #include "DS18B20.h" 7 | 8 | #define UPDATE_INTERVAL 1000 //the udpate speed for the sensor 9 | 10 | /** 11 | * Demonstrates a simple test that passes and one that fails. 12 | * Input/Output via serial. 13 | * To start the test, press 't'. 14 | * 15 | * The tests are run in alphabetical order. 16 | */ 17 | 18 | DS18B20 ds18b20 = DS18B20(D4); 19 | 20 | unsigned long updateTime=0; 21 | int c=0; 22 | double current_temperature=0; 23 | double hot_temperature=0; 24 | double cool_temperature=0; 25 | char String[20]={}; 26 | uint8_t retryTime=0; 27 | 28 | 29 | //this is the first step, which need to wire up the sensor based on the diagram from Fritzing . 30 | test(010_sensor_wireup) 31 | { 32 | c = Serial.read(); 33 | while (c!=-1) { //this is used to clear up the catch in the serial buffer. 34 | c = Serial.read(); 35 | } 36 | Serial.println("Please connect the sensor with photon/electron/core as Fritzing shows"); 37 | Serial.println("Please type 'y' when you are ready"); 38 | c = Serial.read(); 39 | while((c!='Y')&&(c!='y')) 40 | { 41 | c = Serial.read(); 42 | } 43 | assertEqual(c, 'y'); 44 | } 45 | 46 | //this step is used to read oout all the info related with sensor, such as chip type and sensor name. 47 | test(020_sensor_type) 48 | { 49 | 50 | Serial.println("Please type 'y' when the sensor type and name is correct otherwise type 'n'"); 51 | c= Serial.read(); 52 | while((c!='n')&&(c!='y')) 53 | { 54 | c = Serial.read(); 55 | if(millis()-updateTime>UPDATE_INTERVAL) //setup the interval time to make sure the sensor will print out the info every 1 second 56 | { 57 | updateTime=millis(); 58 | if(ds18b20.search()) 59 | { 60 | Serial.printf("This chip type is: %x\r\n",ds18b20.getChipType()); 61 | Serial.printf("This sensor name is: %s\r\n",ds18b20.getChipName()); 62 | ds18b20.getROM(String); 63 | // ds18b20.resetsearch(); //need to reset the search??? 64 | Serial.printf("The id is %s\r\n",String); //get the id of ds18b20 65 | } 66 | else 67 | Serial.println("There is no chip had been found"); 68 | Serial.println("Please type 'y' when the sensor type and name is correct otherwise type 'n'"); 69 | } 70 | } 71 | assertEqual(c,'y'); //a system function which is from unit-test library 72 | } 73 | 74 | //This step is used to read out the temperature from the sensor, that value should be very close to the REAL temperature. 75 | test(030_sensor_value_normal) 76 | { 77 | Serial.println("Please Check the temperature and press 'y' if it's normal temperature"); 78 | c=Serial.read(); 79 | while((c!='n')&&(c!='y')) 80 | { 81 | c=Serial.read(); 82 | if(millis()-updateTime>UPDATE_INTERVAL) 83 | { 84 | updateTime = millis(); 85 | current_temperature = ds18b20.getTemperature(); 86 | if(!ds18b20.crcCheck()&&(retryTime<4)) //make sure the value is correct by checking the crc value,assume to stop the test once it cannot read out the value correclty four times. 87 | { 88 | Serial.printf("Get a bad value.Retry %d times\r\n",retryTime+1); 89 | retryTime++; 90 | if(retryTime==4) 91 | { 92 | Serial.println("No temperature sensor is online now! Need to type 'n' to stop the test"); 93 | } 94 | } 95 | else 96 | { 97 | retryTime=0; 98 | Serial.print("Current temperature is:"); 99 | Serial.print(current_temperature); 100 | Serial.println(" celcuis"); 101 | Serial.println("Type 'y' when the temeprature is reasonable otherwise type 'n'"); 102 | } 103 | } 104 | } 105 | assertEqual(c,'y'); 106 | } 107 | 108 | //This step is used to meassure the temperature when it's close to a heat source, which means the value should be higher than the last step's value. 109 | test(040_sensor_value_hot) 110 | { 111 | Serial.println("Put the sensor close to a heat source and type 'y' when you are ready"); 112 | c=Serial.read(); 113 | while(c!='y') //waiting for the operator to start the test after attach the sensor to a heat source. 114 | { 115 | c=Serial.read(); 116 | } 117 | c=0; //clear the value. 118 | while((c!='n')&&(c!='y')) 119 | { 120 | c=Serial.read(); 121 | if(millis()-updateTime>UPDATE_INTERVAL) 122 | { 123 | updateTime = millis(); 124 | hot_temperature = ds18b20.getTemperature(); 125 | if(!ds18b20.crcCheck()&&(retryTime<4)) 126 | { 127 | Serial.printf("Get a bad value.Retry %d times\r\n",retryTime+1); 128 | retryTime++; 129 | if(retryTime==4) 130 | { 131 | Serial.println("No temperature sensor is online now! Need to stop the test"); 132 | } 133 | } 134 | else 135 | { 136 | retryTime=0; 137 | Serial.print("Current temperature is:"); 138 | Serial.print(hot_temperature); 139 | Serial.println(" celcuis"); 140 | Serial.println("Type 'y' when the temeprature is hotter than last step,otherwise type 'n'"); 141 | } 142 | } 143 | } 144 | assertMore(hot_temperature,current_temperature); 145 | } 146 | 147 | 148 | //This step is used to measure the temperature when it's close to a cool source, which means the value should be lower than the last step's value 149 | test(050_sensor_value_cool) 150 | { 151 | Serial.println("Put the sensor close to a cool source and type 'y' when you are ready"); 152 | c=Serial.read(); 153 | while(c!='y') //waiting for the operator to start the test after attach the sensor to a heat source. 154 | { 155 | c=Serial.read(); 156 | } 157 | c=0; //clear the value 158 | while((c!='n')&&(c!='y')) 159 | { 160 | c=Serial.read(); 161 | if(millis()-updateTime>UPDATE_INTERVAL) 162 | { 163 | updateTime = millis(); 164 | cool_temperature = ds18b20.getTemperature(); 165 | if(!ds18b20.crcCheck()&&(retryTime<4)) 166 | { 167 | Serial.printf("Get a bad value.Retry %d times\r\n",retryTime+1); 168 | retryTime++; 169 | if(retryTime==4) 170 | { 171 | Serial.println("No temperature sensor is online now! Need to stop the test"); 172 | } 173 | } 174 | else 175 | { 176 | retryTime=0; 177 | Serial.print("Current temperature is:"); 178 | Serial.print(cool_temperature); 179 | Serial.println(" celcuis"); 180 | Serial.println("Type 'y' when the temeprature is cooler than last step,otherwise type 'n'"); 181 | } 182 | } 183 | } 184 | assertLess(cool_temperature,current_temperature); 185 | } 186 | 187 | 188 | //This step is used to measure the temperature when there is no power on VCC pin BUT the pull-up resistor on Data pin should still apply power. 189 | test(060_sensor_parasite) 190 | { 191 | Serial.println("Remove the power from VCC pin on temperature sensor and type 'y' when you are ready"); 192 | c=Serial.read(); 193 | while(c!='y') //waiting for the operator to start the test after attach the sensor to a heat source. 194 | { 195 | c=Serial.read(); 196 | } 197 | c=0; //clear the value. 198 | while((c!='n')&&(c!='y')) 199 | { 200 | c=Serial.read(); 201 | if(millis()-updateTime>UPDATE_INTERVAL) 202 | { 203 | updateTime = millis(); 204 | current_temperature = ds18b20.getTemperature(); 205 | if(!ds18b20.crcCheck()&&(retryTime<4)) 206 | { 207 | Serial.printf("Get a bad value.Retry %d times\r\n",retryTime+1); 208 | retryTime++; 209 | if(retryTime==4) 210 | { 211 | Serial.println("No temperature sensor is online now! Need to stop the test"); 212 | } 213 | } 214 | else 215 | { 216 | retryTime=0; 217 | Serial.print("Current temperature is:"); 218 | Serial.print(current_temperature); 219 | Serial.println(" celcuis"); 220 | if(ds18b20.readPowerSupply()) 221 | Serial.println("The sensor works in parasite power mode now"); 222 | else 223 | Serial.println("The sensor isn't work in parasite power mode,please check the wire connection"); 224 | Serial.println("Type 'y' when the temeprature is normal without the power on VCC pin,otherwise type 'n'"); 225 | } 226 | } 227 | } 228 | assertEqual(c,'y'); 229 | } 230 | 231 | //This step is used to verify the funtion of cascading sensor from one wire.Make sure the Data line of all those sensors are connected to the D4 232 | test(070_sensor_cascade) 233 | { 234 | 235 | Serial.println("Put two sensor with exactly the same circuit and type `y` when you are ready"); 236 | c=Serial.read(); 237 | while(c!='y') 238 | { 239 | c=Serial.read(); 240 | } 241 | c=0; 242 | while((c!='y')&&(c!='n')) 243 | { 244 | c=Serial.read(); 245 | if(millis()-updateTime>UPDATE_INTERVAL) 246 | { 247 | updateTime=millis(); 248 | if(ds18b20.search()) //the searched value will be differenet if there are at least two sensor attaced to the same data line. 249 | { 250 | Serial.printf("This chip type is: %x\r\n",ds18b20.getChipType()); 251 | Serial.printf("This sensor name is: %s\r\n",ds18b20.getChipName()); 252 | ds18b20.getROM(String); 253 | // ds18b20.resetsearch(); //need to reset the search??? 254 | Serial.printf("The id is %s\r\n",String); 255 | } 256 | else 257 | Serial.println("There is no chip had been found"); 258 | Serial.println("Please type 'y' when you find two sensors otherwise type 'n'"); 259 | } 260 | } 261 | } 262 | 263 | UNIT_TEST_APP() 264 | --------------------------------------------------------------------------------