Various changes
- cleanup - improved code style - new method for determining stages
This commit is contained in:
parent
905d8b3163
commit
6dbc50875c
@ -4,8 +4,7 @@
|
|||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
#include <SoftwareSerial.h>
|
#include <SoftwareSerial.h>
|
||||||
#include <Streaming.h>
|
#include <Streaming.h>
|
||||||
#include <SPI.h>
|
#define RELAY_ON 0
|
||||||
#define RELAY_ON 0
|
|
||||||
#define RELAY_OFF 1
|
#define RELAY_OFF 1
|
||||||
// #define ESP_TO_ARDUINO_PIN 2
|
// #define ESP_TO_ARDUINO_PIN 2
|
||||||
SoftwareSerial s(5, 6); //RX,TX
|
SoftwareSerial s(5, 6); //RX,TX
|
||||||
@ -62,9 +61,14 @@ void setup()
|
|||||||
//while (!Serial) continue;
|
//while (!Serial) continue;
|
||||||
// DateTime now = rtc.now();
|
// DateTime now = rtc.now();
|
||||||
//StaticJsonBuffer<400> jsonBuffer1;
|
//StaticJsonBuffer<400> jsonBuffer1;
|
||||||
DynamicJsonBuffer jsonBuffer(bufferSize);
|
// DynamicJsonBuffer jsonBuffer(bufferSize);
|
||||||
// StaticJsonBuffer<100> jsonBuffer;
|
StaticJsonBuffer<bufferSize> jsonBuffer;
|
||||||
JsonObject &root = jsonBuffer.parseObject(s);
|
JsonObject &root = jsonBuffer.parseObject(s);
|
||||||
|
// while (!root.success())
|
||||||
|
// {
|
||||||
|
// Serial << "Invalid or no JSON received at setup." << endl;
|
||||||
|
// root = jsonBuffer.parseObject(s);
|
||||||
|
// }
|
||||||
// root.printTo(data);
|
// root.printTo(data);
|
||||||
// while(root == JsonObject::invalid())
|
// while(root == JsonObject::invalid())
|
||||||
// {
|
// {
|
||||||
@ -104,37 +108,39 @@ void loop()
|
|||||||
if (s.available() > 0)
|
if (s.available() > 0)
|
||||||
{
|
{
|
||||||
// delay(100);
|
// delay(100);
|
||||||
DynamicJsonBuffer jsonBuffer(bufferSize);
|
// DynamicJsonBuffer jsonBuffer(bufferSize);
|
||||||
// const size_t bufsize = JSON_ARRAY_SIZE(3) + JSON_OBJECT_SIZE(2) + 3*JSON_OBJECT_SIZE(5);
|
// const size_t bufsize = JSON_ARRAY_SIZE(3) + JSON_OBJECT_SIZE(2) + 3*JSON_OBJECT_SIZE(5);
|
||||||
// StaticJsonBuffer<bufsize> jsonBuffer;
|
// StaticJsonBuffer<bufsize> jsonBuffer;
|
||||||
// StaticJsonBuffer<bufferSize> jsonBuffer;
|
StaticJsonBuffer<bufferSize> jsonBuffer;
|
||||||
JsonObject &root = jsonBuffer.parseObject(s);
|
JsonObject &root = jsonBuffer.parseObject(s);
|
||||||
// if(root.succes())
|
if (root.success())
|
||||||
Serial << F("Received JSON") << endl;
|
{
|
||||||
root.prettyPrintTo(Serial);
|
Serial << F("Received JSON") << endl;
|
||||||
// // data = "";
|
root.prettyPrintTo(Serial);
|
||||||
// root.printTo(data);
|
// // data = "";
|
||||||
// // newData = true;
|
// root.printTo(data);
|
||||||
// }
|
// // newData = true;
|
||||||
|
// }
|
||||||
|
|
||||||
// if (newData)
|
// if (newData)
|
||||||
// {
|
// {
|
||||||
// Serial.println(F("Parsing data"));
|
// Serial.println(F("Parsing data"));
|
||||||
// DynamicJsonBuffer jsonBuffer(bufferSize);
|
// DynamicJsonBuffer jsonBuffer(bufferSize);
|
||||||
// JsonObject &root = jsonBuffer.parseObject(data);
|
// JsonObject &root = jsonBuffer.parseObject(data);
|
||||||
// root.prettyPrintTo(Serial);
|
// root.prettyPrintTo(Serial);
|
||||||
updateBallast(&ballast1, root);
|
updateBallast(&ballast1, root);
|
||||||
updateBallast(&ballast2, root);
|
updateBallast(&ballast2, root);
|
||||||
updateBallast(&ballast3, root);
|
updateBallast(&ballast3, root);
|
||||||
if (root["manual"] == "True")
|
if (root["manual"] == "True")
|
||||||
{
|
{
|
||||||
manualFlag = true;
|
manualFlag = true;
|
||||||
Serial.println(F("Manual set to true"));
|
Serial.println(F("Manual set to true"));
|
||||||
}
|
}
|
||||||
else if (root["manual"] == "False")
|
else if (root["manual"] == "False")
|
||||||
{
|
{
|
||||||
manualFlag = false;
|
manualFlag = false;
|
||||||
Serial.println(F("Manual set to false"));
|
Serial.println(F("Manual set to false"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// newData = false;
|
// newData = false;
|
||||||
}
|
}
|
||||||
@ -143,7 +149,7 @@ void loop()
|
|||||||
if (currentMillis - loopMillis >= loopPeriod)
|
if (currentMillis - loopMillis >= loopPeriod)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (manualFlag == true)
|
if (manualFlag)
|
||||||
{
|
{
|
||||||
Serial.println(F("Manual Mode"));
|
Serial.println(F("Manual Mode"));
|
||||||
manualMode(&ballast1);
|
manualMode(&ballast1);
|
||||||
@ -157,7 +163,7 @@ void loop()
|
|||||||
doBallast(&ballast3);
|
doBallast(&ballast3);
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial << F("Free RAM: ") << getFreeRam() << endl;
|
// Serial << F("Free RAM: ") << getFreeRam() << endl;
|
||||||
loopMillis = currentMillis;
|
loopMillis = currentMillis;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -165,11 +171,11 @@ void loop()
|
|||||||
if (currentMillis - data_send_millis >= data_send_period)
|
if (currentMillis - data_send_millis >= data_send_period)
|
||||||
{
|
{
|
||||||
StaticJsonBuffer<50> jsonBuffer;
|
StaticJsonBuffer<50> jsonBuffer;
|
||||||
JsonObject& root = jsonBuffer.createObject();
|
JsonObject &root = jsonBuffer.createObject();
|
||||||
JsonArray& lcd_data = root.createNestedArray("lcdData");
|
JsonArray &lcd_data = root.createNestedArray("lcdData");
|
||||||
char time_buffer[10];
|
char time_buffer[10];
|
||||||
DateTime now = rtc.now();
|
DateTime now = rtc.now();
|
||||||
sprintf(time_buffer,"%02d:%02d",now.hour(),now.minute());
|
sprintf(time_buffer, "%02d:%02d", now.hour(), now.minute());
|
||||||
// root["hello"] = ("world");
|
// root["hello"] = ("world");
|
||||||
lcd_data.add("90%%");
|
lcd_data.add("90%%");
|
||||||
lcd_data.add(time_buffer);
|
lcd_data.add(time_buffer);
|
||||||
@ -246,36 +252,44 @@ void setupBallast(ballast *b, JsonObject &root, int i)
|
|||||||
void updateBallast(ballast *b, JsonObject &root)
|
void updateBallast(ballast *b, JsonObject &root)
|
||||||
{
|
{
|
||||||
JsonArray &settings = root["settings"];
|
JsonArray &settings = root["settings"];
|
||||||
JsonObject &settings0 = settings[0];
|
// JsonObject &settings0 = settings[0];
|
||||||
JsonObject &settings1 = settings[1];
|
// JsonObject &settings1 = settings[1];
|
||||||
JsonObject &settings2 = settings[2];
|
// JsonObject &settings2 = settings[2];
|
||||||
if (b->id == 1)
|
// if (b->id == 1)
|
||||||
{
|
// {
|
||||||
b->shour = settings0["shour"];
|
// b->shour = settings0["shour"];
|
||||||
unsigned long fP = settings0["fadePeriod"];
|
// unsigned long fP = settings0["fadePeriod"];
|
||||||
b->fadePeriod = fP * 60000;
|
// b->fadePeriod = fP * 60000;
|
||||||
b->smin = settings0["smin"];
|
// b->smin = settings0["smin"];
|
||||||
b->ehour = settings0["ehour"];
|
// b->ehour = settings0["ehour"];
|
||||||
b->emin = settings0["emin"];
|
// b->emin = settings0["emin"];
|
||||||
}
|
// }
|
||||||
if (b->id == 2)
|
// if (b->id == 2)
|
||||||
{
|
// {
|
||||||
b->shour = settings1["shour"];
|
// b->shour = settings1["shour"];
|
||||||
unsigned long fP = settings1["fadePeriod"];
|
// unsigned long fP = settings1["fadePeriod"];
|
||||||
b->fadePeriod = fP * 60000;
|
// b->fadePeriod = fP * 60000;
|
||||||
b->smin = settings1["smin"];
|
// b->smin = settings1["smin"];
|
||||||
b->ehour = settings1["ehour"];
|
// b->ehour = settings1["ehour"];
|
||||||
b->emin = settings1["emin"];
|
// b->emin = settings1["emin"];
|
||||||
}
|
// }
|
||||||
if (b->id == 3)
|
// if (b->id == 3)
|
||||||
{
|
// {
|
||||||
b->shour = settings2["shour"];
|
// b->shour = settings2["shour"];
|
||||||
unsigned long fP = settings2["fadePeriod"];
|
// unsigned long fP = settings2["fadePeriod"];
|
||||||
b->fadePeriod = fP * 60000;
|
// b->fadePeriod = fP * 60000;
|
||||||
b->smin = settings2["smin"];
|
// b->smin = settings2["smin"];
|
||||||
b->ehour = settings2["ehour"];
|
// b->ehour = settings2["ehour"];
|
||||||
b->emin = settings2["emin"];
|
// b->emin = settings2["emin"];
|
||||||
}
|
// }
|
||||||
|
Serial << "id = " << b->id << endl;
|
||||||
|
JsonObject &settings_i = settings[b->id - 1];
|
||||||
|
b->shour = settings_i["shour"];
|
||||||
|
unsigned long fP = settings_i["fadePeriod"];
|
||||||
|
b->fadePeriod = fP * 60000;
|
||||||
|
b->smin = settings_i["smin"];
|
||||||
|
b->ehour = settings_i["ehour"];
|
||||||
|
b->emin = settings_i["emin"];
|
||||||
}
|
}
|
||||||
int getStage(ballast *b)
|
int getStage(ballast *b)
|
||||||
{
|
{
|
||||||
@ -333,22 +347,44 @@ int getStage(ballast *b)
|
|||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int getStage2(ballast *b)
|
||||||
|
{
|
||||||
|
const DateTime now = rtc.now();
|
||||||
|
int hr = now.hour();
|
||||||
|
int mn = now.minute();
|
||||||
|
Serial.print(F("Current hour is: "));
|
||||||
|
Serial.println(hr);
|
||||||
|
Serial.print(F("Current minute is: "));
|
||||||
|
Serial.println(mn);
|
||||||
|
|
||||||
|
const unsigned int current_time = (now.hour() * 60) + now.minute();
|
||||||
|
const unsigned int start_time = ((b->shour * 60) + b->smin);
|
||||||
|
const unsigned int end_time = ((b->ehour * 60) + b->emin);
|
||||||
|
const unsigned int fade_period = (b->fadePeriod / 60000);
|
||||||
|
|
||||||
|
Serial << F("Current time = ") << current_time << endl
|
||||||
|
<< F("Start time = ") << start_time << endl
|
||||||
|
<< F("End time = ") << end_time << endl
|
||||||
|
<< F("Fade Period = ") << fade_period << endl;
|
||||||
|
|
||||||
|
if (current_time < start_time)
|
||||||
|
return 0;
|
||||||
|
else if (current_time >= start_time && current_time < (start_time + fade_period))
|
||||||
|
return 1;
|
||||||
|
else if (current_time >= start_time + fade_period && current_time < end_time)
|
||||||
|
return 2;
|
||||||
|
else if (current_time >= end_time && (current_time < end_time + fade_period))
|
||||||
|
return 3;
|
||||||
|
else
|
||||||
|
return 4;
|
||||||
|
// const unsigned long time_offset = settings_time - current_time;
|
||||||
|
}
|
||||||
|
|
||||||
void doBallast(ballast *b)
|
void doBallast(ballast *b)
|
||||||
{
|
{
|
||||||
if (b->id == 1)
|
// Log.notice(F("-------------------Ballast%d-------------------"));
|
||||||
{
|
Serial << "-------------------Ballast" << b->id << "-------------------" << endl;
|
||||||
Serial.println(F("-------------------Ballast1-------------------"));
|
|
||||||
}
|
|
||||||
if (b->id == 2)
|
|
||||||
{
|
|
||||||
Serial.println(F("-------------------Ballast2-------------------"));
|
|
||||||
}
|
|
||||||
if (b->id == 3)
|
|
||||||
{
|
|
||||||
Serial.println(F("-------------------Ballast3-------------------"));
|
|
||||||
}
|
|
||||||
// DateTime now = rtc.now();
|
|
||||||
// int hr = now.hour();
|
|
||||||
Serial.print(F("Start hour is: "));
|
Serial.print(F("Start hour is: "));
|
||||||
Serial.println(b->shour);
|
Serial.println(b->shour);
|
||||||
Serial.print(F("Start min is: "));
|
Serial.print(F("Start min is: "));
|
||||||
@ -362,101 +398,120 @@ void doBallast(ballast *b)
|
|||||||
Serial.println(b->emin);
|
Serial.println(b->emin);
|
||||||
Serial.print(F("Brightness: "));
|
Serial.print(F("Brightness: "));
|
||||||
Serial.println(b->brightness);
|
Serial.println(b->brightness);
|
||||||
int stage = getStage(b);
|
const int stage = getStage2(b);
|
||||||
|
const int stage2 = getStage(b);
|
||||||
Serial.print(F("Stage: "));
|
Serial.print(F("Stage: "));
|
||||||
Serial.println(stage);
|
Serial.println(stage);
|
||||||
if (stage == 0)
|
Serial << "Stage2 = " << stage2 << endl;
|
||||||
|
switch (stage)
|
||||||
|
{
|
||||||
|
case 0:
|
||||||
{
|
{
|
||||||
b->stage = 0;
|
b->stage = 0;
|
||||||
b->brightness = 0;
|
b->brightness = 0;
|
||||||
analogWrite(b->pin, b->brightness);
|
analogWrite(b->pin, b->brightness);
|
||||||
Serial.print(F("Stage = "));
|
|
||||||
Serial.println(stage);
|
|
||||||
Serial.print(F("brightness: "));
|
Serial.print(F("brightness: "));
|
||||||
Serial.println(b->brightness);
|
Serial.println(b->brightness);
|
||||||
analogWrite(b->relayPin, RELAY_OFF);
|
digitalWrite(b->relayPin, RELAY_OFF);
|
||||||
}
|
}
|
||||||
else if (stage == 1)
|
break;
|
||||||
|
case 1:
|
||||||
{
|
{
|
||||||
if (currentMillis - b->fadeStartMillis >= (b->fadePeriod / 255))
|
if (currentMillis - b->fadeStartMillis >= (b->fadePeriod / 255))
|
||||||
{
|
{
|
||||||
//Serial.println(b->fadePeriod/255);
|
|
||||||
Serial.print(F("Stage = "));
|
|
||||||
Serial.println(stage);
|
|
||||||
digitalWrite(b->relayPin, RELAY_ON);
|
digitalWrite(b->relayPin, RELAY_ON);
|
||||||
if (b->brightness <= 254)
|
if (b->brightness <= 254)
|
||||||
{
|
{
|
||||||
analogWrite(b->pin, b->brightness++);
|
analogWrite(b->pin, b->brightness++);
|
||||||
}
|
}
|
||||||
//d = b->fadePeriod/255;
|
|
||||||
Serial.print(F("Delay is: "));
|
Serial.print(F("Delay is: "));
|
||||||
//t = d / 1000;
|
|
||||||
Serial.print(b->fadePeriod / 255 / 1000);
|
Serial.print(b->fadePeriod / 255 / 1000);
|
||||||
Serial.println(F(" seconds"));
|
Serial.println(F(" seconds"));
|
||||||
Serial.print(F("Brightness: "));
|
Serial.print(F("Brightness: "));
|
||||||
Serial.println(b->brightness);
|
Serial.println(b->brightness);
|
||||||
//if(b->brightness == 255)
|
|
||||||
//b->brightness = 0;
|
|
||||||
//delay(d);
|
|
||||||
b->fadeStartMillis = currentMillis;
|
b->fadeStartMillis = currentMillis;
|
||||||
}
|
}
|
||||||
|
Serial.print(F("Delay is: "));
|
||||||
|
Serial.print(b->fadePeriod / 255 / 1000);
|
||||||
|
Serial.println(F(" seconds"));
|
||||||
|
Serial.print(F("Brightness: "));
|
||||||
|
Serial.println(b->brightness);
|
||||||
|
// DateTime now = rtc.now();
|
||||||
|
// const unsigned int fade_period = (b->fadePeriod / 60000);
|
||||||
|
// const unsigned int current_time = now.hour() * 60 + now.minute();
|
||||||
|
// const unsigned int settings_time = (b->shour * 60 + b->smin + fade_period);
|
||||||
|
// const unsigned int time_offset = fade_period - (settings_time - current_time);
|
||||||
|
// const unsigned int brightness2 = map(time_offset, 0, fade_period, 0, 255);
|
||||||
|
// const unsigned int brightness3 = constrain(brightness2, 0, 255);
|
||||||
|
// analogWrite(b->pin, brightness3);
|
||||||
|
// digitalWrite(b->relayPin, RELAY_ON);
|
||||||
|
// Serial << "Current Time = " << current_time << endl
|
||||||
|
// << "Settings Time = " << settings_time << endl
|
||||||
|
// << "Fade period value = " << fade_period << endl;
|
||||||
|
// Serial << "Time offset = " << time_offset << endl
|
||||||
|
// << "Brightness 2 = " << brightness3 << endl;
|
||||||
}
|
}
|
||||||
else if (stage == 2)
|
break;
|
||||||
|
case 2:
|
||||||
{
|
{
|
||||||
Serial.print(F("Stage = "));
|
|
||||||
Serial.println(stage);
|
|
||||||
b->brightness = 255;
|
b->brightness = 255;
|
||||||
analogWrite(b->pin, b->brightness);
|
analogWrite(b->pin, b->brightness);
|
||||||
|
digitalWrite(b->relayPin, RELAY_ON);
|
||||||
}
|
}
|
||||||
else if (stage == 3)
|
break;
|
||||||
|
case 3:
|
||||||
{
|
{
|
||||||
if (currentMillis - b->fadeStartMillis >= (b->fadePeriod / 255))
|
// if (currentMillis - b->fadeStartMillis >= (b->fadePeriod / 255))
|
||||||
{
|
// {
|
||||||
//Serial.println(b->fadePeriod/255);
|
// if (b->brightness > 0)
|
||||||
Serial.print(F("Stage = "));
|
// {
|
||||||
Serial.println(stage);
|
|
||||||
//if(b->brightness == 0 && (float)hr < (b->ehour + (float)(b->fadePeriod)/3600000))
|
|
||||||
//b->brightness = 255;
|
|
||||||
if (b->brightness > 0)
|
|
||||||
{
|
|
||||||
|
|
||||||
analogWrite(b->pin, --b->brightness);
|
// analogWrite(b->pin, --b->brightness);
|
||||||
}
|
// }
|
||||||
//d = b->fadePeriod/255;
|
// Serial.print(F("Delay is: "));
|
||||||
Serial.print(F("Delay is: "));
|
// Serial.print(b->fadePeriod / 255 / 1000);
|
||||||
//t = d / 1000;
|
// Serial.println(F(" seconds"));
|
||||||
Serial.print(b->fadePeriod / 255 / 1000);
|
// Serial.print(F("Brightness: "));
|
||||||
Serial.println(F(" seconds"));
|
// Serial.println(b->brightness);
|
||||||
Serial.print(F("Brightness: "));
|
// b->fadeStartMillis = currentMillis;
|
||||||
Serial.println(b->brightness);
|
// }
|
||||||
//if(b->brightness == 255)
|
DateTime now = rtc.now();
|
||||||
//b->brightness = 0;
|
const unsigned int fade_period = (b->fadePeriod / 60000);
|
||||||
//delay(d);
|
const unsigned long current_time = now.hour() * 60 + now.minute();
|
||||||
b->fadeStartMillis = currentMillis;
|
const unsigned long settings_time = (b->ehour * 60 + b->emin + fade_period);
|
||||||
}
|
const unsigned long time_offset = fade_period - (settings_time - current_time);
|
||||||
|
const unsigned int brightness2 = map(time_offset, 0, fade_period, 255, 0);
|
||||||
|
const unsigned int brightness3 = constrain(brightness2, 0, 255);
|
||||||
|
b->brightness = brightness3;
|
||||||
|
analogWrite(b->pin, brightness3);
|
||||||
|
digitalWrite(b->relayPin, RELAY_ON);
|
||||||
|
Serial << F("Current Time = ") << current_time << endl
|
||||||
|
<< F("Settings Time = ") << settings_time << endl
|
||||||
|
<< F("Fade period value = ") << fade_period << endl;
|
||||||
|
Serial << F("Time offset = ") << time_offset << endl
|
||||||
|
<< F("Brightness 2 = ") << brightness3 << endl;
|
||||||
}
|
}
|
||||||
else if (stage == 4)
|
break;
|
||||||
|
case 4:
|
||||||
{
|
{
|
||||||
Serial.print(F("Stage = "));
|
|
||||||
Serial.println(stage);
|
|
||||||
analogWrite(b->pin, 0);
|
analogWrite(b->pin, 0);
|
||||||
analogWrite(b->relayPin, RELAY_OFF);
|
digitalWrite(b->relayPin, RELAY_OFF);
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
Serial << F("Error invalid stage provided");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
Serial.println(F("----------------------------------------------"));
|
Serial.println(F("----------------------------------------------"));
|
||||||
}
|
}
|
||||||
|
|
||||||
void manualMode(ballast *b)
|
void manualMode(ballast *b)
|
||||||
{
|
{
|
||||||
/*DateTime now = rtc.now();
|
|
||||||
byte hr = now.hour();
|
|
||||||
byte mn = now.minute();
|
|
||||||
|
|
||||||
if(hr <= b-> shour)
|
|
||||||
{
|
|
||||||
analogWrite("
|
|
||||||
}*/
|
|
||||||
analogWrite(b->pin, 255);
|
analogWrite(b->pin, 255);
|
||||||
analogWrite(b->relayPin, RELAY_ON);
|
digitalWrite(b->relayPin, RELAY_ON);
|
||||||
}
|
}
|
||||||
|
|
||||||
int getFreeRam()
|
int getFreeRam()
|
||||||
|
Loading…
Reference in New Issue
Block a user