Arduino Code For Temperature Sensing
Arduino Code For Temperature Sensing
ATMEGA168
#include <WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <WebServer.h>
const int tempPin = 32; // GPIO pin for DS18B20 temperature sensor
const int lightPin = 35; // GPIO pin for LDR (Light Sensor)
OneWire oneWire(tempPin);
DallasTemperature sensors(&oneWire);
// WiFi credentials
WebServer server(80);
<!DOCTYPE html>
<html>
<head>
<style>
h1 {color: red;}
</style>
</head>
<body>
<h1>Tip Jar</h1>
<div class="login-container">
</form>
</div>
</body>
</html>
)rawliteral";
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Measurements</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
margin: 0;
h1 { text-align: center; }
.circle {
position: relative;
width: 380px;
height: 380px;
border-radius: 50%;
margin: 20px 0;
.lcd-display {
width: 300px;
background-color: deepskyblue;
color: #787676;
padding: 10px;
text-align: center;
.pointer {
width: 5px;
height: 160px;
position: absolute;
top: 7.75%;
left: 49%;
width: 2px;
height: 160px;
width: 2px;
height: 160px;}
.marker {
position: absolute;
width: 1px;
height: 10px;
background-color: black;
.label {
position: absolute;
font-size: 14px;
text-align: center;
transform-origin: center;
color: #c07204f8;
.legend span {
display: block;
padding-top: 10%;
padding-bottom:10%;
</style>
</head>
<body>
<h1>The Vibe</h1>
<div class="lcd-display">
</div>
<hr />
</div>
<div class="legend">
<span><strong>Legend:</strong></span>
</div>
<button class="button"
onclick="window.location.href='/control_page'">Controls</button>
<script>
function addMarkers() {
let angleRad = (angle - 90) * Math.PI / 180; // Adjust angle to start from top
(12 o'clock)
// Create marker
marker.className = 'marker';
marker.style.transform = `rotate(${angle}deg)`;
circle.appendChild(marker);
tempLabel.className = 'label';
tempLabel.style.left = `${tempLabelX}px`;
tempLabel.style.top = `${tempLabelY}px`;
tempLabel.innerHTML = `${Math.round(tempValue)}°C`;
circle.appendChild(tempLabel);
luxLabel.className = 'label';
luxLabel.style.left = `${luxLabelX}px`;
luxLabel.style.top = `${luxLabelY}px`;
circle.appendChild(luxLabel);
}
const tempAngle = mapToAngle(temp, tempMin, tempMax);
document.getElementById('tempPointer').style.transform =
`rotate(${tempAngle}deg) translateX(-50%)`;
document.getElementById('lightPointer').style.transform =
`rotate(${luxAngle}deg) translateX(-50%)`;
function fetchData() {
fetch('/data')
.then(data => {
document.getElementById('Temperature').innerHTML =
`${data.temperature} °C`;
document.getElementById('Intensity').innerHTML = `${data.intensity}
Lux`;
updatePointers(data.temperature, data.intensity);
});
function updateCircleBackground(theme) {
switch (theme) {
case 'dance':
circle.style.background = 'conic-gradient(magenta, red, yellow, green,
cyan, blue, magenta)';
break;
case 'chill':
break;
case 'event':
break;
default:
break;
addMarkers();
fetchData();
fetch('/get-background')
.then(theme => {
updateCircleBackground(theme);
});
setInterval(fetchData, 500);
</script>
</body>
</html>
)rawliteral";
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Controls</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
margin: 0;
background-color: thistle;
padding-top: 20%;
.menu {
padding-bottom: 10%;
#backgroundSelector {
</style>
</head>
<body>
<div class="menu">
</select>
</div>
<script>
function changeBackground() {
const backgroundSelector =
document.getElementById('backgroundSelector');
document.circle.style.background = selectedTheme;
}
function saveBackground() {
const selectedTheme =
document.getElementById('backgroundSelector').value;
</script>
</body>
</html>
)rawliteral";
void handleLogin() {
} else {
<script>
</script>
)";
void handleRoot() {
page.replace("%TEMPERATURE%", String(temperatureC));
page.replace("%INTENSITY%", String(lightIntensity));
void handleData() {
void setup() {
Serial.begin(115200);
// Start DS18B20 temperature sensor
sensors.begin();
WiFi.begin(ssid, password);
Serial.println("Connecting to WiFi...");
delay(1000);
Serial.print(".");
Serial.println("\nConnected to WiFi");
Serial.println(WiFi.localIP());
server.on("/", []() {
});
server.on("/measurements", handleRoot);
server.on("/control_page", []() {
if (server.hasArg("theme")) {
selectedTheme = server.arg("theme");
} else {
});
});
server.begin();
Serial.println("Server started");
void loop() {
sensors.requestTemperatures();
temperatureC = sensors.getTempCByIndex(0);
// Read light intensity from LDR sensor
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.print(lightIntensity);
Serial.println(" Lux");
server.handleClient();