/*** 

Helper Docs:
    - DHT22: https://www.instructables.com/How-to-use-DHT-22-sensor-Arduino-Tutorial/
    - Multiplexer: https://github.com/SunitRaut/Lightweight-CD74HC4067-Arduino
    - Multiplexer Hookup: https://electropeak.com/learn/interfacing-cd74hc4067-16-channel-analog-digital-multiplexer-with-arduino/
    - Additional: https://forum.arduino.cc/t/multiple-dht22-using-74hc4067-multiplexer/1145854
    - Additional: https://www.luisllamas.es/en/more-outputs-inputs-arduino-multiplexer-cd74hc4067/
    - Additional: https://forums.losant.com/t/solved-failed-to-read-from-dht-sensor/1629/7

  NOTE: DHT22 in Wowki may no longer function w/ CD74HC4067, there seems to be no working examples that still output anything but NaN. Needs real-world testing. 
          There's also multiple versions of the DHT22 (some with SDA/SCL and some w/ a digital signal).

        

***/


//#include <light_CD74HC4067.h>;
//#include <DHT.h>;

#include <DHT22.h>
//define pin data
#define pinDATA 13 // SDA, or almost any other I/O pin

DHT22 dht22(pinDATA); 
 
void setup() {
  Serial.begin(115200); //1bit=10µs
  Serial.println("\ntest capteur DTH22");
}

void loop() {
  delay(2000);
  Serial.println(dht22.debug()); //optionnal

  float t = dht22.getTemperature();
  float h = dht22.getHumidity();

  if (dht22.getLastError() != dht22.OK) {
    Serial.print("last error :");
    Serial.println(dht22.getLastError());
  }

  Serial.print("h=");Serial.print(h,1);Serial.print("\t");
  Serial.print("t=");Serial.println(t,1);
  delay(2000); //Collecting period should be : >1.7 second
}

/***






//s0 s1 s2 s3
//CD74HC4067 mux(10, 11, 12, 13); // mux(s0,s1,s2,s3), where s3 is the highest select line (MSB)

// Pins for CD74HC4067 control
const byte MUX_SIG = 37;
const byte MUX_S[4] = {8, 9, 10, 11} ;
const byte lohi[2] = {LOW, HIGH};

// DHT sensor type
//#define DHT_TYPE DHT22

// Number of DHT sensors
#define NUM_SENSORS 3

// Create a DHT object
//DHT dhtSensor(MUX_SIG, DHT_TYPE);
DHT22 dhtSensor(MUX_SIG);


void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  pinMode(MUX_SIG, INPUT);

  for (byte s=0; s<4; s++) pinMode(MUX_S[s], OUTPUT);

  //dhtSensor.begin();
}


void loop() {
  // put your main code here, to run repeatedly:
  delay(10); // this speeds up the simulation
   
   // Loop through all sensors
  //for (int i = 0; i < NUM_SENSORS; i++) {
    // Select the MUX channel
    //selectMuxChannel(i);
    selectMuxChannel(0);

    // Read temperature and humidity from the sensor
    float temperature = dhtSensor.getTemperature();
    float humidity = dhtSensor.getHumidity();


    // Display temperature and humidity on LCD
    //displayData(i + 1, temperature, humidity);
    displayData(0, temperature, humidity);

    // Delay before moving to the next sensor
    //Serial.print(i);
    Serial.print(temperature);
    Serial.print(humidity);
    Serial.println("-------------");

    delay(3500);
  }
//}


void selectMuxChannel(int channel) {
  for (byte s=0; s<4; s++) digitalWrite(MUX_S[s], lohi[bitRead(channel, s)]);
  delayMicroseconds(100);
}


void displayData(int sensorIndex, float temperature, float humidity) {
  Serial.println(temperature);
  Serial.println(humidity);
}

**/