hellomqttt to thingspeak mqtt and ifttt

Dependencies:   Servo MQTTPacket FP

Committer:
jasonberry
Date:
Wed May 05 14:48:01 2021 +0000
Revision:
25:ca1b1098c77f
TEST

Who changed what in which revision?

UserRevisionLine numberNew 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 #include "LM75B.h"
jasonberry 25:ca1b1098c77f 18
jasonberry 25:ca1b1098c77f 19 LM75B::LM75B(PinName sda, PinName scl, Address addr, int hz) : m_I2C(sda, scl), m_ADDR((int)addr)
jasonberry 25:ca1b1098c77f 20 {
jasonberry 25:ca1b1098c77f 21 //Set the I2C bus frequency
jasonberry 25:ca1b1098c77f 22 m_I2C.frequency(hz);
jasonberry 25:ca1b1098c77f 23 }
jasonberry 25:ca1b1098c77f 24
jasonberry 25:ca1b1098c77f 25 bool LM75B::open()
jasonberry 25:ca1b1098c77f 26 {
jasonberry 25:ca1b1098c77f 27 //Probe for the LM75B using a Zero Length Transfer
jasonberry 25:ca1b1098c77f 28 if (!m_I2C.write(m_ADDR, NULL, 0)) {
jasonberry 25:ca1b1098c77f 29 //Return success
jasonberry 25:ca1b1098c77f 30 return true;
jasonberry 25:ca1b1098c77f 31 } else {
jasonberry 25:ca1b1098c77f 32 //Return failure
jasonberry 25:ca1b1098c77f 33 return false;
jasonberry 25:ca1b1098c77f 34 }
jasonberry 25:ca1b1098c77f 35 }
jasonberry 25:ca1b1098c77f 36
jasonberry 25:ca1b1098c77f 37 LM75B::PowerMode LM75B::powerMode()
jasonberry 25:ca1b1098c77f 38 {
jasonberry 25:ca1b1098c77f 39 //Read the 8-bit register value
jasonberry 25:ca1b1098c77f 40 char value = read8(REG_CONF);
jasonberry 25:ca1b1098c77f 41
jasonberry 25:ca1b1098c77f 42 //Return the status of the SHUTDOWN bit
jasonberry 25:ca1b1098c77f 43 if (value & (1 << 0))
jasonberry 25:ca1b1098c77f 44 return POWER_SHUTDOWN;
jasonberry 25:ca1b1098c77f 45 else
jasonberry 25:ca1b1098c77f 46 return POWER_NORMAL;
jasonberry 25:ca1b1098c77f 47 }
jasonberry 25:ca1b1098c77f 48
jasonberry 25:ca1b1098c77f 49 void LM75B::powerMode(PowerMode mode)
jasonberry 25:ca1b1098c77f 50 {
jasonberry 25:ca1b1098c77f 51 //Read the current 8-bit register value
jasonberry 25:ca1b1098c77f 52 char value = read8(REG_CONF);
jasonberry 25:ca1b1098c77f 53
jasonberry 25:ca1b1098c77f 54 //Set or clear the SHUTDOWN bit
jasonberry 25:ca1b1098c77f 55 if (mode == POWER_SHUTDOWN)
jasonberry 25:ca1b1098c77f 56 value |= (1 << 0);
jasonberry 25:ca1b1098c77f 57 else
jasonberry 25:ca1b1098c77f 58 value &= ~(1 << 0);
jasonberry 25:ca1b1098c77f 59
jasonberry 25:ca1b1098c77f 60 //Write the value back out
jasonberry 25:ca1b1098c77f 61 write8(REG_CONF, value);
jasonberry 25:ca1b1098c77f 62 }
jasonberry 25:ca1b1098c77f 63
jasonberry 25:ca1b1098c77f 64 LM75B::OSMode LM75B::osMode()
jasonberry 25:ca1b1098c77f 65 {
jasonberry 25:ca1b1098c77f 66 //Read the 8-bit register value
jasonberry 25:ca1b1098c77f 67 char value = read8(REG_CONF);
jasonberry 25:ca1b1098c77f 68
jasonberry 25:ca1b1098c77f 69 //Return the status of the OS_COMP_INT bit
jasonberry 25:ca1b1098c77f 70 if (value & (1 << 1))
jasonberry 25:ca1b1098c77f 71 return OS_INTERRUPT;
jasonberry 25:ca1b1098c77f 72 else
jasonberry 25:ca1b1098c77f 73 return OS_COMPARATOR;
jasonberry 25:ca1b1098c77f 74 }
jasonberry 25:ca1b1098c77f 75
jasonberry 25:ca1b1098c77f 76 void LM75B::osMode(OSMode mode)
jasonberry 25:ca1b1098c77f 77 {
jasonberry 25:ca1b1098c77f 78 //Read the current 8-bit register value
jasonberry 25:ca1b1098c77f 79 char value = read8(REG_CONF);
jasonberry 25:ca1b1098c77f 80
jasonberry 25:ca1b1098c77f 81 //Set or clear the OS_COMP_INT bit
jasonberry 25:ca1b1098c77f 82 if (mode == OS_INTERRUPT)
jasonberry 25:ca1b1098c77f 83 value |= (1 << 1);
jasonberry 25:ca1b1098c77f 84 else
jasonberry 25:ca1b1098c77f 85 value &= ~(1 << 1);
jasonberry 25:ca1b1098c77f 86
jasonberry 25:ca1b1098c77f 87 //Write the value back out
jasonberry 25:ca1b1098c77f 88 write8(REG_CONF, value);
jasonberry 25:ca1b1098c77f 89 }
jasonberry 25:ca1b1098c77f 90
jasonberry 25:ca1b1098c77f 91 LM75B::OSPolarity LM75B::osPolarity()
jasonberry 25:ca1b1098c77f 92 {
jasonberry 25:ca1b1098c77f 93 //Read the 8-bit register value
jasonberry 25:ca1b1098c77f 94 char value = read8(REG_CONF);
jasonberry 25:ca1b1098c77f 95
jasonberry 25:ca1b1098c77f 96 //Return the status of the OS_POL bit
jasonberry 25:ca1b1098c77f 97 if (value & (1 << 2))
jasonberry 25:ca1b1098c77f 98 return OS_ACTIVE_HIGH;
jasonberry 25:ca1b1098c77f 99 else
jasonberry 25:ca1b1098c77f 100 return OS_ACTIVE_LOW;
jasonberry 25:ca1b1098c77f 101 }
jasonberry 25:ca1b1098c77f 102
jasonberry 25:ca1b1098c77f 103 void LM75B::osPolarity(OSPolarity polarity)
jasonberry 25:ca1b1098c77f 104 {
jasonberry 25:ca1b1098c77f 105 //Read the current 8-bit register value
jasonberry 25:ca1b1098c77f 106 char value = read8(REG_CONF);
jasonberry 25:ca1b1098c77f 107
jasonberry 25:ca1b1098c77f 108 //Set or clear the OS_POL bit
jasonberry 25:ca1b1098c77f 109 if (polarity == OS_ACTIVE_HIGH)
jasonberry 25:ca1b1098c77f 110 value |= (1 << 2);
jasonberry 25:ca1b1098c77f 111 else
jasonberry 25:ca1b1098c77f 112 value &= ~(1 << 2);
jasonberry 25:ca1b1098c77f 113
jasonberry 25:ca1b1098c77f 114 //Write the value back out
jasonberry 25:ca1b1098c77f 115 write8(REG_CONF, value);
jasonberry 25:ca1b1098c77f 116 }
jasonberry 25:ca1b1098c77f 117
jasonberry 25:ca1b1098c77f 118 LM75B::OSFaultQueue LM75B::osFaultQueue()
jasonberry 25:ca1b1098c77f 119 {
jasonberry 25:ca1b1098c77f 120 //Read the 8-bit register value
jasonberry 25:ca1b1098c77f 121 char value = read8(REG_CONF);
jasonberry 25:ca1b1098c77f 122
jasonberry 25:ca1b1098c77f 123 //Return the status of the OS_F_QUE bits
jasonberry 25:ca1b1098c77f 124 if ((value & (1 << 3)) && (value & (1 << 4)))
jasonberry 25:ca1b1098c77f 125 return OS_FAULT_QUEUE_6;
jasonberry 25:ca1b1098c77f 126 else if (!(value & (1 << 3)) && (value & (1 << 4)))
jasonberry 25:ca1b1098c77f 127 return OS_FAULT_QUEUE_4;
jasonberry 25:ca1b1098c77f 128 else if ((value & (1 << 3)) && !(value & (1 << 4)))
jasonberry 25:ca1b1098c77f 129 return OS_FAULT_QUEUE_2;
jasonberry 25:ca1b1098c77f 130 else
jasonberry 25:ca1b1098c77f 131 return OS_FAULT_QUEUE_1;
jasonberry 25:ca1b1098c77f 132 }
jasonberry 25:ca1b1098c77f 133
jasonberry 25:ca1b1098c77f 134 void LM75B::osFaultQueue(OSFaultQueue queue)
jasonberry 25:ca1b1098c77f 135 {
jasonberry 25:ca1b1098c77f 136 //Read the current 8-bit register value
jasonberry 25:ca1b1098c77f 137 char value = read8(REG_CONF);
jasonberry 25:ca1b1098c77f 138
jasonberry 25:ca1b1098c77f 139 //Clear the old OS_F_QUE bits
jasonberry 25:ca1b1098c77f 140 value &= ~(3 << 3);
jasonberry 25:ca1b1098c77f 141
jasonberry 25:ca1b1098c77f 142 //Set the new OS_F_QUE bits
jasonberry 25:ca1b1098c77f 143 if (queue == OS_FAULT_QUEUE_2)
jasonberry 25:ca1b1098c77f 144 value |= (1 << 3);
jasonberry 25:ca1b1098c77f 145 else if (queue == OS_FAULT_QUEUE_4)
jasonberry 25:ca1b1098c77f 146 value |= (2 << 3);
jasonberry 25:ca1b1098c77f 147 else if (queue == OS_FAULT_QUEUE_6)
jasonberry 25:ca1b1098c77f 148 value |= (3 << 3);
jasonberry 25:ca1b1098c77f 149
jasonberry 25:ca1b1098c77f 150 //Write the value back out
jasonberry 25:ca1b1098c77f 151 write8(REG_CONF, value);
jasonberry 25:ca1b1098c77f 152 }
jasonberry 25:ca1b1098c77f 153
jasonberry 25:ca1b1098c77f 154 float LM75B::alertTemp()
jasonberry 25:ca1b1098c77f 155 {
jasonberry 25:ca1b1098c77f 156 //Use the 9-bit helper to read the TOS register
jasonberry 25:ca1b1098c77f 157 return readAlertTempHelper(REG_TOS);
jasonberry 25:ca1b1098c77f 158 }
jasonberry 25:ca1b1098c77f 159
jasonberry 25:ca1b1098c77f 160 void LM75B::alertTemp(float temp)
jasonberry 25:ca1b1098c77f 161 {
jasonberry 25:ca1b1098c77f 162 //Use the 9-bit helper to write to the TOS register
jasonberry 25:ca1b1098c77f 163 return writeAlertTempHelper(REG_TOS, temp);
jasonberry 25:ca1b1098c77f 164 }
jasonberry 25:ca1b1098c77f 165
jasonberry 25:ca1b1098c77f 166 float LM75B::alertHyst()
jasonberry 25:ca1b1098c77f 167 {
jasonberry 25:ca1b1098c77f 168 //Use the 9-bit helper to read the THYST register
jasonberry 25:ca1b1098c77f 169 return readAlertTempHelper(REG_THYST);
jasonberry 25:ca1b1098c77f 170 }
jasonberry 25:ca1b1098c77f 171
jasonberry 25:ca1b1098c77f 172 void LM75B::alertHyst(float temp)
jasonberry 25:ca1b1098c77f 173 {
jasonberry 25:ca1b1098c77f 174 //Use the 9-bit helper to write to the THYST register
jasonberry 25:ca1b1098c77f 175 return writeAlertTempHelper(REG_THYST, temp);
jasonberry 25:ca1b1098c77f 176 }
jasonberry 25:ca1b1098c77f 177
jasonberry 25:ca1b1098c77f 178 float LM75B::temp()
jasonberry 25:ca1b1098c77f 179 {
jasonberry 25:ca1b1098c77f 180 //Signed return value
jasonberry 25:ca1b1098c77f 181 short value;
jasonberry 25:ca1b1098c77f 182
jasonberry 25:ca1b1098c77f 183 //Read the 11-bit raw temperature value
jasonberry 25:ca1b1098c77f 184 value = read16(REG_TEMP) >> 5;
jasonberry 25:ca1b1098c77f 185
jasonberry 25:ca1b1098c77f 186 //Sign extend negative numbers
jasonberry 25:ca1b1098c77f 187 if (value & (1 << 10))
jasonberry 25:ca1b1098c77f 188 value |= 0xFC00;
jasonberry 25:ca1b1098c77f 189
jasonberry 25:ca1b1098c77f 190 //Return the temperature in °C
jasonberry 25:ca1b1098c77f 191 return value * 0.125;
jasonberry 25:ca1b1098c77f 192 }
jasonberry 25:ca1b1098c77f 193
jasonberry 25:ca1b1098c77f 194 #ifdef MBED_OPERATORS
jasonberry 25:ca1b1098c77f 195 LM75B::operator float()
jasonberry 25:ca1b1098c77f 196 {
jasonberry 25:ca1b1098c77f 197 //Return the current temperature reading
jasonberry 25:ca1b1098c77f 198 return temp();
jasonberry 25:ca1b1098c77f 199 }
jasonberry 25:ca1b1098c77f 200 #endif
jasonberry 25:ca1b1098c77f 201
jasonberry 25:ca1b1098c77f 202 char LM75B::read8(char reg)
jasonberry 25:ca1b1098c77f 203 {
jasonberry 25:ca1b1098c77f 204 //Select the register
jasonberry 25:ca1b1098c77f 205 m_I2C.write(m_ADDR, &reg, 1, true);
jasonberry 25:ca1b1098c77f 206
jasonberry 25:ca1b1098c77f 207 //Read the 8-bit register
jasonberry 25:ca1b1098c77f 208 m_I2C.read(m_ADDR, &reg, 1);
jasonberry 25:ca1b1098c77f 209
jasonberry 25:ca1b1098c77f 210 //Return the byte
jasonberry 25:ca1b1098c77f 211 return reg;
jasonberry 25:ca1b1098c77f 212 }
jasonberry 25:ca1b1098c77f 213
jasonberry 25:ca1b1098c77f 214 void LM75B::write8(char reg, char data)
jasonberry 25:ca1b1098c77f 215 {
jasonberry 25:ca1b1098c77f 216 //Create a temporary buffer
jasonberry 25:ca1b1098c77f 217 char buff[2];
jasonberry 25:ca1b1098c77f 218
jasonberry 25:ca1b1098c77f 219 //Load the register address and 8-bit data
jasonberry 25:ca1b1098c77f 220 buff[0] = reg;
jasonberry 25:ca1b1098c77f 221 buff[1] = data;
jasonberry 25:ca1b1098c77f 222
jasonberry 25:ca1b1098c77f 223 //Write the data
jasonberry 25:ca1b1098c77f 224 m_I2C.write(m_ADDR, buff, 2);
jasonberry 25:ca1b1098c77f 225 }
jasonberry 25:ca1b1098c77f 226
jasonberry 25:ca1b1098c77f 227 unsigned short LM75B::read16(char reg)
jasonberry 25:ca1b1098c77f 228 {
jasonberry 25:ca1b1098c77f 229 //Create a temporary buffer
jasonberry 25:ca1b1098c77f 230 char buff[2];
jasonberry 25:ca1b1098c77f 231
jasonberry 25:ca1b1098c77f 232 //Select the register
jasonberry 25:ca1b1098c77f 233 m_I2C.write(m_ADDR, &reg, 1, true);
jasonberry 25:ca1b1098c77f 234
jasonberry 25:ca1b1098c77f 235 //Read the 16-bit register
jasonberry 25:ca1b1098c77f 236 m_I2C.read(m_ADDR, buff, 2);
jasonberry 25:ca1b1098c77f 237
jasonberry 25:ca1b1098c77f 238 //Return the combined 16-bit value
jasonberry 25:ca1b1098c77f 239 return (buff[0] << 8) | buff[1];
jasonberry 25:ca1b1098c77f 240 }
jasonberry 25:ca1b1098c77f 241
jasonberry 25:ca1b1098c77f 242 void LM75B::write16(char reg, unsigned short data)
jasonberry 25:ca1b1098c77f 243 {
jasonberry 25:ca1b1098c77f 244 //Create a temporary buffer
jasonberry 25:ca1b1098c77f 245 char buff[3];
jasonberry 25:ca1b1098c77f 246
jasonberry 25:ca1b1098c77f 247 //Load the register address and 16-bit data
jasonberry 25:ca1b1098c77f 248 buff[0] = reg;
jasonberry 25:ca1b1098c77f 249 buff[1] = data >> 8;
jasonberry 25:ca1b1098c77f 250 buff[2] = data;
jasonberry 25:ca1b1098c77f 251
jasonberry 25:ca1b1098c77f 252 //Write the data
jasonberry 25:ca1b1098c77f 253 m_I2C.write(m_ADDR, buff, 3);
jasonberry 25:ca1b1098c77f 254 }
jasonberry 25:ca1b1098c77f 255
jasonberry 25:ca1b1098c77f 256 float LM75B::readAlertTempHelper(char reg)
jasonberry 25:ca1b1098c77f 257 {
jasonberry 25:ca1b1098c77f 258 //Signed return value
jasonberry 25:ca1b1098c77f 259 short value;
jasonberry 25:ca1b1098c77f 260
jasonberry 25:ca1b1098c77f 261 //Read the 9-bit raw temperature value
jasonberry 25:ca1b1098c77f 262 value = read16(reg) >> 7;
jasonberry 25:ca1b1098c77f 263
jasonberry 25:ca1b1098c77f 264 //Sign extend negative numbers
jasonberry 25:ca1b1098c77f 265 if (value & (1 << 8))
jasonberry 25:ca1b1098c77f 266 value |= 0xFF00;
jasonberry 25:ca1b1098c77f 267
jasonberry 25:ca1b1098c77f 268 //Return the temperature in °C
jasonberry 25:ca1b1098c77f 269 return value * 0.5;
jasonberry 25:ca1b1098c77f 270 }
jasonberry 25:ca1b1098c77f 271
jasonberry 25:ca1b1098c77f 272 void LM75B::writeAlertTempHelper(char reg, float temp)
jasonberry 25:ca1b1098c77f 273 {
jasonberry 25:ca1b1098c77f 274 //Range limit temp
jasonberry 25:ca1b1098c77f 275 if (temp < -55.0)
jasonberry 25:ca1b1098c77f 276 temp = -55.0;
jasonberry 25:ca1b1098c77f 277 else if (temp > 125.0)
jasonberry 25:ca1b1098c77f 278 temp = 125.0;
jasonberry 25:ca1b1098c77f 279
jasonberry 25:ca1b1098c77f 280 //Extract and shift the signed integer
jasonberry 25:ca1b1098c77f 281 short value = temp * 2;
jasonberry 25:ca1b1098c77f 282 value <<= 7;
jasonberry 25:ca1b1098c77f 283
jasonberry 25:ca1b1098c77f 284 //Send the new value
jasonberry 25:ca1b1098c77f 285 write16(reg, value);
jasonberry 25:ca1b1098c77f 286 }