Commit 62368132 authored by Giacomo Lavermicocca's avatar Giacomo Lavermicocca

Bug fix - add Crc control - fix bad comunication antenna - fix web server responses

parent 8f29aee4
Pipeline #240 failed with stages
in 1 minute and 46 seconds
......@@ -22,7 +22,7 @@
#define RST 14 // GPIO14 -- SX127x's RESET
#define DI0 26 // GPIO26 -- SX127x's IRQ(Interrupt Request)
#define BAND 868E6 //you can set band here directly,e.g. 868E6,915E6
#define BAND 915E6 //you can set band here directly,e.g. 868E6,915E6
#define PABOOST true
#define RELE_1 17
......@@ -37,6 +37,10 @@ String lastCommand = "";
String signalLevel = "";
void setup() {
Serial.begin(115200); // initialize serial
while (!Serial);
Serial.println("Begin");
//-------------------------------- DISPLAY ------------------------------
pinMode(16,OUTPUT);
digitalWrite(16, LOW); // set GPIO16 low to reset OLED
......@@ -49,8 +53,11 @@ void setup() {
//------------------------------------------------------------------------
SPI.begin(SCK,MISO,MOSI,SS);
LoRa.enableCrc();
LoRa.setTxPower(20,14);
LoRa.setSignalBandwidth(7.8E3);
LoRa.setPins(SS,RST,DI0);
if (!LoRa.begin(BAND,PABOOST)) {
if (!LoRa.begin(BAND)) {
drawTextOLED("Starting LoRa failed!");
while (1);
}
......@@ -70,19 +77,31 @@ void setup() {
unsigned long previousMillis = 0;
const long interval = 2500;
bool isLoRaOnReceiveMode = false;
void loop() {
// do nothing
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
if (currentMillis - previousMillis > interval) {
//Reset rele
digitalWrite(RELE_1, HIGH);
digitalWrite(RELE_2, HIGH);
digitalWrite(RELE_1, LOW);
digitalWrite(RELE_2, LOW);
}
else
{
}
if(isLoRaOnReceiveMode)
{
LoRa.beginPacket();
LoRa.print(lastCommand);
LoRa.endPacket();
LoRa.receive();
isLoRaOnReceiveMode = false;
}
drawTextOLED(lastCommand);
}
......@@ -120,46 +139,36 @@ void onReceive(int packetSize)
lastCommand = str;
if(str.indexOf("pin : ") >= 0)
if(str.indexOf("p : ") == 0)
{
if(str.indexOf(r1) >= 0) //TODO manca la parte di sicurezza
if(str.indexOf(r1) > 0) //TODO manca la parte di sicurezza
{
setRele1();
}
if(str.indexOf(r2) >= 0)
if(str.indexOf(r2) > 0)
{
setRele2();
}
LoRa.beginPacket();
LoRa.print(str);
LoRa.endPacket();
// put the radio into receive mode
LoRa.receive();
isLoRaOnReceiveMode = true;
}
String val = "";
if(str.indexOf("read : ") >= 0)
if(str.indexOf("r : ") == 0)
{
if(str.indexOf(read_r1) >= 0) //TODO manca la parte di sicurezza
if(str.indexOf(read_r1) > 0) //TODO manca la parte di sicurezza
{
val = readRele1();
}
else if(str.indexOf(read_r2) >= 0)
else if(str.indexOf(read_r2) > 0)
{
val = readRele2();
}
LoRa.beginPacket();
LoRa.print(str);
LoRa.print(" ");
LoRa.print(val);
LoRa.endPacket();
// put the radio into receive mode
LoRa.receive();
isLoRaOnReceiveMode = true;
lastCommand = str + " " + val;
}
signalLevel = String(LoRa.packetRssi(), DEC);
......@@ -167,13 +176,13 @@ void onReceive(int packetSize)
void setRele1()
{
digitalWrite(RELE_1, LOW);
digitalWrite(RELE_1, HIGH);
previousMillis = millis();
}
void setRele2()
{
digitalWrite(RELE_2, LOW);
digitalWrite(RELE_2, HIGH);
previousMillis = millis();
}
......
/*
ESP32 mDNS responder sample
This is an example of an HTTP server that is accessible
via http://esp32.local URL thanks to mDNS responder.
Instructions:
- Update WiFi SSID and password as necessary.
- Flash the sketch to the ESP32 board
- Install host software:
- For Linux, install Avahi (http://avahi.org/).
- For Windows, install Bonjour (http://www.apple.com/support/bonjour/).
- For Mac OSX and iOS support is built in through Bonjour already.
- Point your browser to http://esp32.local, you should see a response.
LoRa Sender commands & store the response
Http comunications with outside
*/
......@@ -22,8 +12,6 @@
#include <SPI.h>
#include <LoRa.h>
#include <ESPmDNS.h>
#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
const char* ssid = "Thomas";
......@@ -41,7 +29,7 @@ WiFiServer server(80);
#define RST 14 // GPIO14 -- SX127x's RESET
#define DI0 26 // GPIO26 -- SX127x's IRQ(Interrupt Request)
#define BAND 868E6 //you can set band here directly,e.g. 868E6,915E6
#define BAND 915E6 //you can set band here directly,e.g. 868E6,915E6
#define PABOOST true
SSD1306 display(0x3c, 4, 15);
......@@ -52,7 +40,7 @@ String lastReceived = "";
String signalLevel = "";
void setup(void)
{
{
//-------- DISPLAY INIT --------------
pinMode(16, OUTPUT);
......@@ -72,16 +60,20 @@ void setup(void)
pinMode(17, OUTPUT);
digitalWrite(17, LOW); // set GPIO17 low
//Initialize serial and wait for port to open:
Serial.begin(115200);
SPI.begin(SCK, MISO, MOSI, SS);
LoRa.enableCrc();
LoRa.setTxPower(20,14);
LoRa.setSignalBandwidth(7.8E3);
LoRa.setPins(SS, RST, DI0);
if (!LoRa.begin(BAND, PABOOST)) {
if (!LoRa.begin(BAND)) {
drawTextOLED("Starting LoRa failed!", "", "", "");
while (1);
}
Serial.begin(115200); // initialize serial
while (!Serial);
Serial.println("Begin");
// register the receive callback
LoRa.onReceive(onReceive);
......@@ -100,24 +92,9 @@ void setup(void)
}
}
ipAddr = WiFi.localIP().toString();
// Set up mDNS responder:
// - first argument is the domain name, in this example
// the fully-qualified domain name is "esp8266.local"
// - second argument is the IP address to advertise
// we send our IP address on the WiFi network
if (!MDNS.begin("esp32")) {
drawTextOLED("DISPLAY OK!", "Error setting up MDNS responder!", "NO COMMAND", "NO LEVEL");
while (1) {
delay(1000);
}
}
// Start TCP (HTTP) server
server.begin();
// Add service to MDNS-SD
MDNS.addService("http", "tcp", 80);
}
void drawTextOLED(String text, String ttext, String rread, String signalLevel) {
......@@ -147,21 +124,12 @@ unsigned long previousMillis = 0;
unsigned long previousMillisGPIO = 0;
const long interval = 10000;
//reqInt == variabile globale per sapere qual'è l'ultimo pin GPIO settato
int reqInt = 17; // just for first run
void loop(void)
{
unsigned long currentMillis = millis();
drawTextOLED(lastCommand, ipAddr, lastReceived, signalLevel);
if(previousMillisGPIO < currentMillis)
{
//reqInt == variabile globale per sapere qual'è l'ultimo pin GPIO settato
digitalWrite(reqInt, LOW); // set GPIO17 low
}
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
......@@ -184,8 +152,6 @@ void loop(void)
return;
}
req = req.substring(addr_start + 1, addr_end);
client.flush();
currentMillis = millis();
......@@ -207,7 +173,7 @@ void loop(void)
previousMillis = currentMillis;
} else if (req.indexOf('/read') > 0) {
MakeRequest();
pinRequestRead(req);
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
s += req;
......@@ -242,8 +208,9 @@ void loop(void)
}
client.print(s);
client.stop();
// Serial.println("Done with client");
Serial.println(s);
}
//----------------------------------------------------------------------------------------------
......@@ -269,14 +236,13 @@ void onReceive(int packetSize)
str += p;
}
if (str.indexOf("read : ") >= 0) {
if (str.indexOf("r : ") == 0) {
lastReceived = str;
} else {
}
lastCommand = str;
lastCommand = str;
Serial.println(lastCommand + " " + lastReceived);
signalLevel = String(LoRa.packetRssi(), DEC);
}
......@@ -291,10 +257,12 @@ void pinRequest(String req)
int reqInt = req.toInt();
// send packet
LoRa.beginPacket();
LoRa.print("pin : ");
LoRa.print("p : ");
LoRa.print(reqInt);
LoRa.endPacket();
delay(1);
// put the radio into receive mode
LoRa.receive();
}
......@@ -305,7 +273,7 @@ void pinRequestRead(String req)
int reqInt = req.toInt();
// send packet
LoRa.beginPacket();
LoRa.print("read : ");
LoRa.print("r : ");
LoRa.print(reqInt);
LoRa.endPacket();
......@@ -316,77 +284,7 @@ void pinRequestRead(String req)
void pinGPIO(String req)
{
req = req.substring(req.lastIndexOf('/') + 1);
reqInt = req.toInt();
int reqInt = req.toInt();
lastCommand = "gpio : " + String(reqInt);
digitalWrite(reqInt, HIGH); // set GPIO17 high
}
//----------------------------------------------------------------------------------------------
//---------------------------------------- ENCRIPTION ------------------------------------------
//----------------------------------------------------------------------------------------------
const char* sserver = "www.howsmyssl.com"; // Server URL
WiFiClientSecure client;
// www.howsmyssl.com root certificate authority, to verify the server
// change it to your server root CA
// SHA1 fingerprint is broken now!
const char* test_root_ca= \
"-----BEGIN CERTIFICATE-----\n" \
"MIIDSjCCAjKgAwIBAgIQRK+wgNajJ7qJMDmGLvhAazANBgkqhkiG9w0BAQUFADA/\n" \
"MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n" \
"DkRTVCBSb290IENBIFgzMB4XDTAwMDkzMDIxMTIxOVoXDTIxMDkzMDE0MDExNVow\n" \
"PzEkMCIGA1UEChMbRGlnaXRhbCBTaWduYXR1cmUgVHJ1c3QgQ28uMRcwFQYDVQQD\n" \
"Ew5EU1QgUm9vdCBDQSBYMzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB\n" \
"AN+v6ZdQCINXtMxiZfaQguzH0yxrMMpb7NnDfcdAwRgUi+DoM3ZJKuM/IUmTrE4O\n" \
"rz5Iy2Xu/NMhD2XSKtkyj4zl93ewEnu1lcCJo6m67XMuegwGMoOifooUMM0RoOEq\n" \
"OLl5CjH9UL2AZd+3UWODyOKIYepLYYHsUmu5ouJLGiifSKOeDNoJjj4XLh7dIN9b\n" \
"xiqKqy69cK3FCxolkHRyxXtqqzTWMIn/5WgTe1QLyNau7Fqckh49ZLOMxt+/yUFw\n" \
"7BZy1SbsOFU5Q9D8/RhcQPGX69Wam40dutolucbY38EVAjqr2m7xPi71XAicPNaD\n" \
"aeQQmxkqtilX4+U9m5/wAl0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNV\n" \
"HQ8BAf8EBAMCAQYwHQYDVR0OBBYEFMSnsaR7LHH62+FLkHX/xBVghYkQMA0GCSqG\n" \
"SIb3DQEBBQUAA4IBAQCjGiybFwBcqR7uKGY3Or+Dxz9LwwmglSBd49lZRNI+DT69\n" \
"ikugdB/OEIKcdBodfpga3csTS7MgROSR6cz8faXbauX+5v3gTt23ADq1cEmv8uXr\n" \
"AvHRAosZy5Q6XkjEGB5YGV8eAlrwDPGxrancWYaLbumR9YbK+rlmM6pZW87ipxZz\n" \
"R8srzJmwN0jP41ZL9c8PDHIyh8bwRLtTcm1D9SZImlJnt1ir/md2cXjbDaJWFBM5\n" \
"JDGFoqgCWjBH4d1QB7wCCZAA62RjYJsWvIjJEubSfZGL+T0yjWW06XyxV3bqxbYo\n" \
"Ob8VZRzI9neWagqNdwvYkQsEjgfbKbYK7p2CNTUQ\n" \
"-----END CERTIFICATE-----\n";
void MakeRequest()
{
client.setCACert(test_root_ca);
//client.setCertificate(test_client_key); // for client verification
//client.setPrivateKey(test_client_cert); // for client verification
Serial.println("\nStarting connection to server...");
if (!client.connect(sserver, 443))
Serial.println("Connection failed!");
else {
Serial.println("Connected to server!");
// Make a HTTP request:
client.println("GET https://www.howsmyssl.com/a/check HTTP/1.0");
client.println("Host: www.howsmyssl.com");
client.println("Connection: close");
client.println();
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
Serial.println("headers received");
break;
}
}
// if there are incoming bytes available
// from the server, read them and print them:
while (client.available()) {
char c = client.read();
Serial.write(c);
}
client.stop();
}
}
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