Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <EEPROM.h>
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #include <NTPClient.h>
- #include <WiFiUdp.h>
- #include <SPI.h>
- #include <Wire.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- #define SCREEN_WIDTH 128
- #define SCREEN_HEIGHT 32
- #define OLED_RESET -1
- Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
- int status = WL_IDLE_STATUS;
- const int oneWireBus = 0;
- const int heatPad = 5;
- float temp;
- float idealTemp = -127.00;
- char highTempAlarm = 30;
- char lowTempAlarm = 24;
- DeviceAddress tempDeviceAddress;
- const int utcOffsetInSeconds = 3600 *2;
- WiFiUDP ntpUDP;
- NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);
- int hours;
- unsigned long previousMillis = 0;
- unsigned long previousTimeUpdateMillis = 0;
- const long interval = 5000;
- const long timeUpdateInterval = 1800000; //30 minutes
- const float normalTemps[24] = {30.00, //0:00am
- 30.00, //1:00 am
- 30.00, //2:00 am
- 30.00, //3:00 am
- 30.00, //4:00 am
- 30.00, //5:00 am
- 31.00, //6:00 am
- 31.00, //7:00 am
- 32.00, //8:00 am
- 32.00, //9:00 am
- 32.00, //10:00 am
- 32.00, //11:00 am
- 32.00, //12:00 am
- 32.00, //13:00 am
- 32.00, //14:00 am
- 32.00, //15:00 am
- 32.00, //16:00 am
- 32.00, //17:00 am
- 32.00, //18:00 am
- 32.00, //19:00 am
- 31.00, //20:00 am
- 31.00, //21:00 am
- 30.00, //22:00 am
- 30.00}; //23:00 am
- const float ciclaggioTemps[24] = {28.00, //0:00am
- 28.00, //1:00 am
- 28.00, //2:00 am
- 28.00, //3:00 am
- 28.00, //4:00 am
- 28.00, //5:00 am
- 29.00, //6:00 am
- 29.00, //7:00 am
- 30.00, //8:00 am
- 30.00, //9:00 am
- 30.00, //10:00 am
- 30.00, //11:00 am
- 30.00, //12:00 am
- 30.00, //13:00 am
- 30.00, //14:00 am
- 30.00, //15:00 am
- 30.00, //16:00 am
- 30.00, //17:00 am
- 30.00, //18:00 am
- 30.00, //19:00 am
- 29.00, //20:00 am
- 29.00, //21:00 am
- 28.00, //22:00 am
- 28.00}; //23:00 am
- OneWire oneWire(oneWireBus);
- DallasTemperature sensors(&oneWire);
- WiFiServer server(80);
- WiFiClient client;
- void setup(){
- //Pin setup
- pinMode(heatPad, OUTPUT);
- //Init Serial, Oled Display and Wifi
- if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
- Serial.println(F("SSD1306 allocation failed"));
- for(;;); // Don't proceed, loop forever
- }
- display.clearDisplay();
- setDisplayText("Inizializing...", "", "");
- Serial.begin(115200, SERIAL_8N1, SERIAL_TX_ONLY);
- WiFi.mode(WIFI_STA);
- WiFi.disconnect();
- Serial.println("\nScanning for networks...");
- int n = WiFi.scanNetworks();
- int bestStrenght = -90;
- int bestNetwork = 0;
- for (int i = 0; i < n; i++){
- if (WiFi.SSID(i) == "SSID" or WiFi.SSID(i) == "SSID"){
- if (WiFi.RSSI(i) > bestStrenght){
- bestStrenght = WiFi.RSSI(i);
- bestNetwork = i;
- }
- }
- }
- Serial.println("Attempting to connect to WPA network...");
- Serial.print("SSID: ");
- if (WiFi.SSID(bestNetwork) == "SSID"){
- Serial.println(WiFi.SSID(bestNetwork));
- WiFi.begin(WiFi.SSID(bestNetwork), "PASS");
- }
- else if (WiFi.SSID(bestNetwork) == "SSID"){
- Serial.println(WiFi.SSID(bestNetwork));
- WiFi.begin(WiFi.SSID(bestNetwork), "PASS");
- }
- //Wait for ESP8266 to connect
- while (WiFi.status() != WL_CONNECTED){
- Serial.print(".");
- delay(250);
- }
- Serial.println("");
- Serial.println(WiFi.localIP());
- setDisplayText(WiFi.localIP().toString(), "", "");
- //Initialize mDNS
- //if (!MDNS.begin("esp8266")){
- //Serial.println("Error setting up MDNS responder!");
- //while (1) { delay(1000); }
- //}
- //else { Serial.println("mDNS responder started"); }
- //Init clock
- timeClient.begin();
- timeClient.update();
- hours = timeClient.getHours();
- //Init server and sensors
- server.begin();
- sensors.begin();
- sensors.getAddress(tempDeviceAddress, 0);
- sensors.setHighAlarmTemp(tempDeviceAddress, highTempAlarm);
- sensors.setLowAlarmTemp(tempDeviceAddress, lowTempAlarm);
- //Init EEPROM
- EEPROM.begin(512);
- }
- void loop(void){
- //Update mDNS
- //MDNS.update();
- //Check temperature and adjust heating according
- unsigned long currentMillis = millis();
- if (currentMillis - previousMillis >= interval){
- if (currentMillis - previousTimeUpdateMillis >= timeUpdateInterval){
- previousTimeUpdateMillis = currentMillis;
- hours = timeClient.getHours();
- Serial.println("Current time: " + String(hours) + ".00");
- }
- previousMillis = currentMillis;
- adjustHeating();
- }
- //If a socket is not connected
- if (!client.connected()) {
- client = server.available(); //Look up for a client
- delay(20);
- }
- //Socket connected
- else {
- Serial.println("Socket still connected");
- //Read socket request
- client.setTimeout(5000);
- String req = client.readStringUntil('\n');
- //Call handleRequest and send results
- if (req != ""){
- String result = handleRequest(req);
- client.flush();
- client.println(result);
- }
- }
- }
- String handleRequest(const String& req) {
- Serial.println(req);
- //Handle request based on identificators
- if (req == "read"){ //Read temp
- String tempMode;
- EEPROM.get(0, tempMode);
- return String(temp) + " " + tempMode + " " + idealTemp;
- }
- else if (req == "setTemp"){ //Set temp
- idealTemp = client.readStringUntil('\n').toFloat();
- client.flush();
- EEPROM.put(1, idealTemp);
- EEPROM.commit();
- return "true";
- }
- else if (req == "nM") { //Set NORMAL MODE
- EEPROM.put(0, "0"); //Normal mode == 0
- EEPROM.commit();
- return "true";
- }
- else if (req == "cM"){ //Set CICLAGGIO MODE
- EEPROM.put(0, "1"); //Ciclaggio mode = 1
- EEPROM.commit();
- return "true";
- }
- else if (req == "mM"){ //Set MANUAL MODE
- EEPROM.put(0, "2"); //Manual mode = 2
- EEPROM.commit();
- return "true";
- }
- else { return "unhandled"; }
- }
- void adjustHeating(){
- String tempMode;
- EEPROM.get(0, tempMode);
- if (tempMode == "0"){
- idealTemp = normalTemps[hours];
- }
- else if (tempMode == "1"){
- idealTemp = ciclaggioTemps[hours];
- }
- else if (tempMode == "2"){
- EEPROM.get(1, idealTemp);
- }
- sensors.requestTemperaturesByAddress(tempDeviceAddress);
- temp = sensors.getTempC(tempDeviceAddress);
- if (sensors.hasAlarm(tempDeviceAddress)){
- Serial.println("ALARM");
- }
- if (temp < idealTemp){
- setDisplayText(WiFi.localIP().toString(), String(temp), "ON");
- digitalWrite(heatPad, HIGH);
- }
- else{
- setDisplayText(WiFi.localIP().toString(), String(temp), "OFF");
- digitalWrite(heatPad, LOW);
- }
- }
- void setDisplayText(String topText, String tempText, String stateText){
- Serial.println("IP: "+topText + "\tTemp: "+tempText + "\tState: "+stateText);
- display.clearDisplay();
- display.fillRect(0, 0, 128, 32, BLACK);
- display.setTextSize(1);
- display.setTextColor(WHITE);
- display.setCursor(0, 0);
- display.println(topText);
- display.setCursor(96, 0);
- display.println(stateText);
- display.setTextSize(2);
- display.setCursor(32, 16);
- display.println(tempText);
- display.display();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement