Marcin Gruda
Created November 9, 2020

Lane Tech HS - Too Hot Flag

A little doohickey that gives a visual cue that your room is too hot while recording that time and temperature in a Google Sheet.

BeginnerShowcase (no instructions)5 hours49
Lane Tech HS - Too Hot Flag

Things used in this project

Hardware components

Argon
Particle Argon
×1
DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE
Google Sheets
Google Sheets

Story

Read more

Schematics

Sensors and Me Schematic

Code

Sensors and Me

C/C++
Code for Particle
#include "Adafruit_DHT.h"

// Written by ladyada, public domain. Modified by Marcin

#define DHTPIN D6

#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);
int servoPin = A3;
Servo myServo;
int servoPos = 0;
  
void setup() 
{
  Serial.begin(9600);
  Serial.println("DHT11 test!");
  // attaches the servo on the A3 pin to the servo object
	myServo.attach(A3);
	dht.begin();
}

void loop() 
{
// Wait a few seconds between measurements.
	delay(10000);

	float h = dht.getHumidity();
	float t = dht.getTempCelcius();
	float f = dht.getTempFarenheit();
	float k = dht.getTempKelvin();
// Check if any reads failed and exit early (to try again).
	if (isnan(h) || isnan(t) || isnan(f)) 
	{
		Serial.println("Failed to read from DHT sensor!");
		return;
	}

	Serial.print("\nHumid: "); 
	Serial.print(h);
	Serial.print("% - ");
	Serial.print("Temp: "); 
	Serial.print(t);
	Serial.print("*C ");
	Serial.print(f);
	Serial.print("*F ");
	Serial.print(k);
	Serial.println("*K ");
    
	if (t > 27.0)
	{
	  Serial.println("Room is too hot");
	  Particle.publish("RoomHot", String(t));
	  myServo.write(180);
        
	} else 
	{
	  myServo.write(0);
	}
}

Pushing Data Script for Google Sheets

JavaScript
// Enter sheet name where data is to be written below
var SHEET_NAME = "Room DHT Collection";

var SCRIPT_PROP = PropertiesService.getScriptProperties(); // new property service

// If you don't want to expose either GET or POST methods you can comment out the appropriate function
function doGet(e){
  return handleResponse(e);
}

function doPost(e){
  return handleResponse(e);
}

function handleResponse(e) { 
  var lock = LockService.getPublicLock();
  lock.waitLock(30000);  // wait 30 seconds before conceding defeat.

  try {
    // next set where we write the data - you could write to multiple/alternate destinations
    var doc = SpreadsheetApp.openById(SCRIPT_PROP.getProperty("key"));
    var sheet = doc.getSheetByName(SHEET_NAME);

    // we'll assume header is in row 1 but you can override with header_row in GET/POST data
    var headRow = e.parameter.header_row || 1;
    var headers = sheet.getRange(1, 2, 1, sheet.getLastColumn()).getValues()[0];
    var nextRow = sheet.getLastRow()+1; // get next row
    var row = [];

    // loop through the header columns
    for (i in headers){
      if (headers[i] == "Timestamp"){ // special case if you include a 'Timestamp' column
        row.push(new Date());
      } else { // else use header name to get data
        row.push(e.parameter[headers[i]]);
      }
    }
    // more efficient to set values as [][] array than individually
    sheet.getRange(nextRow, 1, 1, row.length).setValues([row]);
    // return json success results
    return ContentService
    .createTextOutput(JSON.stringify({"result":"success", "row": nextRow}))
          .setMimeType(ContentService.MimeType.JSON);
  } catch(e){
    // if error return this
    return ContentService
          .createTextOutput(JSON.stringify({"result":"error", "error": e}))
          .setMimeType(ContentService.MimeType.JSON);
  } finally { //release lock
    lock.releaseLock();
  }
}
function setup() {
    var doc = SpreadsheetApp.getActiveSpreadsheet();
    SCRIPT_PROP.setProperty("key", doc.getId());
}

Credits

Marcin Gruda
3 projects • 1 follower
Contact

Comments

Please log in or sign up to comment.