Commit 7173ed07 authored by Giacomo Lavermicocca's avatar Giacomo Lavermicocca

bug fix - change std library

parent 8889639b
#include <SPI.h>
#include <LoRa.h>
// Pin definetion of WIFI LoRa 32
// HelTec AutoMation 2017 support@heltec.cn
#define SCK 5 // GPIO5 -- SX127x's SCK
#define MISO 19 // GPIO19 -- SX127x's MISO
#define MOSI 27 // GPIO27 -- SX127x's MOSI
#define SS 18 // GPIO18 -- SX127x's CS
#define RST 14 // GPIO14 -- SX127x's RESET
#define DI00 26 // GPIO26 -- SX127x's IRQ(Interrupt Request)
#define BAND 915E6 //you can set band here directly,e.g. 868E6,915E6
#define PABOOST true
byte localAddress = 0xBB; // address of this device
byte destination = 0xFF; // destination to send to
String outgoing; // outgoing message
byte msgCount = 0; // count of outgoing messages
long lastSendTime = 0; // last send time
int interval = 2000; // interval between sends
void setup()
{
Serial.begin(115200); // initialize serial
while (!Serial);
Serial.println("LoRa Duplex with callback");
SPI.begin(SCK,MISO,MOSI,SS);
LoRa.setPins(SS,RST,DI00);// set CS, reset, IRQ pins
LoRa.enableCrc();
LoRa.setSyncWord(0xF3);
if (!LoRa.begin(BAND))
{
Serial.println("LoRa init failed. Check your connections.");
while (true); // if failed, do nothing
}
LoRa.onReceive(onReceive);
LoRa.receive();
Serial.println("LoRa init succeeded.");
}
void loop()
{
if (millis() - lastSendTime > interval)
{
String message = "Hello World!"; // send a message
sendMessage(message);
Serial.println("Sending " + message);
lastSendTime = millis(); // timestamp the message
interval = random(2000) + 1000; // 2-3 seconds
LoRa.receive(); // go back into receive mode
}
}
void sendMessage(String outgoing)
{
LoRa.setSyncWord(0xF3);
LoRa.beginPacket(); // start packet
LoRa.write(destination); // add destination address
LoRa.write(localAddress); // add sender address
LoRa.write(msgCount); // add message ID
LoRa.write(outgoing.length()); // add payload length
LoRa.print(outgoing); // add payload
LoRa.endPacket(); // finish packet and send it
msgCount++; // increment message ID
}
void onReceive(int packetSize)
{
if (packetSize == 0) return; // if there's no packet, return
// read packet header bytes:
int recipient = LoRa.read(); // recipient address
byte sender = LoRa.read(); // sender address
byte incomingMsgId = LoRa.read(); // incoming msg ID
byte incomingLength = LoRa.read(); // incoming msg length
String incoming = ""; // payload of packet
while (LoRa.available()) // can't use readString() in callback
{
incoming += (char)LoRa.read(); // add bytes one by one
}
if (incomingLength != incoming.length()) // check length for error
{
Serial.println("error: message length does not match length");
return; // skip rest of function
}
// if the recipient isn't this device or broadcast,
if (recipient != localAddress && recipient != 0xFF)
{
Serial.println("This message is not for me.");
return; // skip rest of function
}
// if message is for this device, or broadcast, print details:
Serial.println("Received from: 0x" + String(sender, HEX));
Serial.println("Sent to: 0x" + String(recipient, HEX));
Serial.println("Message ID: " + String(incomingMsgId));
Serial.println("Message length: " + String(incomingLength));
Serial.println("Message: " + incoming);
Serial.println("RSSI: " + String(LoRa.packetRssi()));
Serial.println("Snr: " + String(LoRa.packetSnr()));
Serial.println();
}
/*
Receiver for exchange commands between LoRa Sender (long range)
Led PWM switch
*/
#include <SPI.h>
#include <LoRa.h>
#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
#include <heltec.h>
// Pin definetion of WIFI LoRa 32
// HelTec AutoMation 2017 support@heltec.cn
#define SCK 5 // GPIO5 -- SX127x's SCK
#define MISO 19 // GPIO19 -- SX127x's MISO
#define MOSI 27 // GPIO27 -- SX127x's MOSI
#define SS 18 // GPIO18 -- SX127x's CS
#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
byte localAddress = 0xBB; // address of this device
byte destination = 0xAA; // destination to send to
#define RELE_1 17
#define RELE_2 21
#define LED_PIN 13
#define RELE_3 13
#define RELE_READ_1 36
#define RELE_READ_2 37
SSD1306 display(0x3c, 4, 15);
String lastCommand = "";
String signalLevel = "";
// --------------- LED PWM CONFIGURATION ----------------
// use first channel of 16 channels (started from zero)
#define LEDC_CHANNEL_0 0
// use 5000 Hz as a LEDC base frequency
#define LEDC_BASE_FREQ 10000
// use 13 bit precission for LEDC timer
#define LEDC_TIMER_13_BIT 13
#define LED_PIN 13
int brightness = 0;
unsigned long previousMillisLed = 0;
const long intervalLed = 60000 * 10;
// Arduino like analogWrite
// value has to be between 0 and valueMax
void ledcAnalogWrite(uint8_t channel, uint32_t value, uint32_t valueMax = 255) {
// calculate duty, 8191 from 2 ^ 13 - 1
uint32_t duty = (8191 / valueMax) * min(value, valueMax);
// write duty to LEDC
ledcWrite(channel, duty);
}
void setup() {
Heltec.begin(true /*DisplayEnable Enable*/, true /*LoRa Enable*/, true /*Serial Enable*/, true /*LoRa use PABOOST*/, 868E6 /*LoRa RF working band*/);
Heltec.display -> clear();
Serial.println("LoRa Sender");
// Setup timer and attach timer to a led pin
ledcSetup(LEDC_CHANNEL_0, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
ledcAttachPin(LED_PIN, LEDC_CHANNEL_0);
LoRa.setTxPowerMax(10);
//LoRa.enableCrc();
LoRa.setPreambleLength(4);
//-------------------------------- 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);
//------------------------------------------------------------------------
SPI.begin(SCK,MISO,MOSI,SS);
LoRa.enableCrc();
LoRa.setTxPower(20,PA_OUTPUT_PA_BOOST_PIN);
LoRa.setPreambleLength(8);
LoRa.setPins(SS,RST,DI0);
if (!LoRa.begin(BAND)) {
drawTextOLED("Starting LoRa failed!");
while (1);
}
// register the receive callback
LoRa.onReceive(onReceive);
// put the radio into receive mode
LoRa.receive();
// LoRa.setTxPower(20,PA_OUTPUT_PA_BOOST_PIN);
// LoRa.enableCrc();
// LoRa.setPreambleLength(1);
//---------------------------------------------
pinMode(RELE_1, OUTPUT);
pinMode(RELE_2, OUTPUT);
pinMode(RELE_3, OUTPUT);
pinMode(RELE_READ_1, INPUT_PULLUP);
pinMode(RELE_READ_2, INPUT_PULLUP);
// register the receive callback
LoRa.onReceive(onReceive);
// put the radio into receive mode
LoRa.receive();
}
unsigned long previousMillis = 0;
const long interval = 2500;
unsigned long previousMillisLed = 0;
const long intervalLed = 60000 * 10;
bool isLoRaOnSenderMode = false;
bool isLedBegin = true;
bool isLedOn = false;
// fading final effects
int fadeAmount = 7;
unsigned long previousMillisLedOff = 0;
const long intervalLedOff = 18;
//delay before switch off
bool isLedOff = false;
unsigned long previousMillisIsOnLedOff = 0;
const long intervalIsOnLedOff = 60000 * 1;
bool isLedOff = true;
void loop() {
// do nothing
unsigned long currentMillis = millis();
if (currentMillis - previousMillis > interval) {
if (isLedOff) {
isLedOff = false;
//Reset rele
digitalWrite(RELE_1, LOW);
digitalWrite(RELE_2, LOW);
}
if(currentMillis - previousMillisLed > intervalLed){
digitalWrite(RELE_3, LOW);
//ESP.restart();
}
if(isLoRaOnSenderMode)
{
for(int i = 0; i < 10; i++)
{
isLoRaOnSenderMode = false;
String outgoing = lastCommand;
for(int i = 0; i < 10; i++){
// send packet
LoRa.beginPacket();
LoRa.print(lastCommand);
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();
isLoRaOnSenderMode = false;
}
// ------------ LED -----------------
if(isLedBegin){
if (currentMillis - previousMillisLed > intervalLed) {
brightness = 0;
isLedBegin = false;
isLedOff = true;
previousMillisLedOff = millis();
fadeAmount = 5;
}
}
if (currentMillis - previousMillisIsOnLedOff > intervalIsOnLedOff) {
isLedOff = false;
isLedBegin = true;
}
if(isLedOff){
if (currentMillis - previousMillisLedOff > intervalLedOff) {
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
previousMillisLedOff = millis();
}
}
ledcAnalogWrite(LEDC_CHANNEL_0, brightness);
drawTextOLED(lastCommand);
}
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);
display.drawString(0, 20, "Level : " + signalLevel);
display.setFont(ArialMT_Plain_10);
display.drawString(0, 40, "Giacomo Lavermicocca");
display.display();
}
String r1 = String(RELE_1, DEC);
String r2 = String(RELE_2, DEC);
String r3 = String(RELE_3, DEC);
String read_r1 = String(RELE_READ_1, DEC);
String read_r2 = String(RELE_READ_2, DEC);
String val = "";
void onReceive(int packetSize)
{
//LoRa.crc();
// received a packet
String str = "";
for (int i = 0; i < packetSize; i++)
if (packetSize == 0) return; // if there's no packet, return
// read packet header bytes:
int recipient = LoRa.read(); // recipient address
byte sender = LoRa.read(); // sender address
byte incomingLength = LoRa.read(); // incoming msg length
String incoming = ""; // payload of packet
while (LoRa.available()) // can't use readString() in callback
{
incoming += (char)LoRa.read(); // add bytes one by one
}
if (incomingLength != incoming.length()) // check length for error
{
Serial.println("error: message length does not match length");
return; // skip rest of function
}
// if the recipient isn't this device or broadcast,
if (recipient != localAddress)
{
char p = (char)LoRa.read();
str += p;
Serial.println("This message is not for me.");
return; // skip rest of function
}
lastCommand = str;
// if message is for this device, or broadcast, print details:
Serial.println("Received from: 0x" + String(sender, HEX));
Serial.println("Sent to: 0x" + String(recipient, HEX));
Serial.println("Message length: " + String(incomingLength));
Serial.println("Message: " + incoming);
Serial.println("RSSI: " + String(LoRa.packetRssi()));
Serial.println("Snr: " + String(LoRa.packetSnr()));
Serial.println();
// received a packet
String str = incoming;
if(str.indexOf("p : ") == 0)
{
......@@ -208,14 +148,19 @@ void onReceive(int packetSize)
{
setRele2();
}
if(str.indexOf(r3) > 0)
{
setRele3();
}
lastCommand = str;
isLoRaOnSenderMode = true;
}
String val = "";
if(str.indexOf("r : ") == 0)
{
{
val = "";
if(str.indexOf(read_r1) > 0)
{
val = readRele1();
......@@ -229,48 +174,42 @@ void onReceive(int packetSize)
lastCommand = str + " " + val;
}
if(str.indexOf("b : ") == 0)
{
val = str.substring(4);
brightness = val.toInt();
previousMillisLed = millis();
isLoRaOnSenderMode = true;
}
signalLevel = String(LoRa.packetRssi(), DEC);
}
void setRele1()
{
isLedOff = true;
digitalWrite(RELE_1, HIGH);
previousMillis = millis();
digitalWrite(RELE_3, HIGH); // LUCI LED
delay(2000);
previousMillisLed = millis();
}
void setRele2()
{
isLedOff = true;
digitalWrite(RELE_2, HIGH);
previousMillis = millis();
// switch on light
brightness = 255;
digitalWrite(RELE_3, HIGH); // LUCI LED
delay(2000);
previousMillisLed = millis();
}
void setRele3()
{
digitalWrite(RELE_3, HIGH);
previousMillisLed = millis();
previousMillisIsOnLedOff = millis() + intervalLed;
isLedOn = false;
}
String readRele1()
{
previousMillis = millis();
String v = String(digitalRead(RELE_READ_1), DEC);
return v;
}
String readRele2()
{
previousMillis = millis();
String v = String(digitalRead(RELE_READ_2), DEC);
return v;
}
This diff is collapsed.
қ҉pp p韛]p p B 9Iлϻˉϻďīpp-]p+pp ċ B П )ЋϻЫϻ ęǙ "u"u!"uS"uMs"u "ua ?"uu " c" "A"m"w 3"w-"w_"we""_"S"s3" " ?" ۋ " c c" " ?" "3s"ÉS"_""we"_w"w-"3w "m"A" " c" uu" ?ua" u"suM"Su"u!"u"weu"wu!"w-u"w uM"mu"Aua"uu""uu"uaA"um"uMw "uw-"u!w"uwe -׉ E""_"S"s3" " ?" ۋ " c c" " ?" "3s"ÉS"_""we"_w"w-"3w "m"A" " c" uu" ?ua" u"suM"Su"u!"u"weu"wu!"w-u"w uM"mu"Aua"uu""uu"uaA"um"uMw "uw-"u!w"uwe"u"u!"uS"uMs"u "ua ?"uu " c" "A"m"w 3"w-"w_"we낉 oU oU[ "s"3s"s" s" 's" s" qs"sY"s"s" " !" 3M" _i" y"u"u"'u"""Y"m"cw"ywM"}w墉 7}"}"y3"c"m " '" " q"'""" y " _ " 3 3"  _" y""ɍ"'" q" ߍ" '" m"c"3y"} oU珉)"u"u3"u"u "uW "u " "- }"ɋ"׋)";"w"wM"w""3"" ;" )" ɋ" S }" } " ") " "͉"ۉ3"׉"w"wM"Kw"")"" }" " u" uW" uE"Wu"u"u"wu"wu"w)u"uE"uW"u"""u"uW"u"u5w"uwM"uw傉1""3"" ;" )" ɋ" S }" } " ") " "͉"ۉ3"׉"w"wM"Kw"")"" }" " u" uW" uE"Wu"u"u"wu"wu"w)u"uE"uW"u"""u"uW"u"u5w"uwM"uw"u"u3"u"u "uW "u " "- }"ɋ"׋)";"w"wM"wׂU牉1""3"" ;" )" ɋ" S }" } " ") " "͉"ۉ3"׉"w"wM"Kw"")"" }" " u" uW" uE"Wu"u"u"wu"wu"w)u"uE"uW"u"""u"uW"u"u5w"uwM"uw"u"u3"u"u "uW "u " "- }"ɋ"׋)";"w"wM"wׂ˗1""3"" ;" )" ɋ" S }" } " ") " "͉"ۉ3"׉"w"wM"Kw"")"" }" " u" uW" uE"Wu"u"u"wu"wu"w)u"uE"uW"u"""u"uW"u"u5w"uwM"uw"u"u3"u"u "uW "u " "- }"ɋ"׋)";"w"wM"wׂ]'ω]"sw"swM"sw"s"sY"s'"s"su"sku"su""i"M"i!"w"us"us"uksY"s"#s"s"s"ws"wMs"wesiǙ"s"3s"s" s" s" ]s" qs"sY"s"s" " !" M" i" "u"u"u""Y"Y""w"ywM"}w墉"}"y3"c" "U '" " q"'""" y " _ " 3 3"  _" y""7"'" q" ]" " m"c"3y"}﯉"w}"wMy"wc"m"ٍ"#""uk'"uI"u" y"i _"M 3"! " "s"s"sY"s q"s "s+ '"s "s"s3"s ˉm"weu"wu!"w-u"w uM"mu"Aua"u""uu%"uaA"um"uMw "uw-"u!w"uwe"u"u!"uS"uMs"u "ua ?"uu [" c""A"m"w 3"w-"w_"we""_"S"s" " ?" ۋ" c c" " " "3"É"_""we"_w"w"3w""" " c" u" ?ua" u"su"Su"u!"uoˉm"weu"wu!"w-u"w uM"mu"Aua"u""uu%"uaA"um"uMw "uw-"u!w"uwe"u"u!"uS"uMs"u "ua ?"uu [" c""A"m"w 3"w-"w_"we""_"S"s" " ?" ۋ" c c" " " "3"É"_""we"_w"w"3w""" " c" u" ?ua" u"su"Su"u!"u;Ѣ "s"3s"s" s" 's+" s" qs"sY"s"s" " !" 3M" _i" y"u"uI"'uk""#""m"cw"ywM"}w墉 7"}"y3"c"m " " ]" q"'"7"" y " _ " 3 3"  _" y""ɍ"'" q" ߍ" 'U" "c"3y"};Ѣ;dp `
\ No newline at end of file
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