As the title says... Chat on...

User avatar
By JeremiahLandi
#35483 Hello,

I am wondering if someone could explain to me the following line:

Code: Select allstring.sub(payload,kdesi[2]+1,#payload)


I understand that kdesi is a variable declared within the function. I just need help with the other items... I don't know the proper terms to google them or I would. A link to explanations would be great as well.

Full function:

Code: Select allfunction ctrlpower(kdesi,payload)
   pwm.close(outpin)
   gpio.mode(outpin,gpio.OUTPUT)
   dotaz=string.sub(payload,kdesi[2]+1,#payload)
   status = dotaz
   if dotaz=="ON"  then gpio.write(outpin,gpio.HIGH) return end
   if dotaz=="OFF" then gpio.write(outpin,gpio.LOW) return end
   if dotaz=="FLC" then pwm.setup(outpin,2,512)pwm.start(outpin) return end
   pwm.setup(outpin,1000,dotaz*10)
   pwm.start(outpin)
end
User avatar
By xtal
#35507 you are extracting a substring from payload
starting at position kdesi[2]+1 to the end of payload string... #payload is length of payload
in this case this would emulate Right$ in basic , you,re cutting off the front of payload.
User avatar
By JeremiahLandi
#35553
xtal wrote:you are extracting a substring from payload

So it is taking the payload and extracting it. The payload is the message that is received by the ESP8266? (This snippet is from a web server).

xtal wrote:starting at position kdesi[2]+1 to the end of payload string... #payload is length of payload

So is the payload put into the kdesi variable at this point?

I can understand that it is searching for the third character in the string.

So does the machine say put payload into kdesi > then read third character > then end since #payload is hit?

xtal wrote:in this case this would emulate Right$ in basic , you,re cutting off the front of payload.


So I understand the Right$ and Left$ reference. Are you saying that because the variable kdesi can only hold three characters or because in this case it is only holding three characters and is returning the rightmost character.

If this makes sense. I was under that kdesi wasn't a limited variable. I just want to confirm this assumption.

Thank you for the help!