Skip to content

Commit

Permalink
add alarm
Browse files Browse the repository at this point in the history
  • Loading branch information
fedyagrib committed Sep 21, 2019
1 parent b42d4ba commit 1fb7642
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 44 deletions.
2 changes: 1 addition & 1 deletion include/Mode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Mode{
int NUM_LEDS = 300; //при изменение - исправить значения в условиях в функциях setStartLed и setNumbLeds на NUM_LEDS+1

int UPDATES_PER_SECOND = 10; //задержка обновления
int BRIGHT_CHANGE = 5;
int BRIGHT_CHANGE = 5;

CRGB * leds = new CRGB[NUM_LEDS];

Expand Down
65 changes: 65 additions & 0 deletions include/NtpClientMine.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#ifndef NTP_CLIENT_HPP_
#define NTP_CLIENT_HPP_

#include <NTPClient.h>
#include <WiFiUdp.h>

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP,"0.pool.ntp.org");

int alarmHours = 7;
int alarmMinutes = 0;
bool alarmSwitch = false;

byte timer=0;

void timeInit(int timeZone){
timeClient.setTimeOffset(timeZone * 3600);
timeClient.begin();
}

void setAlarmHours(int hours){
alarmHours = hours;
alarmSwitch=true;
}

void setAlarmMinutes(int minutes){
alarmMinutes = minutes;
alarmSwitch=true;
}

void setAlarm(int value){
if(value)
alarmSwitch = true;
else
alarmSwitch = false;
}

int getAlarm(){
return alarmHours * 24 + alarmMinutes;
}
int getTime(){
return timeClient.getHours() * 24 + timeClient.getMinutes();
}

int isAlarm(byte interval){

if(timer == 255){
timer = 0;
if(alarmSwitch){
timeClient.update();
int alarmMin=getAlarm() - getTime();
if(abs(alarmMin)<=interval)
return abs(alarmMin);
else if(alarmMin == -interval)
alarmSwitch = false;
}
}
timer++;
return -1;
}




#endif // NTP_CLIENT_HPP_
5 changes: 3 additions & 2 deletions include/Palettes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ DEFINE_GRADIENT_PALETTE( bhw3_62_gp ) {
255, 63, 4, 68};

DEFINE_GRADIENT_PALETTE( bhw1_05_gp ) {
0, 184, 1,128,
0, 255, 0,228,
100, 184, 1,128,
160, 1,193,182,
219, 153,227,190,
255, 255,255,255};
255, 0,255,174};

// Gradient palette "bhw1_07_gp", originally from
// http://soliton.vm.bytemark.co.uk/pub/cpt-city/bhw/bhw1/tn/bhw1_07.png.index.html
Expand Down
9 changes: 5 additions & 4 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
platform = espressif32
board = esp32dev
framework = arduino
; monitor_speed = 115200
;monitor_speed = 115200

; OTA upload hostname
upload_port=ota_ledochka.local
;OTA upload hostname
upload_port=192.168.1.90
upload_flags=
--auth=fuckyoubitch

lib_deps =
FastLED
FastLED
https://github.com/arduino-libraries/NTPClient
13 changes: 10 additions & 3 deletions src/Mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ Mode::~Mode(){}

void Mode::slightChange(int * def_value, int * cur_value, int val_chage = 1){
//если модуль разницы значение больше чем значение val_chage (иначе будет жопа)
if(abs((*cur_value) - (*def_value)) > val_chage){
int temp = abs((*cur_value) - (*def_value));
if( temp >= val_chage){
//если нынешнее значение больше эталоного
if((*cur_value) > (*def_value)){
//то уменьшаем нынешнее
Expand All @@ -28,6 +29,8 @@ void Mode::slightChange(int * def_value, int * cur_value, int val_chage = 1){
//увеличиваем его
(*cur_value) += val_chage;
}
} else if(temp > 0 && temp < val_chage){
(*cur_value) = (*def_value);
}
}

Expand Down Expand Up @@ -72,7 +75,7 @@ void Mode::update(){
}

void Mode::setBright(int value){
if(value>0){
if(value>=0){
BRIGHTNESS = value % 256;
}
}
Expand Down Expand Up @@ -127,5 +130,9 @@ void Mode::setBlending(int value){
}

void Mode::setPersonPalette(byte array[]){
personPalette=CRGBPalette16(array);
CRGB rgbarray[16];
for(int i=0; i<16;i++){
rgbarray[i]=CRGB(array[3*i],array[3*i+1],array[3*i+2]);
}
personPalette=CRGBPalette16(rgbarray);
}
2 changes: 1 addition & 1 deletion src/WifiConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ bool WifiConfig::wifiSTA(const char *ssid, const char *password)

ip = WiFi.localIP();
ip[3] = 90;
WiFi.config(ip, subnet, gateway, dns); //ip=192.168.xxx.90
WiFi.config(ip, WiFi.gatewayIP(), WiFi.subnetMask(), dns); //ip=192.168.xxx.90
WiFi.mode(WIFI_STA); //WiFi mode station (connect to wifi router only)
Serial.print("\nSTA local ip: ");
Serial.println(WiFi.localIP());
Expand Down
93 changes: 60 additions & 33 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
#include "WifiConfig.hpp"
#include "MemConfig.hpp"
#include "Mode.hpp"
#include "NtpClientMine.hpp"
#include <ArduinoOTA.h>
#include <ESPmDNS.h>


#define ESP32

//закоментировать если не нужно обновление по wifi
Expand Down Expand Up @@ -36,11 +38,20 @@ Mode mode;
Command_s command_s;
char msg[255];


void updateState(void);

void setupCore0 (void){}


void loopCore0(void){
int timeToAlarm=isAlarm(30);
if(timeToAlarm!=-1){
mode.setBright(155 - timeToAlarm*5);
delay(4000);
mode.setMode(1);
}

ArduinoOTA.handle();
byte packetSize = Udp.parsePacket();

Expand Down Expand Up @@ -78,9 +89,7 @@ if(!command_s.command.equals("")){
mode.setBright(atoi(command_s.data.c_str()));

STRCASE ("PerMode")
byte color[15];
command_s.data.getBytes(color, 15);
mode.setPersonPalette(color);
mode.setPersonPalette((byte *)(command_s.data.c_str()));

STRCASE ("Delay")
mode.setDelay(atoi(command_s.data.c_str()));
Expand All @@ -106,6 +115,15 @@ if(!command_s.command.equals("")){

STRCASE ("Password")
mem.WriteConf(command_s.data);

STRCASE ("Hours")
setAlarmHours(atoi(command_s.data.c_str()));

STRCASE ("Minutes")
setAlarmMinutes(atoi(command_s.data.c_str()));

STRCASE ("Alarm")
setAlarm(atoi(command_s.data.c_str()));
}
}
command_s.command = "";
Expand All @@ -130,50 +148,59 @@ void taskCore1( void * pvParameters ){
}
}

void ota_update(){
#ifdef OTA_UPDATE
ArduinoOTA.setHostname(OTA_HOSTNAME);
void ota_update(){
#ifdef OTA_UPDATE
ArduinoOTA.setHostname(OTA_HOSTNAME);
ArduinoOTA.setPassword(OTA_PSSWRD);
ArduinoOTA
.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";

// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
Serial.println("Start updating " + type);
})
.onEnd([]() {
Serial.println("\nEnd");
})
.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
})
.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";

// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
Serial.println("Start updating " + type);
})
.onEnd([]() {
Serial.println("\nEnd");
})
.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
})
.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});

ArduinoOTA.begin();
#endif
}
}

void setup() {
//set monitor speed
Serial.begin(115200);
//allocate memory for WIFI SSID and Password
EEPROM.begin(64);

//if esp cant connect to wifi
if(!wifi.wifiSTAFromEeprom())
//switch to WIFI AP
wifi.wifiAP(AP_HOSTNAME,AP_PSSWRD);
//begin udp protocol
Udp.begin(2390);
//config ota update ifdef OTA_UPDATE
ota_update();
//init ntp time
timeInit(3);


//create a task that will be executed in the Task1code() function, with priority 1 and executed on core 0
//Task for core0
xTaskCreatePinnedToCore(
taskCore0, /* Task function. */
"Task1", /* name of task. */
Expand All @@ -184,7 +211,7 @@ void setup() {
0); /* pin task to core 0 */
delay(500);

//create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1
//Task for core1
xTaskCreatePinnedToCore(
taskCore1, /* Task function. */
"Task2", /* name of task. */
Expand Down

0 comments on commit 1fb7642

Please sign in to comment.