freectl.ino 5.77 KB
Newer Older
DooM's avatar
DooM committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
#include <DallasTemperature.h>
#include <OneWire.h>

#include <PubSubClient.h>
#include <Wire.h>
#include "SSD1306.h"
#include "images.h"
SSD1306  display(0x3c, 5, 4);
#include <WiFi.h>
#include <WiFiUdp.h>
#include <NTPClient.h>


const char* ntpServer = "pool.ntp.org";
const long  gmtOffset_sec = 0000;
const int   daylightOffset_sec = 0000;
IPAddress server(192, 168, 178, 3);
unsigned int loopcount = 127;
WiFiUDP ntpUDP;
WiFiClient client;
PubSubClient mqtt_client(client);
NTPClient timeClient(ntpUDP);
OneWire oneWire(13);
DallasTemperature sensors(&oneWire);
DeviceAddress thermometer;

int reqTemp = 0;
float potPos = 0;
float temp = 0.0;
const int wdtTimeout = 6000;
hw_timer_t *timer = NULL;


void IRAM_ATTR resetModule() {
  esp_restart();
}

String ipToString(IPAddress ip) {
  String s = "";
  for (int i = 0; i < 4; i++)
    s += i  ? "." + String(ip[i]) : String(ip[i]);
  return s;
}

void mqtt_connect() {
  mqtt_client.setServer(server, 1883);
  int loops = 0;
  while (!mqtt_client.connected()) {
    if (loops > 5) {
      break;
    }
    loops++;
    String clientId = "FreezeCTL-";
    clientId += String(random(0xffff), HEX);
    if (mqtt_client.connect(clientId.c_str())) {
      mqtt_client.publish("status/freezeCTL/heartbeat", (char*) String(timeClient.getEpochTime()).c_str());
    } else {
      delay(5000);
    }
  }
}

void setup() {
  pinMode(15, OUTPUT);
  // put your setup code here, to run once:
  timer = timerBegin(0, 80, true);                  //timer 0, div 80
  timerAttachInterrupt(timer, &resetModule, true);  //attach callback
  timerAlarmWrite(timer, wdtTimeout * 1000, false); //set time in us
  timerAlarmEnable(timer);
  digitalWrite(15, HIGH);
  display.init();
  display.flipScreenVertically();
  display.setContrast(20);
  display.drawXbm(34, 4, WiFi_Logo_width, WiFi_Logo_height, WiFi_Logo_bits);
  display.setTextAlignment(TEXT_ALIGN_CENTER);
  display.drawString(64, 40, "Initializing...");
  display.display();
  display.clear();
  display.drawXbm(34, 4, WiFi_Logo_width, WiFi_Logo_height, WiFi_Logo_bits);
  display.drawString(64, 40, "Initializing...");
  display.display();
  display.clear();
  delay(150);
  sensors.begin();
  if (! sensors.getAddress(thermometer, 0)) {
    display.drawXbm(34, 4, WiFi_Logo_width, WiFi_Logo_height, WiFi_Logo_bits);
    display.drawString(64, 40, "No DS18B20 found!");
    display.display();
    display.clear();
    delay(2500);
  }
  //sensors.setResolution(thermometer, 10);
  delay(100);
  display.drawXbm(34, 4, WiFi_Logo_width, WiFi_Logo_height, WiFi_Logo_bits);
  display.drawString(64, 40, "Connecting...");
  display.display();
  display.clear();
  WiFi.begin("Stratum0", "stehtinderinfoecke");
  int loops = 0;
  timerWrite(timer, 0);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    if (loops > 4) {
      break;
    }
    loops++;
  }
  randomSeed(micros());
  display.drawXbm(34, 4, WiFi_Logo_width, WiFi_Logo_height, WiFi_Logo_bits);
  display.drawString(64, 40, "Connected!");
  display.drawString(64, 51, "IP: " + ipToString(WiFi.localIP()));
  display.display();
  display.clear();
  delay(1500);
  display.drawXbm(34, 4, WiFi_Logo_width, WiFi_Logo_height, WiFi_Logo_bits);
  display.drawString(64, 40, "NTP sync...");
  display.drawString(64, 51, "IP: " + ipToString(WiFi.localIP()));
  display.display();
  display.clear();
  timerWrite(timer, 0);
  timeClient.begin();
  display.drawXbm(34, 4, WiFi_Logo_width, WiFi_Logo_height, WiFi_Logo_bits);
  display.drawString(64, 40, "MQTT connecting...");
  display.drawString(64, 51, "IP: " + ipToString(WiFi.localIP()));
  display.display();
  display.clear();
  timerWrite(timer, 0);
  mqtt_connect();
  display.drawXbm(34, 4, WiFi_Logo_width, WiFi_Logo_height, WiFi_Logo_bits);
  display.drawString(64, 40, "Init done!");
  display.drawString(64, 51, "IP: " + ipToString(WiFi.localIP()));
  display.display();
  display.clear();
  delay(300);
}

void loop() {
  // put your main code here, to run repeatedly:
  timeClient.update();
  if (temp != 0) {
    timerWrite(timer, 0);
  }
  mqtt_client.loop();
  if (!mqtt_client.connected()) {
    mqtt_connect();
  }

  if (!(loopcount % 290)) {
    if (mqtt_client.connected()) {
      mqtt_client.publish("status/freezeCTL/heartbeat", (char*) String(timeClient.getEpochTime()).c_str());
    }
  }
  if (!(loopcount % 23)) {
    if (sensors.requestTemperaturesByIndex(0)) {
      float temp = sensors.getTempCByIndex(0);
      while (temp == -127) {
        sensors.requestTemperaturesByIndex(0);
        delay(100);
        temp = sensors.getTempCByIndex(0);
      }
    }
    potPos = (analogRead(36) + potPos) / 2.0;
    reqTemp = map(((int)(potPos * 10) / 10), 4095, 0, -10, -35);
    display.setTextAlignment(TEXT_ALIGN_CENTER);
    display.setFont(ArialMT_Plain_24);
    display.drawString(64, 14, String(temp, 1) + " °C");
    display.setFont(ArialMT_Plain_16);
    display.setTextAlignment(TEXT_ALIGN_LEFT);
    display.drawString(0, 48, String(reqTemp) + " °C");
    display.setTextAlignment(TEXT_ALIGN_RIGHT);
    display.setFont(ArialMT_Plain_10);
    String timestr = "UTC: ";
    if (timeClient.getHours() < 10) {
      timestr += "0";
    }
    timestr += String(timeClient.getHours()) + ":";
    if (timeClient.getMinutes() < 10) {
      timestr += "0";
    }
    timestr += String(timeClient.getMinutes());
    display.drawString(128, 52, timestr);
    display.display();
    display.clear();
  }
  if (!(loopcount % 1210)) {
    if (temp < (reqTemp - 1.5)) {
      digitalWrite(15, LOW);
    } 
    if ((temp > (reqTemp + 1.8)) || temp == -127) {
      digitalWrite(15, HIGH);
    }
  }
  if (!(loopcount % 270)) {
    if (mqtt_client.connected()) {
      mqtt_client.publish("status/freezeCTL/istTemp", (char*) String(temp).c_str());
      mqtt_client.publish("status/freezeCTL/sollTemp", (char*) String(reqTemp).c_str());
    }
  }
  loopcount++;
  delay(100);

}