Skip to content

Instantly share code, notes, and snippets.

@mschuetz
Created March 5, 2017 12:58
Show Gist options
  • Save mschuetz/f03e452846e7b70e71ea1108ccdbc647 to your computer and use it in GitHub Desktop.
Save mschuetz/f03e452846e7b70e71ea1108ccdbc647 to your computer and use it in GitHub Desktop.
#include <Wire.h>
#include "Adafruit_TMP007.h"
#include "esp_system.h"
#include "nvs_flash.h"
#include "nvs.h"
Adafruit_TMP007 tmp007;
nvs_handle nvs;
esp_err_t err;
void setup() {
Serial.begin(9600);
Serial.println("Adafruit TMP007 example");
// you can also use tmp007.begin(TMP007_CFG_1SAMPLE) or 2SAMPLE/4SAMPLE/8SAMPLE to have
// lower precision, higher rate sampling. default is TMP007_CFG_16SAMPLE which takes
// 4 seconds per reading (16 samples)
if (!tmp007.begin(TMP007_CFG_4SAMPLE)) {
Serial.println("No sensor found");
while (1);
}
nvs_flash_init();
Serial.println("Opening Non-Volatile Storage (NVS) ... ");
err = nvs_open("irtemp", NVS_READWRITE, &nvs);
if (err != ESP_OK) {
Serial.printf("Error (%d) opening NVS!\n", err);
} else {
Serial.println("Done");
}
}
uint32_t last_temp_update = 0;
float buffer[16];
int buf_idx = 0;
int nvs_idx = 0;
void saveTemperature(float temp) {
Serial.print("buf_idx=");Serial.println(buf_idx);
if (buf_idx % 16 == 15) {
String stridx("i");
stridx += nvs_idx;
Serial.print("saving buffer to nvs ");
Serial.println(stridx);
err = nvs_set_blob(nvs, stridx.c_str(), (const void*) buffer, sizeof(float) * 16);
if (err != ESP_OK) {
Serial.print("nvs set returned error %d");Serial.println(err);
}
nvs_idx++;
}
buffer[buf_idx % 16] = temp;
buf_idx++;
}
void loop() {
uint32_t now = millis();
if (now - last_temp_update > 1000) {
last_temp_update = now;
float objt = tmp007.readObjTempC();
float diet = tmp007.readDieTempC();
Serial.print("Object Temperature: "); Serial.print(objt); Serial.println("*C");
Serial.print("Die Temperature: "); Serial.print(diet); Serial.println("*C");
saveTemperature(objt);
}
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment