Code Monkey home page Code Monkey logo

Comments (9)

sjtupkar avatar sjtupkar commented on May 18, 2024

My sketch running on Arduino hangs when it reaches the line LoRa.endPacket();
The issue is intermittent. The stall occurs after transmitting some packets. Sometimes immediately after couple of packets, sometimes after 10-15 packets.
By putting debug prints, I found that every stall occurs when the LoRa.endPacket is called.

I already shifted the 3.3V supply from Arduino to Raspberry Pi to ensure required current to LoRa module.

My sketch is farily simple.
int LoraData;
// some small code that sets value LoraData based on GPIO values.
LoRa.beginPacket();
LoRa.print(LoraData);
LoRa.endPacket();

from arduino-lora.

sandeepmistry avatar sandeepmistry commented on May 18, 2024

@Franciscopcosta please provide a minimal sketch to reproduce your issue, given the examples works it's most likely something in your sketch.

from arduino-lora.

Franciscopcosta avatar Franciscopcosta commented on May 18, 2024
#include "GSM_MQTT.h"
#include <SoftwareSerial.h>
#include <SPI.h>
#include <LoRa.h>

SoftwareSerial mySerial(2, 3); // RX, TX

//String MQTT_HOST = "m10.cloudmqtt.com";
//String MQTT_PORT = "12385";

String MQTT_HOST = "178.62.119.199";
String MQTT_PORT = "1883";

void setLoraTX() {
  digitalWrite(8, LOW);  //TxEN
  digitalWrite(7, LOW); //RxEN
  delay(100);
  digitalWrite(8, HIGH);  //TxEN
  digitalWrite(7, LOW); //RxEN
}

void setLoraRX() {
  digitalWrite(8, LOW);  //TxEN
  digitalWrite(7, LOW); //RxEN
  delay(100);
  digitalWrite(8, LOW);  //TxEN
  digitalWrite(7, HIGH); //RxEN
}

void GSM_MQTT::AutoConnect(void) {
  //(ClientIdentifier,UserNameFlag,PasswordFlag,UserName,Password,CleanSession,WillFlag,WillQoS,WillRetain,WillTopic,WillTopic)
  connect("GSM800L01", 1, 1, "admin", "admin", 1, 0, 0, 0, "", "");
  //connect("GSM800L02", 1, 1, "ihyoofdv", "__UzGYSOaNN8", 1, 0, 0, 0, "", "");
}

void GSM_MQTT::OnConnect(void) {
  //subscribe(0, _generateMessageID(), "SampleTopic", 1);
  //subscribe(0, _generateMessageID(), "boat/001/message_operator", 1); //(DUP,Message ID,SubTopic,SubQoS)
  subscribe(0, _generateMessageID(), "boat/002/message_operator", 1);
  //publish(0, 0, 0, _generateMessageID(), "SampleTopic", "Hello"); //(DUP,QoS RETAIN,Message ID,Topic,Message)
}

void GSM_MQTT::OnMessage(char *Topic, int TopicLength, char *Message, int MessageLength) {
  digitalWrite(5, HIGH);
  delay(30);
  digitalWrite(5, LOW);
  setLoraTX();
  String payload2Send = "002";
  payload2Send += (String)Message;
  sendMessage(payload2Send);
  setLoraRX();
  //LoRa.receive();

  //  mySerial.println("LoRa sent!");
  //
  //  mySerial.println(TopicLength);
  //  mySerial.println(Topic);
  //  mySerial.println(MessageLength);
  //  mySerial.println(Message);
  bip(1,100);
}

GSM_MQTT MQTT(100); //20 is the keepalive duration in seconds

void setup() {
  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(5, OUTPUT);
  setLoraRX();
  delay(100);
  MQTT.begin();
  LoRa.setPins(10, 9, 30);
  LoRa.setSpreadingFactor(12);
  LoRa.enableCrc();
  //LoRa.setTxPower(30);
  if (!LoRa.begin(868E6)) {
    mySerial.println("Starting LoRa failed!");
    while (1);
  }
  LoRa.receive();
  LoRa.onReceive(onReceive);
}


void sendMessage(String outgoing) {
  bip(2,200);
  delay(550);
  LoRa.beginPacket();                   // start packet
  bip(3,300);
  LoRa.print(outgoing);                 // add payload
  bip(4,400);
  LoRa.endPacket();                     // finish packet and send it
  delay(550);
  //  delay(200);
  //  LoRa.beginPacket();                   // start packet
  //  LoRa.print(outgoing);                 // add payload
  //  LoRa.endPacket();                     // finish packet and send it
  //  delay(200);
  //  LoRa.beginPacket();                   // start packet
  //  LoRa.print(outgoing);                 // add payload
  //  LoRa.endPacket();                     // finish packet and send it

  bip(10,50);
}


//unsigned long previousMilliss = 0;
//const long interval = 2000;

void bip(int bips, int duration) {
  for (int i = 0; i < bips; i++) {
    digitalWrite(5, HIGH);
    delay(duration);
    digitalWrite(5, LOW);
    delay(duration);
  }
}

void loop() {
  //unsigned long currentMillis = millis();
  //onReceive(LoRa.parsePacket());
  if (MQTT.available())
  {
    onReceive(LoRa.parsePacket());
  }
  MQTT.processing();
  delay(1);
  yield();
}

void onReceive(int packetSize) {
  if (packetSize == 0) return;
  if (packetSize) {
    //Serial.print("Received packet '");
    String RSSIvalue = String(LoRa.packetRssi());
    String SNR = String(LoRa.packetSnr());
    String rcvdPayload = "";
    while (LoRa.available()) {
      rcvdPayload += (char)LoRa.read();
    }
    rcvdPayload += "/";
    rcvdPayload += RSSIvalue;
    rcvdPayload += "/";
    rcvdPayload += SNR;

    char payload[50];
    rcvdPayload.toCharArray(payload, (rcvdPayload.length() + 1));

    if (payload[0] == '0' && payload[1] == '0' && payload[2] == '1' && payload[4] != 'M' && payload[4] != 'P') MQTT.publish(0, 0, 0, "lora1", "boat/001/location", payload);
    if (payload[0] == '0' && payload[1] == '0' && payload[2] == '1' && payload[4] == 'P') MQTT.publish(0, 0, 0, "lora1", "boat/001/distress", payload);

    if (payload[0] == '0' && payload[1] == '0' && payload[2] == '2') MQTT.publish(0, 0, 0, "lora1", "boat/002/location", payload);
    if (payload[0] == '0' && payload[1] == '0' && payload[2] == '2' && payload[4] == 'M') MQTT.publish(0, 0, 0, "lora1", "boat/002/message_boat", payload);
    if (payload[0] == '0' && payload[1] == '0' && payload[2] == '2' && payload[4] == 'P' && payload[5] != '0') MQTT.publish(0, 0, 0, "lora1", "boat/002/distress", payload);
    if (payload[0] == '0' && payload[1] == '0' && payload[2] == '2' && payload[4] == 'P' && payload[5] == '0' && payload[6] == '0') MQTT.publish(0, 0, 0, "lora1", "boat/002/distress_end", "off");
  }
}

it blocks when reach the line: sendMessage(payload2Send);

from arduino-lora.

sandeepmistry avatar sandeepmistry commented on May 18, 2024

@Franciscopcosta can you please further simply the sketch you provided to a minimal case to reproduce the issue.

from arduino-lora.

sandeepmistry avatar sandeepmistry commented on May 18, 2024

Closing this for now due to lack of activity. Please provide requested info. if you are still interested.

from arduino-lora.

bkomac avatar bkomac commented on May 18, 2024

For future reference ... Same issue with me. Must be a power problem.
Try powering down tx with LoRa.setTxPower(2) to minimum right after LoRa.begin.

from arduino-lora.

tp035741 avatar tp035741 commented on May 18, 2024

Im facing the same problem after many tries and error i found out that LoRa.endPacket(); stop my loop so only one packet is sent. i tried it with other codes of mine it worked normally and i received all the data in the gateway. can i get help for this one please :)

from arduino-lora.

universam1 avatar universam1 commented on May 18, 2024

If you face a watchdog situation like with an ESP you can check out my PR for non blocking implementation that permits your code to continue and feed the watchdog

from arduino-lora.

xtrinch avatar xtrinch commented on May 18, 2024

Having the same issue, most often it hangs after the first packet being sent, but sometimes it manages to send a couple (up to maybe 20) and then freezes

from arduino-lora.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.