Combined Turbidity & Temp Sensors With Esp 8266

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

// Include the libraries

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <WiFi.h>

// Pin configurations
#define ONE_WIRE_BUS 2
#define sensorPin A0 // Assuming the sensor pin is connected to A0

// WiFi credentials
const char* ssid = "SERVANAWireless"; // Change this to your WiFi SSID
const char* password = "PASSWORD10"; // Change this to your WiFi password

// Variables
float volt = 0;
int ntu = 0;

// LCD Configuration
LiquidCrystal_I2C lcd(0x27, 2, 16); // Change the address if necessary

// OneWire instance
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress insideThermometer;

void setup()
{
// Serial and LCD initialization
Serial.begin(9600);
lcd.begin(2, 16);
lcd.backlight();

// Dallas Temperature initialization


Serial.println("Dallas Temperature IC Control Library Demo");
sensors.begin();

Serial.print("Locating devices...");
Serial.print("Found ");
Serial.print(sensors.getDeviceCount(), DEC);
Serial.println(" devices.");

Serial.print("Parasite power is: ");


if (sensors.isParasitePowerMode())
Serial.println("ON");
else
Serial.println("OFF");

if (!sensors.getAddress(insideThermometer, 0))
Serial.println("Unable to find address for Device 0");

Serial.print("Device 0 Address: ");


printAddress(insideThermometer);
Serial.println();

sensors.setResolution(insideThermometer, 9);

Serial.print("Device 0 Resolution: ");


Serial.print(sensors.getResolution(insideThermometer), DEC);
Serial.println();

// WiFi connection
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {


delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}

void loop()
{
// Temperature measurement
Serial.print("Requesting temperatures...");
sensors.requestTemperatures();
Serial.println("DONE");

printTemperature(insideThermometer);
// Voltage measurement
volt = 0;
for (int i = 0; i < 800; i++)
{
volt += ((float)analogRead(sensorPin) / 1023) * 5;
}
volt = volt / 800;
volt = round_to_dp(volt, 2);
if (volt < 2.5)
{
ntu = 3000;
}
else
{
ntu = -1120.4 * square(volt) + 5742.3 * volt - 4353.8;
}

// LCD Display
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(volt);
lcd.print(" V");

lcd.setCursor(0, 1);
lcd.print(ntu);
lcd.print(" NTU");

// WiFi communication
// You can add your code here to send or receive data via WiFi
// For example, you can use the ESP8266HTTPClient library to make HTTP requests
// See https://arduino-esp8266.readthedocs.io/en/latest/esp8266httpclient.html for more details

delay(10);
}

float round_to_dp(float in_value, int decimal_place)


{
float multiplier = powf(10.0f, decimal_place);
in_value = roundf(in_value * multiplier) / multiplier;
return in_value;
}

void printTemperature(DeviceAddress deviceAddress)


{
float tempC = sensors.getTempC(deviceAddress);
if (tempC == DEVICE_DISCONNECTED_C)
{
Serial.println("Error: Could not read temperature data");
return;
}
Serial.print("Temp C: ");
Serial.print(tempC);
Serial.print(" Temp F: ");
Serial.println(DallasTemperature::toFahrenheit(tempC));
}

void printAddress(DeviceAddress deviceAddress)


{
for (uint8_t i = 0; i < 8; i++)
{
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}

You might also like