hellomqttt to thingspeak mqtt and ifttt
Dependencies: Servo MQTTPacket FP
LM75B/LM75B.h@25:ca1b1098c77f, 2021-05-05 (annotated)
- Committer:
- jasonberry
- Date:
- Wed May 05 14:48:01 2021 +0000
- Revision:
- 25:ca1b1098c77f
TEST
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jasonberry | 25:ca1b1098c77f | 1 | /* LM75B Driver Library |
jasonberry | 25:ca1b1098c77f | 2 | * Copyright (c) 2013 Neil Thiessen |
jasonberry | 25:ca1b1098c77f | 3 | * |
jasonberry | 25:ca1b1098c77f | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
jasonberry | 25:ca1b1098c77f | 5 | * you may not use this file except in compliance with the License. |
jasonberry | 25:ca1b1098c77f | 6 | * You may obtain a copy of the License at |
jasonberry | 25:ca1b1098c77f | 7 | * |
jasonberry | 25:ca1b1098c77f | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
jasonberry | 25:ca1b1098c77f | 9 | * |
jasonberry | 25:ca1b1098c77f | 10 | * Unless required by applicable law or agreed to in writing, software |
jasonberry | 25:ca1b1098c77f | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
jasonberry | 25:ca1b1098c77f | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
jasonberry | 25:ca1b1098c77f | 13 | * See the License for the specific language governing permissions and |
jasonberry | 25:ca1b1098c77f | 14 | * limitations under the License. |
jasonberry | 25:ca1b1098c77f | 15 | */ |
jasonberry | 25:ca1b1098c77f | 16 | |
jasonberry | 25:ca1b1098c77f | 17 | #ifndef LM75B_H |
jasonberry | 25:ca1b1098c77f | 18 | #define LM75B_H |
jasonberry | 25:ca1b1098c77f | 19 | |
jasonberry | 25:ca1b1098c77f | 20 | #include "mbed.h" |
jasonberry | 25:ca1b1098c77f | 21 | |
jasonberry | 25:ca1b1098c77f | 22 | /** LM75B class. |
jasonberry | 25:ca1b1098c77f | 23 | * Used for controlling an LM75B temperature sensor connected via I2C. |
jasonberry | 25:ca1b1098c77f | 24 | * |
jasonberry | 25:ca1b1098c77f | 25 | * Example: |
jasonberry | 25:ca1b1098c77f | 26 | * @code |
jasonberry | 25:ca1b1098c77f | 27 | * #include "mbed.h" |
jasonberry | 25:ca1b1098c77f | 28 | * #include "LM75B.h" |
jasonberry | 25:ca1b1098c77f | 29 | * |
jasonberry | 25:ca1b1098c77f | 30 | * //Create an LM75B object at the default address (ADDRESS_0) |
jasonberry | 25:ca1b1098c77f | 31 | * LM75B sensor(p28, p27); |
jasonberry | 25:ca1b1098c77f | 32 | * |
jasonberry | 25:ca1b1098c77f | 33 | * int main() |
jasonberry | 25:ca1b1098c77f | 34 | * { |
jasonberry | 25:ca1b1098c77f | 35 | * //Try to open the LM75B |
jasonberry | 25:ca1b1098c77f | 36 | * if (sensor.open()) { |
jasonberry | 25:ca1b1098c77f | 37 | * printf("Device detected!\n"); |
jasonberry | 25:ca1b1098c77f | 38 | * |
jasonberry | 25:ca1b1098c77f | 39 | * while (1) { |
jasonberry | 25:ca1b1098c77f | 40 | * //Print the current temperature |
jasonberry | 25:ca1b1098c77f | 41 | * printf("Temp = %.3f\n", (float)sensor); |
jasonberry | 25:ca1b1098c77f | 42 | * |
jasonberry | 25:ca1b1098c77f | 43 | * //Sleep for 0.5 seconds |
jasonberry | 25:ca1b1098c77f | 44 | * wait(0.5); |
jasonberry | 25:ca1b1098c77f | 45 | * } |
jasonberry | 25:ca1b1098c77f | 46 | * } else { |
jasonberry | 25:ca1b1098c77f | 47 | * error("Device not detected!\n"); |
jasonberry | 25:ca1b1098c77f | 48 | * } |
jasonberry | 25:ca1b1098c77f | 49 | * } |
jasonberry | 25:ca1b1098c77f | 50 | * @endcode |
jasonberry | 25:ca1b1098c77f | 51 | */ |
jasonberry | 25:ca1b1098c77f | 52 | class LM75B |
jasonberry | 25:ca1b1098c77f | 53 | { |
jasonberry | 25:ca1b1098c77f | 54 | public: |
jasonberry | 25:ca1b1098c77f | 55 | /** Represents the different I2C address possibilities for the LM75B |
jasonberry | 25:ca1b1098c77f | 56 | */ |
jasonberry | 25:ca1b1098c77f | 57 | enum Address { |
jasonberry | 25:ca1b1098c77f | 58 | ADDRESS_0 = (0x48 << 1), /**< A[2:0] pins = 000 */ |
jasonberry | 25:ca1b1098c77f | 59 | ADDRESS_1 = (0x49 << 1), /**< A[2:0] pins = 001 */ |
jasonberry | 25:ca1b1098c77f | 60 | ADDRESS_2 = (0x4A << 1), /**< A[2:0] pins = 010 */ |
jasonberry | 25:ca1b1098c77f | 61 | ADDRESS_3 = (0x4B << 1), /**< A[2:0] pins = 011 */ |
jasonberry | 25:ca1b1098c77f | 62 | ADDRESS_4 = (0x4C << 1), /**< A[2:0] pins = 100 */ |
jasonberry | 25:ca1b1098c77f | 63 | ADDRESS_5 = (0x4D << 1), /**< A[2:0] pins = 101 */ |
jasonberry | 25:ca1b1098c77f | 64 | ADDRESS_6 = (0x4E << 1), /**< A[2:0] pins = 110 */ |
jasonberry | 25:ca1b1098c77f | 65 | ADDRESS_7 = (0x4F << 1) /**< A[2:0] pins = 111 */ |
jasonberry | 25:ca1b1098c77f | 66 | }; |
jasonberry | 25:ca1b1098c77f | 67 | |
jasonberry | 25:ca1b1098c77f | 68 | /** Represents the power mode of the LM75B |
jasonberry | 25:ca1b1098c77f | 69 | */ |
jasonberry | 25:ca1b1098c77f | 70 | enum PowerMode { |
jasonberry | 25:ca1b1098c77f | 71 | POWER_NORMAL, /**< Chip is enabled and samples every 100ms */ |
jasonberry | 25:ca1b1098c77f | 72 | POWER_SHUTDOWN /**< Chip is in low-power shutdown mode */ |
jasonberry | 25:ca1b1098c77f | 73 | }; |
jasonberry | 25:ca1b1098c77f | 74 | |
jasonberry | 25:ca1b1098c77f | 75 | /** Represents OS pin mode of the LM75B |
jasonberry | 25:ca1b1098c77f | 76 | */ |
jasonberry | 25:ca1b1098c77f | 77 | enum OSMode { |
jasonberry | 25:ca1b1098c77f | 78 | OS_COMPARATOR, /**< OS is asserted when the temperature reaches the alert threshold, and de-asserted when the temperature drops below the alert hysteresis threshold */ |
jasonberry | 25:ca1b1098c77f | 79 | OS_INTERRUPT /**< OS is asserted when the temperature reaches the alert threshold, or drops below the alert hysteresis threshold, and only de-asserted when a register has been read */ |
jasonberry | 25:ca1b1098c77f | 80 | }; |
jasonberry | 25:ca1b1098c77f | 81 | |
jasonberry | 25:ca1b1098c77f | 82 | /** Represents OS pin polarity of the LM75B |
jasonberry | 25:ca1b1098c77f | 83 | */ |
jasonberry | 25:ca1b1098c77f | 84 | enum OSPolarity { |
jasonberry | 25:ca1b1098c77f | 85 | OS_ACTIVE_LOW, /**< OS is a logic low when asserted, and a logic high when de-asserted */ |
jasonberry | 25:ca1b1098c77f | 86 | OS_ACTIVE_HIGH /**< OS is a logic high when asserted, and a logic low when de-asserted */ |
jasonberry | 25:ca1b1098c77f | 87 | }; |
jasonberry | 25:ca1b1098c77f | 88 | |
jasonberry | 25:ca1b1098c77f | 89 | /** Represents OS pin fault queue length of the LM75B |
jasonberry | 25:ca1b1098c77f | 90 | */ |
jasonberry | 25:ca1b1098c77f | 91 | enum OSFaultQueue { |
jasonberry | 25:ca1b1098c77f | 92 | OS_FAULT_QUEUE_1, /**< OS is asserted after 1 fault */ |
jasonberry | 25:ca1b1098c77f | 93 | OS_FAULT_QUEUE_2, /**< OS is asserted after 2 consecutive faults */ |
jasonberry | 25:ca1b1098c77f | 94 | OS_FAULT_QUEUE_4, /**< OS is asserted after 4 consecutive faults */ |
jasonberry | 25:ca1b1098c77f | 95 | OS_FAULT_QUEUE_6 /**< OS is asserted after 6 consecutive faults */ |
jasonberry | 25:ca1b1098c77f | 96 | }; |
jasonberry | 25:ca1b1098c77f | 97 | |
jasonberry | 25:ca1b1098c77f | 98 | /** Create an LM75B object connected to the specified I2C pins with the specified I2C slave address |
jasonberry | 25:ca1b1098c77f | 99 | * |
jasonberry | 25:ca1b1098c77f | 100 | * @param sda The I2C data pin. |
jasonberry | 25:ca1b1098c77f | 101 | * @param scl The I2C clock pin. |
jasonberry | 25:ca1b1098c77f | 102 | * @param addr The I2C slave address (defaults to ADDRESS_0). |
jasonberry | 25:ca1b1098c77f | 103 | * @param hz The I2C bus frequency (defaults to 400kHz). |
jasonberry | 25:ca1b1098c77f | 104 | */ |
jasonberry | 25:ca1b1098c77f | 105 | LM75B(PinName sda, PinName scl, Address addr = ADDRESS_0, int hz = 400000); |
jasonberry | 25:ca1b1098c77f | 106 | |
jasonberry | 25:ca1b1098c77f | 107 | /** Probe for the LM75B and indicate if it's present on the bus |
jasonberry | 25:ca1b1098c77f | 108 | * |
jasonberry | 25:ca1b1098c77f | 109 | * @returns |
jasonberry | 25:ca1b1098c77f | 110 | * 'true' if the device exists on the bus, |
jasonberry | 25:ca1b1098c77f | 111 | * 'false' if the device doesn't exist on the bus. |
jasonberry | 25:ca1b1098c77f | 112 | */ |
jasonberry | 25:ca1b1098c77f | 113 | bool open(); |
jasonberry | 25:ca1b1098c77f | 114 | |
jasonberry | 25:ca1b1098c77f | 115 | /** Get the current power mode of the LM75B |
jasonberry | 25:ca1b1098c77f | 116 | * |
jasonberry | 25:ca1b1098c77f | 117 | * @returns The current power mode as a PowerMode enum. |
jasonberry | 25:ca1b1098c77f | 118 | */ |
jasonberry | 25:ca1b1098c77f | 119 | LM75B::PowerMode powerMode(); |
jasonberry | 25:ca1b1098c77f | 120 | |
jasonberry | 25:ca1b1098c77f | 121 | /** Set the power mode of the LM75B |
jasonberry | 25:ca1b1098c77f | 122 | * |
jasonberry | 25:ca1b1098c77f | 123 | * @param mode The new power mode as a PowerMode enum. |
jasonberry | 25:ca1b1098c77f | 124 | */ |
jasonberry | 25:ca1b1098c77f | 125 | void powerMode(PowerMode mode); |
jasonberry | 25:ca1b1098c77f | 126 | |
jasonberry | 25:ca1b1098c77f | 127 | /** Get the current OS pin mode of the LM75B |
jasonberry | 25:ca1b1098c77f | 128 | * |
jasonberry | 25:ca1b1098c77f | 129 | * @returns The current OS pin mode as an OSMode enum. |
jasonberry | 25:ca1b1098c77f | 130 | */ |
jasonberry | 25:ca1b1098c77f | 131 | LM75B::OSMode osMode(); |
jasonberry | 25:ca1b1098c77f | 132 | |
jasonberry | 25:ca1b1098c77f | 133 | /** Set the OS pin mode of the LM75B |
jasonberry | 25:ca1b1098c77f | 134 | * |
jasonberry | 25:ca1b1098c77f | 135 | * @param mode The new OS pin mode as an OSMode enum. |
jasonberry | 25:ca1b1098c77f | 136 | */ |
jasonberry | 25:ca1b1098c77f | 137 | void osMode(OSMode mode); |
jasonberry | 25:ca1b1098c77f | 138 | |
jasonberry | 25:ca1b1098c77f | 139 | /** Get the current OS pin polarity of the LM75B |
jasonberry | 25:ca1b1098c77f | 140 | * |
jasonberry | 25:ca1b1098c77f | 141 | * @returns The current OS pin polarity as an OSPolarity enum. |
jasonberry | 25:ca1b1098c77f | 142 | */ |
jasonberry | 25:ca1b1098c77f | 143 | LM75B::OSPolarity osPolarity(); |
jasonberry | 25:ca1b1098c77f | 144 | |
jasonberry | 25:ca1b1098c77f | 145 | /** Set the OS pin polarity of the LM75B |
jasonberry | 25:ca1b1098c77f | 146 | * |
jasonberry | 25:ca1b1098c77f | 147 | * @param polarity The new OS pin polarity as an OSPolarity enum. |
jasonberry | 25:ca1b1098c77f | 148 | */ |
jasonberry | 25:ca1b1098c77f | 149 | void osPolarity(OSPolarity polarity); |
jasonberry | 25:ca1b1098c77f | 150 | |
jasonberry | 25:ca1b1098c77f | 151 | /** Get the current OS pin fault queue length of the LM75B |
jasonberry | 25:ca1b1098c77f | 152 | * |
jasonberry | 25:ca1b1098c77f | 153 | * @returns The current OS pin fault queue length as an OSFaultQueue enum. |
jasonberry | 25:ca1b1098c77f | 154 | */ |
jasonberry | 25:ca1b1098c77f | 155 | LM75B::OSFaultQueue osFaultQueue(); |
jasonberry | 25:ca1b1098c77f | 156 | |
jasonberry | 25:ca1b1098c77f | 157 | /** Set the OS pin fault queue length of the LM75B |
jasonberry | 25:ca1b1098c77f | 158 | * |
jasonberry | 25:ca1b1098c77f | 159 | * @param queue The new OS pin fault queue length as an OSFaultQueue enum. |
jasonberry | 25:ca1b1098c77f | 160 | */ |
jasonberry | 25:ca1b1098c77f | 161 | void osFaultQueue(OSFaultQueue queue); |
jasonberry | 25:ca1b1098c77f | 162 | |
jasonberry | 25:ca1b1098c77f | 163 | /** Get the current alert temperature threshold of the LM75B |
jasonberry | 25:ca1b1098c77f | 164 | * |
jasonberry | 25:ca1b1098c77f | 165 | * @returns The current alert temperature threshold in °C. |
jasonberry | 25:ca1b1098c77f | 166 | */ |
jasonberry | 25:ca1b1098c77f | 167 | float alertTemp(); |
jasonberry | 25:ca1b1098c77f | 168 | |
jasonberry | 25:ca1b1098c77f | 169 | /** Set the alert temperature threshold of the LM75B |
jasonberry | 25:ca1b1098c77f | 170 | * |
jasonberry | 25:ca1b1098c77f | 171 | * @param temp The new alert temperature threshold in °C. |
jasonberry | 25:ca1b1098c77f | 172 | */ |
jasonberry | 25:ca1b1098c77f | 173 | void alertTemp(float temp); |
jasonberry | 25:ca1b1098c77f | 174 | |
jasonberry | 25:ca1b1098c77f | 175 | /** Get the current alert temperature hysteresis threshold of the LM75B |
jasonberry | 25:ca1b1098c77f | 176 | * |
jasonberry | 25:ca1b1098c77f | 177 | * @returns The current alert temperature hysteresis threshold in °C. |
jasonberry | 25:ca1b1098c77f | 178 | */ |
jasonberry | 25:ca1b1098c77f | 179 | float alertHyst(); |
jasonberry | 25:ca1b1098c77f | 180 | |
jasonberry | 25:ca1b1098c77f | 181 | /** Set the alert temperature hysteresis threshold of the LM75B |
jasonberry | 25:ca1b1098c77f | 182 | * |
jasonberry | 25:ca1b1098c77f | 183 | * @param temp The new alert temperature hysteresis threshold in °C. |
jasonberry | 25:ca1b1098c77f | 184 | */ |
jasonberry | 25:ca1b1098c77f | 185 | void alertHyst(float temp); |
jasonberry | 25:ca1b1098c77f | 186 | |
jasonberry | 25:ca1b1098c77f | 187 | /** Get the current temperature measurement of the LM75B |
jasonberry | 25:ca1b1098c77f | 188 | * |
jasonberry | 25:ca1b1098c77f | 189 | * @returns The current temperature measurement in °C. |
jasonberry | 25:ca1b1098c77f | 190 | */ |
jasonberry | 25:ca1b1098c77f | 191 | float temp(); |
jasonberry | 25:ca1b1098c77f | 192 | |
jasonberry | 25:ca1b1098c77f | 193 | #ifdef MBED_OPERATORS |
jasonberry | 25:ca1b1098c77f | 194 | /** A shorthand for temp() |
jasonberry | 25:ca1b1098c77f | 195 | * |
jasonberry | 25:ca1b1098c77f | 196 | * @returns The current temperature measurement in °C. |
jasonberry | 25:ca1b1098c77f | 197 | */ |
jasonberry | 25:ca1b1098c77f | 198 | operator float(); |
jasonberry | 25:ca1b1098c77f | 199 | #endif |
jasonberry | 25:ca1b1098c77f | 200 | |
jasonberry | 25:ca1b1098c77f | 201 | private: |
jasonberry | 25:ca1b1098c77f | 202 | //I2C register addresses |
jasonberry | 25:ca1b1098c77f | 203 | enum Register { |
jasonberry | 25:ca1b1098c77f | 204 | REG_TEMP = 0x00, |
jasonberry | 25:ca1b1098c77f | 205 | REG_CONF = 0x01, |
jasonberry | 25:ca1b1098c77f | 206 | REG_THYST = 0x02, |
jasonberry | 25:ca1b1098c77f | 207 | REG_TOS = 0x03 |
jasonberry | 25:ca1b1098c77f | 208 | }; |
jasonberry | 25:ca1b1098c77f | 209 | |
jasonberry | 25:ca1b1098c77f | 210 | //Member variables |
jasonberry | 25:ca1b1098c77f | 211 | I2C m_I2C; |
jasonberry | 25:ca1b1098c77f | 212 | const int m_ADDR; |
jasonberry | 25:ca1b1098c77f | 213 | |
jasonberry | 25:ca1b1098c77f | 214 | //Internal functions |
jasonberry | 25:ca1b1098c77f | 215 | char read8(char reg); |
jasonberry | 25:ca1b1098c77f | 216 | void write8(char reg, char data); |
jasonberry | 25:ca1b1098c77f | 217 | unsigned short read16(char reg); |
jasonberry | 25:ca1b1098c77f | 218 | void write16(char reg, unsigned short data); |
jasonberry | 25:ca1b1098c77f | 219 | float readAlertTempHelper(char reg); |
jasonberry | 25:ca1b1098c77f | 220 | void writeAlertTempHelper(char reg, float temp); |
jasonberry | 25:ca1b1098c77f | 221 | }; |
jasonberry | 25:ca1b1098c77f | 222 | |
jasonberry | 25:ca1b1098c77f | 223 | #endif |