Migliorare il codice

Buonasera a tutti,

condivido il codice che uso come bridge per una rete mesh, come si potrebbe migliorare?

grazie a tutti per l'eventuale aiuto

//************************************************************
// this is a simple example that uses the painlessMesh library to
// connect to a another network and relay messages from a MQTT broker to the nodes of the mesh network.
// To send a message to a mesh node, you can publish it to "painlessMesh/to/12345678" where 12345678 equals the nodeId.
// To broadcast a message to all nodes in the mesh you can publish it to "painlessMesh/to/broadcast".
// When you publish "getNodes" to "painlessMesh/to/gateway" you receive the mesh topology as JSON
// Every message from the mesh which is send to the gateway node will be published to "painlessMesh/from/12345678" where 12345678 
// is the nodeId from which the packet was send.
//************************************************************

unsigned long thisMillis = 0;
unsigned long lastMillis = 0;
unsigned long interval;//5 secondi
unsigned long now;

#include <Arduino.h>
//#include <painlessMesh.h>
#include "namedMesh.h"
#include <PubSubClient.h>
#include <WiFiClient.h>

#define   MESH_PREFIX     "whateverYouLike"
#define   MESH_PASSWORD   "somethingSneaky"
#define   MESH_PORT       5555

#define   STATION_SSID     "linksys"
#define   STATION_PASSWORD ""


#define HOSTNAME "MQTT_Bridge"

#define ROLE    "mqttbridge"
#define VERSION "MQTTBRIDGE v1.0.14"
#define MESSAGE "mqttbridge "

// Prototypes
//void receivedCallback( const uint32_t &from, const String &msg );
void receivedCallback( const String &from, const String &msg );
void mqttCallback(char* topic, byte* payload, unsigned int length);

IPAddress getlocalIP();
IPAddress myIP(0,0,0,0);
IPAddress myAPIP(0,0,0,0);
//IPAddress mqttBroker(10, 42, 0, 1);
IPAddress mqttBroker(192, 168, 1, 60);

//painlessMesh  mesh;
namedMesh  mesh;

String msgg;
String nodeName = "bridgemqtt"; // Name needs to be unique
String myNetworkDetails;

WiFiClient wifiClient;
PubSubClient mqttClient(mqttBroker, 1883, mqttCallback, wifiClient);

void setup() {
  Serial.begin(115200);
  Serial.setDebugOutput(true); 
  //mesh.setDebugMsgTypes( ERROR | STARTUP | CONNECTION | DEBUG | MESH_STATUS | SYNC | COMMUNICATION | GENERAL | REMOTE );  // set before init() so that you can see startup messages
  mesh.setDebugMsgTypes( ERROR | STARTUP ); 

  // Channel set to 6. Make sure to use the same channel for your mesh and for you other
  // network (STATION_SSID)
  mesh.init( MESH_PREFIX, MESH_PASSWORD, MESH_PORT, WIFI_AP_STA, 11 );
  mesh.initOTAReceive(ROLE);
  mesh.setRoot(true);
  mesh.setContainsRoot(true);
  mesh.setName(nodeName);
  mesh.onReceive(&receivedCallback);
  mesh.stationManual(STATION_SSID, STATION_PASSWORD);
  mesh.setHostname(HOSTNAME);
  
}

void update_status()
{
  long uptime = millis() / 60000L;
  char buf[16];
  dtostrf(uptime,2,0, buf);
  mqttClient.publish("home/bridge/uptime",buf);
  String noded = String(mesh.getNodeId());
  mqttClient.publish("home/bridge/nodeid",noded.c_str());
  long fr=ESP.getFreeHeap();
  dtostrf(fr,2,0, buf);
  mqttClient.publish("home/bridge/freememory",buf);
  myNetworkDetails = String(mesh.getNodeId()) + " ready! IP " + myIP.toString() + " wifi channel " + String(WiFi.channel()) + " Version " + String(VERSION);
  mqttClient.publish("home/bridge/mynetwork",myNetworkDetails.c_str());
  mqttClient.publish( "home/bridge/getrt", mesh.subConnectionJson(false).c_str());
}

void send_id()
{
  msgg = "sendnodeid";
  mesh.sendBroadcast(msgg);
}

void reconnect()
{
  while (!mqttClient.connected()) 
    {
    Serial.println("Attempting MQTT connection...");    
    // Attemp to connect
    if (mqttClient.connect("BridgeMqttMesh")) 
      {
      Serial.println("Connected");  
      mqttClient.publish("home/bridge/start","Ready!");
      mqttClient.subscribe("home/comand/#");
      } 
    else
      {
      Serial.print("Failed, rc=");
      Serial.print(mqttClient.state());
      Serial.println(" try again in 2 seconds");
      // Wait 2 seconds before retrying
      delay(2000);
      mesh.update();
      mqttClient.loop();
      }
    }
}

void loop() {
  mesh.update();
  mqttClient.loop();

   if(myIP != getlocalIP()){
    myIP = getlocalIP();
    Serial.println("My IP is " + myIP.toString());
}

    if (!mqttClient.connected())  {
      {reconnect();}
    }
  
  if((millis() - interval) > 100000) { //600000 - 10 min
        update_status();
        send_id();
        interval=millis();
    }
}


//void receivedCallback( String from, String &msg ) {
  void receivedCallback( const String &from, const String &msg ) {
  Serial.printf("bridge: Received from %s msg=%s\n", from.c_str(), msg.c_str());  
  
  char *primo;
  char *secondo;
  char msg_array[100];
  msg.toCharArray(msg_array, 100);
  primo=strtok(msg_array, "/");
  secondo=strtok(NULL, "/");
    
  String topic = "home/" + String(from.c_str() + String("/") + String(primo));
  Serial.printf("bridge: Send server mqtt %s sec %s\n", topic.c_str(), secondo);

  mqttClient.publish(topic.c_str(), secondo);
  
}


void mqttCallback(char* topic, uint8_t* payload, unsigned int length) {
  
  char* cleanPayload = (char*)malloc(length+1);
  memcpy(cleanPayload, payload, length);
  cleanPayload[length] = '\0';
  String msg = String(cleanPayload);
  free(cleanPayload);

  String targetStr = String(topic).substring(12);//5

  mesh.sendSingle(targetStr, msg);
  Serial.printf("bridge: Send target %s msg %s\n", targetStr, msg);

  if(msg == "nodelist")
    {
      auto nodes = mesh.getNodeList(true);
      String str;
      for (auto &&id : nodes)
        str += String(id) + String(" ");
      mqttClient.publish("home/bridge/nodelist", str.c_str());
    }
    else if(msg == "getrt")
      {
      mqttClient.publish( "home/bridge/getrt", mesh.subConnectionJson(false).c_str());
      }
  //  if(msg == "nodetree")
  //    {
   //   mqttClient.publish( "painlessMesh/from/nodetree", mesh.asNodeTree().c_str() );
   //   }
  
}

IPAddress getlocalIP() {
  return IPAddress(mesh.getStationIP());
}

installando questo codice su un esp32 si riavvia a spot e mi da questi errori, ma non capisco il problema, qualcuno sa di cosa si tratta?

grazie

Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
PC : 0x400d4f30 PS : 0x00060330 A0 : 0x800d4eb4 A1 : 0x3ffb1d30
A2 : 0x3ffdb548 A3 : 0xabba123c A4 : 0x3ffc1f20 A5 : 0x00000000
A6 : 0x00000003 A7 : 0x00060f23 A8 : 0x3f400fc0 A9 : 0x3ffb1d10
A10 : 0x3ffdb540 A11 : 0xabba1248 A12 : 0x3ffb643c A13 : 0x00000000
A14 : 0x3ffd34a8 A15 : 0x3ffb1e64 SAR : 0x0000000a EXCCAUSE: 0x0000001c
EXCVADDR: 0xabba1240 LBEG : 0x4000c46c LEND : 0x4000c477 LCOUNT : 0x00000000

ELF file SHA256: 0000000000000000

Backtrace: 0x400d4f30:0x3ffb1d30 0x400d4eb1:0x3ffb1d50 0x400d4ef9:0x3ffb1d70 0x400d4f3d:0x3ffb1d90 0x400d4eb1:0x3ffb1db0 0x400d4ef9:0x3ffb1dd0 0x400d4f3d:0x3ffb1df0 0x400d5139:0x3ffb1e10 0x400d80ef:0x3ffb1e50 0x400d81ef:0x3ffb1ed0 0x400dcb1e:0x3ffb1f00 0x400dcc6f:0x3ffb1f20 0x400d2b86:0x3ffb1f40 0x400da616:0x3ffb1f60 0x400e2915:0x3ffb1fb0 0x40089a1e:0x3ffb1fd0

Rebooting...
ets Jun 8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1044
load:0x40078000,len:10124
load:0x40080400,len:5856
entry 0x400806a8

setLogLevel: ERROR | STARTUP | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE | DEBUG |
STARTUP: init(): 0

E' brutto rimanere senza neanche una risposta, ma io posso solo risponderti che non lo so... :frowning:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.