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.
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
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.
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.
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.