MrPsi/NodeMCU-Wixel

LUA to Arduino ESP8266 conversion

Closed this issue · 15 comments

Hi MrPsi,
Me again - NodeMCU Wixels are going great, no issues. This post is not an issue as such, but have no other way to contact you.

I am wanting to play around with one of my nodes to do some other things, but I just cannot understand the LUA code, how things are structured, what files do what etc. Its such a confusing language.
Have you ever considered porting it over to the Arduino core for the ESP8266?
So much easier to understand...
I am going to pursue this and see if I can figure it out, but at this stage its a total blur as to what the LUA code even does. It really is such a weird language to me.
The Arduino core for ESP8266 is pretty good now.
Would love to hear your thoughts on this.

Thanks

MrPsi commented

Hi,

I'm glad that the NodeMCU-Wixel is working good for you. :)

I have had thoughts of porting the code to C. Manly because it would be fun. But lack of time has prevented me from doing so. The main drawback with Lua I think, is memory usage. It loads all the code into memory. Hence the division of the code into modules and all the "require()" and "package.loaded[modName] = nil" calls to load and allow for garbage collection.

Lua is quite similar to Java Script, I think you will master it in no time :)

What are you planning on doing?

Hi,
I am attempting to port it with some help from a colleague, we shall see how far we get.
I want to connect a display to the NodeMCU, just for local node information and for the hell of it really. But when its ported to C, the options for things that could be done are so much more than they are when written in LUA, for me anyway. Such a confusing language! :)
Ill keep you posted

MrPsi commented

Hi,
Are you having any luck porting it?

Hi MrPsi,
It certainly is a challenge. For every line of LUA, there are like 10 lines of C required.
Is there a way I can chat to you outside of this Issue area? Are you on Gitter?
Would love to send the code we have so far.
Thanks

Hopefully have this finished tomorrow, if I get some more time. Well over half way, and the part that is done is working correctly.

Hi @MrPsi
Struggling with one last bit. Can you help?

local serverFormat = {
        TransmissionId = tonumber(values[5]),
        TransmitterId = values[0],
        RawValue = tonumber(values[1]),
        FilteredValue = tonumber(values[2]),
        BatteryLife = tonumber(values[3]),
        ReceivedSignalStrength = tonumber(values[4]),
        CaptureDateTime = 0,
        Uploaded = 0,
        UploadAttempts = 0,
        UploaderBatteryLife = 0,
        RelativeTime = "r" }
    
    local isEncodeSuccess, json = pcall(cjson.encode, serverFormat)
    if isEncodeSuccess == false then
        -- Unable to encode to json.
        return nil
    end
    
    -- Can't multiply to get milliseconds, will overflow. Use string manipulation.
    local sec = tmr.time()
    local relativeSec = sec - item.sec
    local relativeZeros = "000"
    if relativeSec == 0 then
        relativeZeros = ""
    end
    json = string.gsub(json, "\"r\"", relativeSec .. relativeZeros)
    
return json

The Lua script encodes the data into a JSON and that is the format which is sent from the Nodemcu. If we knew what a typical packet looked like we could ignore that process and create our own.
Once we know what the packet is like we can finish it.

Are you able to help?
Thanks

I wasn't able to configure my nodeMCE jet, but I bet you will get something like the following as per the wixel prints the output printf("%lu %lu %lu %hhu %d %hhu %d \r\n", pPkt->src_addr,dex_num_decoder(pPkt->raw),dex_num_decoder(pPkt->filtered)*2, pPkt->battery, getPacketRSSI(pPkt),pPkt->txId,adcConvertToMillivolts(adcRead(0)));

See below my last 3 readings. hope this helps

9999999 92624 90720 216 -90 174 3003
9999999 96032 95504 216 -85 182 0
9999999 104000 96912 216 -80 186 0
9999999 -> TransmiterId
92624   -> raw
90720   -> filtered
216     -> battery
-90     -> ReceivedSignalStrength
174     -> TransimissionId
3003    -> adcConvertToMillivolts

Hi @rmsryu, yep I have sniffed the comms between Wixel and NodeMCU. Think we have this bit sorted. Its now the encoding of this information into the JSON file.
Here are my last 3

6691766 85440 92800 216 -97 228 3008
6691766 91008 87008 216 -95 240 0
6691766 73136 84688 216 -66 244 0

Unsure what the relevance is of the ADC read though... Given the NodeMCU/Wixel isnt battery powered, I assume this is just a spurious reading from a floating pin on the Wixel...

Regarding the JSON part which is what gets sent out of the WiFi, likely look at using the library here:
https://github.com/bblanchon/ArduinoJson

Still a little more to figure out.

MrPsi commented

Hi, sorry for late answer. It seems like I no longer get notifications of new messages.

You can reach me on my e-mail if you want to contact me directly. petergson(at)yahoo.com

Regarding the json format, it looks like this:
{"BatteryLife":211,"TransmitterId":"5298263","RelativeTime":1000,"TransmissionId":1,"ReceivedSignalStrength":-95,"UploaderBatteryLife":0,"Uploaded":0,"UploadAttempts":0,"CaptureDateTime":1458907827000,"FilteredValue":121632,"RawValue":108240}

Hi again,
Thanks. Things are progressing again, we are almost there.
Have the NodeMCU talking to the Wixel, and doing packet stuff. Just need to finalise the LIST handling, and sort Relative Time and Capture Time, and then I think its done.

Here is a debug output:

Starting Up
Connected
192.168.178.28
Serial rec
6691766 116736 116416 216 -81 25 3003
Connected to client
{"numberOfRecords":200,"version":1,"includeInfo":true}{"BatteryLife":216,"TransmitterId":"6691766","RelativeTime":83000,"TransmissionId":25,"ReceivedSignalStrength":-81,"UploaderBatteryLife":0,"Uploaded":0,"UploadAttempts":0,"CaptureDateTime":0,"FilteredValue":116736,"RawValue":116416}

One query I did have, was the last value in the received string. 3003 in the case above. Am I right in thinking this is from the ADC of the Wixel, which would normally be used for battery voltage/level checking in a non-NodeMCU setup? So that number is not used for anything in this configuration, correct?

Thanks

MrPsi commented

Hi,

Looks good. Seems like you are almost done.
You are correct regarding the last value. It is not relevant in this setup. It is a legacy from the other implementation of the Wixel code.
How ever, if you want to build a battery powered NodeMCU-Wixel you could use the UploaderBatteryLife to report battery level I guess.

@WanaGo are you planning on publishing your code somewhere?

@savek-cc Yes, when its finished :)
Just ironing out the last problems.

Totally forgot about it, but here is my Arduino C version if anyone wants it. Still full credit to MsPSI.
This might be easier to work with if people are wanting to do mods, I dont know.
@MrPsi - Please have a look and see what you think :)
Have got 2 nodes in the house using your code, and 1 using mine, and they have all been running OK for months.
https://github.com/WanaGo/NodeMCU-Wixel-Arduino

MrPsi commented

Good to see that you got it working. I think it looks good. The more alternatives the better :)