Commit f3015e63 authored by Giacomo Lavermicocca's avatar Giacomo Lavermicocca

Some others fixs

parent 8d65ebf1
......@@ -14,7 +14,6 @@
#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
......@@ -28,18 +27,16 @@
#define BAND 868E6 //you can set band here directly,e.g. 868E6,915E6
#define PABOOST true
#define RELE_1 12
#define RELE_2 13 //2
#define RELE_1 17
#define RELE_2 21
#define RELE_READ_1 36
#define RELE_READ_2 37
SSD1306 display(0x3c, 4, 15);
String str;
int addr = 0;
#define EEPROM_SIZE 1
int eValue = 0;
int eeValue = 0;
void setup() {
//-------------------------------- DISPLAY ------------------------------
pinMode(16,OUTPUT);
......@@ -55,14 +52,6 @@ void setup() {
Serial.begin(115200);
while (!Serial);
if (!EEPROM.begin(EEPROM_SIZE))
{
Serial.println("failed to initialise EEPROM"); delay(1000000);
}
eeValue = (EEPROM.read(addr));
Serial.println(String(eeValue));
Serial.println("LoRa Receiver Callback");
SPI.begin(SCK,MISO,MOSI,SS);
LoRa.setPins(SS,RST,DI0);
......@@ -71,8 +60,14 @@ void setup() {
while (1);
}
pinMode(RELE_1,OUTPUT);
pinMode(RELE_2,OUTPUT);
pinMode(RELE_1, OUTPUT);
pinMode(RELE_2, OUTPUT);
//
// digitalWrite(RELE_1, HIGH);
// digitalWrite(RELE_2, HIGH);
//
// pinMode(RELE_READ_1, INPUT_PULLUP);
// pinMode(RELE_READ_2, INPUT_PULLUP);
// register the receive callback
LoRa.onReceive(onReceive);
......@@ -81,7 +76,7 @@ void setup() {
LoRa.receive();
}
unsigned long previousMillis = millis();
unsigned long previousMillis = 0;
const long interval = 2500;
void loop() {
......@@ -89,18 +84,16 @@ void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
//Reset rele
digitalWrite(RELE_1, LOW);
digitalWrite(RELE_2, LOW);
} else {
Serial.println(eeValue);
eeValue = genNextPrime(eeValue);
// EEPROM.write(addr, eeValue);
// EEPROM.commit();
digitalWrite(RELE_1, HIGH);
digitalWrite(RELE_2, HIGH);
//Serial.println("IN OFF...");
}
else
{
//Serial.println("IN ON...");
//digitalWrite(RELE_1, LOW);
//digitalWrite(RELE_2, LOW);
}
drawTextOLED(str);
display.display();
}
......@@ -120,12 +113,15 @@ void drawTextOLED(String text) {
display.drawString(0, 40, "Giacomo Lavermicocca");
}
String r1 = String(RELE_1, DEC);
String r2 = String(RELE_2, DEC);
void onReceive(int packetSize)
{
// received a packet
Serial.print("Received packet '");
// read packet
str = "Last command : ";
str = "L.C. : ";
for (int i = 0; i < packetSize; i++)
{
char p = (char)LoRa.read();
......@@ -133,38 +129,55 @@ void onReceive(int packetSize)
str += p;
}
//delay(1000);
Serial.println(str);
if(str.indexOf("pin : ") >= 0)
{
int extraParam = 111;
delay(1000);
{
if(str.indexOf(r1) >= 0) //TODO manca la parte di sicurezza
{
setRele1();
}
if(str.indexOf(r2) >= 0)
{
setRele2();
}
LoRa.beginPacket();
LoRa.print("received : ");
LoRa.print(str);
LoRa.endPacket();
Serial.println(str);
// put the radio into receive mode
LoRa.receive();
}
if(str.indexOf("read : ") >= 0)
{
//Serial.println(str);
String r1 = String(RELE_1, DEC);
String r2 = String(RELE_2, DEC);
String val;
if(extraParam == 111)
if(str.indexOf(r1) >= 0) //TODO manca la parte di sicurezza
{
val = readRele1();
}
else if(str.indexOf(r2) >= 0)
{
if(str.indexOf(r1) >= 0) //TODO manca la parte di sicurezza
{
setRele1();
}
if(str.indexOf(r2) >= 0)
{
setRele2();
}
val = readRele2();
}
LoRa.beginPacket();
LoRa.print("received : ");
LoRa.print(str);
LoRa.print(" ");
LoRa.print(extraParam);
LoRa.print(" read ");
LoRa.print(val);
LoRa.endPacket();
// put the radio into receive mode
LoRa.receive();
}
// print RSSI of packet
Serial.print("' with RSSI ");
Serial.print(str);
......@@ -174,40 +187,28 @@ void onReceive(int packetSize)
void setRele1()
{
digitalWrite(RELE_1, HIGH);
Serial.println("RELE_1");
digitalWrite(RELE_1, LOW);
previousMillis = millis();
}
void setRele2()
{
digitalWrite(RELE_2, HIGH);
Serial.println("RELE_2");
digitalWrite(RELE_2, LOW);
previousMillis = millis();
}
//-------------------------- CODIFICA --------------------------
long i = 2; // Start at 2
long genNextPrime(const long currentPrime) {
long i = currentPrime + 1;
while (!isPrime(i)) {
i++;
}
return i;
String readRele1()
{
previousMillis = millis();
String v = String(digitalRead(RELE_READ_1), DEC);
return v;
}
int isPrime(long num) {
// Only have to check for divisible for the sqrt(number)
int upper = sqrt(num);
// Check if the number is evenly divisible (start at 2 going up)
for (long cnum = 2; cnum <= upper; cnum++) {
long mod = num % cnum; // Remainder
if (mod == 0) {
return 0;
} // If the remainer is 0 it's evenly divisible
}
return 1; // If you get this far it's prime
String readRele2()
{
previousMillis = millis();
String v = String(digitalRead(RELE_READ_2), DEC);
return v;
}
This diff is collapsed.
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