ruiqiao
Published

Temperature, humidity, and water level monitoring

In this project, we used DHT11 to detect temperature and humidity and water level sensor to detect water level. There are 4 LEDs to indicate

BeginnerShowcase (no instructions)393
Temperature, humidity, and water level monitoring

Things used in this project

Software apps and online services

Google Sheets
Google Sheets
PushingBox
Arduino Web Editor
Arduino Web Editor

Story

Read more

Schematics

1_NYb5Jpvjp7.png

Code

Untitled file

C/C++
//-----------------------------------------------
//Since Arduino can't https, we need to use Pushingbox API (uses http)to run 
//the Google Script (uses https). Alternatly use Ivan's SecureWifi encryption


#include <WiFi101.h>
#include "DHT.h"

#define DHTPIN 6    // what pin we're connected to, pin1 is 5th pin from end

// Uncomment whatever DHT sensor type you're using!
#define DHTTYPE DHT11  // DHT 11
//#define DHTTYPE DHT21  // DHT 21


DHT dht(DHTPIN,DHTTYPE);

const char WEBSITE[] = "api.pushingbox.com"; //pushingbox API server
const String devid = "v8FF17EFF7B58B29"; //device ID on Pushingbox for our Scenario

const char* MY_SSID = "VIRGIN610";
const char* MY_PWD =  "3745E496949E";


int status = WL_IDLE_STATUS;
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)

int led1 = 1;
int led2 = 2;
int led3 = 3;
int led4 = 4;

void setup() {
    // initialize the digital pins as an outputs.
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) 
  {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);
  }

  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) 
  {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(MY_SSID);
    //Connect to WPA/WPA2 network.Change this line if using open/WEP network
    status = WiFi.begin(MY_SSID, MY_PWD);

    // wait 10 seconds for connection:
    delay(10000);
  }
  
  Serial.println("Connected to wifi");
  printWifiStatus();
  dht.begin();
  
}

void loop() {

   // Wait between measurements.
  delay(10000);

  //prefer to use float, but package size or float conversion isnt working
  //will revise in future with a string fuction or float conversion function

  int humidityData = dht.readHumidity();
  // Read temperature as Celsius (the default)
  int celData = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  int fehrData = dht.readTemperature(true);
  
    // read the input on analog pin 1:
  int waterLevel = analogRead(A1);
  
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  if (waterLevel > 10) {digitalWrite(led1, HIGH); }
  if (waterLevel > 250) {digitalWrite(led2, HIGH); }
  if (waterLevel > 350) {digitalWrite(led3, HIGH); }
  if (waterLevel > 390) {digitalWrite(led4, HIGH); }
  

  // Check if any reads failed and exit early (to try again).
  if (isnan(humidityData) || isnan(celData) || isnan(fehrData))
  {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  if (isnan(waterLevel))
  {
    Serial.println("Failed to read from water level sensor!");
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  int hifData = dht.computeHeatIndex(fehrData, humidityData);
  // Compute heat index in Celsius (isFahreheit = false)
  int hicData = dht.computeHeatIndex(celData, humidityData, false);

  Serial.print("Humidity: ");
  Serial.print(humidityData);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(celData);
  Serial.print(" *C ");
  Serial.print(fehrData);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(hicData);
  Serial.print(" *C ");
  Serial.print(hifData);
  Serial.print(" Water Level ");
  Serial.print(waterLevel);
  Serial.println(" \n");

Serial.println("\nSending Data to Server..."); 
  // if you get a connection, report back via serial:
WiFiClient client;  //Instantiate WiFi object, can scope from here or Globally

    //API service using WiFi Client through PushingBox then relayed to Google
    if (client.connect(WEBSITE, 80))
      { 
         client.print("GET /pushingbox?devid=" + devid
       + "&humidityData=" + (String) humidityData
       + "&celData="      + (String) celData
       + "&fehrData="     + (String) fehrData
       + "&hicData="      + (String) hicData
       + "&hifData="      + (String) hifData
       + "&waterLevel="   + (String) waterLevel
         );

      // HTTP 1.1 provides a persistent connection, allowing batched requests
      // or pipelined to an output buffer
      client.println(" HTTP/1.1"); 
      client.print("Host: ");
      client.println(WEBSITE);
      client.println("User-Agent: MKR1000/1.0");
      //for MKR1000, unlike esp8266, do not close connection
      client.println();
      Serial.println("\nData Sent"); 
      }
}


void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

Credits

ruiqiao
0 projects • 0 followers

Comments