Roberto Vasquez(Mars)Marwa Mabrouk
Published

IOT Project Group 30: Temperature, Humidity, and Moisture

A cool introductory project to expose the user into the world of IOT and electrical/mechanical engineering

IntermediateFull instructions provided5 hours103
IOT Project Group 30: Temperature, Humidity, and Moisture

Things used in this project

Hardware components

DHT11 Temperature & Humidity Sensor (3 pins)
DHT11 Temperature & Humidity Sensor (3 pins)
×1
Photon 2
Particle Photon 2
×2
USB-A to Mini-USB Cable
USB-A to Mini-USB Cable
×2
Gravity: Analog Capacitive Soil Moisture Sensor- Corrosion Resistant
DFRobot Gravity: Analog Capacitive Soil Moisture Sensor- Corrosion Resistant
×1
LED (generic)
LED (generic)
×2

Software apps and online services

ThingSpeak API
ThingSpeak API
Particle Build Web IDE
Particle Build Web IDE
Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Wire Stripper & Cutter, 22-10 AWG Capacity Copper & Aluminium Wires
Wire Stripper & Cutter, 22-10 AWG Capacity Copper & Aluminium Wires
Premium Female/Male Extension Jumper Wires, 40 x 6" (150mm)
Premium Female/Male Extension Jumper Wires, 40 x 6" (150mm)

Story

Read more

Schematics

Sensors Schematic (DH11, Soil Moisture [V2.0])

The following contains a picture of the sensors schematic used for this project. (Please note moisture sensor in schematics is V1.0 but one used is V2.0)

Receiver Sensors Schematic (DH11, Soil Moisture [V2.0])

Here is the schematic of the receiver for the photon particle 2.

Code

DHT, and Moisture Sensors Particle Publish Code

C/C++
The code was written on WEB IDE, and then the corresponding libraries were added to it. It published the data to webhook and it also searches for a message from the receiver photon once it is connected.
// This #include statement was automatically added by the Particle IDE.
#include <I2CSoilMoistureSensor.h>
// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_DHT_Particle.h>
#define DHTPIN D2       // Pin where the DHT11 is connected
#define DHTTYPE DHT11   // DHT 11
DHT dht(DHTPIN, DHTTYPE);
// Double-Switch with Par()ticle Photon - works without Libaries - tested with v0.7.0-rc.3
// Ingo Lohs, v1.1 v. 15.12.2018
// Projekt, um eine Flüssigkeit in einem Rohr bzw. Becken zu detektieren und eine Signalisierung via IFTTT abzugeben
// Einsatz bei Peter Ens

    /* 
             -----[    ]-----
     2xVCC  -|VIN        3V3|-    
     2xGND  -|GND        RST|-
            -|TX        VBAT|-
            -|RX         GND|- 
            -|WKP         D7|-
            -|DAC         D6|-
            -|A5          D5|-
            -|A4          D4|- SwitchSensor_Rohr DIGITAL-PIN > Becken
            -|A3          D3|-     
            -|A2          D2|-      
            -|A1          D1|- SCL 
            -|A0          D0|- SDA = SwitchSensor_Rohr DIGITAL-PIN > Rohr
              \____________/
             
    */

// **************

const int SwitchSensor_Rohr = D3;   // Sensor mit VCC/GND/DIGITAL-PIN an PIN D0 vom Photon
//const int SwitchSensor_Becken = D4; // Sensor mit VCC/GND/DIGITAL-PIN an PIN D4 vom Photon
const int internalLED1 = D0;
const int internalLED2 = D1;
bool firstfire_switch_Rohr = true;
bool firstfire_switch_Becken = true;
unsigned long lastmillis = 0; // time for interation the loop

void setup() {
  Serial.begin(9600);
  dht.begin();
 pinMode(SwitchSensor_Rohr,INPUT);
 //pinMode(SwitchSensor_Becken,INPUT);
 pinMode(internalLED1,OUTPUT);
 pinMode(internalLED2,OUTPUT);
 
 Particle.subscribe("photon_connection_status", connectionStatusHandler);
}

void loop() 
{
    
    if ((millis() - lastmillis) > 1000) {
        lastmillis = millis();
        readData_Switch_Rohr();
        //   readData_Switch_Becken();
        // Wait a few seconds between measurements.
        //	delay(2000);

        // Reading temperature or humidity takes about 250 milliseconds!
        // Sensor readings may also be up to 2 seconds 'old' (its a 
        // very slow sensor)
        float h = dht.getHumidity();
        // Read temperature as Celsius
        float t = dht.getTempCelcius();
        // Read temperature as Farenheit
        float f = dht.getTempFarenheit();
	
	  // Format the data as a query string for ThingSpeak
	String data = String(h); 
	String data3 = String(f);

  Particle.publish("Temperature ", data, PRIVATE);
  Particle.publish("Humidity ", data3, PRIVATE);

  //Particle.publish("Humidity", data3, PRIVATE);
  
  // Wait for 5 seconds before the next update
  delay(5000);
  }
}

void connectionStatusHandler(const char *event, const char *data) 
    {
  Serial.printlnf("Particle B is connected and will be displaying data shortly %s", data);
  delay(5000);
    }
    
void readData_Switch_Rohr() {
    
    	float l = internalLED1;
    	float x = internalLED2;

  String data1 = String(x);
  String data2 = String(l);
  int proximity_Rohr = digitalRead(SwitchSensor_Rohr); 
  
    if (proximity_Rohr == 1) 
    {
    Serial.println("All is ok - am Rohr");  
    digitalWrite(internalLED1, LOW);
    digitalWrite(internalLED2, HIGH);
    firstfire_switch_Rohr = true;
    // Format the data as a query string for ThingSpeak


  // Trigger the integration (webhook) to send data to ThingSpeak
    Particle.publish("readings", String::format("{\"No Moisture Detected}", x));
   //Particle.publish("moisture", data1, PUBLIC);
   Particle.publish("Moisture Sensor", data2, PUBLIC);
    }
    else
    {
    Serial.println("Switch ERROR Reading Value - am Rohr");
    digitalWrite(internalLED1, HIGH);
    delay(500);
    digitalWrite(internalLED2, LOW);
 Particle.publish("readings", String::format("{\"Moisture Detected}", l));

 Particle.publish("Moisture Sensor", data1, PUBLIC);

    } 

}

DHT, and Moisture Sensors Particle Subscribe Code

C/C++
The code was created with the help of particle integration, and then the Arduino JSON library was added to it. it also published to the webhook once it is connected and is receiving the data from the other photon.
// This #include statement was automatically added by the Particle IDE.
#include <ArduinoJson.h>
#include "Particle.h"

// Function declarations
void setup();
void myHandler(const char *event, const char *data);

void setup() {
  // Start Serial communication
  Serial.begin(9600);
// Put initialization code here
  Particle.function("sendConnectionStatus", sendConnectionStatusHandler);
  // Subscribe to the integration response events
  Particle.subscribe("hook-response/Moisture Sensor", myHandler, MY_DEVICES);
  Particle.subscribe("hook-response/Temperature", myHandler, MY_DEVICES);
  Particle.subscribe("hook-response/Humidity", myHandler, MY_DEVICES);
  
  
}
void loop() {
  //main loop code here

  // Check if the Photon is connected to the Particle Cloud
  if (Particle.connected()) {
    // Send a connection status message to the other Photon
    Particle.publish("photon_connection_status", "Photon is connected");
  }
  // Example: publish data to the Particle console
  float moistureValue = 0;
  float temperatureValue = 60.7;
  float humidityValue = 40.8;

  char data[128]; 
  snprintf(data, sizeof(data), "{\"moisture\":%.2f,\"temperature\":%.2f,\"humidity\":%.2f}", moistureValue, temperatureValue, humidityValue);

  // Publish the data to the Particle console
  Particle.publish("SensorData", data, PUBLIC);

  delay(5000);
}

int sendConnectionStatusHandler(String command) {
  // Send a connection status message to the other Photon
  Particle.publish("photon_connection_status", "Photon is connected");
  return 1; // Acknowledge the function call
}

// integration response
void myHandler(const char *event, const char *data) {
  // Print the received data to Serial
  Serial.println("Received response from webhook:");
  Serial.println(data);


  // Create a JSON object
  StaticJsonDocument<256> doc; 

  // Deserialize the JSON data
  DeserializationError error = deserializeJson(doc, data);

  // Check for errors
  if (error) {
    Serial.print("Failed to parse JSON: ");
    Serial.println(error.c_str());
    return;
  }

  // Extract the result value
  const char *result = doc["result"];

  Serial.print("Result: ");
  Serial.println(result);
}

Credits

Roberto Vasquez
1 project • 1 follower
(Mars)Marwa Mabrouk
1 project • 1 follower

Comments