Compare commits
4 Commits
developmen
...
staging
Author | SHA1 | Date | |
---|---|---|---|
458673ac86 | |||
2df921c435 | |||
6dbc50875c | |||
905d8b3163 |
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,7 +1,4 @@
|
||||
.pio
|
||||
.pioenvs
|
||||
.piolibdeps
|
||||
.vscode/.browse.c_cpp.db*
|
||||
.vscode/c_cpp_properties.json
|
||||
.vscode/launch.json
|
||||
.vscode
|
||||
|
@ -10,7 +10,11 @@
|
||||
|
||||
[env:nanoatmega328]
|
||||
platform = atmelavr
|
||||
; lib_extra_dirs = ~/Documents/Arduino/libraries
|
||||
board = nanoatmega328
|
||||
framework = arduino
|
||||
monitor_speed = 115200
|
||||
monitor_speed = 115200
|
||||
|
||||
lib_deps =
|
||||
ArduinoJson@5.13.4
|
||||
Streaming
|
||||
RTClib@1.2.0
|
16
src/.vscode/c_cpp_properties.json
vendored
16
src/.vscode/c_cpp_properties.json
vendored
@ -1,16 +0,0 @@
|
||||
{
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Linux",
|
||||
"includePath": [
|
||||
"${workspaceFolder}/**"
|
||||
],
|
||||
"defines": ["__AVR_ATmega328p__"],
|
||||
"compilerPath": "/usr/bin/gcc",
|
||||
"cStandard": "c11",
|
||||
"cppStandard": "c++17",
|
||||
"intelliSenseMode": "clang-x64"
|
||||
}
|
||||
],
|
||||
"version": 4
|
||||
}
|
441
src/main.cpp
Executable file
441
src/main.cpp
Executable file
@ -0,0 +1,441 @@
|
||||
#include <Arduino.h>
|
||||
#include <Wire.h>
|
||||
#include "RTClib.h"
|
||||
#include <ArduinoJson.h>
|
||||
#include <SoftwareSerial.h>
|
||||
#include <Streaming.h>
|
||||
#include "main.h"
|
||||
SoftwareSerial s(5, 6); //RX,TX
|
||||
RTC_DS1307 rtc;
|
||||
|
||||
struct ballast ballast1, ballast2, ballast3;
|
||||
struct ballast *ballasts[BALLAST_SIZE] = {
|
||||
&ballast1, &ballast2, &ballast3};
|
||||
|
||||
unsigned long currentMillis;
|
||||
unsigned long loopMillis;
|
||||
unsigned long data_send_millis;
|
||||
static bool manualFlag;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
|
||||
//Tell the esp that arduino has turned on
|
||||
s.begin(9600);
|
||||
// analogWrite(13, 255);
|
||||
digitalWrite(13, 1);
|
||||
|
||||
StaticJsonBuffer<bufferSize> jsonBuffer;
|
||||
JsonObject &root = jsonBuffer.parseObject(s);
|
||||
|
||||
setupBallasts(ballasts, BALLAST_SIZE, root);
|
||||
// setupBallast(&ballast2, root, 2);
|
||||
// setupBallast(&ballast3, root, 3);
|
||||
root.prettyPrintTo(Serial);
|
||||
Serial.println(F("Settings initialized via json"));
|
||||
if (root["manual"] == F("True"))
|
||||
{
|
||||
manualFlag = true;
|
||||
}
|
||||
|
||||
loopMillis = millis();
|
||||
data_send_millis = millis();
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
|
||||
while (!rtc.begin())
|
||||
{
|
||||
Serial.println(F("Couldn't find RTC"));
|
||||
delay(100);
|
||||
}
|
||||
|
||||
while (!rtc.isrunning())
|
||||
{
|
||||
Serial.println(F("RTC is NOT running!"));
|
||||
}
|
||||
|
||||
const DateTime now = rtc.now();
|
||||
const int hour = now.hour();
|
||||
const int min = now.minute();
|
||||
|
||||
if (s.available() > 0)
|
||||
{
|
||||
StaticJsonBuffer<bufferSize> jsonBuffer;
|
||||
JsonObject &root = jsonBuffer.parseObject(s);
|
||||
if (root.success())
|
||||
{
|
||||
Serial << F("Received JSON") << endl;
|
||||
root.prettyPrintTo(Serial);
|
||||
|
||||
updateBallast(&ballast1, root);
|
||||
updateBallast(&ballast2, root);
|
||||
updateBallast(&ballast3, root);
|
||||
if (root["manual"] == "True")
|
||||
{
|
||||
manualFlag = true;
|
||||
Serial.println(F("Manual set to true"));
|
||||
}
|
||||
else if (root["manual"] == "False")
|
||||
{
|
||||
manualFlag = false;
|
||||
Serial.println(F("Manual set to false"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
currentMillis = millis();
|
||||
if (currentMillis - loopMillis >= loopPeriod)
|
||||
{
|
||||
|
||||
if (manualFlag)
|
||||
{
|
||||
Serial.println(F("Manual Mode"));
|
||||
manualMode(&ballast1);
|
||||
manualMode(&ballast2);
|
||||
manualMode(&ballast3);
|
||||
}
|
||||
else
|
||||
{
|
||||
doBallast(&ballast1, hour, min);
|
||||
doBallast(&ballast2, hour, min);
|
||||
doBallast(&ballast3, hour, min);
|
||||
}
|
||||
|
||||
// Serial << F("Free RAM: ") << getFreeRam() << endl;
|
||||
loopMillis = currentMillis;
|
||||
}
|
||||
|
||||
currentMillis = millis();
|
||||
if (currentMillis - data_send_millis >= data_send_period)
|
||||
{
|
||||
StaticJsonBuffer<50> jsonBuffer;
|
||||
JsonObject &root = jsonBuffer.createObject();
|
||||
JsonArray &lcd_data = root.createNestedArray("lcdData");
|
||||
char time_buffer[10];
|
||||
sprintf(time_buffer, "%02d:%02d", hour, min);
|
||||
// root["hello"] = ("world");
|
||||
lcd_data.add("90%%");
|
||||
lcd_data.add(time_buffer);
|
||||
root.printTo(s);
|
||||
root.printTo(Serial);
|
||||
data_send_millis = millis();
|
||||
Serial << endl;
|
||||
}
|
||||
delay(100);
|
||||
}
|
||||
|
||||
void setupBallast(ballast *b)
|
||||
{
|
||||
b->id = 1;
|
||||
b->shour = 14;
|
||||
b->smin = 0;
|
||||
b->fadePeriod = 60 * 60000;
|
||||
b->stage = 0;
|
||||
b->brightness = 0;
|
||||
b->pin = 11;
|
||||
}
|
||||
int setupBallasts(ballast *b_arr[], size_t arr_size, JsonObject &root)
|
||||
{
|
||||
if (arr_size < 3)
|
||||
{
|
||||
Serial << "Error: array size less than 3" << endl;
|
||||
return -1;
|
||||
}
|
||||
JsonArray &settings = root["settings"];
|
||||
JsonObject &settings0 = settings[0];
|
||||
JsonObject &settings1 = settings[1];
|
||||
JsonObject &settings2 = settings[2];
|
||||
{
|
||||
ballast *b = b_arr[0];
|
||||
b->id = 1;
|
||||
b->shour = settings0["shour"];
|
||||
unsigned long fP = settings0["fadePeriod"];
|
||||
b->fadePeriod = fP * 60000;
|
||||
b->smin = settings0["smin"];
|
||||
b->ehour = settings0["ehour"];
|
||||
b->emin = settings0["emin"];
|
||||
b->brightness = 0;
|
||||
b->pin = 11;
|
||||
b->relayPin = 2;
|
||||
b->fadeStartMillis = millis();
|
||||
pinMode(b->pin, OUTPUT);
|
||||
pinMode(b->relayPin, OUTPUT);
|
||||
}
|
||||
{
|
||||
ballast *b = b_arr[1];
|
||||
b->id = 2;
|
||||
b->shour = settings1["shour"];
|
||||
unsigned long fP = settings1["fadePeriod"];
|
||||
b->fadePeriod = fP * 60000;
|
||||
b->smin = settings1["smin"];
|
||||
b->ehour = settings1["ehour"];
|
||||
b->emin = settings1["emin"];
|
||||
b->brightness = 0;
|
||||
b->pin = 10;
|
||||
b->relayPin = 4;
|
||||
b->fadeStartMillis = millis();
|
||||
pinMode(b->pin, OUTPUT);
|
||||
pinMode(b->relayPin, OUTPUT);
|
||||
}
|
||||
{
|
||||
ballast *b = b_arr[2];
|
||||
b->id = 3;
|
||||
b->shour = settings2["shour"];
|
||||
unsigned long fP = settings2["fadePeriod"];
|
||||
b->fadePeriod = fP * 60000;
|
||||
b->smin = settings2["smin"];
|
||||
b->ehour = settings2["ehour"];
|
||||
b->emin = settings2["emin"];
|
||||
b->brightness = 0;
|
||||
b->pin = 9;
|
||||
b->relayPin = 7;
|
||||
b->fadeStartMillis = millis();
|
||||
pinMode(b->pin, OUTPUT);
|
||||
pinMode(b->relayPin, OUTPUT);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void updateBallast(ballast *b, JsonObject &root)
|
||||
{
|
||||
JsonArray &settings = root["settings"];
|
||||
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 hr, int mn)
|
||||
{
|
||||
Serial.print(F("Current hour is: "));
|
||||
Serial.println(hr);
|
||||
Serial.print(F("Current minute is: "));
|
||||
Serial.println(mn);
|
||||
|
||||
if (hr < b->shour)
|
||||
return 0;
|
||||
if ((float)hr >= (float)b->shour && hr < (b->shour + (float)(b->fadePeriod) / 3600000 + (float)(b->smin) / 60))
|
||||
{
|
||||
if (hr == b->shour)
|
||||
{
|
||||
if (mn < b->smin)
|
||||
return 0;
|
||||
else
|
||||
return 1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
if ((float)hr >= (b->shour + (float)(b->fadePeriod) / 3600000 + (float)(b->smin) / 60) && hr < b->ehour)
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
if ((float)hr >= (b->ehour + (float)(b->fadePeriod) / 3600000))
|
||||
{
|
||||
if (hr == (b->ehour + (float)(b->fadePeriod) / 3600000))
|
||||
{
|
||||
if (mn < b->emin)
|
||||
return 3;
|
||||
else
|
||||
return 4;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Serial.println((b->ehour + (float)(b->fadePeriod)/3600000));
|
||||
return 4;
|
||||
}
|
||||
}
|
||||
if ((float)hr >= (float)b->ehour && hr < (b->ehour + (float)(b->fadePeriod) / 3600000 + (float)(b->emin) / 60))
|
||||
{
|
||||
if (hr == b->ehour)
|
||||
{
|
||||
if (mn < b->emin)
|
||||
return 2;
|
||||
else
|
||||
return 3;
|
||||
}
|
||||
else
|
||||
return 3;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int getStage2(ballast *b, int hr, int mn)
|
||||
{
|
||||
Serial.print(F("Current hour is: "));
|
||||
Serial.println(hr);
|
||||
Serial.print(F("Current minute is: "));
|
||||
Serial.println(mn);
|
||||
|
||||
const unsigned int current_time = (hr * 60) + mn;
|
||||
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, int hr, int mn)
|
||||
{
|
||||
// Log.notice(F("-------------------Ballast%d-------------------"));
|
||||
Serial << "-------------------Ballast" << b->id << "-------------------" << endl;
|
||||
Serial.print(F("Start hour is: "));
|
||||
Serial.println(b->shour);
|
||||
Serial.print(F("Start min is: "));
|
||||
Serial.println(b->smin);
|
||||
Serial.print(F("Fade Period is: "));
|
||||
Serial.print((b->fadePeriod) / 60000);
|
||||
Serial.println(F(" minutes"));
|
||||
Serial.print(F("End hour is: "));
|
||||
Serial.println(b->ehour);
|
||||
Serial.print(F("End min is: "));
|
||||
Serial.println(b->emin);
|
||||
Serial.print(F("Brightness: "));
|
||||
Serial.println(b->brightness);
|
||||
const int stage = getStage2(b, hr, mn);
|
||||
const int stage2 = getStage(b, hr, mn);
|
||||
Serial.print(F("Stage: "));
|
||||
Serial.println(stage);
|
||||
Serial << "Stage2 = " << stage2 << endl;
|
||||
switch (stage)
|
||||
{
|
||||
case 0:
|
||||
{
|
||||
b->stage = 0;
|
||||
b->brightness = 0;
|
||||
analogWrite(b->pin, b->brightness);
|
||||
Serial.print(F("brightness: "));
|
||||
Serial.println(b->brightness);
|
||||
digitalWrite(b->relayPin, RELAY_OFF);
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
{
|
||||
if (currentMillis - b->fadeStartMillis >= (b->fadePeriod / 255))
|
||||
{
|
||||
digitalWrite(b->relayPin, RELAY_ON);
|
||||
if (b->brightness <= 254)
|
||||
{
|
||||
analogWrite(b->pin, b->brightness++);
|
||||
}
|
||||
Serial.print(F("Delay is: "));
|
||||
Serial.print(b->fadePeriod / 255 / 1000);
|
||||
Serial.println(F(" seconds"));
|
||||
Serial.print(F("Brightness: "));
|
||||
Serial.println(b->brightness);
|
||||
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;
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
{
|
||||
b->brightness = 255;
|
||||
analogWrite(b->pin, b->brightness);
|
||||
digitalWrite(b->relayPin, RELAY_ON);
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
{
|
||||
// if (currentMillis - b->fadeStartMillis >= (b->fadePeriod / 255))
|
||||
// {
|
||||
// if (b->brightness > 0)
|
||||
// {
|
||||
|
||||
// analogWrite(b->pin, --b->brightness);
|
||||
// }
|
||||
// Serial.print(F("Delay is: "));
|
||||
// Serial.print(b->fadePeriod / 255 / 1000);
|
||||
// Serial.println(F(" seconds"));
|
||||
// Serial.print(F("Brightness: "));
|
||||
// Serial.println(b->brightness);
|
||||
// b->fadeStartMillis = currentMillis;
|
||||
// }
|
||||
const unsigned int fade_period = (b->fadePeriod / 60000);
|
||||
const unsigned long current_time = hr * 60 + mn;
|
||||
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;
|
||||
}
|
||||
break;
|
||||
case 4:
|
||||
{
|
||||
analogWrite(b->pin, 0);
|
||||
digitalWrite(b->relayPin, RELAY_OFF);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
{
|
||||
Serial << F("Error invalid stage provided");
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
Serial.println(F("----------------------------------------------"));
|
||||
}
|
||||
|
||||
void manualMode(ballast *b)
|
||||
{
|
||||
analogWrite(b->pin, 255);
|
||||
digitalWrite(b->relayPin, RELAY_ON);
|
||||
}
|
||||
|
||||
int getFreeRam()
|
||||
{
|
||||
extern int __heap_start, *__brkval;
|
||||
int v;
|
||||
|
||||
v = (int)&v - (__brkval == 0 ? (int)&__heap_start : (int)__brkval);
|
||||
|
||||
Serial.print("Free RAM = ");
|
||||
Serial.println(v, DEC);
|
||||
|
||||
return v;
|
||||
}
|
31
src/main.h
Normal file
31
src/main.h
Normal file
@ -0,0 +1,31 @@
|
||||
#include <ArduinoJson.h>
|
||||
#define RELAY_ON 0
|
||||
#define RELAY_OFF 1
|
||||
#define BALLAST_SIZE 3
|
||||
|
||||
struct ballast
|
||||
{
|
||||
int id;
|
||||
int shour;
|
||||
int smin;
|
||||
uint32_t fadePeriod;
|
||||
int ehour;
|
||||
int emin;
|
||||
int stage;
|
||||
int brightness;
|
||||
int pin;
|
||||
unsigned long fadeStartMillis;
|
||||
int relayPin;
|
||||
};
|
||||
|
||||
void setupBallast(ballast *b);
|
||||
int setupBallasts(ballast *b_arr[], size_t arr_size, JsonObject &root);
|
||||
void doBallast(ballast *b, int hr, int mn);
|
||||
void updateBallast(ballast *b, JsonObject &root);
|
||||
void manualMode(ballast *b);
|
||||
int getFreeRam();
|
||||
const size_t bufferSize = JSON_ARRAY_SIZE(3) + JSON_OBJECT_SIZE(2) + 3 * JSON_OBJECT_SIZE(5) + 180;
|
||||
|
||||
const unsigned long loopPeriod = 1000;
|
||||
|
||||
const unsigned long data_send_period = 2000;
|
@ -1,473 +0,0 @@
|
||||
#include <Arduino.h>
|
||||
#include <Wire.h>
|
||||
#include "RTClib.h"
|
||||
#include <ArduinoJson.h>
|
||||
#include <SoftwareSerial.h>
|
||||
#include <Streaming.h>
|
||||
#include <SPI.h>
|
||||
#define RELAY_ON 0
|
||||
#define RELAY_OFF 1
|
||||
// #define ESP_TO_ARDUINO_PIN 2
|
||||
SoftwareSerial s(5, 6); //RX,TX
|
||||
RTC_DS1307 rtc;
|
||||
|
||||
struct ballast
|
||||
{
|
||||
int id;
|
||||
int shour;
|
||||
int smin;
|
||||
uint32_t fadePeriod;
|
||||
int ehour;
|
||||
int emin;
|
||||
int stage;
|
||||
int brightness;
|
||||
int pin;
|
||||
unsigned long fadeStartMillis;
|
||||
int relayPin;
|
||||
};
|
||||
struct ballast ballast1, ballast2, ballast3;
|
||||
// struct ballast ballasts[3];
|
||||
|
||||
void setupBallast(ballast *b);
|
||||
void setupBallast(ballast *b, JsonObject &root, int i);
|
||||
void doBallast(ballast *b);
|
||||
void updateBallast(ballast *b, JsonObject &root);
|
||||
void manualMode(ballast *b);
|
||||
int getFreeRam();
|
||||
const size_t bufferSize = JSON_ARRAY_SIZE(3) + JSON_OBJECT_SIZE(2) + 3 * JSON_OBJECT_SIZE(5) + 180;
|
||||
|
||||
unsigned long currentMillis;
|
||||
unsigned long loopMillis;
|
||||
unsigned long loopPeriod = 1000;
|
||||
unsigned long data_send_millis;
|
||||
unsigned long data_send_period = 2000;
|
||||
static bool manualFlag;
|
||||
|
||||
// char roul[] = "is a fag";
|
||||
|
||||
// char data[60];
|
||||
|
||||
void setup()
|
||||
{
|
||||
// put your setup code here, to run once:
|
||||
Serial.begin(115200);
|
||||
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
|
||||
//Tell me esp that arduino has turned on
|
||||
s.begin(9600);
|
||||
// analogWrite(13, 255);
|
||||
digitalWrite(13, 1);
|
||||
|
||||
// strcpy(roul,"");
|
||||
|
||||
//while (!Serial) continue;
|
||||
// DateTime now = rtc.now();
|
||||
//StaticJsonBuffer<400> jsonBuffer1;
|
||||
DynamicJsonBuffer jsonBuffer(bufferSize);
|
||||
// StaticJsonBuffer<100> jsonBuffer;
|
||||
JsonObject &root = jsonBuffer.parseObject(s);
|
||||
// root.printTo(data);
|
||||
// while(root == JsonObject::invalid())
|
||||
// {
|
||||
// Serial.println(F("Error code1");
|
||||
// }
|
||||
setupBallast(&ballast1, root, 1);
|
||||
setupBallast(&ballast2, root, 2);
|
||||
setupBallast(&ballast3, root, 3);
|
||||
root.prettyPrintTo(Serial);
|
||||
Serial.println(F("Settings initialized via json"));
|
||||
if (root["manual"] == F("True"))
|
||||
{
|
||||
manualFlag = true;
|
||||
}
|
||||
|
||||
loopMillis = millis();
|
||||
data_send_millis = millis();
|
||||
}
|
||||
|
||||
// // static bool newData = false;
|
||||
|
||||
void loop()
|
||||
{
|
||||
|
||||
while (!rtc.begin())
|
||||
{
|
||||
Serial.println(F("Couldn't find RTC"));
|
||||
//while (1);
|
||||
delay(100);
|
||||
}
|
||||
|
||||
while (!rtc.isrunning())
|
||||
{
|
||||
Serial.println(F("RTC is NOT running!"));
|
||||
}
|
||||
|
||||
if (s.available() > 0)
|
||||
{
|
||||
// delay(100);
|
||||
DynamicJsonBuffer jsonBuffer(bufferSize);
|
||||
// const size_t bufsize = JSON_ARRAY_SIZE(3) + JSON_OBJECT_SIZE(2) + 3*JSON_OBJECT_SIZE(5);
|
||||
// StaticJsonBuffer<bufsize> jsonBuffer;
|
||||
// StaticJsonBuffer<bufferSize> jsonBuffer;
|
||||
JsonObject &root = jsonBuffer.parseObject(s);
|
||||
// if(root.succes())
|
||||
Serial << F("Received JSON") << endl;
|
||||
root.prettyPrintTo(Serial);
|
||||
// // data = "";
|
||||
// root.printTo(data);
|
||||
// // newData = true;
|
||||
// }
|
||||
|
||||
// if (newData)
|
||||
// {
|
||||
// Serial.println(F("Parsing data"));
|
||||
// DynamicJsonBuffer jsonBuffer(bufferSize);
|
||||
// JsonObject &root = jsonBuffer.parseObject(data);
|
||||
// root.prettyPrintTo(Serial);
|
||||
updateBallast(&ballast1, root);
|
||||
updateBallast(&ballast2, root);
|
||||
updateBallast(&ballast3, root);
|
||||
if (root["manual"] == "True")
|
||||
{
|
||||
manualFlag = true;
|
||||
Serial.println(F("Manual set to true"));
|
||||
}
|
||||
else if (root["manual"] == "False")
|
||||
{
|
||||
manualFlag = false;
|
||||
Serial.println(F("Manual set to false"));
|
||||
}
|
||||
// newData = false;
|
||||
}
|
||||
|
||||
currentMillis = millis();
|
||||
if (currentMillis - loopMillis >= loopPeriod)
|
||||
{
|
||||
|
||||
if (manualFlag == true)
|
||||
{
|
||||
Serial.println(F("Manual Mode"));
|
||||
manualMode(&ballast1);
|
||||
manualMode(&ballast2);
|
||||
manualMode(&ballast3);
|
||||
}
|
||||
else
|
||||
{
|
||||
doBallast(&ballast1);
|
||||
doBallast(&ballast2);
|
||||
doBallast(&ballast3);
|
||||
}
|
||||
|
||||
Serial << F("Free RAM: ") << getFreeRam() << endl;
|
||||
loopMillis = currentMillis;
|
||||
}
|
||||
|
||||
currentMillis = millis();
|
||||
if (currentMillis - data_send_millis >= data_send_period)
|
||||
{
|
||||
StaticJsonBuffer<50> jsonBuffer;
|
||||
JsonObject& root = jsonBuffer.createObject();
|
||||
JsonArray& lcd_data = root.createNestedArray("lcdData");
|
||||
char time_buffer[10];
|
||||
DateTime now = rtc.now();
|
||||
sprintf(time_buffer,"%02d:%02d",now.hour(),now.minute());
|
||||
// root["hello"] = ("world");
|
||||
lcd_data.add("90%%");
|
||||
lcd_data.add(time_buffer);
|
||||
root.printTo(s);
|
||||
root.printTo(Serial);
|
||||
data_send_millis = millis();
|
||||
Serial << endl;
|
||||
}
|
||||
delay(100);
|
||||
}
|
||||
|
||||
void setupBallast(ballast *b)
|
||||
{
|
||||
b->id = 1;
|
||||
b->shour = 14;
|
||||
b->smin = 0;
|
||||
b->fadePeriod = 60 * 60000;
|
||||
//b->stage = 0;
|
||||
b->brightness = 0;
|
||||
b->pin = 11;
|
||||
}
|
||||
void setupBallast(ballast *b, JsonObject &root, int i)
|
||||
{
|
||||
pinMode(b->pin, OUTPUT);
|
||||
pinMode(b->relayPin, OUTPUT);
|
||||
JsonArray &settings = root["settings"];
|
||||
JsonObject &settings0 = settings[0];
|
||||
JsonObject &settings1 = settings[1];
|
||||
JsonObject &settings2 = settings[2];
|
||||
if (i == 1)
|
||||
{
|
||||
b->id = 1;
|
||||
b->shour = settings0["shour"];
|
||||
unsigned long fP = settings0["fadePeriod"];
|
||||
b->fadePeriod = fP * 60000;
|
||||
b->smin = settings0["smin"];
|
||||
b->ehour = settings0["ehour"];
|
||||
b->emin = settings0["emin"];
|
||||
b->brightness = 0;
|
||||
b->pin = 11;
|
||||
b->relayPin = 2;
|
||||
b->fadeStartMillis = millis();
|
||||
}
|
||||
else if (i == 2)
|
||||
{
|
||||
b->id = 2;
|
||||
b->shour = settings1["shour"];
|
||||
unsigned long fP = settings1["fadePeriod"];
|
||||
b->fadePeriod = fP * 60000;
|
||||
b->smin = settings1["smin"];
|
||||
b->ehour = settings1["ehour"];
|
||||
b->emin = settings1["emin"];
|
||||
b->brightness = 0;
|
||||
b->pin = 10;
|
||||
b->relayPin = 4;
|
||||
b->fadeStartMillis = millis();
|
||||
}
|
||||
else if (i == 3)
|
||||
{
|
||||
b->id = 3;
|
||||
b->shour = settings2["shour"];
|
||||
unsigned long fP = settings2["fadePeriod"];
|
||||
b->fadePeriod = fP * 60000;
|
||||
b->smin = settings2["smin"];
|
||||
b->ehour = settings2["ehour"];
|
||||
b->emin = settings2["emin"];
|
||||
b->brightness = 0;
|
||||
b->pin = 9;
|
||||
b->relayPin = 7;
|
||||
b->fadeStartMillis = millis();
|
||||
}
|
||||
}
|
||||
|
||||
void updateBallast(ballast *b, JsonObject &root)
|
||||
{
|
||||
JsonArray &settings = root["settings"];
|
||||
JsonObject &settings0 = settings[0];
|
||||
JsonObject &settings1 = settings[1];
|
||||
JsonObject &settings2 = settings[2];
|
||||
if (b->id == 1)
|
||||
{
|
||||
b->shour = settings0["shour"];
|
||||
unsigned long fP = settings0["fadePeriod"];
|
||||
b->fadePeriod = fP * 60000;
|
||||
b->smin = settings0["smin"];
|
||||
b->ehour = settings0["ehour"];
|
||||
b->emin = settings0["emin"];
|
||||
}
|
||||
if (b->id == 2)
|
||||
{
|
||||
b->shour = settings1["shour"];
|
||||
unsigned long fP = settings1["fadePeriod"];
|
||||
b->fadePeriod = fP * 60000;
|
||||
b->smin = settings1["smin"];
|
||||
b->ehour = settings1["ehour"];
|
||||
b->emin = settings1["emin"];
|
||||
}
|
||||
if (b->id == 3)
|
||||
{
|
||||
b->shour = settings2["shour"];
|
||||
unsigned long fP = settings2["fadePeriod"];
|
||||
b->fadePeriod = fP * 60000;
|
||||
b->smin = settings2["smin"];
|
||||
b->ehour = settings2["ehour"];
|
||||
b->emin = settings2["emin"];
|
||||
}
|
||||
}
|
||||
int getStage(ballast *b)
|
||||
{
|
||||
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);
|
||||
|
||||
if (hr < b->shour)
|
||||
return 0;
|
||||
if ((float)hr >= (float)b->shour && hr < (b->shour + (float)(b->fadePeriod) / 3600000 + (float)(b->smin) / 60))
|
||||
{
|
||||
if (hr == b->shour)
|
||||
{
|
||||
if (mn < b->smin)
|
||||
return 0;
|
||||
else
|
||||
return 1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
if ((float)hr >= (b->shour + (float)(b->fadePeriod) / 3600000 + (float)(b->smin) / 60) && hr < b->ehour)
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
if ((float)hr >= (b->ehour + (float)(b->fadePeriod) / 3600000))
|
||||
{
|
||||
if (hr == (b->ehour + (float)(b->fadePeriod) / 3600000))
|
||||
{
|
||||
if (mn < b->emin)
|
||||
return 3;
|
||||
else
|
||||
return 4;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Serial.println((b->ehour + (float)(b->fadePeriod)/3600000));
|
||||
return 4;
|
||||
}
|
||||
}
|
||||
if ((float)hr >= (float)b->ehour && hr < (b->ehour + (float)(b->fadePeriod) / 3600000 + (float)(b->emin) / 60))
|
||||
{
|
||||
if (hr == b->ehour)
|
||||
{
|
||||
if (mn < b->emin)
|
||||
return 2;
|
||||
else
|
||||
return 3;
|
||||
}
|
||||
else
|
||||
return 3;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
void doBallast(ballast *b)
|
||||
{
|
||||
if (b->id == 1)
|
||||
{
|
||||
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.println(b->shour);
|
||||
Serial.print(F("Start min is: "));
|
||||
Serial.println(b->smin);
|
||||
Serial.print(F("Fade Period is: "));
|
||||
Serial.print((b->fadePeriod) / 60000);
|
||||
Serial.println(F(" minutes"));
|
||||
Serial.print(F("End hour is: "));
|
||||
Serial.println(b->ehour);
|
||||
Serial.print(F("End min is: "));
|
||||
Serial.println(b->emin);
|
||||
Serial.print(F("Brightness: "));
|
||||
Serial.println(b->brightness);
|
||||
int stage = getStage(b);
|
||||
Serial.print(F("Stage: "));
|
||||
Serial.println(stage);
|
||||
if (stage == 0)
|
||||
{
|
||||
b->stage = 0;
|
||||
b->brightness = 0;
|
||||
analogWrite(b->pin, b->brightness);
|
||||
Serial.print(F("Stage = "));
|
||||
Serial.println(stage);
|
||||
Serial.print(F("brightness: "));
|
||||
Serial.println(b->brightness);
|
||||
analogWrite(b->relayPin, RELAY_OFF);
|
||||
}
|
||||
else if (stage == 1)
|
||||
{
|
||||
if (currentMillis - b->fadeStartMillis >= (b->fadePeriod / 255))
|
||||
{
|
||||
//Serial.println(b->fadePeriod/255);
|
||||
Serial.print(F("Stage = "));
|
||||
Serial.println(stage);
|
||||
digitalWrite(b->relayPin, RELAY_ON);
|
||||
if (b->brightness <= 254)
|
||||
{
|
||||
analogWrite(b->pin, b->brightness++);
|
||||
}
|
||||
//d = b->fadePeriod/255;
|
||||
Serial.print(F("Delay is: "));
|
||||
//t = d / 1000;
|
||||
Serial.print(b->fadePeriod / 255 / 1000);
|
||||
Serial.println(F(" seconds"));
|
||||
Serial.print(F("Brightness: "));
|
||||
Serial.println(b->brightness);
|
||||
//if(b->brightness == 255)
|
||||
//b->brightness = 0;
|
||||
//delay(d);
|
||||
b->fadeStartMillis = currentMillis;
|
||||
}
|
||||
}
|
||||
else if (stage == 2)
|
||||
{
|
||||
Serial.print(F("Stage = "));
|
||||
Serial.println(stage);
|
||||
b->brightness = 255;
|
||||
analogWrite(b->pin, b->brightness);
|
||||
}
|
||||
else if (stage == 3)
|
||||
{
|
||||
if (currentMillis - b->fadeStartMillis >= (b->fadePeriod / 255))
|
||||
{
|
||||
//Serial.println(b->fadePeriod/255);
|
||||
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);
|
||||
}
|
||||
//d = b->fadePeriod/255;
|
||||
Serial.print(F("Delay is: "));
|
||||
//t = d / 1000;
|
||||
Serial.print(b->fadePeriod / 255 / 1000);
|
||||
Serial.println(F(" seconds"));
|
||||
Serial.print(F("Brightness: "));
|
||||
Serial.println(b->brightness);
|
||||
//if(b->brightness == 255)
|
||||
//b->brightness = 0;
|
||||
//delay(d);
|
||||
b->fadeStartMillis = currentMillis;
|
||||
}
|
||||
}
|
||||
else if (stage == 4)
|
||||
{
|
||||
Serial.print(F("Stage = "));
|
||||
Serial.println(stage);
|
||||
analogWrite(b->pin, 0);
|
||||
analogWrite(b->relayPin, RELAY_OFF);
|
||||
}
|
||||
Serial.println(F("----------------------------------------------"));
|
||||
}
|
||||
|
||||
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->relayPin, RELAY_ON);
|
||||
}
|
||||
|
||||
int getFreeRam()
|
||||
{
|
||||
extern int __heap_start, *__brkval;
|
||||
int v;
|
||||
|
||||
v = (int)&v - (__brkval == 0 ? (int)&__heap_start : (int)__brkval);
|
||||
|
||||
Serial.print("Free RAM = ");
|
||||
Serial.println(v, DEC);
|
||||
|
||||
return v;
|
||||
}
|
Loading…
Reference in New Issue
Block a user