Commit c5f0ff69 authored by Giacomo Lavermicocca's avatar Giacomo Lavermicocca
Browse files

begin encription logic

parent 30d1ef65
Loading
Loading
Loading
Loading
+114 −28
Changes for LoRaReceiverInterrupt/LoRaReceiverInterrupt.ino: 114 added lines, 28 removed lines.
Original line number Diff line number Diff line
@@ -13,6 +13,8 @@
*/
#include <SPI.h>
#include <LoRa.h>
#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
#include "EEPROM.h"

// Pin definetion of WIFI LoRa 32
// HelTec AutoMation 2017 support@heltec.cn 
@@ -26,15 +28,33 @@
#define BAND    868E6  //you can set band here directly,e.g. 868E6,915E6
#define PABOOST true

#define RELE_1 16
#define RELE_2 17 //2
#define RELE_1 12
#define RELE_2 13 //2

int counter = 0;
SSD1306  display(0x3c, 4, 15);

void setup() {
String str;

int addr = 0;
#define EEPROM_SIZE 1
int eeValue = 0;

  pinMode(25,OUTPUT);
  digitalWrite(25, HIGH);   // turn the LED on (HIGH is the voltage level)
void setup() {
  //-------------------------------- 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);
  //------------------------------------------------------------------------

  if (!EEPROM.begin(EEPROM_SIZE))
  {
    Serial.println("failed to initialise EEPROM"); delay(1000000);
  }
  
  Serial.begin(115200);
  while (!Serial);
@@ -57,9 +77,37 @@ void setup() {
  LoRa.receive();
}

unsigned long previousMillis =  millis();
const long interval = 2500;

void loop() {  
  // do nothing
                     // wait for a second
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    //Reset rele
    digitalWrite(RELE_1, LOW);
    digitalWrite(RELE_2, LOW);
    eeValue = (EEPROM.read(addr));
    Serial.println(String(eeValue));
  }
  
  drawTextOLED(str);
  display.display();
}

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);
  String rssi = "RSSI " + String(LoRa.packetRssi(), DEC);
  display.drawString(0, 20, rssi);

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

void onReceive(int packetSize)
@@ -67,7 +115,7 @@ void onReceive(int packetSize)
  // received a packet
  Serial.print("Received packet '");
  // read packet
  String str;
  str = "Last command : ";
  for (int i = 0; i < packetSize; i++)
  {
    char p = (char)LoRa.read();
@@ -75,41 +123,79 @@ void onReceive(int packetSize)
    str += p;
  }

  if(str == "hello 0")
  if(str.indexOf("pin : ") >= 0)
  {
    digitalWrite(RELE_1, HIGH);   // turn the LED on (HIGH is the voltage level)
    int extraParam = 111;
    delay(1000);
    
    Serial.println(str);

    digitalWrite(RELE_2, HIGH);   // turn the LED on (HIGH is the voltage level)
    //delay(2000);
    //digitalWrite(RELE_1, LOW);   // turn the LED on (HIGH is the voltage level)
    String r1 = String(RELE_1, DEC);
    String r2 = String(RELE_2, DEC);

    //digitalWrite(RELE_2, LOW);   // turn the LED on (HIGH is the voltage level)
    digitalWrite(25, HIGH);   // turn the LED on (HIGH is the voltage level)
    eeValue = 1234;

    EEPROM.write(addr, eeValue);
    EEPROM.commit();
    
    if(extraParam == 111)
    {
      if(str.indexOf(r1) >= 0) //TODO manca la parte di sicurezza
      {
        setRele1();
      }
      if(str.indexOf(r2) >= 0)
      {
        setRele2();
      }
    }
    
    delay(2000);
    LoRa.beginPacket();
    LoRa.print("hello ");
    LoRa.print(counter%=2);
    LoRa.print("received : ");
    LoRa.print(str);
    LoRa.print(" ");
    LoRa.print(extraParam);
    LoRa.endPacket();
    
    // put the radio into receive mode
    LoRa.receive();    
  }
  // print RSSI of packet
  Serial.print("' with RSSI ");
  Serial.print(str);
  Serial.print(" ");
  Serial.println(LoRa.packetRssi());
}

    counter++;
void setRele1()
{
  digitalWrite(RELE_1, HIGH);
  previousMillis = millis();
}

void setRele2()
{
  digitalWrite(RELE_2, HIGH);
  previousMillis = millis();
}

//-------------------------- CODIFICA --------------------------

  } else if(str == "hello 1") {
long i = 2; // Start at 2

    digitalWrite(RELE_1, LOW);   // turn the LED on (HIGH is the voltage level)
int is_prime(long num) {
  // Only have to check for divisible for the sqrt(number)
  int upper = sqrt(num);

    digitalWrite(RELE_2, LOW);   // turn the LED on (HIGH is the voltage level)
  // Check if the number is evenly divisible (start at 2 going up)
  for (long cnum = 2; cnum <= upper; cnum++) {
    long mod = num % cnum; // Remainder

    digitalWrite(25, LOW);    // turn the LED off by making the voltage LOW
    if (mod == 0) { 
      return 0;
    } // If the remainer is 0 it's evenly divisible
  }

    // print RSSI of packet
  Serial.print("' with RSSI ");
  Serial.print(str);
  Serial.print(" ");
  Serial.println(LoRa.packetRssi());
  return 1; // If you get this far it's prime
}
+64 −34
Changes for LoRaSender/LoRaSender.ino: 64 added lines, 34 removed lines.
Original line number Diff line number Diff line
@@ -15,14 +15,14 @@

 */


#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiClient.h>

#include <SPI.h>
#include <LoRa.h>

#include <ESPmDNS.h>

const char* ssid = "Thomas";
const char* password = "viapiccardi47!!thomas";

@@ -41,8 +41,6 @@ WiFiServer server(80);
#define BAND    868E6  //you can set band here directly,e.g. 868E6,915E6
#define PABOOST true

int counter = 0;

void setup(void)
{
    pinMode(25,OUTPUT);
@@ -97,6 +95,10 @@ void setup(void)
    MDNS.addService("http", "tcp", 80);
}

String s;
unsigned long previousMillis = 0;
const long interval = 10000;

void loop(void)
{  
    // Check if a client has connected
@@ -129,7 +131,10 @@ void loop(void)
    Serial.println(req);
    client.flush();

    String s;
    Serial.println("Sending 200");
    
    unsigned long currentMillis = millis();
    
    if (req == "/")
    {
      IPAddress ip = WiFi.localIP();
@@ -137,40 +142,42 @@ void loop(void)
      s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>Hello from ESP32 at ";
      s += ipStr;
      s += "</html>\r\n\r\n";      
        Serial.println("Sending 200");

        Serial.print("Sending packet: ");
    } else if(req.indexOf('/pin') > 0) {
      pinRequest(req);     
      s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>Hello from ESP32 at pin : ";
      s += req.substring(req.lastIndexOf('/')+1);
      s += "</html>\r\n\r\n";
      
        Serial.println(counter);
        // send packet
        LoRa.beginPacket();
        LoRa.print("hello ");
        LoRa.print(counter%=2);
        LoRa.endPacket();
      previousMillis = currentMillis;
      
        // put the radio into receive mode
        LoRa.receive();
    } else if(req.indexOf('/check') > 0) {
      //OK send s to browser

        counter++;
        digitalWrite(25, HIGH);   // turn the LED on (HIGH is the voltage level)
        delay(1000);                       // wait for a second
        digitalWrite(25, LOW);    // turn the LED off by making the voltage LOW
        delay(1000);                       // wait for a second
      if (currentMillis - previousMillis >= interval) {
        s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>Last command timeout";
        s += "</html>\r\n\r\n";
      }
    else
    {
    } else {
        s = "HTTP/1.1 404 Not Found\r\n\r\n";
        Serial.println("Sending 404");
    }

    client.print(s);

    Serial.println("Done with client");
}

//----------------------------------------------------------------------------------------------
//--------------------------------------- LORA RECEIVE -----------------------------------------
//----------------------------------------------------------------------------------------------




// code = 0 --> null
// code = 11 --> pinInput 1 --> read sensor
// code = 12 --> pinInput 2
// code = 13 --> pinInput 3
// code = 21 --> pinOutput 1 --> write to
// code = 22 --> pinOutput 2
// code = 23 --> pinOutput 3

void onReceive(int packetSize)
{
@@ -185,19 +192,42 @@ void onReceive(int packetSize)
    str += p;
  }

  if(str == "hello 0")
  if(str.indexOf("response : ") >= 0)
  {
    digitalWrite(25, HIGH);   // turn the LED on (HIGH is the voltage level)
    
  } else if(str == "hello 1") {
    
    digitalWrite(25, LOW);    // turn the LED off by making the voltage LOW
    //prepare : it will ask next time brower interactions
    s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>Last read of receive : ";
    s += str;
    s += "</html>\r\n\r\n";
  }
    
  // print RSSI of packet
  Serial.print("' with RSSI ");
  Serial.print(str);
  Serial.print(" ");
  Serial.println(LoRa.packetRssi());
}

//----------------------------------------------------------------------------------------------
//------------------------------------------ REQUEST -------------------------------------------
//----------------------------------------------------------------------------------------------

void pinRequest(String req)
{
    Serial.println("pinRequest...");
    req = req.substring(req.lastIndexOf('/')+1);
    int reqInt = req.toInt();
    Serial.println(reqInt);
    // send packet
    LoRa.beginPacket();
    LoRa.print("pin : ");
    LoRa.print(reqInt);
    LoRa.endPacket();
    
    // put the radio into receive mode
    LoRa.receive();
}

//----------------------------------------------------------------------------------------------
//---------------------------------------- ENCRIPTION ------------------------------------------
//----------------------------------------------------------------------------------------------