Skip to content

Instantly share code, notes, and snippets.

@dbrosy
Last active November 8, 2019 08:55
Show Gist options
  • Save dbrosy/dbf5b35438233b86f891f6413e2cd0bb to your computer and use it in GitHub Desktop.
Save dbrosy/dbf5b35438233b86f891f6413e2cd0bb to your computer and use it in GitHub Desktop.
TTN OTAA Activation

Add this to node in ttn

from network import LoRa
import ubinascii
lora = LoRa()
print("DevEUI: %s" % (ubinascii.hexlify(lora.mac()).decode('ascii').upper()))
Decoder for 2 floats (8 bytes)
function bytesToFloat32(bytes) {
    var sign = (bytes & 0x80000000) ? -1 : 1;
    var exponent = ((bytes >> 23) & 0xFF) - 127;
    var significand = (bytes & ~(-1 << 23));

    if (exponent == 128) 
        return sign * ((significand) ? Number.NaN : Number.POSITIVE_INFINITY);

    if (exponent == -127) {
        if (significand == 0) return sign * 0.0;
        exponent = -126;
        significand /= (1 << 22);
    } else significand = (significand | (1 << 23)) / (1 << 23);

    return sign * significand * Math.pow(2, exponent);
}

function Decoder(bytes, port) {
	var t = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
	var h = bytes[7] << 24 | bytes[6] << 16 | bytes[5] << 8 | bytes[4];
	return{
		temperature:  bytesToFloat32(t).toFixed(5),
		humidity: bytesToFloat32(h).toFixed(4)
	};
}
Decoder for 2 floats (8 bytes) gps
function bytesToFloat32(bytes) {
    var sign = (bytes & 0x80000000) ? -1 : 1;
    var exponent = ((bytes >> 23) & 0xFF) - 127;
    var significand = (bytes & ~(-1 << 23));

    if (exponent == 128) 
        return sign * ((significand) ? Number.NaN : Number.POSITIVE_INFINITY);

    if (exponent == -127) {
        if (significand == 0) return sign * 0.0;
        exponent = -126;
        significand /= (1 << 22);
    } else significand = (significand | (1 << 23)) / (1 << 23);

    return sign * significand * Math.pow(2, exponent);
}

function Decoder(bytes, port) {
	var lat = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
	var long = bytes[7] << 24 | bytes[6] << 16 | bytes[5] << 8 | bytes[4];
	return{
		latitude:  bytesToFloat32(lat).toFixed(7),
		longitude: bytesToFloat32(long).toFixed(7)
	};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment