...
 
Commits (2)
/* /*
Receiver for exchange commands between LoRa Sender (long range) Receiver for exchange commands between LoRa Sender (long range)
*/ */
#include <heltec.h> #include <heltec.h>
byte localAddress = 0xBB; // address of this device byte localAddress = 0xDD; // address of this device
byte destination = 0xAA; // destination to send to byte destination = 0xDE; // destination to send to
#define RELE_1 17 #define RELE_1 17
#define RELE_2 21 #define RELE_2 21
...@@ -16,16 +16,18 @@ byte destination = 0xAA; // destination to send to ...@@ -16,16 +16,18 @@ byte destination = 0xAA; // destination to send to
String lastCommand = ""; String lastCommand = "";
String signalLevel = ""; String signalLevel = "";
#define BAND 868E6
void setup() { void setup() {
Heltec.begin(true /*DisplayEnable Enable*/, true /*LoRa Enable*/, true /*Serial Enable*/, true /*LoRa use PABOOST*/, 868E6 /*LoRa RF working band*/); Heltec.begin(true /*DisplayEnable Enable*/, true /*LoRa Enable*/, true /*Serial Enable*/, true /*LoRa use PABOOST*/, BAND /*LoRa RF working band*/);
Heltec.display -> clear(); Heltec.display -> clear();
Serial.println("LoRa Sender"); Serial.println("LoRa Sender");
LoRa.setTxPowerMax(10); LoRa.setTxPowerMax(15);
//LoRa.enableCrc(); //LoRa.enableCrc();
LoRa.setPreambleLength(4); LoRa.setPreambleLength(4);
// register the receive callback // register the receive callback
LoRa.onReceive(onReceive); LoRa.onReceive(onReceive);
// put the radio into receive mode // put the radio into receive mode
...@@ -36,7 +38,7 @@ void setup() { ...@@ -36,7 +38,7 @@ void setup() {
// LoRa.setPreambleLength(1); // LoRa.setPreambleLength(1);
//--------------------------------------------- //---------------------------------------------
pinMode(RELE_1, OUTPUT); pinMode(RELE_1, OUTPUT);
pinMode(RELE_2, OUTPUT); pinMode(RELE_2, OUTPUT);
pinMode(RELE_3, OUTPUT); pinMode(RELE_3, OUTPUT);
...@@ -47,42 +49,62 @@ void setup() { ...@@ -47,42 +49,62 @@ void setup() {
unsigned long previousMillisLed = 0; unsigned long previousMillisLed = 0;
const long intervalLed = 60000 * 10; const long intervalLed = 60000 * 10;
unsigned long previousMillisRele = 0;
const long intervalRele = 1000 * 10;
bool isLoRaOnSenderMode = false; bool isLoRaOnSenderMode = false;
bool isLedOff = true; bool isLedOff = true;
bool lockRequestsFor10sec = false;
void loop() { void loop() {
unsigned long currentMillis = millis(); unsigned long currentMillis = millis();
if (currentMillis - previousMillisRele > intervalRele) {
lockRequestsFor10sec = false;
} else {
lockRequestsFor10sec = true;
Heltec.display -> clear();
Heltec.display -> drawString(0, 0, lastCommand);
Heltec.display -> display();
}
if (isLedOff) { if (isLedOff) {
delay(2000);
isLedOff = false; isLedOff = false;
//Reset rele //Reset rele
digitalWrite(RELE_1, LOW); digitalWrite(RELE_1, LOW);
digitalWrite(RELE_2, LOW); digitalWrite(RELE_2, LOW);
} }
if(currentMillis - previousMillisLed > intervalLed){ if (currentMillis - previousMillisLed > intervalLed) {
digitalWrite(RELE_3, LOW); digitalWrite(RELE_3, LOW);
//ESP.restart(); //ESP.restart();
} }
if(isLoRaOnSenderMode) if (isLoRaOnSenderMode)
{ {
isLoRaOnSenderMode = false; isLoRaOnSenderMode = false;
Heltec.display -> clear();
Heltec.display -> drawString(0, 0, lastCommand);
Heltec.display -> display();
String outgoing = lastCommand; String outgoing = lastCommand;
for(int i = 0; i < 10; i++){ for (int i = 0; i < 10; i++) {
// send packet // send packet
LoRa.beginPacket(); LoRa.beginPacket();
LoRa.write(destination); // add destination address LoRa.write(destination); // add destination address
LoRa.write(localAddress); // add sender address LoRa.write(localAddress); // add sender address
LoRa.write(outgoing.length()); // add payload length LoRa.write(outgoing.length()); // add payload length
LoRa.print(outgoing); // add payload LoRa.print(outgoing); // add payload
LoRa.endPacket(); LoRa.endPacket();
delay(2000);
} }
// put the radio into receive mode // put the radio into receive mode
LoRa.receive(); LoRa.receive();
} }
...@@ -91,6 +113,7 @@ void loop() { ...@@ -91,6 +113,7 @@ void loop() {
String r1 = String(RELE_1, DEC); String r1 = String(RELE_1, DEC);
String r2 = String(RELE_2, DEC); String r2 = String(RELE_2, DEC);
String r3 = String(RELE_3, DEC); String r3 = String(RELE_3, DEC);
String r0 = "0";
String read_r1 = String(RELE_READ_1, DEC); String read_r1 = String(RELE_READ_1, DEC);
String read_r2 = String(RELE_READ_2, DEC); String read_r2 = String(RELE_READ_2, DEC);
...@@ -134,68 +157,76 @@ void onReceive(int packetSize) ...@@ -134,68 +157,76 @@ void onReceive(int packetSize)
Serial.println("RSSI: " + String(LoRa.packetRssi())); Serial.println("RSSI: " + String(LoRa.packetRssi()));
Serial.println("Snr: " + String(LoRa.packetSnr())); Serial.println("Snr: " + String(LoRa.packetSnr()));
Serial.println(); Serial.println();
// received a packet // received a packet
String str = incoming; String str = incoming;
if(str.indexOf("p : ") == 0) if (str.indexOf("p : ") == 0)
{ {
if(str.indexOf(r1) > 0) if (lockRequestsFor10sec == false) {
{ lastCommand = str;
setRele1(); previousMillisRele = millis();
} isLedOff = true;
if(str.indexOf(r2) > 0) if (str.indexOf(r1) > 0)
{ {
setRele2(); setRele1();
} }
if(str.indexOf(r3) > 0) if (str.indexOf(r2) > 0)
{ {
setRele3(); setRele2();
}
if (str.indexOf(r3) > 0)
{
setRele3();
}
if (str.indexOf(r0) > 0)
{
setRele0();
}
} }
lastCommand = str;
isLoRaOnSenderMode = true;
} }
if(str.indexOf("r : ") == 0) if (str.indexOf("r : ") == 0)
{ {
val = ""; val = "";
if(str.indexOf(read_r1) > 0) if (str.indexOf(read_r1) > 0)
{ {
val = readRele1(); val = readRele1();
} }
else if(str.indexOf(read_r2) > 0) else if (str.indexOf(read_r2) > 0)
{ {
val = readRele2(); val = readRele2();
} }
isLoRaOnSenderMode = true;
lastCommand = str + " " + val; lastCommand = str + " " + val;
isLoRaOnSenderMode = true;
} }
} }
void setRele0()
{
digitalWrite(RELE_1, HIGH);
digitalWrite(RELE_2, HIGH);
digitalWrite(RELE_3, HIGH); // LUCI LED
previousMillisLed = millis();
}
void setRele1() void setRele1()
{ {
isLedOff = true;
digitalWrite(RELE_1, HIGH); digitalWrite(RELE_1, HIGH);
digitalWrite(RELE_3, HIGH); // LUCI LED digitalWrite(RELE_3, HIGH); // LUCI LED
delay(2000);
previousMillisLed = millis(); previousMillisLed = millis();
} }
void setRele2() void setRele2()
{ {
isLedOff = true;
digitalWrite(RELE_2, HIGH); digitalWrite(RELE_2, HIGH);
digitalWrite(RELE_3, HIGH); // LUCI LED digitalWrite(RELE_3, HIGH); // LUCI LED
delay(2000);
previousMillisLed = millis(); previousMillisLed = millis();
} }
void setRele3() void setRele3()
{ {
digitalWrite(RELE_3, HIGH); digitalWrite(RELE_3, HIGH);
......
/* /*
LoRa Sender commands & store the response LoRa Sender commands & store the response
Http comunications with outside Http comunications with outside
...@@ -10,8 +10,8 @@ ...@@ -10,8 +10,8 @@
const char* ssid = "Thomas"; const char* ssid = "Thomas";
const char* password = "viapiccardi47!!thomas"; const char* password = "viapiccardi47!!thomas";
byte localAddress = 0xAA; // address of this device byte localAddress = 0xDE; // address of this device
byte destination = 0xBB; // destination to send to byte destination = 0xDD; // destination to send to
// TCP server at port 80 will respond to HTTP requests // TCP server at port 80 will respond to HTTP requests
WiFiServer server(80); WiFiServer server(80);
...@@ -26,11 +26,11 @@ void WIFISetUp(void) ...@@ -26,11 +26,11 @@ void WIFISetUp(void)
delay(1000); delay(1000);
WiFi.mode(WIFI_STA); WiFi.mode(WIFI_STA);
WiFi.setAutoConnect(true); WiFi.setAutoConnect(true);
WiFi.begin("Thomas","viapiccardi47!!thomas"); WiFi.begin("Thomas", "viapiccardi47!!thomas");
delay(100); delay(100);
byte count = 0; byte count = 0;
while(WiFi.status() != WL_CONNECTED && count < 10) while (WiFi.status() != WL_CONNECTED && count < 10)
{ {
count ++; count ++;
delay(500); delay(500);
...@@ -39,11 +39,11 @@ void WIFISetUp(void) ...@@ -39,11 +39,11 @@ void WIFISetUp(void)
} }
Heltec.display -> clear(); Heltec.display -> clear();
if(WiFi.status() == WL_CONNECTED) if (WiFi.status() == WL_CONNECTED)
{ {
Heltec.display -> drawString(0, 0, "Connecting...OK."); Heltec.display -> drawString(0, 0, "Connecting...OK.");
Heltec.display -> display(); Heltec.display -> display();
// delay(500); // delay(500);
} }
else else
{ {
...@@ -59,22 +59,24 @@ void WIFISetUp(void) ...@@ -59,22 +59,24 @@ void WIFISetUp(void)
delay(500); delay(500);
} }
#define BAND 868E6
void setup(void) void setup(void)
{ {
Heltec.begin(true /*DisplayEnable Enable*/, true /*LoRa Enable*/, true /*Serial Enable*/, true /*LoRa use PABOOST*/, 868E6 /*LoRa RF working band*/); Heltec.begin(true /*DisplayEnable Enable*/, true /*LoRa Enable*/, false /*Serial Enable*/, true /*LoRa use PABOOST*/, BAND /*LoRa RF working band*/);
Heltec.display -> clear(); Heltec.display -> clear();
Serial.println("LoRa Sender"); Serial.println("LoRa Sender");
LoRa.setTxPowerMax(10); LoRa.setTxPowerMax(15);
//LoRa.enableCrc(); //LoRa.enableCrc();
LoRa.setPreambleLength(4); LoRa.setPreambleLength(4);
LoRa.onReceive(onReceive); LoRa.onReceive(onReceive);
LoRa.receive(); LoRa.receive();
//------------------------------------- //-------------------------------------
WIFISetUp(); WIFISetUp();
// Start TCP (HTTP) server // Start TCP (HTTP) server
server.begin(); server.begin();
...@@ -88,7 +90,7 @@ void setup(void) ...@@ -88,7 +90,7 @@ void setup(void)
String s; String s;
unsigned long previousMillis = 0; unsigned long previousMillis = 0;
const long interval = 10000; const long interval = 25000;
int val = 0; int val = 0;
void loop(void) void loop(void)
...@@ -98,14 +100,14 @@ void loop(void) ...@@ -98,14 +100,14 @@ void loop(void)
if (WiFi.status() != WL_CONNECTED) { if (WiFi.status() != WL_CONNECTED) {
ESP.restart(); ESP.restart();
} }
long rssi = WiFi.RSSI(); long rssi = WiFi.RSSI();
if(rssi == 0) if (rssi == 0)
{ {
ESP.restart(); ESP.restart();
} }
// Check if a client has connected // Check if a client has connected
WiFiClient client = server.available(); WiFiClient client = server.available();
if (!client) { if (!client) {
...@@ -116,7 +118,7 @@ void loop(void) ...@@ -116,7 +118,7 @@ void loop(void)
while (client.connected() && !client.available()) { while (client.connected() && !client.available()) {
delay(1); delay(1);
} }
// Read the first line of HTTP request // Read the first line of HTTP request
String req = client.readStringUntil('\r'); String req = client.readStringUntil('\r');
...@@ -138,10 +140,9 @@ void loop(void) ...@@ -138,10 +140,9 @@ void loop(void)
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\nHello from ESP32 at "; s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\nHello from ESP32 at ";
s += ipStr; s += ipStr;
s += ""; s += "";
} else if (req.indexOf("/pin") >= 0) { } else if (req.indexOf("/pin") >= 0) {
pinRequest(req); pinRequest(req);
lastCommandReceived = "";
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"; s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
s += req; s += req;
s += ""; s += "";
...@@ -150,7 +151,6 @@ void loop(void) ...@@ -150,7 +151,6 @@ void loop(void)
} else if (req.indexOf("/brightness") >= 0) { } else if (req.indexOf("/brightness") >= 0) {
setBrightness(req); setBrightness(req);
lastCommandReceived = "";
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"; s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
s += req; s += req;
s += ""; s += "";
...@@ -158,7 +158,7 @@ void loop(void) ...@@ -158,7 +158,7 @@ void loop(void)
previousMillis = currentMillis; previousMillis = currentMillis;
} else if (req.indexOf("/read") >= 0) { } else if (req.indexOf("/read") >= 0) {
pinRequestRead(req); pinRequestRead(req);
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"; s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
s += req; s += req;
...@@ -235,16 +235,8 @@ void onReceive(int packetSize) ...@@ -235,16 +235,8 @@ void onReceive(int packetSize)
Serial.println("RSSI: " + String(LoRa.packetRssi())); Serial.println("RSSI: " + String(LoRa.packetRssi()));
Serial.println("Snr: " + String(LoRa.packetSnr())); Serial.println("Snr: " + String(LoRa.packetSnr()));
Serial.println(); Serial.println();
if (incoming.indexOf("r : ") == 0) {
lastCommandReceived = incoming;
}
if (incoming.indexOf("p : ") == 0) { if (incoming.indexOf("r : ") == 0) {
lastCommandReceived = incoming;
}
if (incoming.indexOf("b : ") == 0) {
lastCommandReceived = incoming; lastCommandReceived = incoming;
} }
} }
...@@ -259,18 +251,20 @@ void pinRequest(String req) ...@@ -259,18 +251,20 @@ void pinRequest(String req)
String outgoing = "p : " + req; String outgoing = "p : " + req;
// send packet for (int i = 0; i < 5; i++) {
LoRa.beginPacket(); // send packet
LoRa.beginPacket();
LoRa.write(destination); // add destination address
LoRa.write(localAddress); // add sender address LoRa.write(destination); // add destination address
LoRa.write(outgoing.length()); // add payload length LoRa.write(localAddress); // add sender address
LoRa.print(outgoing); // add payload LoRa.write(outgoing.length()); // add payload length
LoRa.print(outgoing); // add payload
LoRa.endPacket();
LoRa.endPacket();
// put the radio into receive mode delay(2000);
LoRa.receive(); }
lastCommandReceived = outgoing;
} }
void setBrightness(String req) void setBrightness(String req)
...@@ -279,18 +273,20 @@ void setBrightness(String req) ...@@ -279,18 +273,20 @@ void setBrightness(String req)
String outgoing = "b : " + req; String outgoing = "b : " + req;
// send packet for (int i = 0; i < 5; i++) {
LoRa.beginPacket(); // send packet
LoRa.beginPacket();
LoRa.write(destination); // add destination address
LoRa.write(localAddress); // add sender address LoRa.write(destination); // add destination address
LoRa.write(outgoing.length()); // add payload length LoRa.write(localAddress); // add sender address
LoRa.print(outgoing); // add payload LoRa.write(outgoing.length()); // add payload length
LoRa.print(outgoing); // add payload
LoRa.endPacket();
LoRa.endPacket();
// put the radio into receive mode delay(2000);
LoRa.receive(); }
lastCommandReceived = outgoing;
} }
void pinRequestRead(String req) void pinRequestRead(String req)
...@@ -299,16 +295,19 @@ void pinRequestRead(String req) ...@@ -299,16 +295,19 @@ void pinRequestRead(String req)
String outgoing = "r : " + req; String outgoing = "r : " + req;
// send packet for (int i = 0; i < 5; i++) {
LoRa.beginPacket(); // send packet
LoRa.beginPacket();
LoRa.write(destination); // add destination address
LoRa.write(localAddress); // add sender address LoRa.write(destination); // add destination address
LoRa.write(outgoing.length()); // add payload length LoRa.write(localAddress); // add sender address
LoRa.print(outgoing); // add payload LoRa.write(outgoing.length()); // add payload length
LoRa.print(outgoing); // add payload
LoRa.endPacket();
LoRa.endPacket();
delay(2000);
}
// put the radio into receive mode // put the radio into receive mode
LoRa.receive(); LoRa.receive();
} }