#include <OneWire.h>
#include <DallasTemperature.h>

#define TEMP_PIN  1 // ESP32 pin GPIO 1 connected to DS18B20 sensor's DATA pin
#define PH_PIN 4
#define SALINITAS_PIN 5
#define DO_PIN 6

OneWire oneWire(TEMP_PIN);
DallasTemperature DS18B20(&oneWire);
float tempC; // temperature in Celsius
float tempF; // temperature in Fahrenheit
float Ph;
float salinitas;
float DO;

float floatMap(float x, float in_min, float in_max, float out_min, float out_max) {
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

void setup() {
  Serial.begin(9600); // initialize serial
  DS18B20.begin();    // initialize the DS18B20 sensor
  Serial.println("Temperature ~ Ph ~ Salinitas ~ DO");
}

void loop() {
  DS18B20.requestTemperatures();       // send the command to get temperatures
  tempC = DS18B20.getTempCByIndex(0);  // read temperature in °C
  //tempF = tempC * 9 / 5 + 32; // convert °C to °F

  int analogPh = analogRead(PH_PIN);
  int analogSalinitas = analogRead(SALINITAS_PIN);
  int analogDO = analogRead(DO_PIN);

  Ph = floatMap (analogPh, 0, 4095, 0, 14);
  salinitas = floatMap (analogSalinitas, 0, 4095, 0, 50);
  DO = floatMap (analogDO, 0, 4095, 0, 40);

  Serial.print(tempC); 
  Serial.print("°C");
  Serial.print("  ~  ");
  //Serial.print(tempF); 
  //Serial.println("°F");
  Serial.print(Ph); 
  Serial.print("  ~  ");
  Serial.print(salinitas); 
  Serial.print("ppt");
  Serial.print("  ~  ");
  Serial.print(DO); 
  Serial.println("mg/L");
  
  delay(500);
}