Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Include the libraries we need
- #define BLYNK_PRINT Serial // Defines the object that is used for printing
- //#define BLYNK_DEBUG // Optional, this enables more detailed prints
- #define ONE_WIRE_BUS_BEER D1
- #define ONE_WIRE_BUS_AMBIENT D2
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #include <ESP8266WiFi.h>
- #include <BlynkSimpleEsp8266.h>
- #include <ESP8266HTTPClient.h>
- #include <ArduinoJson.h>
- #include <TimeLib.h>
- #include <WidgetRTC.h>
- #include "FS.h"
- const char* device = "F1";
- const char* address = "http://192.168.88.88:1880/F1";
- float tempC;
- float ambient;
- float pressure;
- float Tsp;
- float Thyst;
- float Psp;
- float Physt;
- int Pzero;
- int bitsprbar = 1;
- float coolthreshold;
- bool syncneeded;
- int temp_steptime[10];
- float temp_steptemp[10];
- float temp_stepramp[10];
- const char* fermnames[10];
- String beername;
- int stepsize;
- int nextsteptime;
- int nextramptime;
- int stepcounter;
- int fermenting;
- int currenttime;
- String startTimeDate;
- time_t starttime;
- const char pressdelay = 1; //seconds
- int lastpress;
- int conversiontime = 2000;
- const int resolution = 11;
- unsigned long lastTime;
- const char auth[] = "";
- const char ssid[] = "";
- const char pass[] = "";
- HTTPClient http; //HTTPClient used in getdata
- WidgetLED ledheat(V0);
- WidgetLED ledcool(V13);
- WidgetLED ledpressure(V11);
- WidgetTerminal terminal(V69);
- WidgetLCD lcd(V68);
- WidgetRTC rtc;
- // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
- OneWire oneWire_beer(ONE_WIRE_BUS_BEER);
- OneWire oneWire_ambient(ONE_WIRE_BUS_AMBIENT);
- // Pass our oneWire reference to DS18B20 lib.
- DallasTemperature sensor_beer(&oneWire_beer);
- DallasTemperature sensor_ambient(&oneWire_ambient);
- BlynkTimer timer;
- BLYNK_CONNECTED() {
- Blynk.syncAll();
- }
- BLYNK_WRITE(V3) { // Value from Blynk slider or numeric input
- Tsp = param.asFloat(); // assigning incoming value from pin V1 to a variable
- }
- BLYNK_WRITE(V10) { // Value from Blynk slider or numeric input
- Psp = param.asFloat(); // assigning incoming value from pin V1 to a variable
- }
- BLYNK_WRITE(V5) { // Value from Blynk slider or numeric input
- Thyst = param.asFloat(); // assigning incoming value from pin V2 to a variable
- }
- BLYNK_WRITE(V6) { // Value from Blynk slider or numeric input
- Physt = param.asFloat(); // assigning incoming value from pin V2 to a variable
- }
- BLYNK_WRITE(V7) { // Value from Blynk slider or numeric input
- Pzero = param.asInt(); // assigning incoming value from pin V2 to a variable
- }
- BLYNK_WRITE(V8) { // Value from Blynk slider or numeric input
- bitsprbar = param.asInt(); // assigning incoming value from pin V2 to a variable
- }
- BLYNK_WRITE(V14) { // Value from Blynk slider or numeric input
- coolthreshold = param.asFloat(); // assigning incoming value from pin V2 to a variable
- }
- BLYNK_WRITE(V51) {
- int loadbutton = param.asInt();
- if (loadbutton == 1 && currenttime - lastpress >= pressdelay) {
- lastpress = now();
- getdata();
- loadbutton = 0;
- }
- }
- BLYNK_WRITE(V52) {
- beername = param[0].asString();
- stepsize = param[1].asInt();
- }
- BLYNK_WRITE(V54) {
- int clearbutton = param.asInt();
- if (clearbutton == 1 && currenttime - lastpress >= pressdelay && syncneeded == false) {
- clearbutton = 0;
- lastpress = now();
- cleardata();
- }
- }
- BLYNK_WRITE(V55) {
- int startbutton = param.asInt();
- if (startbutton == 1 && (currenttime - lastpress >= pressdelay) && syncneeded == false) {
- lastpress = now();
- startferment();
- }
- }
- BLYNK_WRITE(V56) {
- startTimeDate = param.asString();
- }
- BLYNK_WRITE(V110) {
- stepcounter = param.asInt();
- }
- BLYNK_WRITE(V111) {
- fermenting = param.asInt();
- }
- BLYNK_WRITE(V120) {
- nextsteptime = param.asInt();
- }
- BLYNK_WRITE(V122) {
- starttime = param.asInt();
- }
- BLYNK_WRITE(V123) {
- nextramptime = param.asInt();
- }
- void reportpressurevalue() {
- Blynk.virtualWrite(V9, pressure);
- Blynk.virtualWrite(V10, Psp);
- }
- void reporttime() { //reports reading to blynk every time reportvalue gets called
- currenttime = now();
- Blynk.virtualWrite(V121, currenttime);
- if (syncneeded == true) {
- Blynk.virtualWrite(V52, beername, stepsize);
- byte atstep = stepsize - 1;
- terminal.clear();
- if (atstep >= 0) {
- // String fermname = String(fermnames[0]);
- String s = (String(fermnames[0]) + " @ " + String(temp_steptemp[0]) + "°C" + " for " + String(temp_steptime[0]) + " days");
- terminal.println(s);
- }
- if (atstep >= 1) {
- String s = (String(fermnames[1]) + " @ " + String(temp_steptemp[1]) + "°C" + " for " + String(temp_steptime[1]) + " days");
- terminal.println(s);
- // terminal.flush();
- }
- if (atstep >= 2) {
- String s = (String(fermnames[2]) + " @ " + String(temp_steptemp[2]) + "°C" + " for " + String(temp_steptime[2]) + " days");
- terminal.println(s);
- //terminal.flush();
- }
- if (atstep >= 3) {
- String s = (String(fermnames[3]) + " @ " + String(temp_steptemp[3]) + "°C" + " for " + String(temp_steptime[3]) + " days");
- terminal.println(s);
- // terminal.flush();
- }
- if (atstep >= 4) {
- String s = (String(fermnames[4]) + " @ " + String(temp_steptemp[4]) + "°C" + " for " + String(temp_steptime[4]) + " days");
- terminal.println(s);
- // terminal.flush();
- }
- if (atstep >= 5) {
- String s = (String(fermnames[5]) + " @ " + String(temp_steptemp[5]) + "°C" + " for " + String(temp_steptime[5]) + " days");
- terminal.println(s);
- // terminal.flush();
- }
- if (atstep >= 6) {
- String s = (String(fermnames[6]) + " @ " + String(temp_steptemp[6]) + "°C" + " for " + String(temp_steptime[6]) + " days");
- terminal.println(s);
- // terminal.flush();
- }
- if (atstep >= 7) {
- String s = (String(fermnames[7]) + " @ " + String(temp_steptemp[7]) + "°C" + " for " + String(temp_steptime[7]) + " days");
- terminal.println(s);
- // terminal.flush();
- }
- if (atstep >= 8) {
- String s = (String(fermnames[8]) + " @ " + String(temp_steptemp[8]) + "°C" + " for " + String(temp_steptime[8]) + " days");
- terminal.println(s);
- // terminal.flush();
- }
- if (atstep >= 9) {
- String s = (String(fermnames[9]) + " @ " + String(temp_steptemp[9]) + "°C" + " for " + String(temp_steptime[9]) + " days");
- terminal.println(s);
- //terminal.flush();
- }
- terminal.flush();
- syncneeded = false;
- delay(2000);
- }
- }
- void getdata() { // Opens a http GET to Node Red, deserialize the JSON and save the arrays in SPIFFS.
- Serial.println("getting data");
- if (WiFi.status() == WL_CONNECTED)
- {
- http.begin(address);
- int httpCode = http.GET();
- if (httpCode > 0) {
- const size_t capacity = 14 * JSON_ARRAY_SIZE(0) + 6 * JSON_ARRAY_SIZE(1) + 4 * JSON_ARRAY_SIZE(2) + 3 * JSON_ARRAY_SIZE(3) + JSON_ARRAY_SIZE(4) + JSON_ARRAY_SIZE(10) + 18 * JSON_OBJECT_SIZE(2) + 10 * JSON_OBJECT_SIZE(3) + 3 * JSON_OBJECT_SIZE(4) + 4 * JSON_OBJECT_SIZE(5) + JSON_OBJECT_SIZE(6) + 13 * JSON_OBJECT_SIZE(7) + JSON_OBJECT_SIZE(8) + 3 * JSON_OBJECT_SIZE(9) + 6 * JSON_OBJECT_SIZE(12) + JSON_OBJECT_SIZE(13) + 2 * JSON_OBJECT_SIZE(14) + 2 * JSON_OBJECT_SIZE(15) + 3 * JSON_OBJECT_SIZE(16) + 4 * JSON_OBJECT_SIZE(19) + JSON_OBJECT_SIZE(22) + 4 * JSON_OBJECT_SIZE(23) + JSON_OBJECT_SIZE(30) + JSON_OBJECT_SIZE(39) + JSON_OBJECT_SIZE(63) + 10740;
- DynamicJsonDocument doc(capacity);
- deserializeJson(doc, http.getString());
- const char* beer = doc["recipe"]["name"];
- beername = String(beer);
- Serial.println(beer);
- JsonArray steps_repo = doc["recipe"]["fermentation"]["steps"];
- stepsize = steps_repo.size();
- int i = 0;
- for (JsonObject repo : steps_repo) {
- temp_steptime[i] = repo["stepTime"].as<int>();
- i++;
- //Serial.println(temp_steptime[i]);
- }
- // open file for writing steptime
- File f = SPIFFS.open("/steptime.txt", "w");
- if (!f) {
- Serial.println("file open failed");
- }
- Serial.println("====== Writing to SPIFFS file =========");
- // write 10 strings to file
- for (byte z = 0; z < stepsize; z++) {
- f.println(temp_steptime[z]);
- Serial.print("written:... ");
- Serial.println(temp_steptime[z]);
- }
- f.close();
- i = 0;
- for (JsonObject repo : steps_repo) {
- temp_steptemp[i] = repo["stepTemp"].as<float>();
- i++;
- //Serial.println(temp_steptemp[i]);
- }
- // open file for writing steptemp
- File a = SPIFFS.open("/steptemp.txt", "w");
- if (!a) {
- Serial.println("file open failed");
- }
- Serial.println("====== Writing to SPIFFS file =========");
- // write 10 strings to file
- for (byte z = 0; z < stepsize; z++) {
- a.println(temp_steptemp[z]);
- Serial.print("written:... ");
- Serial.println(temp_steptemp[z]);
- }
- a.close();
- i = 0;
- for (JsonObject repo : steps_repo) {
- temp_stepramp[i] = repo["ramp"].as<int>();
- i++;
- //Serial.println(temp_stepramp[i]);
- }
- // open file for writing stepramp
- File c = SPIFFS.open("/stepramp.txt", "w");
- if (!c) {
- Serial.println("file open failed");
- }
- Serial.println("====== Writing to SPIFFS file =========");
- // write 10 strings to file
- for (byte z = 0; z < stepsize; z++) {
- c.println(temp_stepramp[z]);
- Serial.print("written:... ");
- Serial.println(temp_stepramp[z]);
- }
- c.close();
- i = 0;
- for (JsonObject repo : steps_repo) {
- fermnames[i] = repo["type"].as<char *>();
- i++;
- //Serial.println(fermnames[i]);
- }
- // open file for writing steptemp
- File d = SPIFFS.open("/fermnames.txt", "w");
- if (!d) {
- Serial.println("file open failed");
- }
- Serial.println("====== Writing to SPIFFS file =========");
- // write 10 strings to file
- for (byte z = 0; z < stepsize; z++) {
- d.println(fermnames[z]);
- Serial.print("written:... ");
- Serial.println(fermnames[z]);
- }
- d.close();
- syncneeded = true;
- Serial.println("OK");
- }
- http.end(); //Close connection
- }
- }
- void startferment() { //when startbutton is pressed
- if (syncneeded == false) {
- fermenting = 1;
- starttime = now();
- String startTimeDate = String(day(starttime)) + "." + month(starttime) + "." + year(starttime) + " " + hour(starttime) + ":" + minute(starttime);
- stepcounter = 0;
- nextsteptime = starttime + (temp_steptime[0] * 86400);
- nextramptime = nextsteptime - (temp_stepramp[0] * 86400);
- Tsp = temp_steptemp[stepcounter];
- String s = "Step" + String(stepcounter + 1) + " @ " + String(temp_steptemp[stepcounter]) + "°C";
- lcd.clear();
- lcd.print(0, 0, "Fermenting");
- lcd.print(0, 1, s);
- Blynk.virtualWrite(V3, Tsp);
- Blynk.virtualWrite(V56, startTimeDate);
- Blynk.virtualWrite(V111, fermenting);
- Blynk.virtualWrite(V120, nextsteptime);
- Blynk.virtualWrite(V122, starttime);
- Blynk.virtualWrite(V123, nextramptime);
- }
- }
- void cleardata() { // when clearbutton is pressed
- if (syncneeded == false) {
- fermenting = 0;
- stepcounter = 0;
- nextsteptime = 1666666666;
- nextramptime = 1666666666;
- terminal.clear();
- lcd.clear();
- lcd.print(0, 0, "Stopped");
- Blynk.virtualWrite(V56, "x");
- Blynk.virtualWrite(V110, stepcounter);
- Blynk.virtualWrite(V111, fermenting);
- Blynk.virtualWrite(V120, nextsteptime);
- Blynk.virtualWrite(V123, nextramptime);
- Serial.println("clear");
- }
- }
- void newstep() { //when its time to go to next step
- if (syncneeded == false) {
- stepcounter += 1;
- if (stepcounter >= (stepsize - 1) ) {
- stepcounter = (stepsize - 1);
- }
- Tsp = temp_steptemp[stepcounter];
- int totalsteptime = 0;
- for (int i = 0; i <= stepcounter; i++) {
- totalsteptime += temp_steptime[i];
- }
- nextsteptime = starttime + (totalsteptime * 86400);
- nextramptime = nextsteptime - (temp_stepramp[stepcounter] * 86400);
- String s = "step" + String(stepcounter + 1) + " @ " + String(temp_steptemp[stepcounter]) + "°C";
- lcd.clear();
- lcd.print(0, 0, "Fermenting");
- lcd.print(0, 1, s);
- Blynk.virtualWrite(V3, Tsp);
- Blynk.virtualWrite(V110, stepcounter);
- Blynk.virtualWrite(V120, nextsteptime);
- Blynk.virtualWrite(V123, nextramptime);
- }
- }
- void ramp() { // calculate ramp setpoint
- if (currenttime > nextramptime && fermenting == 1) {
- int next = stepcounter + 1;
- float a = (currenttime - nextramptime);
- float c = (nextsteptime - nextramptime);
- float rampfactor = a / c;
- Tsp = (rampfactor * (temp_steptemp[next] - temp_steptemp[stepcounter]) + temp_steptemp[stepcounter]);
- String s = String(temp_steptemp[stepcounter + 1]) + "-> " + String(temp_steptemp[next + 1]) + "°C";
- String t = "Ramping->step" + String(next);
- lcd.clear();
- lcd.print(0, 0, t);
- lcd.print(0, 1, s);
- Blynk.virtualWrite(V3, Tsp);
- }
- }
- void readpressuresensor() { //read pressure sensor
- if (syncneeded == false) {//
- // read the value from the analog sensor:
- int currentSensorValue = analogRead(A0);
- //map readings from purgepot to purgetime
- pressure = ((currentSensorValue - Pzero) / bitsprbar);
- // Serial.print("analog: "); Serial.println(currentSensorValue);
- }
- }
- void readtempsensor() { //read tempsensors
- tempC = sensor_beer.getTempCByIndex(0);
- ambient = sensor_ambient.getTempCByIndex(0);
- sensor_ambient.requestTemperatures();
- sensor_beer.requestTemperatures();
- Blynk.virtualWrite(V1, tempC);
- Blynk.virtualWrite(V12, ambient);
- }
- void tcp() { //send logdata to Node Red
- const uint16_t port = 8881; // port to use
- const char * host = "192.168.88.88"; // address of server
- // Use WiFiClient class to create TCP connections
- WiFiClient client;
- if (!client.connect(host, port)) {
- Serial.println("connection failed");
- Serial.println("wait 5 sec...");
- loop();
- }
- const size_t capacity = JSON_OBJECT_SIZE(12);
- DynamicJsonDocument doc(capacity);
- JsonObject object = doc.to<JsonObject>();
- doc["name"] = device;
- doc["temp"] = tempC;
- //doc["aux_temp"] = 15.61;
- doc["ext_temp"] = ambient;
- doc["temp_unit"] = "C";
- //doc["gravity"] = 1.042;
- //doc["gravity_unit"] = "SG";
- doc["pressure"] = pressure;
- doc["pressure_unit"] = "BAR";
- //doc["ph"] = 4.12;
- //doc["comment"] = "Hello World";
- doc["beer"] = beername;
- serializeJsonPretty(object, client);
- Serial.println("TCP sent");
- client.stop();
- }
- void setup()
- {
- // start serial port
- Serial.begin(9600);
- Blynk.begin(auth, ssid, pass);
- rtc.begin();
- currenttime = now();
- Blynk.syncAll();
- Serial.println("Start SPIFFS....");
- SPIFFS.begin();
- Serial.println("Started");
- delay(3000);
- // Next lines have to be done ONLY ONCE!!!!!When SPIFFS is formatted ONCE you can comment these lines out!!
- //Serial.println("Please wait 30 secs for SPIFFS to be formatted");
- // SPIFFS.format();
- // Serial.println("Spiffs formatted");
- File f = SPIFFS.open("/steptime.txt", "r");
- if (!f) {
- Serial.println("file open failed");
- } Serial.println("====== Reading steptime from SPIFFS file =======");
- for (int i = 0; i < stepsize; i++) {
- int reading = f.parseInt();
- temp_steptime[i] = reading;
- Serial.print("Reading... ");
- Serial.println(temp_steptime[i]);
- }
- f.close();
- File a = SPIFFS.open("/steptemp.txt", "r");
- if (!a) {
- Serial.println("file open failed");
- } Serial.println("====== Reading steptemp from SPIFFS file =======");
- for (int i = 0; i < stepsize; i++) {
- float reading = a.parseFloat();
- temp_steptemp[i] = reading;
- Serial.print("Reading... ");
- Serial.println(temp_steptemp[i]);
- }
- a.close();
- File c = SPIFFS.open("/stepramp.txt", "r");
- if (!c) {
- Serial.println("file open failed");
- } Serial.println("====== Reading stepramp from SPIFFS file =======");
- for (int i = 0; i < stepsize; i++) {
- float reading = c.parseFloat();
- temp_stepramp[i] = reading;
- Serial.print("Reading... ");
- Serial.println(temp_stepramp[i]);
- }
- c.close();
- File d = SPIFFS.open("/fermnames.txt", "r");
- if (!d) {
- Serial.println("file open failed");
- } Serial.println("====== Reading fermnames from SPIFFS file =======");
- for (byte i = 0; i < stepsize; i++) {
- String linefromfile = d.readStringUntil('\n');
- const char *stringtochar = linefromfile.c_str();
- fermnames[i] = stringtochar;
- Serial.print("Reading... ");
- Serial.println(fermnames[i]);
- }
- d.close();
- sensor_beer.begin();
- sensor_ambient.begin();
- sensor_beer.setResolution(resolution);
- sensor_ambient.setResolution(resolution);
- sensor_ambient.requestTemperatures();
- sensor_beer.requestTemperatures();
- sensor_beer.setWaitForConversion(false);
- sensor_ambient.setWaitForConversion(false);
- timer.setInterval(2000L, reporttime);
- timer.setInterval(5000L, reportpressurevalue);
- timer.setInterval(500L, readpressuresensor);
- timer.setInterval(10000L, readtempsensor);
- timer.setInterval(60000L, ramp);
- timer.setInterval(30000L, tcp);
- }
- void loop() {
- Blynk.run();
- timer.run(); // Initiates BlynkTimer
- // setpoint increase
- if (now() > nextsteptime) {
- if (fermenting == 1); {
- newstep();
- }
- }
- //heat
- if ((tempC <= Tsp - Thyst) && (ambient < Tsp)) {
- ledheat.on();
- }
- else if (tempC >= Tsp) {
- ledheat.off();
- }
- //cool
- if ((tempC >= Tsp + Thyst) && ((ambient + coolthreshold) > Tsp)) {
- ledcool.on();
- }
- else if (tempC <= Tsp) {
- ledcool.off();
- }
- //pressure
- if (pressure >= Psp - Physt) {
- ledpressure.on();
- }
- else if (pressure <= Psp) {
- ledpressure.off();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment