Commit eefaf6ba authored by Giacomo Lavermicocca's avatar Giacomo Lavermicocca

Some bug fix and test Arduino prj - add mechanical drawings for laser cutter

parent 401460af
#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();
}
...@@ -71,11 +71,10 @@ void setup() { ...@@ -71,11 +71,10 @@ void setup() {
SPI.begin(SCK,MISO,MOSI,SS); SPI.begin(SCK,MISO,MOSI,SS);
LoRa.enableCrc(); LoRa.enableCrc();
LoRa.setTxPower(20,14); LoRa.setTxPower(20,PA_OUTPUT_PA_BOOST_PIN);
LoRa.setSignalBandwidth(7.8E3); LoRa.setPreambleLength(8);
LoRa.setSpreadingFactor(12);
LoRa.setPins(SS,RST,DI0); LoRa.setPins(SS,RST,DI0);
if (!LoRa.begin(BAND)) { if (!LoRa.begin(BAND, PABOOST)) {
drawTextOLED("Starting LoRa failed!"); drawTextOLED("Starting LoRa failed!");
while (1); while (1);
} }
...@@ -121,10 +120,12 @@ void loop() { ...@@ -121,10 +120,12 @@ void loop() {
if(isLoRaOnSenderMode) if(isLoRaOnSenderMode)
{ {
delay(2000); for(int i = 0; i < 10; i++)
LoRa.beginPacket(); {
LoRa.print(lastCommand); LoRa.beginPacket();
LoRa.endPacket(); LoRa.print(lastCommand);
LoRa.endPacket();
}
LoRa.receive(); LoRa.receive();
isLoRaOnSenderMode = false; isLoRaOnSenderMode = false;
...@@ -187,6 +188,7 @@ String read_r2 = String(RELE_READ_2, DEC); ...@@ -187,6 +188,7 @@ String read_r2 = String(RELE_READ_2, DEC);
void onReceive(int packetSize) void onReceive(int packetSize)
{ {
//LoRa.crc();
// received a packet // received a packet
String str = ""; String str = "";
for (int i = 0; i < packetSize; i++) for (int i = 0; i < packetSize; i++)
......
...@@ -29,7 +29,7 @@ WiFiServer server(80); ...@@ -29,7 +29,7 @@ WiFiServer server(80);
#define RST 14 // GPIO14 -- SX127x's RESET #define RST 14 // GPIO14 -- SX127x's RESET
#define DI0 26 // GPIO26 -- SX127x's IRQ(Interrupt Request) #define DI0 26 // GPIO26 -- SX127x's IRQ(Interrupt Request)
#define BAND 915E6 //you can set band here directly,e.g. 868E6,915E6 #define BAND 868E6 //you can set band here directly,e.g. 868E6,915E6
#define PABOOST true #define PABOOST true
SSD1306 display(0x3c, 4, 15); SSD1306 display(0x3c, 4, 15);
...@@ -40,7 +40,12 @@ String lastReceived = ""; ...@@ -40,7 +40,12 @@ String lastReceived = "";
String signalLevel = ""; String signalLevel = "";
void setup(void) void setup(void)
{ {
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
//-------- DISPLAY INIT -------------- //-------- DISPLAY INIT --------------
pinMode(16, OUTPUT); pinMode(16, OUTPUT);
...@@ -62,18 +67,17 @@ void setup(void) ...@@ -62,18 +67,17 @@ void setup(void)
SPI.begin(SCK, MISO, MOSI, SS); SPI.begin(SCK, MISO, MOSI, SS);
LoRa.enableCrc(); LoRa.enableCrc();
LoRa.setTxPower(20,14); LoRa.setTxPower(20,PA_OUTPUT_PA_BOOST_PIN);
LoRa.setSignalBandwidth(7.8E3); LoRa.setPreambleLength(8);
LoRa.setSpreadingFactor(12);
LoRa.setPins(SS, RST, DI0); LoRa.setPins(SS, RST, DI0);
if (!LoRa.begin(BAND)) { if (!LoRa.begin(BAND, PABOOST)) {
drawTextOLED("Starting LoRa failed!", "", "", ""); drawTextOLED("Starting LoRa failed!", "", "", "");
while (1); while (1);
} }
// register the receive callback WiFi.mode (WIFI_STA);
LoRa.onReceive(onReceive); WiFi.setSleep(false);
// Connect to WiFi network // Connect to WiFi network
WiFi.begin(ssid, password); WiFi.begin(ssid, password);
...@@ -125,6 +129,13 @@ void loop(void) ...@@ -125,6 +129,13 @@ void loop(void)
unsigned long currentMillis = millis(); unsigned long currentMillis = millis();
drawTextOLED(lastCommand, ipAddr, lastReceived, signalLevel); drawTextOLED(lastCommand, ipAddr, lastReceived, signalLevel);
long rssi = WiFi.RSSI();
if(rssi == 0)
{
ESP.restart();
}
// Check if a client has connected // Check if a client has connected
WiFiClient client = server.available(); WiFiClient client = server.available();
...@@ -136,7 +147,7 @@ void loop(void) ...@@ -136,7 +147,7 @@ void loop(void)
while (client.connected() && !client.available()) { while (client.connected() && !client.available()) {
delay(1); delay(1);
} }
// Read the first line of HTTP request // Read the first line of HTTP request
String req = client.readStringUntil('\r'); String req = client.readStringUntil('\r');
...@@ -221,6 +232,7 @@ void loop(void) ...@@ -221,6 +232,7 @@ void loop(void)
void onReceive(int packetSize) void onReceive(int packetSize)
{ {
//LoRa.crc();
// received a packet // received a packet
// read packet // read packet
String str; String str;
...@@ -231,19 +243,20 @@ void onReceive(int packetSize) ...@@ -231,19 +243,20 @@ void onReceive(int packetSize)
} }
if (str.indexOf("r : ") == 0) { if (str.indexOf("r : ") == 0) {
lastCommand = str;
lastReceived = str; lastReceived = str;
} }
if (str.indexOf("p : ") == 0) { if (str.indexOf("p : ") == 0) {
lastCommand = str; lastCommand = str;
lastReceived = "";
} }
if (str.indexOf("b : ") == 0) { if (str.indexOf("b : ") == 0) {
lastCommand = str; lastCommand = str;
lastReceived = "";
} }
lastCommand = str;
signalLevel = String(LoRa.packetRssi(), DEC); signalLevel = String(LoRa.packetRssi(), DEC);
} }
...@@ -255,15 +268,18 @@ void pinRequest(String req) ...@@ -255,15 +268,18 @@ void pinRequest(String req)
{ {
req = req.substring(req.lastIndexOf('/') + 1); req = req.substring(req.lastIndexOf('/') + 1);
int reqInt = req.toInt(); int reqInt = req.toInt();
// send packet
LoRa.beginPacket();
LoRa.print("p : ");
LoRa.print(reqInt);
LoRa.endPacket();
delay(1); for(int i=0;i < 10;i++)
{
// send packet
LoRa.beginPacket();
LoRa.print("p : ");
LoRa.print(reqInt);
LoRa.endPacket();
}
// put the radio into receive mode // put the radio into receive mode
LoRa.onReceive(onReceive);
LoRa.receive(); LoRa.receive();
} }
...@@ -271,15 +287,17 @@ void setBrightness(String req) ...@@ -271,15 +287,17 @@ void setBrightness(String req)
{ {
req = req.substring(req.lastIndexOf('/') + 1); req = req.substring(req.lastIndexOf('/') + 1);
int reqInt = req.toInt(); int reqInt = req.toInt();
// send packet for(int i=0;i < 10;i++)
LoRa.beginPacket(); {
LoRa.print("b : "); // send packet
LoRa.print(reqInt); LoRa.beginPacket();
LoRa.endPacket(); LoRa.print("b : ");
LoRa.print(reqInt);
delay(1); LoRa.endPacket();
}
// put the radio into receive mode // put the radio into receive mode
LoRa.onReceive(onReceive);
LoRa.receive(); LoRa.receive();
} }
...@@ -287,12 +305,16 @@ void pinRequestRead(String req) ...@@ -287,12 +305,16 @@ void pinRequestRead(String req)
{ {
req = req.substring(req.lastIndexOf('/') + 1); req = req.substring(req.lastIndexOf('/') + 1);
int reqInt = req.toInt(); int reqInt = req.toInt();
// send packet for(int i=0;i < 10;i++)
LoRa.beginPacket(); {
LoRa.print("r : "); // send packet
LoRa.print(reqInt); LoRa.beginPacket();
LoRa.endPacket(); LoRa.print("r : ");
LoRa.print(reqInt);
LoRa.endPacket();
}
// put the radio into receive mode // put the radio into receive mode
LoRa.onReceive(onReceive);
LoRa.receive(); LoRa.receive();
} }
қ҉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