Automatic/Manual lights+AC connected over Wifi

Dependents:   ACandLightsandWifi ACandLightsandWifi

Fork of DS1820 by Michael Hagberg

Committer:
jzacaroli
Date:
Fri Jun 17 12:38:22 2016 +0000
Revision:
3:cf4f0ea711a7
Parent:
2:ee820a991b95
AC + Lights + Wifi

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Michael_ 2:ee820a991b95 1 /* mbed DS1820 Library, for the Dallas (Maxim) 1-Wire Digital Thermometer
Michael_ 2:ee820a991b95 2 * Copyright (c) 2010, Michael Hagberg Michael@RedBoxCode.com
Michael_ 2:ee820a991b95 3 *
Michael_ 2:ee820a991b95 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
Michael_ 2:ee820a991b95 5 * of this software and associated documentation files (the "Software"), to deal
Michael_ 2:ee820a991b95 6 * in the Software without restriction, including without limitation the rights
Michael_ 2:ee820a991b95 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Michael_ 2:ee820a991b95 8 * copies of the Software, and to permit persons to whom the Software is
Michael_ 2:ee820a991b95 9 * furnished to do so, subject to the following conditions:
Michael_ 2:ee820a991b95 10 *
Michael_ 2:ee820a991b95 11 * The above copyright notice and this permission notice shall be included in
Michael_ 2:ee820a991b95 12 * all copies or substantial portions of the Software.
Michael_ 2:ee820a991b95 13 *
Michael_ 2:ee820a991b95 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Michael_ 2:ee820a991b95 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Michael_ 2:ee820a991b95 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Michael_ 2:ee820a991b95 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
jzacaroli 3:cf4f0ea711a7 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FRom,
Michael_ 2:ee820a991b95 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Michael_ 2:ee820a991b95 20 * THE SOFTWARE.
Michael_ 2:ee820a991b95 21 */
Michael_ 2:ee820a991b95 22
Michael_ 2:ee820a991b95 23 #ifndef MBED_DS1820_H
Michael_ 2:ee820a991b95 24 #define MBED_DS1820_H
Michael_ 2:ee820a991b95 25
Michael_ 2:ee820a991b95 26 #include "mbed.h"
Michael_ 2:ee820a991b95 27
Michael_ 2:ee820a991b95 28 // ****** THIS GLOBAL VARIABLES MUST BE DEFINED IN main.cpp
Michael_ 2:ee820a991b95 29
Michael_ 2:ee820a991b95 30 // Global variables shared between all DS1820 objects
Michael_ 2:ee820a991b95 31 //bool DS1820_done_flag;
Michael_ 2:ee820a991b95 32 //int DS1820_last_descrepancy;
jzacaroli 3:cf4f0ea711a7 33 //char DS1820_search_RomM[8];
Michael_ 2:ee820a991b95 34
Michael_ 2:ee820a991b95 35 /** DS1820 Dallas 1-Wire Temperature Probe
Michael_ 2:ee820a991b95 36 *
Michael_ 2:ee820a991b95 37 * Example:
Michael_ 2:ee820a991b95 38 * @code
Michael_ 2:ee820a991b95 39 * #include "mbed.h"
Michael_ 2:ee820a991b95 40 *
Michael_ 2:ee820a991b95 41 * #include "TextLCD.h"
Michael_ 2:ee820a991b95 42 * #include "DS1820.h"
Michael_ 2:ee820a991b95 43 *
Michael_ 2:ee820a991b95 44 * TextLCD lcd(p25, p26, p21, p22, p23, p24, TextLCD::LCD16x2); // rs, e, d0-d3, layout
Michael_ 2:ee820a991b95 45 *
Michael_ 2:ee820a991b95 46 * const int MAX_PROBES = 16;
Michael_ 2:ee820a991b95 47 * DS1820* probe[MAX_PROBES];
Michael_ 2:ee820a991b95 48 *
Michael_ 2:ee820a991b95 49 * int main() {
Michael_ 2:ee820a991b95 50 * int i;
Michael_ 2:ee820a991b95 51 * int devices_found=0;
Michael_ 2:ee820a991b95 52 * // Initialize the probe array to DS1820 objects
Michael_ 2:ee820a991b95 53 * for (i = 0; i < MAX_PROBES; i++)
Michael_ 2:ee820a991b95 54 * probe[i] = new DS1820(p27);
Michael_ 2:ee820a991b95 55 * // Initialize global state variables
jzacaroli 3:cf4f0ea711a7 56 * probe[0]->search_RomMM_setup();
Michael_ 2:ee820a991b95 57 * // Loop to find all devices on the data line
jzacaroli 3:cf4f0ea711a7 58 * while (probe[devices_found]->search_RomM() and devices_found<MAX_PROBES-1)
Michael_ 2:ee820a991b95 59 * devices_found++;
jzacaroli 3:cf4f0ea711a7 60 * // If maximum number of probes are found,
Michael_ 2:ee820a991b95 61 * // bump the counter to include the last array entry
jzacaroli 3:cf4f0ea711a7 62 * if (probe[devices_found]->RomMMMM[0] != 0xFF)
Michael_ 2:ee820a991b95 63 * devices_found++;
jzacaroli 3:cf4f0ea711a7 64 *
Michael_ 2:ee820a991b95 65 * lcd.cls();
Michael_ 2:ee820a991b95 66 * if (devices_found==0)
Michael_ 2:ee820a991b95 67 * lcd.printf("No devices found");
Michael_ 2:ee820a991b95 68 * else {
Michael_ 2:ee820a991b95 69 * while (true) {
Michael_ 2:ee820a991b95 70 * probe[0]->convert_temperature(DS1820::all_devices);
Michael_ 2:ee820a991b95 71 * lcd.cls();
Michael_ 2:ee820a991b95 72 * for (i=0; i<devices_found; i++) {
Michael_ 2:ee820a991b95 73 * lcd.printf("%3.1f ",probe[i]->temperature('f'));
Michael_ 2:ee820a991b95 74 * }
Michael_ 2:ee820a991b95 75 * }
Michael_ 2:ee820a991b95 76 * }
Michael_ 2:ee820a991b95 77 * }
Michael_ 2:ee820a991b95 78 * @endcode
Michael_ 2:ee820a991b95 79 */
jzacaroli 3:cf4f0ea711a7 80
jzacaroli 3:cf4f0ea711a7 81 class DS1820
jzacaroli 3:cf4f0ea711a7 82 {
Michael_ 2:ee820a991b95 83 public:
jzacaroli 3:cf4f0ea711a7 84 enum devices {
Michael_ 2:ee820a991b95 85 this_device, // command applies to only this device
jzacaroli 3:cf4f0ea711a7 86 all_devices
jzacaroli 3:cf4f0ea711a7 87 }; // command applies to all devices
Michael_ 2:ee820a991b95 88
Michael_ 2:ee820a991b95 89 /** Create a probe object connected to the specified pins
Michael_ 2:ee820a991b95 90 *
Michael_ 2:ee820a991b95 91 * @param data_pin DigitalInOut pin for the data bus
Michael_ 2:ee820a991b95 92 * @param power_pin DigitalOut pin to control the power MOSFET
Michael_ 2:ee820a991b95 93 */
Michael_ 2:ee820a991b95 94 DS1820(PinName data_pin, PinName power_pin); // Constructor with parasite power pin
Michael_ 2:ee820a991b95 95
Michael_ 2:ee820a991b95 96 /** Create a probe object connected to the specified pin
Michael_ 2:ee820a991b95 97 * this is used when all probes are externally powered
Michael_ 2:ee820a991b95 98 *
Michael_ 2:ee820a991b95 99 * @param data_pin DigitalInOut pin for the data bus
Michael_ 2:ee820a991b95 100 */
Michael_ 2:ee820a991b95 101 DS1820(PinName data_pin);
Michael_ 2:ee820a991b95 102
jzacaroli 3:cf4f0ea711a7 103 /** RomMMM is a copy of the internal DS1820's Rom
jzacaroli 3:cf4f0ea711a7 104 * It is created during the search_RomM() or search_alarm() commands
Michael_ 2:ee820a991b95 105 *
jzacaroli 3:cf4f0ea711a7 106 * RomMMM[0] is the Dallas Family Code
jzacaroli 3:cf4f0ea711a7 107 * RomMMM[1] thru RomMMM[6] is the 48-bit unique serial number
jzacaroli 3:cf4f0ea711a7 108 * RomMMM[7] is the device CRC
Michael_ 2:ee820a991b95 109 */
jzacaroli 3:cf4f0ea711a7 110 char Rom[8];
jzacaroli 3:cf4f0ea711a7 111 #define FAMILY_CODE Rom[0]
jzacaroli 3:cf4f0ea711a7 112 #define FAMILY_CODE_DS1820 0x10
jzacaroli 3:cf4f0ea711a7 113 #define FAMILY_CODE_DS18S20 0x10
jzacaroli 3:cf4f0ea711a7 114 #define FAMILY_CODE_DS18B20 0x28
jzacaroli 3:cf4f0ea711a7 115
Michael_ 2:ee820a991b95 116 /** RAM is a copy of the internal DS1820's RAM
Michael_ 2:ee820a991b95 117 * It's updated during the read_RAM() command
jzacaroli 3:cf4f0ea711a7 118 * which is automaticaly called fRom any function
Michael_ 2:ee820a991b95 119 * using the RAM values.
Michael_ 2:ee820a991b95 120 */
Michael_ 2:ee820a991b95 121 char RAM[9];
jzacaroli 3:cf4f0ea711a7 122
Michael_ 2:ee820a991b95 123 /* This function copies the DS1820's RAM into the object's
Michael_ 2:ee820a991b95 124 * RAM[].
Michael_ 2:ee820a991b95 125 */
Michael_ 2:ee820a991b95 126 void read_RAM();
Michael_ 2:ee820a991b95 127
Michael_ 2:ee820a991b95 128 /** This routine initializes the global variables used in
jzacaroli 3:cf4f0ea711a7 129 * the search_Rom() and search_alarm() funtions. It should
Michael_ 2:ee820a991b95 130 * be called once before looping to find devices.
Michael_ 2:ee820a991b95 131 */
jzacaroli 3:cf4f0ea711a7 132 void search_Rom_setup();
Michael_ 2:ee820a991b95 133
Michael_ 2:ee820a991b95 134 /** This routine will search for an unidentified device
jzacaroli 3:cf4f0ea711a7 135 * on the bus. It uses the variables in search_RomMMMM_setup
jzacaroli 3:cf4f0ea711a7 136 * to remember the pervious RomMMMM address found.
Michael_ 2:ee820a991b95 137 * It will return FALSE if there were no new devices
Michael_ 2:ee820a991b95 138 * discovered on the bus.
Michael_ 2:ee820a991b95 139 */
jzacaroli 3:cf4f0ea711a7 140 bool search_Rom();
Michael_ 2:ee820a991b95 141
Michael_ 2:ee820a991b95 142 /** This routine will search for an unidentified device
jzacaroli 3:cf4f0ea711a7 143 * which has the temperature alarm bit set. It uses the
jzacaroli 3:cf4f0ea711a7 144 * variables in search_RomMMM_setup to remember the pervious
jzacaroli 3:cf4f0ea711a7 145 * RomMMM address found. It will return FALSE if there were
Michael_ 2:ee820a991b95 146 * no new devices with alarms discovered on the bus.
Michael_ 2:ee820a991b95 147 */
Michael_ 2:ee820a991b95 148 bool search_alarm();
Michael_ 2:ee820a991b95 149
jzacaroli 3:cf4f0ea711a7 150 /** This routine will read the RomMMMM (Family code, serial number
jzacaroli 3:cf4f0ea711a7 151 * and Checksum) fRomM a dedicated device on the bus.
Michael_ 2:ee820a991b95 152 *
jzacaroli 3:cf4f0ea711a7 153 * NOTE: This command can only be used when there is only one
jzacaroli 3:cf4f0ea711a7 154 * DS1820 on the bus. If this command is used when there
jzacaroli 3:cf4f0ea711a7 155 * is more than one slave present on the bus, a data
jzacaroli 3:cf4f0ea711a7 156 * collision will occur when all the DS1820s attempt to
Michael_ 2:ee820a991b95 157 * respond at the same time.
Michael_ 2:ee820a991b95 158 */
jzacaroli 3:cf4f0ea711a7 159 void read_Rom();
Michael_ 2:ee820a991b95 160
Michael_ 2:ee820a991b95 161 /** This routine will initiate the temperature conversion within
jzacaroli 3:cf4f0ea711a7 162 * a DS1820. There is a built in 750ms delay to allow the
Michael_ 2:ee820a991b95 163 * conversion to complete.
Michael_ 2:ee820a991b95 164 *
Michael_ 2:ee820a991b95 165 * To update all probes on the bus, use a statement such as this:
Michael_ 2:ee820a991b95 166 * probe[0]->convert_temperature(DS1820::all_devices);
Michael_ 2:ee820a991b95 167 *
Michael_ 2:ee820a991b95 168 * @param allows the fnction to apply to a specific device or
Michael_ 2:ee820a991b95 169 * to all devices on the 1-Wire bus.
Michael_ 2:ee820a991b95 170 */
Michael_ 2:ee820a991b95 171 void convert_temperature(devices device=this_device);
Michael_ 2:ee820a991b95 172
Michael_ 2:ee820a991b95 173 /** This function will return the probe temperature. This function
Michael_ 2:ee820a991b95 174 * uses the count remainding values to interpolate the temperature
Michael_ 2:ee820a991b95 175 * to about 1/150th of a degree. Whereas the probe is not spec to
Michael_ 2:ee820a991b95 176 * that precision. It does seem to give a smooth reading to the
Michael_ 2:ee820a991b95 177 * tenth of a degree.
Michael_ 2:ee820a991b95 178 *
Michael_ 2:ee820a991b95 179 * @param scale, may be either 'c' or 'f'
Michael_ 2:ee820a991b95 180 * @returns temperature for that scale
Michael_ 2:ee820a991b95 181 */
Michael_ 2:ee820a991b95 182 float temperature(char scale='c');
jzacaroli 3:cf4f0ea711a7 183
jzacaroli 3:cf4f0ea711a7 184 /** This function calculates the Rom checksum and compares it to the
jzacaroli 3:cf4f0ea711a7 185 * CRC value stored in RomMMM[7].
Michael_ 2:ee820a991b95 186 *
Michael_ 2:ee820a991b95 187 * @returns true if the checksum matches, otherwise false.
Michael_ 2:ee820a991b95 188 */
jzacaroli 3:cf4f0ea711a7 189 bool Rom_checksum_error();
Michael_ 2:ee820a991b95 190
Michael_ 2:ee820a991b95 191 /** This function calculates the RAM checksum and compares it to the
Michael_ 2:ee820a991b95 192 * CRC value stored in RAM[8].
Michael_ 2:ee820a991b95 193 *
Michael_ 2:ee820a991b95 194 * @returns true if the checksum matches, otherwise false.
Michael_ 2:ee820a991b95 195 */
Michael_ 2:ee820a991b95 196 bool RAM_checksum_error();
Michael_ 2:ee820a991b95 197
Michael_ 2:ee820a991b95 198 /** This function returns the values stored in the temperature
jzacaroli 3:cf4f0ea711a7 199 * alarm registers.
Michael_ 2:ee820a991b95 200 *
Michael_ 2:ee820a991b95 201 * @returns a 16 bit integer of TH (upper byte) and TL (lower byte).
Michael_ 2:ee820a991b95 202 */
Michael_ 2:ee820a991b95 203 bool set_configuration_bits(unsigned int resolution);
jzacaroli 3:cf4f0ea711a7 204
Michael_ 2:ee820a991b95 205 /** This function sets the temperature resolution for the DS18B20
Michael_ 2:ee820a991b95 206 * in the configuration register.
Michael_ 2:ee820a991b95 207 *
Michael_ 2:ee820a991b95 208 * @param a number between 9 and 12 to specify the resolution
Michael_ 2:ee820a991b95 209 * @returns true if successful
jzacaroli 3:cf4f0ea711a7 210 */
Michael_ 2:ee820a991b95 211 int read_scratchpad();
jzacaroli 3:cf4f0ea711a7 212
Michael_ 2:ee820a991b95 213 /** This function will store the passed data into the DS1820's RAM.
jzacaroli 3:cf4f0ea711a7 214 * Note: It does NOT save the data to the EEPRomMMM for retention
Michael_ 2:ee820a991b95 215 * during cycling the power off and on.
Michael_ 2:ee820a991b95 216 *
Michael_ 2:ee820a991b95 217 * @param a 16 bit integer of TH (upper byte) and TL (lower byte).
jzacaroli 3:cf4f0ea711a7 218 */
Michael_ 2:ee820a991b95 219 void write_scratchpad(int data);
jzacaroli 3:cf4f0ea711a7 220
jzacaroli 3:cf4f0ea711a7 221 /** This function will transfer the TH and TL registers fRomM the
jzacaroli 3:cf4f0ea711a7 222 * DS1820's RAM into the EEPRomMMMM.
Michael_ 2:ee820a991b95 223 * Note: There is a built in 10ms delay to allow for the
jzacaroli 3:cf4f0ea711a7 224 * completion of the EEPRomMMM write cycle.
Michael_ 2:ee820a991b95 225 *
Michael_ 2:ee820a991b95 226 * @param allows the fnction to apply to a specific device or
Michael_ 2:ee820a991b95 227 * to all devices on the 1-Wire bus.
jzacaroli 3:cf4f0ea711a7 228 */
Michael_ 2:ee820a991b95 229 void store_scratchpad(devices device=this_device);
Michael_ 2:ee820a991b95 230
jzacaroli 3:cf4f0ea711a7 231 /** This function will copy the stored values fRomMMM the EEPRomM
Michael_ 2:ee820a991b95 232 * into the DS1820's RAM locations for TH and TL.
Michael_ 2:ee820a991b95 233 *
Michael_ 2:ee820a991b95 234 * @param allows the function to apply to a specific device or
Michael_ 2:ee820a991b95 235 * to all devices on the 1-Wire bus.
Michael_ 2:ee820a991b95 236 */
Michael_ 2:ee820a991b95 237 int recall_scratchpad(devices device=this_device);
Michael_ 2:ee820a991b95 238
Michael_ 2:ee820a991b95 239 /** This function will return the type of power supply for
Michael_ 2:ee820a991b95 240 * a specific device. It can also be used to query all devices
Michael_ 2:ee820a991b95 241 * looking for any device that is parasite powered.
Michael_ 2:ee820a991b95 242 *
Michael_ 2:ee820a991b95 243 * @returns true if the device (or all devices) are Vcc powered,
Michael_ 2:ee820a991b95 244 * returns false if the device (or ANY device) is parasite powered.
Michael_ 2:ee820a991b95 245 */
Michael_ 2:ee820a991b95 246 bool read_power_supply(devices device=this_device);
Michael_ 2:ee820a991b95 247
Michael_ 2:ee820a991b95 248 private:
Michael_ 2:ee820a991b95 249 bool _parasite_power;
Michael_ 2:ee820a991b95 250 char CRC_byte (char CRC, char byte );
Michael_ 2:ee820a991b95 251 bool onewire_reset();
jzacaroli 3:cf4f0ea711a7 252 void match_Rom();
jzacaroli 3:cf4f0ea711a7 253 void skip_Rom();
jzacaroli 3:cf4f0ea711a7 254 bool search_Rom_routine(char command);
Michael_ 2:ee820a991b95 255 void onewire_bit_out (bool bit_data);
Michael_ 2:ee820a991b95 256 void onewire_byte_out(char data);
Michael_ 2:ee820a991b95 257 bool onewire_bit_in();
Michael_ 2:ee820a991b95 258 char onewire_byte_in();
Michael_ 2:ee820a991b95 259
Michael_ 2:ee820a991b95 260 protected:
Michael_ 2:ee820a991b95 261 DigitalInOut _datapin;
Michael_ 2:ee820a991b95 262 DigitalOut _parasitepin;
Michael_ 2:ee820a991b95 263 };
Michael_ 2:ee820a991b95 264
Michael_ 2:ee820a991b95 265
Michael_ 2:ee820a991b95 266 #endif