Final version for Phase 2
parent
eb12cb9f3a
commit
22ea8a1893
|
|
@ -3,42 +3,86 @@
|
|||
#include <SoftwareSerial.h>
|
||||
#include <WiFiEsp.h>
|
||||
|
||||
// Wifi (Hotspot)
|
||||
// ============ Settings you may need to change ============
|
||||
|
||||
// Wifi (phone hotspot)
|
||||
const char wifiSSID[] = "FreeWifi";
|
||||
const char wifiPASS[] = "temppassword";
|
||||
|
||||
// Computer that runs the Go server (IP of the PC in the hotspot network).
|
||||
// The server prints it when it starts. If the PC gets a new IP, change it here.
|
||||
const char SERVER_IP[] = "172.20.46.81";
|
||||
const int SERVER_PORT = 8080;
|
||||
|
||||
const unsigned long POLL_INTERVAL = 5000UL; // ask the server every 5 s
|
||||
const unsigned long FAILSAFE_MS = 30000UL; // no server answer for 30 s -> heating off
|
||||
const unsigned long SENSOR_STALE_MS = 30000UL; // no valid sensor value for 30 s -> heating off
|
||||
const float HYSTERESIS = 0.5; // on below (target - 0.5), off at target
|
||||
// (keep in sync with HYST in index.html)
|
||||
|
||||
// ============ Hardware ============
|
||||
|
||||
// ESP8266 (Wifi module, AT firmware)
|
||||
SoftwareSerial wifiSerial(2, 3); // RX = 2, TX = 3
|
||||
SoftwareSerial wifiSerial(51, 52); // RX, TX
|
||||
WiFiEspClient client;
|
||||
|
||||
// LCD Display
|
||||
// DHT11 + LCD
|
||||
#define DHT_SENSOR_TYPE DHT_TYPE_11
|
||||
static const int DHT_SENSOR_PIN = A1;
|
||||
DHT_nonblocking dht_sensor(DHT_SENSxOR_PIN, DHT_SENSOR_TYPE);
|
||||
DHT_nonblocking dht_sensor(DHT_SENSOR_PIN, DHT_SENSOR_TYPE);
|
||||
|
||||
LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // RS, EN, D4, D5, D6, D7
|
||||
|
||||
// Light sensor and LEDs
|
||||
int lightPin = A0; // LDR
|
||||
int dataPin = 4; // DS
|
||||
int latchPin = 5; // ST_CP
|
||||
int clockPin = 6; // SH_CP
|
||||
int lightPin = A7; // LDR
|
||||
int dataPin = 4; // DS
|
||||
int latchPin = 5; // ST_CP
|
||||
int clockPin = 6; // SH_CP
|
||||
int leds = 0;
|
||||
|
||||
// API
|
||||
// ============ State ============
|
||||
|
||||
const float NO_TEMP = -999.0;
|
||||
const unsigned long API_INTERVAL = 600000UL; // 10m
|
||||
const unsigned long RETRY_INTERVAL = 30000UL; // 30s
|
||||
|
||||
// Outdoor temperature (Open-Meteo)
|
||||
const unsigned long API_INTERVAL = 60000UL; // 60s
|
||||
const unsigned long RETRY_INTERVAL = 30000UL; // 30s
|
||||
unsigned long lastApiFetch = 0;
|
||||
unsigned long fetchInterval = API_INTERVAL;
|
||||
float outdoorTemp = NO_TEMP;
|
||||
|
||||
// Connect to hotspot if not connected, then fetch the current temperature
|
||||
// Indoor temperature (DHT11)
|
||||
float lastIndoorTemp = NO_TEMP;
|
||||
unsigned long lastIndoorMillis = 0;
|
||||
|
||||
// Orders from the server
|
||||
char modeLetter = '?'; // 'A' automatic, 'M' manual, '?' = no connection yet / lost
|
||||
float targetTemp = NO_TEMP;
|
||||
unsigned long lastServerOk = 0;
|
||||
bool serverEverOk = false;
|
||||
unsigned long lastPoll = 0;
|
||||
|
||||
// MOSFET module (heating elements)
|
||||
// The heater is only allowed to run while heatRequest is true AND the
|
||||
// 60 s ON / 5 s OFF cycle below is in its ON phase.
|
||||
const int mosfetPin = 13;
|
||||
unsigned long previousMosfetMillis = 0;
|
||||
bool mosfetState = true; // phase of the cycle (not the pin!)
|
||||
const unsigned long mosfetOnTime = 60000UL; // 60 seconds ON
|
||||
const unsigned long mosfetOffTime = 5000UL; // 5 seconds OFF
|
||||
bool heatRequest = false; // thermostat wants heat
|
||||
|
||||
// ============ Wifi ============
|
||||
|
||||
bool ensureWifi() {
|
||||
if (WiFi.status() == WL_CONNECTED) return true;
|
||||
WiFi.begin(wifiSSID, wifiPASS);
|
||||
return WiFi.status() == WL_CONNECTED;
|
||||
}
|
||||
|
||||
// Fetch the current outdoor temperature
|
||||
float fetchOutdoorTemp() {
|
||||
if (WiFi.status() != WL_CONNECTED) {
|
||||
WiFi.begin(wifiSSID, wifiPASS);
|
||||
if (WiFi.status() != WL_CONNECTED) return NO_TEMP;
|
||||
}
|
||||
if (!ensureWifi()) return NO_TEMP;
|
||||
|
||||
client.setTimeout(5000);
|
||||
if (!client.connect("api.open-meteo.com", 80)) return NO_TEMP;
|
||||
|
|
@ -48,7 +92,6 @@ float fetchOutdoorTemp() {
|
|||
"Connection: close\r\n"
|
||||
"\r\n"));
|
||||
|
||||
// Skip ahead to the number in {"current":{..."temperature_2m":12.3}}
|
||||
float t = NO_TEMP;
|
||||
if (client.find("\"current\":") && client.find("\"temperature_2m\":")) {
|
||||
t = client.parseFloat();
|
||||
|
|
@ -57,6 +100,42 @@ float fetchOutdoorTemp() {
|
|||
return t;
|
||||
}
|
||||
|
||||
// Send indoor temp + heating state to the local server, read mode + target back.
|
||||
// Answer looks like: mode=A;target=21.00
|
||||
bool pollServer() {
|
||||
if (!ensureWifi()) return false;
|
||||
|
||||
client.setTimeout(3000);
|
||||
if (!client.connect(SERVER_IP, SERVER_PORT)) return false;
|
||||
|
||||
client.print(F("GET /api/arduino?heat="));
|
||||
client.print(heatRequest ? 1 : 0);
|
||||
if (indoorValid()) {
|
||||
client.print(F("&temp="));
|
||||
client.print(lastIndoorTemp, 1);
|
||||
}
|
||||
client.print(F(" HTTP/1.1\r\nHost: "));
|
||||
client.print(SERVER_IP);
|
||||
client.print(F("\r\nConnection: close\r\n\r\n"));
|
||||
|
||||
bool ok = false;
|
||||
if (client.find("mode=")) {
|
||||
char m = 0;
|
||||
if (client.readBytes(&m, 1) == 1 && client.find("target=")) {
|
||||
float t = client.parseFloat();
|
||||
if ((m == 'A' || m == 'M') && t >= 5.0 && t <= 30.0) {
|
||||
modeLetter = m;
|
||||
targetTemp = t;
|
||||
ok = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
client.stop();
|
||||
return ok;
|
||||
}
|
||||
|
||||
// ============ Helpers ============
|
||||
|
||||
// Write to LCD display
|
||||
void lcdLine(uint8_t row, const char *text) {
|
||||
lcd.setCursor(0, row);
|
||||
|
|
@ -72,10 +151,9 @@ void updateShiftRegister() {
|
|||
digitalWrite(latchPin, HIGH);
|
||||
}
|
||||
|
||||
// Measure temperature and humidity
|
||||
// Measure temperature
|
||||
static bool measure_environment(float *temp, float *hum) {
|
||||
static unsigned long timestamp = millis();
|
||||
|
||||
if (millis() - timestamp > 3000ul) {
|
||||
if (dht_sensor.measure(temp, hum) == true) {
|
||||
timestamp = millis();
|
||||
|
|
@ -85,12 +163,32 @@ static bool measure_environment(float *temp, float *hum) {
|
|||
return false;
|
||||
}
|
||||
|
||||
bool indoorValid() {
|
||||
return lastIndoorTemp != NO_TEMP && (millis() - lastIndoorMillis) < SENSOR_STALE_MS;
|
||||
}
|
||||
|
||||
bool serverFresh() {
|
||||
return serverEverOk && (millis() - lastServerOk) < FAILSAFE_MS;
|
||||
}
|
||||
|
||||
// Thermostat: decides whether heating is wanted
|
||||
void updateThermostat() {
|
||||
if (!serverFresh() || !indoorValid() || targetTemp == NO_TEMP) {
|
||||
heatRequest = false; // fail-safe: no data -> no heat
|
||||
return;
|
||||
}
|
||||
if (lastIndoorTemp < targetTemp - HYSTERESIS) heatRequest = true;
|
||||
else if (lastIndoorTemp >= targetTemp) heatRequest = false;
|
||||
// in between: keep the previous decision (hysteresis)
|
||||
}
|
||||
|
||||
void setup() {
|
||||
pinMode(mosfetPin, OUTPUT);
|
||||
digitalWrite(mosfetPin, LOW); // heater off from the very start
|
||||
pinMode(latchPin, OUTPUT);
|
||||
pinMode(dataPin, OUTPUT);
|
||||
pinMode(clockPin, OUTPUT);
|
||||
|
||||
Serial.begin(115200);
|
||||
lcd.begin(16, 2);
|
||||
lcdLine(0, "Connecting WiFi");
|
||||
lcdLine(1, "");
|
||||
|
|
@ -107,23 +205,75 @@ void setup() {
|
|||
lastApiFetch = millis();
|
||||
fetchInterval = (outdoorTemp == NO_TEMP) ? RETRY_INTERVAL : API_INTERVAL;
|
||||
lcd.clear();
|
||||
|
||||
mosfetState = true;
|
||||
previousMosfetMillis = millis();
|
||||
lastPoll = millis() - POLL_INTERVAL; // poll the server right away
|
||||
}
|
||||
|
||||
void loop() {
|
||||
float indoorTemp;
|
||||
float indoorHum;
|
||||
unsigned long currentMillis = millis();
|
||||
|
||||
if (measure_environment(&indoorTemp, &indoorHum) == true) {
|
||||
char tempStr[10];
|
||||
dtostrf(indoorTemp, 4, 1, tempStr);
|
||||
char line0[20];
|
||||
snprintf(line0, sizeof(line0), "In: %s C", tempStr);
|
||||
lcdLine(0, line0);
|
||||
// 60 s ON / 5 s OFF cycle (upper limit for the heater)
|
||||
if (mosfetState) {
|
||||
if (currentMillis - previousMosfetMillis >= mosfetOnTime) {
|
||||
mosfetState = false;
|
||||
previousMosfetMillis = currentMillis;
|
||||
}
|
||||
} else {
|
||||
if (currentMillis - previousMosfetMillis >= mosfetOffTime) {
|
||||
mosfetState = true;
|
||||
previousMosfetMillis = currentMillis;
|
||||
}
|
||||
}
|
||||
|
||||
if (millis() - lastApiFetch >= fetchInterval) {
|
||||
unsigned long remainingSeconds = 0;
|
||||
if (mosfetState) {
|
||||
remainingSeconds = (mosfetOnTime - (currentMillis - previousMosfetMillis)) / 1000;
|
||||
} else {
|
||||
remainingSeconds = (mosfetOffTime - (currentMillis - previousMosfetMillis)) / 1000;
|
||||
}
|
||||
|
||||
// Indoor temp
|
||||
float indoorTemp, indoorHum;
|
||||
if (measure_environment(&indoorTemp, &indoorHum) == true) {
|
||||
lastIndoorTemp = indoorTemp;
|
||||
lastIndoorMillis = currentMillis;
|
||||
}
|
||||
|
||||
// Ask the local server every POLL_INTERVAL (blocks for a moment)
|
||||
if (currentMillis - lastPoll >= POLL_INTERVAL) {
|
||||
if (pollServer()) {
|
||||
lastServerOk = millis();
|
||||
serverEverOk = true;
|
||||
}
|
||||
lastPoll = millis();
|
||||
currentMillis = millis();
|
||||
}
|
||||
if (!serverFresh()) modeLetter = '?';
|
||||
|
||||
// Thermostat + heater output
|
||||
updateThermostat();
|
||||
bool heaterOn = heatRequest && mosfetState;
|
||||
digitalWrite(mosfetPin, heaterOn ? HIGH : LOW);
|
||||
|
||||
// LCD row 0 (Temp + heater state + countdown of the ON/OFF cycle)
|
||||
char line0[20];
|
||||
char tempStr[6];
|
||||
if (lastIndoorTemp != NO_TEMP) dtostrf(lastIndoorTemp, 4, 1, tempStr);
|
||||
else strcpy(tempStr, "--- ");
|
||||
if (heatRequest) {
|
||||
snprintf(line0, sizeof(line0), "In:%sC %s %2lu", tempStr, heaterOn ? "ON " : "OFF", remainingSeconds);
|
||||
} else {
|
||||
snprintf(line0, sizeof(line0), "In:%sC OFF --", tempStr);
|
||||
}
|
||||
lcdLine(0, line0);
|
||||
|
||||
// API outdoor temp
|
||||
if (currentMillis - lastApiFetch >= fetchInterval) {
|
||||
float t = fetchOutdoorTemp();
|
||||
lastApiFetch = millis();
|
||||
currentMillis = millis();
|
||||
if (t != NO_TEMP) {
|
||||
outdoorTemp = t;
|
||||
fetchInterval = API_INTERVAL;
|
||||
|
|
@ -132,16 +282,20 @@ void loop() {
|
|||
}
|
||||
}
|
||||
|
||||
// LCD row 1: outdoor temp, mode letter (A / M / ?) in the bottom right corner
|
||||
char left[16];
|
||||
if (outdoorTemp != NO_TEMP) {
|
||||
char outStr[10];
|
||||
dtostrf(outdoorTemp, 4, 1, outStr);
|
||||
char line1[20];
|
||||
snprintf(line1, sizeof(line1), "Out: %s C", outStr);
|
||||
lcdLine(1, line1);
|
||||
snprintf(left, sizeof(left), "Out: %s C", outStr);
|
||||
} else {
|
||||
lcdLine(1, "Out: API Error");
|
||||
snprintf(left, sizeof(left), "Out: API Error");
|
||||
}
|
||||
char line1[20];
|
||||
snprintf(line1, sizeof(line1), "%-15s%c", left, modeLetter);
|
||||
lcdLine(1, line1);
|
||||
|
||||
// Light sensor LEDs
|
||||
int reading = analogRead(lightPin);
|
||||
int numLEDSLit = reading / 57;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,15 +1,15 @@
|
|||
#include <LiquidCrystal.h>
|
||||
#include <dht_nonblocking.h>
|
||||
#include <SoftwareSerial.h>
|
||||
#include <WiFiEsp.h>
|
||||
|
||||
// Wifi (Hotspot)
|
||||
const char wifiSSID[] = "FreeWifi";
|
||||
const char wifiPASS[] = "temppassword";
|
||||
|
||||
// ESP8266 (Wifi module)
|
||||
// ESP8266 (Wifi module, AT firmware)
|
||||
SoftwareSerial wifiSerial(2, 3); // RX = 2, TX = 3
|
||||
const bool DEBUG = true; // Serial Monitor debug output
|
||||
const int responseTime = 100;
|
||||
WiFiEspClient client;
|
||||
|
||||
// LCD Display
|
||||
#define DHT_SENSOR_TYPE DHT_TYPE_11
|
||||
|
|
@ -19,168 +19,50 @@ DHT_nonblocking dht_sensor(DHT_SENSOR_PIN, DHT_SENSOR_TYPE);
|
|||
LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // RS, EN, D4, D5, D6, D7
|
||||
|
||||
// Light sensor and LEDs
|
||||
int lightPin = A0; // LDR
|
||||
int dataPin = 4; // DS
|
||||
int latchPin = 5; // ST_CP
|
||||
int clockPin = 6; // SH_CP
|
||||
int lightPin = A7; // LDR
|
||||
int dataPin = 4; // DS
|
||||
int latchPin = 5; // ST_CP
|
||||
int clockPin = 6; // SH_CP
|
||||
int leds = 0;
|
||||
|
||||
// API
|
||||
const float NO_TEMP = -999.0;
|
||||
const unsigned long API_INTERVAL = 600000UL; // 10m
|
||||
const unsigned long API_INTERVAL = 60000UL; // 60s
|
||||
const unsigned long RETRY_INTERVAL = 30000UL; // 30s
|
||||
unsigned long lastApiFetch = 0;
|
||||
unsigned long fetchInterval = API_INTERVAL;
|
||||
float outdoorTemp = NO_TEMP;
|
||||
|
||||
// HTTP GET request
|
||||
const char HTTP_REQUEST[] PROGMEM =
|
||||
"GET /v1/forecast?latitude=49.46998&longitude=8.4819¤t=temperature_2m HTTP/1.1\r\n"
|
||||
"Host: api.open-meteo.com\r\n"
|
||||
"Accept-Encoding: identity\r\n"
|
||||
"Connection: close\r\n"
|
||||
"\r\n";
|
||||
// MOSFET module (heating elements)
|
||||
const int mosfetPin = 13;
|
||||
unsigned long previousMosfetMillis = 0;
|
||||
bool mosfetState = true;
|
||||
const unsigned long mosfetOnTime = 60000UL/*60000UL*/; // 60 seconds ON
|
||||
const unsigned long mosfetOffTime = 5000UL; // 60 seconds OFF
|
||||
|
||||
// Helpers
|
||||
// Clear serial buffer
|
||||
void flushInput() {
|
||||
while (wifiSerial.available()) wifiSerial.read();
|
||||
}
|
||||
// Store the indoor temp
|
||||
float lastIndoorTemp = NO_TEMP;
|
||||
|
||||
// Discard incoming data for a given time
|
||||
void drain(unsigned long ms) {
|
||||
unsigned long start = millis();
|
||||
while (millis() - start < ms) flushInput();
|
||||
}
|
||||
|
||||
// Send AT command
|
||||
void espSend(const __FlashStringHelper *cmd) {
|
||||
flushInput();
|
||||
wifiSerial.println(cmd);
|
||||
}
|
||||
|
||||
// Wait for specific responses
|
||||
uint8_t waitForEither(const char *a, const char *b, unsigned long timeoutMs) {
|
||||
const uint8_t na = strlen(a);
|
||||
const uint8_t nb = strlen(b);
|
||||
char wa[24] = {0};
|
||||
char wb[24] = {0};
|
||||
unsigned long start = millis();
|
||||
|
||||
while (millis() - start < timeoutMs) {
|
||||
while (wifiSerial.available()) {
|
||||
char c = wifiSerial.read();
|
||||
if (DEBUG) Serial.write(c);
|
||||
|
||||
memmove(wa, wa + 1, na - 1);
|
||||
wa[na - 1] = c;
|
||||
memmove(wb, wb + 1, nb - 1);
|
||||
wb[nb - 1] = c;
|
||||
|
||||
if (memcmp(wa, a, na) == 0) return 1;
|
||||
if (memcmp(wb, b, nb) == 0) return 2;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool waitFor(const char *token, unsigned long timeoutMs) {
|
||||
return waitForEither(token, token, timeoutMs) == 1;
|
||||
}
|
||||
|
||||
// Parse float from serial stream
|
||||
float readNumber(unsigned long timeoutMs) {
|
||||
char buf[10];
|
||||
uint8_t i = 0;
|
||||
bool done = false;
|
||||
unsigned long start = millis();
|
||||
|
||||
while (!done && millis() - start < timeoutMs) {
|
||||
while (wifiSerial.available()) {
|
||||
char c = wifiSerial.read();
|
||||
if (c == ',' || c == '}') { done = true; break; }
|
||||
if (i < sizeof(buf) - 1) buf[i++] = c;
|
||||
}
|
||||
}
|
||||
buf[i] = '\0';
|
||||
if (!done || i == 0) return NO_TEMP;
|
||||
return atof(buf);
|
||||
}
|
||||
|
||||
// Connect to Wifi
|
||||
bool joinWifi() {
|
||||
espSend(F("AT+CWMODE=1"));
|
||||
waitForEither("OK", "ERROR", 1000);
|
||||
|
||||
flushInput();
|
||||
wifiSerial.print(F("AT+CWJAP=\""));
|
||||
wifiSerial.print(wifiSSID);
|
||||
wifiSerial.print(F("\",\""));
|
||||
wifiSerial.print(wifiPASS);
|
||||
wifiSerial.println('"');
|
||||
if (waitForEither("OK", "FAIL", 20000) != 1) return false;
|
||||
|
||||
espSend(F("AT+CIPDNS_CUR=1,\"8.8.8.8\",\"8.8.4.4\""));
|
||||
waitForEither("OK", "ERROR", 1000);
|
||||
|
||||
espSend(F("AT+CIPMUX=0"));
|
||||
waitForEither("OK", "ERROR", 1000);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check Wifi
|
||||
bool ensureWifi() {
|
||||
espSend(F("AT+CWJAP?"));
|
||||
if (waitForEither("+CWJAP:", "No AP", 3000) == 1) return true;
|
||||
Serial.println(F("\nWi-Fi not connected, (re)joining..."));
|
||||
return joinWifi();
|
||||
}
|
||||
|
||||
// API Temperature
|
||||
// Connect to hotspot if not connected, then fetch the current temperature
|
||||
float fetchOutdoorTemp() {
|
||||
Serial.println(F("\n--- Fetching outdoor temp ---"));
|
||||
drain(300);
|
||||
|
||||
if (!ensureWifi()) {
|
||||
Serial.println(F("\nERROR: no Wi-Fi"));
|
||||
return NO_TEMP;
|
||||
}
|
||||
drain(200);
|
||||
|
||||
espSend(F("AT+CIPCLOSE"));
|
||||
waitForEither("OK", "ERROR", 1000);
|
||||
|
||||
espSend(F("AT+CIPSTART=\"TCP\",\"api.open-meteo.com\",80"));
|
||||
if (waitForEither("OK", "ERROR", 10000) != 1) {
|
||||
Serial.println(F("\nERROR: could not open TCP socket"));
|
||||
return NO_TEMP;
|
||||
if (WiFi.status() != WL_CONNECTED) {
|
||||
WiFi.begin(wifiSSID, wifiPASS);
|
||||
if (WiFi.status() != WL_CONNECTED) return NO_TEMP;
|
||||
}
|
||||
|
||||
flushInput();
|
||||
wifiSerial.print(F("AT+CIPSEND="));
|
||||
wifiSerial.println(strlen_P(HTTP_REQUEST));
|
||||
if (!waitFor(">", 3000)) {
|
||||
Serial.println(F("\nERROR: no '>' prompt from ESP"));
|
||||
espSend(F("AT+CIPCLOSE"));
|
||||
waitForEither("OK", "ERROR", 1000);
|
||||
return NO_TEMP;
|
||||
}
|
||||
wifiSerial.print((const __FlashStringHelper *)HTTP_REQUEST);
|
||||
client.setTimeout(5000);
|
||||
if (!client.connect("api.open-meteo.com", 80)) return NO_TEMP;
|
||||
|
||||
client.print(F("GET /v1/forecast?latitude=49.46998&longitude=8.4819¤t=temperature_2m HTTP/1.1\r\n"
|
||||
"Host: api.open-meteo.com\r\n"
|
||||
"Connection: close\r\n"
|
||||
"\r\n"));
|
||||
|
||||
float t = NO_TEMP;
|
||||
if (waitFor("\"current\":", 8000) && waitFor("\"temperature_2m\":", 2000)) {
|
||||
t = readNumber(2000);
|
||||
}
|
||||
|
||||
espSend(F("AT+CIPCLOSE"));
|
||||
waitForEither("OK", "ERROR", 1000);
|
||||
|
||||
if (t == NO_TEMP) {
|
||||
Serial.println(F("\nERROR: no temperature in response"));
|
||||
} else {
|
||||
Serial.print(F("\nSUCCESS! Outdoor temp: "));
|
||||
Serial.println(t);
|
||||
if (client.find("\"current\":") && client.find("\"temperature_2m\":")) {
|
||||
t = client.parseFloat();
|
||||
}
|
||||
client.stop();
|
||||
return t;
|
||||
}
|
||||
|
||||
|
|
@ -199,10 +81,10 @@ void updateShiftRegister() {
|
|||
digitalWrite(latchPin, HIGH);
|
||||
}
|
||||
|
||||
// Measure temperature and humidity
|
||||
// Measure temperature
|
||||
static bool measure_environment(float *temp, float *hum) {
|
||||
static unsigned long timestamp = millis();
|
||||
|
||||
// FIXED syntax error below
|
||||
if (millis() - timestamp > 3000ul) {
|
||||
if (dht_sensor.measure(temp, hum) == true) {
|
||||
timestamp = millis();
|
||||
|
|
@ -212,65 +94,82 @@ static bool measure_environment(float *temp, float *hum) {
|
|||
return false;
|
||||
}
|
||||
|
||||
// Main methods
|
||||
void setup() {
|
||||
pinMode(latchPin, OUTPUT);
|
||||
pinMode(dataPin, OUTPUT);
|
||||
pinMode(clockPin, OUTPUT);
|
||||
pinMode(13, OUTPUT);
|
||||
pinMode(mosfetPin, OUTPUT);
|
||||
|
||||
Serial.begin(115200);
|
||||
lcd.begin(16, 2);
|
||||
lcdLine(0, "Init WiFi...");
|
||||
lcdLine(0, "Connecting WiFi");
|
||||
lcdLine(1, "");
|
||||
|
||||
wifiSerial.begin(9600);
|
||||
delay(1000);
|
||||
WiFi.init(&wifiSerial);
|
||||
|
||||
bool alive = false;
|
||||
for (uint8_t i = 0; i < 3 && !alive; i++) {
|
||||
espSend(F("AT"));
|
||||
alive = (waitForEither("OK", "ERROR", 1000) == 1);
|
||||
}
|
||||
|
||||
if (!alive) {
|
||||
Serial.println(F("\nESP8266 does not answer."));
|
||||
if (WiFi.status() == WL_NO_SHIELD) {
|
||||
lcdLine(0, "ESP not found");
|
||||
delay(2000);
|
||||
} else {
|
||||
espSend(F("ATE0"));
|
||||
waitForEither("OK", "ERROR", 1000);
|
||||
|
||||
lcdLine(0, "Connecting WiFi");
|
||||
if (joinWifi()) {
|
||||
lcdLine(0, "WiFi Connected!");
|
||||
} else {
|
||||
lcdLine(0, "WiFi failed");
|
||||
}
|
||||
delay(1000);
|
||||
}
|
||||
lcd.clear();
|
||||
|
||||
outdoorTemp = fetchOutdoorTemp();
|
||||
lastApiFetch = millis();
|
||||
fetchInterval = (outdoorTemp == NO_TEMP) ? RETRY_INTERVAL : API_INTERVAL;
|
||||
lcd.clear();
|
||||
|
||||
digitalWrite(mosfetPin, HIGH);
|
||||
mosfetState = true;
|
||||
previousMosfetMillis = millis();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
float indoorTemp;
|
||||
float indoorHum;
|
||||
unsigned long currentMillis = millis();
|
||||
|
||||
if (measure_environment(&indoorTemp, &indoorHum) == true) {
|
||||
char tempStr[10];
|
||||
dtostrf(indoorTemp, 4, 1, tempStr);
|
||||
char line0[20];
|
||||
snprintf(line0, sizeof(line0), "In: %s C", tempStr);
|
||||
lcdLine(0, line0);
|
||||
// MOSFET timer
|
||||
if (mosfetState) {
|
||||
if (currentMillis - previousMosfetMillis >= mosfetOnTime) {
|
||||
mosfetState = false;
|
||||
previousMosfetMillis = currentMillis;
|
||||
digitalWrite(mosfetPin, LOW);
|
||||
}
|
||||
} else {
|
||||
if (currentMillis - previousMosfetMillis >= mosfetOffTime) {
|
||||
mosfetState = true;
|
||||
previousMosfetMillis = currentMillis;
|
||||
digitalWrite(mosfetPin, HIGH);
|
||||
}
|
||||
}
|
||||
|
||||
if (millis() - lastApiFetch >= fetchInterval) {
|
||||
unsigned long remainingSeconds = 0;
|
||||
if (mosfetState) {
|
||||
remainingSeconds = (mosfetOnTime - (currentMillis - previousMosfetMillis)) / 1000;
|
||||
} else {
|
||||
remainingSeconds = (mosfetOffTime - (currentMillis - previousMosfetMillis)) / 1000;
|
||||
}
|
||||
|
||||
// Indoor temp
|
||||
float indoorTemp, indoorHum;
|
||||
if (measure_environment(&indoorTemp, &indoorHum) == true) {
|
||||
lastIndoorTemp = indoorTemp;
|
||||
}
|
||||
|
||||
// LCD row 0 (Temp + Countdown)
|
||||
char line0[20];
|
||||
if (lastIndoorTemp != NO_TEMP) {
|
||||
char tempStr[6];
|
||||
dtostrf(lastIndoorTemp, 4, 1, tempStr);
|
||||
// Formats into exactly 16 chars: "In:22.5C ON 10"
|
||||
snprintf(line0, sizeof(line0), "In:%sC %s %2lu", tempStr, mosfetState ? "ON " : "OFF", remainingSeconds);
|
||||
} else {
|
||||
snprintf(line0, sizeof(line0), "In:--- C %s %2lu", mosfetState ? "ON " : "OFF", remainingSeconds);
|
||||
}
|
||||
lcdLine(0, line0);
|
||||
|
||||
// API outdoor temp
|
||||
if (currentMillis - lastApiFetch >= fetchInterval) {
|
||||
float t = fetchOutdoorTemp();
|
||||
lastApiFetch = millis();
|
||||
lastApiFetch = currentMillis;
|
||||
if (t != NO_TEMP) {
|
||||
outdoorTemp = t;
|
||||
fetchInterval = API_INTERVAL;
|
||||
|
|
@ -279,6 +178,7 @@ void loop() {
|
|||
}
|
||||
}
|
||||
|
||||
// LCD row 1
|
||||
if (outdoorTemp != NO_TEMP) {
|
||||
char outStr[10];
|
||||
dtostrf(outdoorTemp, 4, 1, outStr);
|
||||
|
|
@ -289,15 +189,16 @@ void loop() {
|
|||
lcdLine(1, "Out: API Error");
|
||||
}
|
||||
|
||||
// Light sensor LEDs
|
||||
int reading = analogRead(lightPin);
|
||||
int numLEDSLit = reading / 57;
|
||||
int numLEDSLit = reading / 57;
|
||||
|
||||
if (numLEDSLit > 8) numLEDSLit = 8;
|
||||
leds = 0;
|
||||
for (int i = 0; i < numLEDSLit; i++) {
|
||||
leds = leds + (1 << i);
|
||||
leds = leds + (1 << i);
|
||||
}
|
||||
updateShiftRegister();
|
||||
|
||||
delay(responseTime);
|
||||
delay(100);
|
||||
}
|
||||
|
|
@ -11,7 +11,7 @@ const char wifiPASS[] = "temppassword";
|
|||
|
||||
// Computer that runs the Go server (IP of the PC in the hotspot network).
|
||||
// The server prints it when it starts. If the PC gets a new IP, change it here.
|
||||
const char SERVER_IP[] = "172.20.46.81";
|
||||
const char SERVER_IP[] = "10.103.181.81";
|
||||
const int SERVER_PORT = 8080;
|
||||
|
||||
const unsigned long POLL_INTERVAL = 5000UL; // ask the server every 5 s
|
||||
|
|
@ -23,7 +23,7 @@ const float HYSTERESIS = 0.5; // on below (target - 0.5), off
|
|||
// ============ Hardware ============
|
||||
|
||||
// ESP8266 (Wifi module, AT firmware)
|
||||
SoftwareSerial wifiSerial(0, 1); // RX, TX
|
||||
SoftwareSerial wifiSerial(51, 52); // RX, TX
|
||||
WiFiEspClient client;
|
||||
|
||||
// DHT11 + LCD
|
||||
|
|
@ -35,9 +35,9 @@ LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // RS, EN, D4, D5, D6, D7
|
|||
|
||||
// Light sensor and LEDs
|
||||
int lightPin = A7; // LDR
|
||||
int dataPin = 4; // DS
|
||||
int latchPin = 5; // ST_CP
|
||||
int clockPin = 6; // SH_CP
|
||||
int dataPin = 42; // DS
|
||||
int latchPin = 44; // ST_CP
|
||||
int clockPin = 46; // SH_CP
|
||||
int leds = 0;
|
||||
|
||||
// ============ State ============
|
||||
|
|
|
|||
Loading…
Reference in New Issue