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;
}
......@@ -4,149 +4,99 @@
Http comunications with outside
*/
#include <LoRa.h>
#include <heltec.h>
#include <WiFiClientSecure.h>
#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
const char* ssid = "Thomas";
const char* password = "viapiccardi47!!thomas";
byte localAddress = 0xAA; // address of this device
byte destination = 0xBB; // destination to send to
// TCP server at port 80 will respond to HTTP requests
WiFiServer server(80);
// 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
//#define PABOOST true
SSD1306 display(0x3c, 4, 15);
String ipAddr = "";
String lastCommand = "";
String lastReceived = "";
String lastCommandReceived = "";
String signalLevel = "";
int val = 0;
void setup(void)
void WIFISetUp(void)
{
// Serial.begin(115200);
// while (!Serial) {
// ; // wait for serial port to connect. Needed for native USB port only
// }
//-------- DISPLAY INIT --------------
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);
//drawTextOLED("DISPLAY OK!", "NO ADDR", "NO COMMAND", "NO LEVEL");
//-------------------------------------
pinMode(17, OUTPUT);
digitalWrite(17, LOW); // set GPIO17 low
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);
// Set WiFi to station mode and disconnect from an AP if it was previously connected
WiFi.disconnect(true);
delay(1000);
WiFi.mode(WIFI_STA);
WiFi.setAutoConnect(true);
WiFi.begin("Thomas","viapiccardi47!!thomas");
delay(100);
byte count = 0;
while(WiFi.status() != WL_CONNECTED && count < 10)
{
count ++;
delay(500);
Heltec.display -> drawString(0, 0, "Connecting...");
Heltec.display -> display();
}
LoRa.noCrc();
Heltec.display -> clear();
if(WiFi.status() == WL_CONNECTED)
{
Heltec.display -> drawString(0, 0, "Connecting...OK.");
Heltec.display -> display();
// delay(500);
}
else
{
Heltec.display -> clear();
Heltec.display -> drawString(0, 0, "Connecting...Failed");
Heltec.display -> display();
//w hile(1);
}
Heltec.display -> drawString(0, 10, "WIFI Setup done");
String ipAddr = WiFi.localIP().toString();
Heltec.display -> drawString(0, 20, ipAddr);
Heltec.display -> display();
delay(500);
}
WiFi.mode (WIFI_STA);
WiFi.setSleep(false);
void setup(void)
{
Heltec.begin(true /*DisplayEnable Enable*/, true /*LoRa Enable*/, true /*Serial Enable*/, true /*LoRa use PABOOST*/, 868E6 /*LoRa RF working band*/);
Heltec.display -> clear();
// Connect to WiFi network
WiFi.begin(ssid, password);
Serial.println("LoRa Sender");
String progress = ".";
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
//drawTextOLED("DISPLAY OK!", progress, "NO COMMAND", "NO LEVEL");
progress += ".";
if(progress.length() > 30)
{
ESP.restart();
}
}
ipAddr = WiFi.localIP().toString();
LoRa.setTxPowerMax(10);
//LoRa.enableCrc();
LoRa.setPreambleLength(4);
LoRa.onReceive(onReceive);
LoRa.receive();
//-------------------------------------
WIFISetUp();
// Start TCP (HTTP) server
server.begin();
//SETUP PIN
pinMode(17, INPUT); // set pin to input
digitalWrite(17, HIGH); // turn on pullup resistors
pinMode(21, INPUT); // set pin to input
digitalWrite(21, HIGH); // turn on pullup resistors
pinMode(17, INPUT); // set pin to input
digitalWrite(17, INPUT_PULLUP); // turn on pullup resistors
pinMode(21, INPUT); // set pin to input
digitalWrite(21, INPUT_PULLUP); // turn on pullup resistors
}
//void drawTextOLED(String text, String ttext, String rread, String signalLevel) {
// display.clear();
// display.setTextAlignment(TEXT_ALIGN_LEFT);
//
// display.setFont(ArialMT_Plain_10);
// display.drawString(0, 0, "IP addr : " + ttext);
//
// display.setFont(ArialMT_Plain_10);
// display.drawString(0, 12, "L.C. : " + text);
//
// display.setFont(ArialMT_Plain_10);
// display.drawString(0, 24, "L.R. : " + rread);
//
// display.setFont(ArialMT_Plain_10);
// display.drawString(0, 36, "Level : " + signalLevel);
//
// display.setFont(ArialMT_Plain_10);
// display.drawString(0, 48, "Giacomo Lavermicocca");
//
// display.display();
//}
String s;
unsigned long previousMillis = 0;
const long interval = 10000;
int val = 0;
void loop(void)
{
val = digitalRead(17);
if(val == 1){
pinRequest(String(17));
}
val = digitalRead(21);
if(val == 1){
pinRequest(String(21));
}
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
LoRa.sleep();
}
if (WiFi.status() != WL_CONNECTED) {
ESP.restart();
ESP.restart();
}
long rssi = WiFi.RSSI();
......@@ -155,8 +105,6 @@ void loop(void)
{
ESP.restart();
}
//drawTextOLED(lastCommand, ipAddr, lastReceived, signalLevel);
// Check if a client has connected
WiFiClient client = server.available();
......@@ -193,7 +141,7 @@ void loop(void)
} else if (req.indexOf("/pin") >= 0) {
pinRequest(req);
lastCommand = "";
lastCommandReceived = "";
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
s += req;
s += "";
......@@ -202,7 +150,7 @@ void loop(void)
} else if (req.indexOf("/brightness") >= 0) {
setBrightness(req);
lastCommand = "";
lastCommandReceived = "";
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
s += req;
s += "";
......@@ -227,12 +175,11 @@ void loop(void)
s += "";
} else {
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\nL.C. : ";
s += lastCommand;
s += lastCommandReceived;
s += "";
}
} else {