Nicholas RobertsEmily RobidouxAndrew Henegar
Published

MEGR 3171 - Temperature and Humidity Sensor IOT Project

We built a temperature and humidity sensor for a cigar humidor which sends the temperature and humidity remotely to your phone.

IntermediateFull instructions provided1 hour138
MEGR 3171 - Temperature and Humidity Sensor IOT Project

Things used in this project

Hardware components

Photon 2
Particle Photon 2
×1
Adafruit FeatherWing Doubler
×1
Adafruit FeatherWing OLED
×1
Adafruit Sensirion SHT40 Temperature & Humidity Sensor
×1
Adafruit STEMMA QT/QWIIC JST SH 4-Pin Cable
×1
onn. Portable Battery Charger - 3350 mAh
Any battery charger/pack will work of at least 500 mAh for the Photon 2.
×1

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE
Particle Mobile App

Hand tools and fabrication machines

Soldering Iron Kit, Weller XNT/THM Tips
Soldering Iron Kit, Weller XNT/THM Tips
Any type of soldering kit will do. You just need to solder the FeatherWing Doubler together.

Story

Read more

Schematics

Circuit Diagram

Code

Main Code

C/C++
User will also need to add Adafruit libraries. The Particle Web IDE can do this for you automatically.
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
#include <Adafruit_SHT4x.h>

Adafruit_SHT4x sht4 = Adafruit_SHT4x();

//SYSTEM_MODE(MANUAL);
SYSTEM_THREAD(ENABLED);

Adafruit_SH1107 display = Adafruit_SH1107(64, 128, &Wire);

double tempF = 0;
double humi = 0;
double battV = 0;

unsigned long previousMillis = 0; 
const long interval = 1000; //set how often you want to read and display sensor readings (in milliseconds)

void setup() 
{
  //Register the Particle variables that you can access over the cloud
  Particle.variable("temp", tempF); //variable to hold temperature measurement
  Particle.variable("humi", humi);  //variable to hold the humidity measurement
  Particle.variable("batt", battV); //variable to hold the battery voltage measurement

  pinMode(S5, INPUT_PULLUP);
  pinMode(D7, OUTPUT);
  Serial.begin(115200);

  // Call function to initialize the sensor
  initSHT40();

  // Initialize the OLED screen
  display.begin(0x3C, true); // Address 0x3C default
  display.clearDisplay();
  display.display();
  display.setRotation(1);
  display.setTextSize(1);
  display.setTextColor(SH110X_WHITE, SH110X_BLACK);
  display.setCursor(0, 0);

  // Subscribe values to Particle
  Particle.subscribe("temperature and Humidity", myHandler, MY_DEVICES);

  // Call function to draw the console on the OLED screen
  drawConsole();
}

void myHandler(const char *event, const char *data) {
}

void loop() 
{
  unsigned long currentMillis = millis();

  //update the console once per set interval
  if (currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis;
    updateConsole();
  }
  Particle.publish("Temperature and Humidity", data, PRIVATE);
  delay(1000);
}

//Draw the console on the OLED screen
void drawConsole(void)
{
  //Draw the console table
  display.drawLine(42,0,42,44,SH110X_WHITE);
  display.drawLine(84,0,84,44,SH110X_WHITE);
  display.drawLine(0,14,128,14,SH110X_WHITE);
  display.drawLine(0,44,128,44,SH110X_WHITE);
    
  display.setCursor(10,4); display.print("Temp");
  display.setCursor(54,4); display.print("Humi");
  display.setCursor(96,4); display.print("Batt ");
  display.display();

}

//Update the console with latest values
void updateConsole(void)
{
  sensors_event_t humidity, temp;
  sht4.getEvent(&humidity, &temp);
  //Change default Celcius measurement to Fahrenheit
  tempF = ((1.8*temp.temperature)+32);
  humi = humidity.relative_humidity;
  //LiPo positive terminal is connected to the A6 pin aka VBAT_MEAS
  //This is a special 5V tolerant pin
  //The ADC measurement is referenced to 5V instead of 3.3V
  battV = (analogRead(A6)*5.0)/4095;//LiPo positive terminal is connected to the A6 pin

  //Display a message on the bottom
  //You can choose to use this space to display any text or information you want
  display.setCursor(0,50); display.print("  Particle Photon 2");

  //Read and display the temperature
  display.setCursor(6,20); display.print(tempF);
  display.setCursor(20,32); display.print("F");


  display.setCursor(50,20); display.print(humi);
  display.setCursor(56,32); display.print("%rH");

  display.setCursor(92,20); display.print(battV);

  display.setCursor(102,32); display.print("V");
  display.display();

}

void initSHT40(void)
{
  Serial.println("Adafruit SHT4x test");
  if (! sht4.begin()) {
    Serial.println("Couldn't find SHT4x");
    while (1) delay(1);
  }
  Serial.println("Found SHT4x sensor");
  Serial.print("Serial number 0x");
  Serial.println(sht4.readSerial(), HEX);

  sht4.setPrecision(SHT4X_HIGH_PRECISION);
  switch (sht4.getPrecision()) {
     case SHT4X_HIGH_PRECISION: 
       Serial.println("High precision");
       break;
     case SHT4X_MED_PRECISION: 
       Serial.println("Med precision");
       break;
     case SHT4X_LOW_PRECISION: 
       Serial.println("Low precision");
       break;
  }

  sht4.setHeater(SHT4X_NO_HEATER);
  switch (sht4.getHeater()) {
     case SHT4X_NO_HEATER: 
       Serial.println("No heater");
       break;
     case SHT4X_HIGH_HEATER_1S: 
       Serial.println("High heat for 1 second");
       break;
     case SHT4X_HIGH_HEATER_100MS: 
       Serial.println("High heat for 0.1 second");
       break;
     case SHT4X_MED_HEATER_1S: 
       Serial.println("Medium heat for 1 second");
       break;
     case SHT4X_MED_HEATER_100MS: 
       Serial.println("Medium heat for 0.1 second");
       break;
     case SHT4X_LOW_HEATER_1S: 
       Serial.println("Low heat for 1 second");
       break;
     case SHT4X_LOW_HEATER_100MS: 
       Serial.println("Low heat for 0.1 second");
       break;
  }
}

Credits

Nicholas Roberts
1 project • 2 followers
Emily Robidoux
1 project • 2 followers
Andrew Henegar
1 project • 2 followers
Thanks to Mohit Bhoite.

Comments