// Arduino slave three bytes

#include <Arduino.h>
#include <Wire.h>
int My_I2C_Address = 0x08;

#include <Servo.h>
Servo myservo1; 
Servo myservo2;

#include "DHT.h"
#define DHTPIN 2     
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);

byte i2c_rcv_opcode;    //Command received from I2C bus
byte i2c_rcv_target;    //Command received from I2C bus
byte i2c_rcv_value;     //Command received from I2C bus

byte i2c_sensor_value;
int sensorsData[11];

typedef union
{
  float temperature;
  uint8_t tBytes[4];
} FLOATUNION_t;

union { 
uint8_t t[4]; 
float computedTemp; 
} tData;

typedef union
{
  float humidity;
  uint8_t hBytes[4];
} FLOATUNION_h;

union { 
uint8_t h[4]; 
float computedHum; 
} hData;


//*************** received  Command handler function ****************
void commandRcv(int numBytes)
{
  while(Wire.available()) 
  {
    i2c_rcv_opcode = Wire.read();
    i2c_rcv_target = Wire.read();
    i2c_rcv_value = Wire.read();
  }
  switch ( i2c_rcv_opcode) 
      {
        case 10:
        digitalWrite(i2c_rcv_target,i2c_rcv_value) ;
        break;
        case 11:
        myservo1.write(i2c_rcv_value) ;
        delay(1000); 
        break;
        case 12:
        myservo2.write(i2c_rcv_value) ;
        delay(1000); 
        break;
       case 20:
       i2c_sensor_value= analogRead(i2c_rcv_target) ;
       sensorsData[i2c_rcv_target] = i2c_sensor_value;
       delay(1000); 
        break;
        case 30:
        float sensor_temp = dht.readTemperature();
        float sensor_hum = dht.readHumidity();
        FLOATUNION_t tFloat; 
        tFloat.temperature = sensor_temp; // Assign a temperature to the float

        FLOATUNION_h hFloat; 
        hFloat.humidity = sensor_hum; // Assign a humidity to the float

        // Check if any reads failed and exit early (to try again).
        if (isnan(sensor_temp)) {
          Serial.println(F("Failed to read from DHT sensor!"));
          return;  
        }
        for (int i=0; i<4; i++)
        {
          sensorsData[i+3] = tFloat.tBytes[i];
          sensorsData[i+7] = hFloat.hBytes[i];
        }
        for(int i=0; i<=10; i++)
  {
    // Wire.write(sensorsData[i]);
     Serial.println(sensorsData[i]);
  }
        break;
        default:
        break;
      }
}
// *************requests data handler function ******************
void dataRqst()
{
  for(int i=0; i<=10; i++)
  {
     Wire.write(sensorsData[i]);
     Serial.println(sensorsData[i]);
  }
 
}

void setup() {

  int inMin = 3; // Lowest output pin
  int inMax = 8; // Highest output pin
  for(int i=inMin; i<=inMax; i++)
      {
         pinMode(i, OUTPUT);       
      }
  for(int i=5; i<=8; i++)
      {
         digitalWrite(i,1); // relays OFF
      }
  Serial.begin(115200);
  dht.begin();
  Wire.begin(My_I2C_Address);           // join I2C bus as Slave with address 0x08
  myservo1.attach(3);
  myservo2.attach(4);
 
  Wire.onReceive(commandRcv);    // register an event handler for received command
  Wire.onRequest(dataRqst);   // register an event handler for data requests
 }

void loop() {
  
  float sensor_temp = dht.readTemperature();
  float sensor_hum = dht.readHumidity();
  FLOATUNION_t tFloat; 
  tFloat.temperature = sensor_temp; // Assign a temperature to the float

  FLOATUNION_h hFloat; 
  hFloat.humidity = sensor_hum; // Assign a humidity to the float

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

  Serial.print(F("Humidity: "));
  Serial.print(sensor_hum);
  Serial.print(F("%  Temperature: "));
  Serial.print(sensor_temp);
  Serial.println(F("°C "));

  Serial.println("Temperature bytes ");
  for (int i=0; i<4; i++)
{
  Serial.print(tFloat.tBytes[i], HEX); // Print the hex representation of the float
  Serial.println(' ');
}
 
  // recombine bytes to float
 
for (int i=0; i<4; i++)
{
  tData.t[i] = tFloat.tBytes[i]; 
}

Serial.print(F("Computed temperature: "));
Serial.print(tData.computedTemp); 
 Serial.println(F("°C "));

  // Wait a few seconds between measurements.
  delay(5000);


 // humidity *********
  Serial.println("Humidity bytes ");
  for (int i=0; i<4; i++)
{
  Serial.print(hFloat.hBytes[i], HEX); // Print the hex representation of the float
  Serial.println(' ');
}
  

  // recombine bytes to float
 
for (int i=0; i<4; i++)
{
  hData.h[i] = hFloat.hBytes[i]; 
}

Serial.print(F("Computed humidity: "));
Serial.print(hData.computedHum); 
 Serial.println(F("% "));
 // Wait a few seconds between measurements.
  delay(5000);

}
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module