/*
HC-SR04 Ultrasonic Sensor Example.
Turn the LED on when an object is within 100cm range.
Copyright (C) 2021, Uri Shaked
*/
/* How to use DHT-11 sensor with Arduino uno
Temperature and humidity sensor
Dev: Michalis Vasilakis // Date: 1/7/2015 // www.ardumotive.com */
//Libraries
//#include <ESP32_I2C_Slave.h>
#include <DHT.h>
#include <Wire.h>
#include <EasyTransferI2C.h>
//create object
//LiquidCrystal_I2C ET;
struct RECEIVE_DATA_STRUCTURE{
//put your variable definitions here for the data you want to receive
//THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
int temperature;
float humidity;
float distance;
};
//give a name to the group of data
RECEIVE_DATA_STRUCTURE mydata;
//Constants
#define DHTPIN 25 // what pin we're connected to
#define DHTTYPE DHT11 // DHT 11 (AM2302)
#define I2C_SLAVE_ADDRESS 9 //define slave i2c address
// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);
//Variables
int led1=22;
int led2=23;
int chk;
float hum; //Stores humidity value
float temp; //Stores temperature value
#define ECHO_PIN 24
#define TRIG_PIN 2
void setup() {
Serial.begin(115200);
Wire.begin();
//start the library, pass in the data details and the name of the serial port. Can be Serial, Serial1, Serial2, etc.
//define handler function on receiving data
Wire.onReceive();
pinMode(led2, OUTPUT);
dht.begin();
/*pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);*/
}
float readDistanceCM() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
int duration = pulseIn(ECHO_PIN, HIGH);
return duration * 0.034 / 2;
}
void loop() {
//check and see if a data packet has come in.
if(receiveData()){
//this is how you access the variables. [name of the group].[variable name]
//since we have data, we will blink it out.
for(int i = mydata.temperature; i>0; i--){
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
}
}
float distance = readDistanceCM();
bool isNearby = distance < 100;
digitalWrite(led1, isNearby);
Serial.print("Measured distance: ");
Serial.println(readDistanceCM());
//Read data and store it to variables hum and temp
hum = dht.readHumidity();
temp = dht.readTemperature();
//Print temp and humidity values to serial monitor
Serial.print("Humidity: ");
Serial.print(hum);
Serial.print(" %, Temp: ");
Serial.print(temp);
Serial.println(" Celsius");
digitalWrite(led2, LOW);
if (temp>2)
{
digitalWrite(led2, HIGH);
Serial.print("");
Serial.print(" HOT CLIMATE");
}
delay(2000);
}
void receiveData(int numBytes) {}