#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
// GPIOs where the NTC thermistors are connected
const int ntcPins[4] = {32, 33, 34, 35};
// WiFi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Google Sheets API details
const char* google_script_id = "AKfycbzqP-j1L4cLd2TkbbbYWMGUI7_q09IZqja56CQ9RQrjCrRYBax08MNN2u6101fmSKAPSA";
// Constants for the NTC thermistor and resistor
const float nominalResistance = 10000; // 10kΩ resistor
const float nominalTemperature = 25; // Nominal temperature for the thermistor
const float bCoefficient = 3950; // Beta coefficient of the thermistor
const float seriesResistor = 10000; // Value of the series resistor
// OLED display settings
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Timing variables
unsigned long previousMillis = 0;
const long interval = 10000; // Interval for sending data to Google Sheets (10 seconds)
void setup() {
// Start the Serial Monitor
Serial.begin(115200);
// Initialize OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.display();
delay(2000);
display.clearDisplay();
// Connect to WiFi
connectToWiFi();
}
void loop() {
// Update OLED screen in real-time
displayTemperatures();
// Handle timing for sending data to Google Sheets
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// Prepare the data to be sent
String data = prepareData();
// Send data to Google Sheets
sendToGoogleSheets(data);
}
// Update WiFi status indicator
updateWiFiStatus();
}
void connectToWiFi() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
String prepareData() {
String data = "timestamp=" + String(millis()) + "&";
for (int i = 0; i < 4; i++) {
float temperatureC = readTemperature(ntcPins[i]);
if (isnan(temperatureC)) {
Serial.print("Error: Could not read temperature data from sensor ");
Serial.println(i + 1);
data += "sensor" + String(i + 1) + "=Error&";
} else {
data += "sensor" + String(i + 1) + "=" + String(temperatureC) + "&";
}
}
// Remove the last '&'
data.remove(data.length() - 1);
return data;
}
float readTemperature(int pin) {
int adcValue = analogRead(pin);
float resistance = seriesResistor / ((4095.0 / adcValue) - 1);
float steinhart;
steinhart = resistance / nominalResistance; // (R/Ro)
steinhart = log(steinhart); // ln(R/Ro)
steinhart /= bCoefficient; // 1/B * ln(R/Ro)
steinhart += 1.0 / (nominalTemperature + 273.15); // + (1/To)
steinhart = 1.0 / steinhart; // Invert
steinhart -= 273.15; // Convert to Celsius
return steinhart;
}
void displayTemperatures() {
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
for (int i = 0; i < 4; i++) {
float temperatureC = readTemperature(ntcPins[i]);
display.print("Sensor ");
display.print(i + 1);
display.print(": ");
if (isnan(temperatureC)) {
display.println("Error");
} else {
display.print(temperatureC);
display.println(" C");
}
}
display.display();
}
void updateWiFiStatus() {
// Draw a small dot indicator for WiFi status
display.fillCircle(120, 60, 3, (WiFi.status() == WL_CONNECTED) ? SSD1306_WHITE : SSD1306_BLACK);
display.drawCircle(120, 60, 3, SSD1306_WHITE);
display.display();
}
void sendToGoogleSheets(const String& data) {
WiFiClientSecure client;
HTTPClient http;
client.setInsecure();
String url = String("https://script.google.com/macros/s/") + google_script_id + "/exec?" + data;
http.begin(client, url);
int httpCode = http.GET();
if (httpCode > 0) {
Serial.println("Data sent successfully");
} else {
Serial.println("Error in sending data: " + String(httpCode));
}
http.end();
}