Send sensor data to LoRa mesh network

I’ve purchased a few LoRa ESP32 from Heltec, and I’m trying to retrieve data from sensors, and send that to a specific node that could be reached by hoping nodes from the mesh network.

The issue is that I cannot find the function where I can send/recieve a package, string, or something from the device to the mesh.

Hope anybody can help me, cheers (✿^‿^)

1 Like

For text:
interface.sendText(variable name, destinationId, wantAck, hopLimit)
For data:
interface.sendData()

you may also need to override:
onRecieve and onConnection methods

for more try to look at the following link of Python API:

https://meshtastic.github.io/Meshtastic-python/meshtastic/index.html

Here is a example which sends system time on a regular interval of 30 seconds:

import meshtastic
from pubsub import pub
import threading
import time
from time import sleep
import datetime

def onReceive(packet, interface): # called when a packet arrives
  print(f"Received: {packet}")

def onConnection(interface, topic=pub.AUTO_TOPIC): # called when we (re)connect to the radio
  print ("starting...")
  rt = RepeatedTimer(30, sendText) # no need of rt.start()

def sendText(): # called every x seconds
  currTime = datetime.datetime.now().strftime("%H:%M:%S")
  interface.sendText(currTime)
  print("Message sent: " + currTime)

pub.subscribe(onReceive, "meshtastic.receive")
pub.subscribe(onConnection, "meshtastic.connection.established")
# By default will try to find a meshtastic device, otherwise provide a device path like /dev/ttyUSB0
interface = meshtastic.SerialInterface()

class RepeatedTimer(object): # Timer helper class
  def __init__(self, interval, function, *args, **kwargs):
    self._timer = None
    self.interval = interval
    self.function = function
    self.args = args
    self.kwargs = kwargs
    self.is_running = False
    self.next_call = time.time()
    self.start()

  def _run(self):
    self.is_running = False
    self.start()
    self.function(*self.args, **self.kwargs)

  def start(self):
    if not self.is_running:
      self.next_call += self.interval
      self._timer = threading.Timer(self.next_call - time.time(), self._run)
      self._timer.start()
      self.is_running = True

  def stop(self):
    self._timer.cancel()
    self.is_running = False
2 Likes

Thank you very much for your response.

I’m going to try what you’ve shared with me.

But first, I want to know if I’m getting myself well explained. Is this code able to be run on the ESP? or I need a computer to send the packets (text, data) i want?

Thanks in advance, i’m a begginer with this. Really appreciate your help.

Yes you need to run it on using your computer and connect the device using USB. Before that you also need to burn the device with the firmware. you shall get all the information regarding this on https://meshtastic.org/ as well as by searching in this community.

Welcome to the project @dbtegui

The example given to you would be a ‘computer’ talking to the Meshtastic node using the python interface.

This is the fast and easy way to make things like you want happen, especially for testing.

It is possible to create a plugin that will do what you want that runs directly on the Meshtastic node, but this requires much more programing experience and a more in-depth understanding of the project.

This is a great place to start: Temperature Sensing - #4 by crossan007

2 Likes

Hi, I’ve been trying to do something very similar for a long time, I’m trying to create a Messageplugin plugin.

I would have to modify protobuf and import it by hand with a new data structure (depending on the need) and what does the platfom do?

Can someone tell me if I’m on the right track

Yay, this is what I was asking for. Now I have to find how to make it work for my needs.

Thank you very much @Spor7biker

I don’t think you are on the wrong track.

But… Ideally you would learn about the project, and do some research on these forums on your own to see what others are doing and follow some of the existing topics. And then ask specific questions.

1 Like