"DHCP" (I_ID_REQUEST) Handler



  • This code is to be used in conjunction of a set up MQTT broker (Mosquitto) and the prior flow listed in "Flow to turn SerialGateway into MQTT-SerialGateway"

    This watches all data coming in from topic MySensors/255/255/3/0/3/ and does a lookup in the DB for a new unused ID. Then it assigns the sensors the ID as well as updating the DB.

    There may be some bugs, as testing this code requires extra work.

    Essentially, in a location in EEprom lives the assigned ID number. So in order to test this, you need to:

    load ClearEEPROM sketch. open Serial console and wait till done.
    Then load a sketch that doesn't have the Node ID set (#define NODE_ID 1.6 and gw.begin(NULL, AUTO) in 1.5)
    Assigning happens once. If messed up, load clearEEPROM and do again

    BIG NOTE: You do need read/write access to file /root/leases.file from whatever user you are running in Node-Red. I chose /root/leases.file because I wanted it easy to find and of indication that it is an important file. You can change it, but make sure to catch all the file nodes that reference to it.

    [{"id":"8e3e8f02.7fd0c","type":"mqtt-broker","z":"63842e72.893f78","broker":"localhost","port":"8883","clientid":"","usetls":false,"verifyservercert":true,"compatmode":true,"keepalive":"15","cleansession":true,"willTopic":"","willQos":"0","willRetain":null,"willPayload":"","birthTopic":"","birthQos":"0","birthRetain":null,"birthPayload":""},{"id":"778435ae.0d9c4c","type":"mqtt in","z":"63842e72.893f78","name":"Incoming DHCP request","topic":"MySensors/255/255/3/+/3","broker":"8e3e8f02.7fd0c","x":112.5,"y":136,"wires":[["c77f47f6.5dcc1"]]},{"id":"c77f47f6.5dcc1","type":"file in","z":"63842e72.893f78","name":"","filename":"/root/leases.file","format":"utf8","x":307,"y":137,"wires":[["a09e2794.97bca8"]]},{"id":"a09e2794.97bca8","type":"csv","z":"63842e72.893f78","name":"","sep":",","hdrin":"","hdrout":"","multi":"mult","ret":"\\n","temp":"","x":450,"y":136,"wires":[["f33caa99.b57308"]]},{"id":"f33caa99.b57308","type":"function","z":"63842e72.893f78","name":"Saves and emits assigned ID","func":"var lease = -1 ;\nvar splitString;\n\nfor (var i=0; (i<msg.payload.length) && (lease == -1);i++){\n    if(msg.payload[i].col1 != i){\n        msg.payload.splice(i,0, {col1: i, col2:\"temporary\"});\n        lease = i ;\n    }\n}\n\nif (lease == -1){ // means there's no holes in the lease file\n    lease = msg.payload.length ;\n    msg.payload.push( {col1: lease, col2:\"temporary\"} ); // returns next number in order\n}\n\nvar msg2 = { payload:lease};\n\nreturn [msg, msg2];","outputs":"2","noerr":0,"x":640,"y":136,"wires":[["15c23ce9.f74c03","ccd034fb.54b728"],["50521f88.55082","ccd034fb.54b728"]]},{"id":"15c23ce9.f74c03","type":"csv","z":"63842e72.893f78","name":"","sep":",","hdrin":"","hdrout":"","multi":"one","ret":"\\n","temp":"col1,col2","x":663,"y":96,"wires":[["2092d4a1.661c84"]]},{"id":"2092d4a1.661c84","type":"file","z":"63842e72.893f78","name":"","filename":"/root/leases.file","appendNewline":false,"createDir":false,"overwriteFile":"true","x":670,"y":55,"wires":[]},{"id":"50521f88.55082","type":"mqtt out","z":"63842e72.893f78","name":"","topic":"MySensors/255/255/3/0/4","qos":"","retain":"","broker":"8e3e8f02.7fd0c","x":636,"y":199,"wires":[]},{"id":"ccd034fb.54b728","type":"debug","z":"63842e72.893f78","name":"DHCP result","active":false,"console":"false","complete":"payload","x":865,"y":135,"wires":[]},{"id":"420b8cdd.3a696c","type":"comment","z":"63842e72.893f78","name":"I_ID_REQUEST handler (255;255;3;0;3 , 255;255;3;0;4;[ID])","info":"Beginning listens to messages of 255;255;3;0;3;\\n , which is broadcast request for ID\nLeases file is read, and interpreted as CSV. \nFunction block does 2 things:\n    1. Adds new sensor's ID to lease file\n    2. Sends back to MQTT the ID the device is granted to 255;255;3;0;4;[ID]\\n","x":272,"y":83,"wires":[]}]
    
    


  • This looks awsome and exactly what i was after. I use ESP8266's with NRF24L01+'s as relay nodes connecting back to a cloud mqtt server but have had to statically assign id's.

    Thanks!!!


Log in to reply
 

Suggested Topics

  • 9
  • 1
  • 9
  • 2
  • 2
  • 36

74
Online

11.4k
Users

11.1k
Topics

112.7k
Posts