Skip to content

This blueprint shows how to measure the Muon's temperature sensor and log a rolling window of readings using Particle Ledger.

License

Notifications You must be signed in to change notification settings

particle-iot/blueprint-intermediate-temperature-ledger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Blueprint - Intermediate - Temperature Device to Cloud Ledger

Difficulty: Intermediate

Estimated Time: 30 minutes

Hardware Needed: Muon


Overview

This blueprint shows how to use a Device to Cloud Ledger with the Muon's onboard TMP112A I2C temperature sensor.


Tools & Materials


Steps

  1. Clone this repository:

    git clone https://github.com/particle-iot/blueprint-intermediate-temperature-ledger.git
    cd blueprint-intermediate-temperature-ledger
  2. Open the project in Particle Workbench or your preferred editor.

  3. Flash to your device:

    1. Configure project for your device using Particle Workbench and the command pallette (cmd / ctrl + shift + p): Configure project for device
    2. Select your device model and Device OS release: Select device Select device OS
  4. Open a serial terminal:

    1. Open a serial monitor session by choosing Particle: Serial monitor from the command pallette: Serial monitor
  5. Observe output in your terminal. You’ll see a timestamped line like:

    0000010545 [app] INFO: Temperature: 26.88 C / 80.38 F
    
  6. Make a Device to Cloud Ledger instance named temperature:

    1. Navigate to the Ledger cloud service: Navigate to Ledger cloud services
    2. Create a new Ledger: Create new Ledger
    3. Choose Device to Cloud Ledger: Device to Cloud
    4. Give the new Ledger a name and a description: Ledger name and description
  7. Get Ledger instance for the device:

    1. Back in the Particle console, navigate to your temperature Ledger and choose "Get Instance": Get Ledger instance Ledger instance

Expected Output

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

How It Works

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();
    }
  }
}

Topics Covered


Extensions

Try creating a rolling window of 10 temperature samples.

About

This blueprint shows how to measure the Muon's temperature sensor and log a rolling window of readings using Particle Ledger.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages