Hi, I was wondering if anyone else has had the same problem, I can use LoRa.onReceive on any of my boards without issue until I try to also use Wifi. As soon as I do that the boards will work just fine until the onReceive function is called at which point the boards crash with an "Interrupt timeout on CPU1". I have tried various delays, yields to correct the problem but nothing seems to work ?
Interestingly if I also try to use the OLED from within the onReceive function I also experience the same problem.
Any help or advice would be much appreciated. cheers
include
include // include libraries
include
include
define SS 18
define RST 14
define DI0 26
define BAND 433E6
define PABOOST true
const char* ssid = "";
const char* password = "**";
String outgoing; // outgoing messages
String incoming; //incoming messages
byte msgCount = 0; // count of outgoing messages
byte localAddress = 0x01; // address of this device
byte destination = 0x02; // destination to send to
long lastSendTime = 0; // last send time
int interval = 2000; // interval between sends
const char mqtt_server = "mymqtt.server.com";
const char topic1 = "lora/slave1/led";
const char* mqttusername = "";
const char* mqttpassword = "**";
const char* mqttclientname = "LoraMaster";
int mqttport = 1883;
int ledpin1 = 17;
String strTopic;
String strPayload;
String switch1;
int button1 = 21;
Hi, I was wondering if anyone else has had the same problem, I can use LoRa.onReceive on any of my boards without issue until I try to also use Wifi. As soon as I do that the boards will work just fine until the onReceive function is called at which point the boards crash with an "Interrupt timeout on CPU1". I have tried various delays, yields to correct the problem but nothing seems to work ?
Interestingly if I also try to use the OLED from within the onReceive function I also experience the same problem.
Any help or advice would be much appreciated. cheers
include
include // include libraries
include
include
define SS 18
define RST 14
define DI0 26
define BAND 433E6
define PABOOST true
const char* ssid = ""; const char* password = "**"; String outgoing; // outgoing messages String incoming; //incoming messages byte msgCount = 0; // count of outgoing messages byte localAddress = 0x01; // address of this device byte destination = 0x02; // destination to send to long lastSendTime = 0; // last send time int interval = 2000; // interval between sends const char mqtt_server = "mymqtt.server.com"; const char topic1 = "lora/slave1/led"; const char* mqttusername = ""; const char* mqttpassword = "**"; const char* mqttclientname = "LoraMaster"; int mqttport = 1883; int ledpin1 = 17; String strTopic; String strPayload; String switch1; int button1 = 21;
WiFiClient espClient; PubSubClient client(espClient);
void reconnect() { while (!client.connected()) { if (client.connect(mqttclientname,mqttusername,mqttpassword)) { client.subscribe(topic1); client.publish("slave1/switch", "idle", true); } else { delay(5000); } } } void callback(char topic, byte payload, unsigned int length) { payload[length] = '\0'; strTopic = String((char)topic); if(strTopic == topic1) { switch1 = String((char)payload); if(switch1 == "on") { digitalWrite(ledpin1, HIGH); outgoing = "slave1led1on"; sendMessage(outgoing); LoRa.receive(); } else { digitalWrite(ledpin1, LOW); outgoing = "slave1led1off"; sendMessage(outgoing); LoRa.receive();
} } yield(); }
void setup() { pinMode(ledpin1, OUTPUT); digitalWrite(ledpin1, LOW); pinMode(button1, INPUT_PULLUP); //pinMode(26, INPUT); delay(10); Serial.begin(9600); // initialize serial WiFi.enableSTA(true); delay(2000); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); client.setServer(mqtt_server, mqttport); client.setCallback(callback); Serial.println("LoRa Duplex with callback"); // override the default CS, reset, and IRQ pins (optional) SPI.begin(5,19,27,18); LoRa.setPins(SS,RST,DI0); if (!LoRa.begin(BAND,PABOOST)) {
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 (!client.connected()) { reconnect(); } client.loop(); yield(); }
void sendMessage(String outgoing) { 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, so 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 (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())); delay(50); yield(); }