-->
Page 1 of 1

eLua NodeMCU Int to Byte/Binary

PostPosted: Sun Apr 07, 2019 1:33 pm
by Weberet
Hi everyone, new user / maker here working on my first electronics / NodeMCU IoT project which is a discord status notifier.

I'm pretty familiar with with basic programming and have written a discord bot which runs on a raspberry pi written in python. This bot sends a single integer to adafruit io from 0-255 wehenver the discord server status changes; I'm looking to convert this integer to binary from the NodeMCU itself as it will contain a list of colored status lights to flash through.

I would prefer this to be in a function / method (not sure of the terminology used for lua) that takes the int as an argument and simply returns the binary.

I've tried some of the following:
Code: Select allfunction toBits(num,bits)
    -- returns a table of bits, most significant first.
    bits = bits or math.max(1, select(2, math.frexp(num)))
    local t = {} -- will contain the bits       
    for b = bits, 1, -1 do
        t[b] = math.fmod(num, 2)
        num = math.floor((num - t[b]) / 2)
    end
    return t
end


and:

Code: Select allfunction bits(num)
    local t={}
    while num>0 do
        rest=num%2
        table.insert(t,1,rest)
        num=(num-rest)/2
    end return table.concat(t)
end


But have not been able to get the syntax right using arduino IDE and this strange modified version of Lua or I believe % (modulo) also doesn't work in this modififed version and I'm unsure how to implement it through a library or elsewhere.

Any suggestions / advice is appreciated.

Thank you.