Commit 425ad89a authored by Giacomo Lavermicocca's avatar Giacomo Lavermicocca

Fading effects added

parent 62368132
/*
The default interrupt pin in SX1276/8(DIO0) connected to ESP32's GPIO26
by Aaron.Lee from HelTec AutoMation, ChengDu, China
成都惠利特自动化科技有限公司
www.heltec.cn
this project also realess in GitHub:
https://github.com/Heltec-Aaron-Lee/WiFi_Kit_series
Receiver for exchange commands between LoRa Sender (long range)
Led PWM switch
*/
#include <SPI.h>
#include <LoRa.h>
......@@ -27,6 +20,7 @@
#define RELE_1 17
#define RELE_2 21
#define LED_PIN 13
#define RELE_READ_1 36
#define RELE_READ_2 37
......@@ -36,10 +30,36 @@ SSD1306 display(0x3c, 4, 15);
String lastCommand = "";
String signalLevel = "";
// --------------- LED PWM CONFIGURATION ----------------
// use first channel of 16 channels (started from zero)
#define LEDC_CHANNEL_0 0
// use 5000 Hz as a LEDC base frequency
#define LEDC_BASE_FREQ 10000
// use 13 bit precission for LEDC timer
#define LEDC_TIMER_13_BIT 13
#define LED_PIN 13
int brightness = 0;
unsigned long previousMillisLed = 0;
const long intervalLed = 60000 * 3;
// Arduino like analogWrite
// value has to be between 0 and valueMax
void ledcAnalogWrite(uint8_t channel, uint32_t value, uint32_t valueMax = 255) {
// calculate duty, 8191 from 2 ^ 13 - 1
uint32_t duty = (8191 / valueMax) * min(value, valueMax);
// write duty to LEDC
ledcWrite(channel, duty);
}
void setup() {
Serial.begin(115200); // initialize serial
while (!Serial);
Serial.println("Begin");
// Setup timer and attach timer to a led pin
ledcSetup(LEDC_CHANNEL_0, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
ledcAttachPin(LED_PIN, LEDC_CHANNEL_0);
//-------------------------------- DISPLAY ------------------------------
pinMode(16,OUTPUT);
......@@ -79,6 +99,19 @@ unsigned long previousMillis = 0;
const long interval = 2500;
bool isLoRaOnReceiveMode = false;
bool isLedBegin = true;
bool isLedOn = false;
// fading final effects
int fadeAmount = 7;
unsigned long previousMillisLedOff = 0;
const long intervalLedOff = 18;
//delay before switch off
bool isLedOff = false;
unsigned long previousMillisIsOnLedOff = 0;
const long intervalIsOnLedOff = 30000 * 1;
void loop() {
// do nothing
unsigned long currentMillis = millis();
......@@ -87,10 +120,6 @@ void loop() {
digitalWrite(RELE_1, LOW);
digitalWrite(RELE_2, LOW);
}
else
{
}
if(isLoRaOnReceiveMode)
{
......@@ -101,6 +130,36 @@ void loop() {
LoRa.receive();
isLoRaOnReceiveMode = false;
}
// ------------ LED -----------------
if(isLedBegin){
if (currentMillis - previousMillisLed > intervalLed) {
brightness = 0;
isLedBegin = false;
isLedOff = true;
previousMillisLedOff = millis();
fadeAmount = 5;
}
}
if (currentMillis - previousMillisIsOnLedOff > intervalIsOnLedOff) {
isLedOff = false;
isLedBegin = true;
}
if(isLedOff){
if (currentMillis - previousMillisLedOff > intervalLedOff) {
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
previousMillisLedOff = millis();
}
}
ledcAnalogWrite(LEDC_CHANNEL_0, brightness);
drawTextOLED(lastCommand);
}
......@@ -139,9 +198,11 @@ void onReceive(int packetSize)
lastCommand = str;
Serial.println(lastCommand);
if(str.indexOf("p : ") == 0)
{
if(str.indexOf(r1) > 0) //TODO manca la parte di sicurezza
if(str.indexOf(r1) > 0)
{
setRele1();
}
......@@ -157,7 +218,7 @@ void onReceive(int packetSize)
if(str.indexOf("r : ") == 0)
{
if(str.indexOf(read_r1) > 0) //TODO manca la parte di sicurezza
if(str.indexOf(read_r1) > 0)
{
val = readRele1();
}
......@@ -170,6 +231,17 @@ void onReceive(int packetSize)
lastCommand = str + " " + val;
}
if(str.indexOf("b : ") == 0)
{
val = str.substring(4);
brightness = val.toInt();
previousMillisLed = millis();
isLoRaOnReceiveMode = true;
}
signalLevel = String(LoRa.packetRssi(), DEC);
}
......@@ -178,6 +250,11 @@ void setRele1()
{
digitalWrite(RELE_1, HIGH);
previousMillis = millis();
// switch on light
brightness = 255;
previousMillisLed = millis();
previousMillisIsOnLedOff = millis() + intervalLed;
isLedOn = false;
}
void setRele2()
......
......@@ -121,7 +121,6 @@ void drawTextOLED(String text, String ttext, String rread, String signalLevel) {
String s;
unsigned long previousMillis = 0;
unsigned long previousMillisGPIO = 0;
const long interval = 10000;
void loop(void)
......@@ -163,7 +162,7 @@ void loop(void)
s += ipStr;
s += "";
} else if (req.indexOf('/pin') > 0) {
} else if (req.indexOf("/pin") >= 0) {
pinRequest(req);
lastCommand = "";
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
......@@ -172,7 +171,16 @@ void loop(void)
previousMillis = currentMillis;
} else if (req.indexOf('/read') > 0) {
} else if (req.indexOf("/brightness") >= 0) {
setBrightness(req);
lastCommand = "";
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
s += req;
s += "";
previousMillis = currentMillis;
} else if (req.indexOf("/read") >= 0) {
pinRequestRead(req);
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
......@@ -181,7 +189,7 @@ void loop(void)
previousMillis = currentMillis;
} else if (req.indexOf('/check') > 0) {
} else if (req.indexOf("/check") >= 0) {
//OK send s to browser
if (currentMillis - previousMillis >= interval) {
......@@ -193,15 +201,6 @@ void loop(void)
s += lastCommand;
s += "";
}
} else if (req.indexOf('/gpio') > 0) {
//OK send s to browser
pinGPIO(req);
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
s += req;
s += "";
previousMillisGPIO = currentMillis + 2000;
} else {
s = "HTTP/1.1 404 Not Found\r\n\r\n";
// Serial.println("Sending 404");
......@@ -267,24 +266,32 @@ void pinRequest(String req)
LoRa.receive();
}
void pinRequestRead(String req)
void setBrightness(String req)
{
req = req.substring(req.lastIndexOf('/') + 1);
int reqInt = req.toInt();
// send packet
LoRa.beginPacket();
LoRa.print("r : ");
LoRa.print("b : ");
LoRa.print(reqInt);
LoRa.endPacket();
delay(1);
// put the radio into receive mode
LoRa.receive();
}
void pinGPIO(String req)
void pinRequestRead(String req)
{
req = req.substring(req.lastIndexOf('/') + 1);
int reqInt = req.toInt();
// send packet
LoRa.beginPacket();
LoRa.print("r : ");
LoRa.print(reqInt);
LoRa.endPacket();
lastCommand = "gpio : " + String(reqInt);
// put the radio into receive mode
LoRa.receive();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment