Commit 7b11a41a authored by Giacomo Lavermicocca's avatar Giacomo Lavermicocca

General fixes

parent 55ffcb53
/*
LoRa Sender commands & store the response
Http comunications with outside
......@@ -10,8 +10,8 @@
const char* ssid = "Thomas";
const char* password = "viapiccardi47!!thomas";
byte localAddress = 0xAA; // address of this device
byte destination = 0xBB; // destination to send to
byte localAddress = 0xDE; // address of this device
byte destination = 0xDD; // destination to send to
// TCP server at port 80 will respond to HTTP requests
WiFiServer server(80);
......@@ -26,11 +26,11 @@ void WIFISetUp(void)
delay(1000);
WiFi.mode(WIFI_STA);
WiFi.setAutoConnect(true);
WiFi.begin("Thomas","viapiccardi47!!thomas");
WiFi.begin("Thomas", "viapiccardi47!!thomas");
delay(100);
byte count = 0;
while(WiFi.status() != WL_CONNECTED && count < 10)
while (WiFi.status() != WL_CONNECTED && count < 10)
{
count ++;
delay(500);
......@@ -39,11 +39,11 @@ void WIFISetUp(void)
}
Heltec.display -> clear();
if(WiFi.status() == WL_CONNECTED)
if (WiFi.status() == WL_CONNECTED)
{
Heltec.display -> drawString(0, 0, "Connecting...OK.");
Heltec.display -> display();
// delay(500);
// delay(500);
}
else
{
......@@ -59,22 +59,24 @@ void WIFISetUp(void)
delay(500);
}
#define BAND 868E6
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();
Serial.println("LoRa Sender");
LoRa.setTxPowerMax(10);
LoRa.setTxPowerMax(15);
//LoRa.enableCrc();
LoRa.setPreambleLength(4);
LoRa.onReceive(onReceive);
LoRa.receive();
//-------------------------------------
WIFISetUp();
// Start TCP (HTTP) server
server.begin();
......@@ -88,7 +90,7 @@ void setup(void)
String s;
unsigned long previousMillis = 0;
const long interval = 10000;
const long interval = 25000;
int val = 0;
void loop(void)
......@@ -98,14 +100,14 @@ void loop(void)
if (WiFi.status() != WL_CONNECTED) {
ESP.restart();
}
long rssi = WiFi.RSSI();
if(rssi == 0)
if (rssi == 0)
{
ESP.restart();
}
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
......@@ -116,7 +118,7 @@ void loop(void)
while (client.connected() && !client.available()) {
delay(1);
}
// Read the first line of HTTP request
String req = client.readStringUntil('\r');
......@@ -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 += ipStr;
s += "";
} else if (req.indexOf("/pin") >= 0) {
pinRequest(req);
lastCommandReceived = "";
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
s += req;
s += "";
......@@ -150,7 +151,6 @@ void loop(void)
} else if (req.indexOf("/brightness") >= 0) {
setBrightness(req);
lastCommandReceived = "";
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
s += req;
s += "";
......@@ -158,7 +158,7 @@ void loop(void)
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";
s += req;
......@@ -235,16 +235,8 @@ void onReceive(int packetSize)
Serial.println("RSSI: " + String(LoRa.packetRssi()));
Serial.println("Snr: " + String(LoRa.packetSnr()));
Serial.println();
if (incoming.indexOf("r : ") == 0) {
lastCommandReceived = incoming;
}
if (incoming.indexOf("p : ") == 0) {
lastCommandReceived = incoming;
}
if (incoming.indexOf("b : ") == 0) {
if (incoming.indexOf("r : ") == 0) {
lastCommandReceived = incoming;
}
}
......@@ -259,18 +251,20 @@ void pinRequest(String req)
String outgoing = "p : " + req;
// send packet
LoRa.beginPacket();
LoRa.write(destination); // add destination address
LoRa.write(localAddress); // add sender address
LoRa.write(outgoing.length()); // add payload length
LoRa.print(outgoing); // add payload
LoRa.endPacket();
// put the radio into receive mode
LoRa.receive();
for (int i = 0; i < 5; i++) {
// send packet
LoRa.beginPacket();
LoRa.write(destination); // add destination address
LoRa.write(localAddress); // add sender address
LoRa.write(outgoing.length()); // add payload length
LoRa.print(outgoing); // add payload
LoRa.endPacket();
delay(2000);
}
lastCommandReceived = outgoing;
}
void setBrightness(String req)
......@@ -279,18 +273,20 @@ void setBrightness(String req)
String outgoing = "b : " + req;
// send packet
LoRa.beginPacket();
LoRa.write(destination); // add destination address
LoRa.write(localAddress); // add sender address
LoRa.write(outgoing.length()); // add payload length
LoRa.print(outgoing); // add payload
LoRa.endPacket();
// put the radio into receive mode
LoRa.receive();
for (int i = 0; i < 5; i++) {
// send packet
LoRa.beginPacket();
LoRa.write(destination); // add destination address
LoRa.write(localAddress); // add sender address
LoRa.write(outgoing.length()); // add payload length
LoRa.print(outgoing); // add payload
LoRa.endPacket();
delay(2000);
}
lastCommandReceived = outgoing;
}
void pinRequestRead(String req)
......@@ -299,16 +295,19 @@ void pinRequestRead(String req)
String outgoing = "r : " + req;
// send packet
LoRa.beginPacket();
LoRa.write(destination); // add destination address
LoRa.write(localAddress); // add sender address
LoRa.write(outgoing.length()); // add payload length
LoRa.print(outgoing); // add payload
LoRa.endPacket();
for (int i = 0; i < 5; i++) {
// send packet
LoRa.beginPacket();
LoRa.write(destination); // add destination address
LoRa.write(localAddress); // add sender address
LoRa.write(outgoing.length()); // add payload length
LoRa.print(outgoing); // add payload
LoRa.endPacket();
delay(2000);
}
// 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