LoRaReceiverInterrupt.ino 6.16 KB
Newer Older
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
1
/* 
2 3
    Receiver for exchange commands between LoRa Sender (long range)
    Led PWM switch
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
4 5 6
*/
#include <SPI.h>
#include <LoRa.h>
7
#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
8 9 10 11 12 13 14 15 16 17

// Pin definetion of WIFI LoRa 32
// HelTec AutoMation 2017 support@heltec.cn 
#define SCK     5    // GPIO5  -- SX127x's SCK
#define MISO    19   // GPIO19 -- SX127x's MISO
#define MOSI    27   // GPIO27 -- SX127x's MOSI
#define SS      18   // GPIO18 -- SX127x's CS
#define RST     14   // GPIO14 -- SX127x's RESET
#define DI0     26   // GPIO26 -- SX127x's IRQ(Interrupt Request)

18
#define BAND    868E6  //you can set band here directly,e.g. 868E6,915E6
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
19

Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
20 21
#define RELE_1 17
#define RELE_2 21
22
#define LED_PIN 13
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
23 24 25

#define RELE_READ_1 36
#define RELE_READ_2 37
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
26

27 28
SSD1306  display(0x3c, 4, 15);

29 30
String lastCommand = "";
String signalLevel = "";
31

32 33 34 35 36 37 38 39 40 41
// --------------- 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;
42
const long intervalLed = 60000 * 10;
43 44 45 46 47 48 49 50 51 52 53

// 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);
}

Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
54
void setup() {
55 56 57 58

  // 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);
59
  
60 61 62 63 64 65 66 67 68 69 70
  //-------------------------------- DISPLAY ------------------------------
  pinMode(16,OUTPUT);
  digitalWrite(16, LOW);    // set GPIO16 low to reset OLED
  delay(50); 
  digitalWrite(16, HIGH); // while OLED is running, must set GPIO16 in high
  // Initialising the UI will init the display too.
  display.init();
  display.flipScreenVertically();
  display.setFont(ArialMT_Plain_10);
  //------------------------------------------------------------------------

Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
71
  SPI.begin(SCK,MISO,MOSI,SS);
72
  LoRa.enableCrc();
73 74
  LoRa.setTxPower(20,PA_OUTPUT_PA_BOOST_PIN);
  LoRa.setPreambleLength(8);
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
75
  LoRa.setPins(SS,RST,DI0);
76
  if (!LoRa.begin(BAND)) {
77
    drawTextOLED("Starting LoRa failed!");
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
78 79 80
    while (1);
  }

Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
81 82
  pinMode(RELE_1, OUTPUT);
  pinMode(RELE_2, OUTPUT);
83

Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
84 85
  pinMode(RELE_READ_1, INPUT_PULLUP);
  pinMode(RELE_READ_2, INPUT_PULLUP);
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
86 87 88 89 90 91 92 93

  // register the receive callback
  LoRa.onReceive(onReceive);

  // put the radio into receive mode
  LoRa.receive();
}

Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
94
unsigned long previousMillis = 0;
95
const long interval = 2500;
96
bool isLoRaOnSenderMode = false;
97

98 99 100 101 102 103 104 105 106 107 108
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;
109
const long intervalIsOnLedOff = 60000 * 1;
110

111
void loop() {  
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
112
  // do nothing
113
  unsigned long currentMillis = millis();
114
  if (currentMillis - previousMillis > interval) {
115
    //Reset rele
116 117
    digitalWrite(RELE_1, LOW);
    digitalWrite(RELE_2, LOW);
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
118
  }
119

120
  if(isLoRaOnSenderMode)
121
  {
122 123 124 125 126 127
    for(int i = 0; i < 10; i++)
    {
      LoRa.beginPacket();
      LoRa.print(lastCommand);
      LoRa.endPacket();
    }
128 129
    
    LoRa.receive();
130
    isLoRaOnSenderMode = false;
131
  }
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

  // ------------ 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);
162
  
163
  drawTextOLED(lastCommand);
164 165 166 167 168 169 170 171 172 173
}

void drawTextOLED(String text) {
  display.clear();
  display.setTextAlignment(TEXT_ALIGN_LEFT);
  
  display.setFont(ArialMT_Plain_10);
  display.drawString(0, 0, text);
  
  display.setFont(ArialMT_Plain_10);
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
174
  display.drawString(0, 20, "Level : " + signalLevel);
175 176 177

  display.setFont(ArialMT_Plain_10);
  display.drawString(0, 40, "Giacomo Lavermicocca");
178 179

  display.display();
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
180 181
}

Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
182 183 184
String r1 = String(RELE_1, DEC);
String r2 = String(RELE_2, DEC);

Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
185 186 187
String read_r1 = String(RELE_READ_1, DEC);
String read_r2 = String(RELE_READ_2, DEC);

Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
188 189
void onReceive(int packetSize)
{
190
  //LoRa.crc();
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
191
  // received a packet
192
  String str = "";
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
193 194 195 196 197 198
  for (int i = 0; i < packetSize; i++)
  {
    char p = (char)LoRa.read();
    str += p;
  }

199
  lastCommand = str;
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
200

201
  if(str.indexOf("p : ") == 0)
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
202
  {        
203
    if(str.indexOf(r1) > 0)
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
204 205 206
    {
      setRele1();
    }
207
    if(str.indexOf(r2) > 0)
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
208 209 210
    {
      setRele2();
    }
211

212
    isLoRaOnSenderMode = true;
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
213
  }
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
214 215

  String val = "";
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
216
  
217
  if(str.indexOf("r : ") == 0)
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
218
  {    
219
    if(str.indexOf(read_r1) > 0)
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
220 221 222
    {
      val = readRele1();
    }
223
    else if(str.indexOf(read_r2) > 0)
224
    {
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
225
      val = readRele2();
226
    }
227

228
    isLoRaOnSenderMode = true;
229 230

    lastCommand = str + " " + val;
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
231
  }
232 233 234 235 236 237 238 239 240

  if(str.indexOf("b : ") == 0)
  {    
    val = str.substring(4);

    brightness = val.toInt();

    previousMillisLed = millis();

241
    isLoRaOnSenderMode = true;
242
  }
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
243
  
244
  signalLevel = String(LoRa.packetRssi(), DEC);
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
245 246
}

247 248
void setRele1()
{
249
  digitalWrite(RELE_1, HIGH);
250 251 252 253 254
  previousMillis = millis();
}

void setRele2()
{
255
  digitalWrite(RELE_2, HIGH);
256
  previousMillis = millis();
257 258 259 260 261
  // switch on light
  brightness = 255;
  previousMillisLed = millis();
  previousMillisIsOnLedOff = millis() + intervalLed;
  isLedOn = false;
262 263
}

Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
264 265 266 267 268
String readRele1()
{
  previousMillis = millis();
  String v = String(digitalRead(RELE_READ_1), DEC);
  return v;
269 270
}

Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
271 272 273 274 275
String readRele2()
{
  previousMillis = millis();
  String v = String(digitalRead(RELE_READ_2), DEC);
  return v;
276
}