Difficulty: Intermediate
Estimated Time: 30 minutes
Hardware Needed: Muon
This blueprint shows how to use a Device to Cloud Ledger with the Muon's onboard TMP112A I2C temperature sensor.
- Particle Muon
- Particle Workbench or Particle CLI
- A Particle account with a claimed device
-
Clone this repository:
git clone https://github.com/particle-iot/blueprint-intermediate-temperature-ledger.git cd blueprint-intermediate-temperature-ledger -
Open the project in Particle Workbench or your preferred editor.
-
Flash to your device:
-
Open a serial terminal:
-
Observe output in your terminal. You’ll see a timestamped line like:
0000010545 [app] INFO: Temperature: 26.88 C / 80.38 F -
Make a Device to Cloud Ledger instance named
temperature: -
Get Ledger instance for the device:
0000010246 [system.ledger] INFO: Requesting ledger info
0000010499 [system.ledger] INFO: Received ledger info
0000010545 [app] INFO: Temperature: 26.88 C / 80.38 F
0000071021 [app] INFO: Temperature: 26.94 C / 80.49 F
0000131493 [app] INFO: Temperature: 26.88 C / 80.38 F
The Muon's onboard I2C TMP112A temperature sensor is initialized and read by the following two function definitions:
void initializeTemperature()
{
Wire.begin();
Wire.beginTransmission(TMP112A_ADDR);
// Select configuration register
Wire.write(0x01);
// Continuous conversion, comparator mode, 12-bit resolution
Wire.write(0x60);
Wire.write(0xA0);
// Stop I2C Transmission
Wire.endTransmission();
delay(300);
}
void readTemperature(TemperatureReading *reading)
{
unsigned data[2] = {0, 0};
// Start I2C Transmission
Wire.beginTransmission(TMP112A_ADDR);
// Select data register
Wire.write(0x00);
// Stop I2C Transmission
Wire.endTransmission();
delay(300);
// Request 2 bytes of data
Wire.requestFrom(TMP112A_ADDR, 2);
// Read 2 bytes of data
// temp msb, temp lsb
if (Wire.available() == 2)
{
data[0] = Wire.read();
data[1] = Wire.read();
}
// Convert the data to 12-bits
int temp = ((data[0] * 256) + data[1]) / 16;
if (temp > 2048)
{
temp -= 4096;
}
float cTemp = temp * 0.0625;
float fTemp = cTemp * 1.8 + 32;
reading->degreesC = cTemp;
reading->degreesF = fTemp;
}The temperature Ledger is assigned in setup:
// setup() runs once, when the device is first turned on
void setup()
{
// Put initialization like pinMode and begin functions here
temperatureLedger = Particle.ledger("temperature");
}If the device is connected to the Particle cloud, and the predetermined period has elapsed, then the temperature Ledger is updated with tempC and tempF:
void loop()
{
if (Particle.connected())
{
if ((lastPublish == 0) || (millis() - lastPublish >= publishPeriod.count()))
{
readTemperature(&tempReading);
Log.info("Temperature: %.2f C / %.2f F", tempReading.degreesC, tempReading.degreesF);
Variant data;
data.set("tempF", tempReading.degreesF);
data.set("tempC", tempReading.degreesC);
if (Time.isValid())
{
data.set("time", Time.format(TIME_FORMAT_ISO8601_FULL)); // Time.format returns a String
}
temperatureLedger.set(data);
lastPublish = millis();
}
}
}Try creating a rolling window of 10 temperature samples.









