class SigFoxSender: def __init__(self): # init Sigfox for RCZ1 (Europe) self.sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ1) # create a Sigfox socket self.socket = socket.socket(socket.AF_SIGFOX, socket.SOCK_RAW) # make the socket blocking self.socket.setblocking(True) # configure it as uplink only self.socket.setsockopt(socket.SOL_SIGFOX, socket.SO_RX, False) print('SigFox socket created') print('MAC: {} - ID: {} - RSSI: {} - PAC: {}'.format( hexlify(self.sigfox.mac()).decode(), hexlify(self.sigfox.id()).decode(), self.sigfox.rssi(), hexlify(self.sigfox.pac()).decode())) def getSignalStrength(self): return self.sigfox.rssi() def transformValue(self, value): # divide by 10 (convention as high values are expected at some point): transformed = value / 10 # cast value to int: transformed = int(transformed) # avoid negative values: transformed = 0 if (transformed < 0) else transformed # as we are packing the value in a single byte, make it 255 as max value: transformed = 255 if (transformed > 255) else transformed # return transformed value: return transformed def packMesageForSigFox(self, ozone, temperature, humidity): # casting floats to ints in values array (only ozone values): values = [self.transformValue(x) for x in ozone] # adding temperature and humidity to values array (casted to ints): values.append(int(temperature)) values.append(int(humidity)) # returning array packed for sigfox # sigfox custom grammar to use: ozone1::uint:8 ozone2::uint:8 ozone3::uint:8 ozone4::uint:8 ozone5::uint:8 ozone6::uint:8 ozone7::uint:8 ozone8::uint:8 ozone9::uint:8 ozone10::uint:8 temperature::uint:8 humidity::uint:8 return struct.pack('B' * len(values), *values) # return struct.pack('>BBBBBBBBBB', ozone..., int(temperature), int(humidity)) def sendMessage(self, message): return self.socket.send(message) def sendValues(self, ozone, temperature, humidity): res = self.sendMessage( self.packMesageForSigFox(ozone, temperature, humidity)) return res
class SigfoxNotifier(): def __init__(self): self._sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ1) self._socket = socket.socket(socket.AF_SIGFOX, socket.SOCK_RAW) self._socket.setblocking(True) self._socket.setsockopt(socket.SOL_SIGFOX, socket.SO_RX, False) self._temp = 0 self._last_use = 0 self._in_use = False print('sigfox mac: {}, pac: {}, id: {}'.format( binascii.hexlify(self._sigfox.mac()), binascii.hexlify(self._sigfox.pac()), binascii.hexlify(self._sigfox.id()))) def handle_notifications(self, notifications): for n in notifications: msg = bytearray() self._last_use = 0 if n.type == NotificationQueue.VIBRATION_STARTED: self._in_use = True elif n.type == NotificationQueue.VIBRATION_STOPPED: self._in_use = False self._last_use = n.data elif n.type == NotificationQueue.AMBIENT: self._temp = int(n.data[0]) else: print('unsupported event {}'.format(n.type)) continue msg.append(self._in_use) msg.extend(self._last_use.to_bytes(4, 'little')) msg.extend(self._temp.to_bytes(4, 'little')) print(list(msg)) self._socket.send(msg)
from network import Sigfox import binascii # initalise Sigfox for RCZ1 (You may need a different RCZ Region) sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ1) # print Sigfox Device ID print('Sigfox Device ID: {}'.format(binascii.hexlify(sigfox.id()))) # print Sigfox PAC number print('Sigfox PAC number: {}'.format(binascii.hexlify(sigfox.pac())))
from network import Sigfox import socket import time import pycom import binascii pycom.heartbeat(False) sigfoxDevice = Sigfox(mode=Sigfox.FSK, frequency=920800000) #Set transmision frequency sigfoxSocket = socket.socket(socket.AF_SIGFOX, socket.SOCK_RAW) # create a Sigfox socket deviceID = binascii.hexlify(sigfoxDevice.id()) # get device ID devicePAC = binascii.hexlify(sigfoxDevice.pac()) # get device PAC sigfoxSocket.setblocking(True) num = 0 while True: #sigfoxSocket.send(deviceID) print("Message ID: " + str(num) + " receiving from " + str(sigfoxSocket.recv(64))) time.sleep(1) num = num + 1
from network import Sigfox import socket import binascii import time # init Sigfox for RCZ4 (Chile) sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ4) s = socket.socket(socket.AF_SIGFOX, socket.SOCK_RAW) # print Sigfox Device ID print("ID: ", binascii.hexlify(sigfox.id())) # print Sigfox PAC number print("PAC: ", binascii.hexlify(sigfox.pac())) # make the socket blocking s.setblocking(True) # configure it as uplink only s.setsockopt(socket.SOL_SIGFOX, socket.SO_RX, False) s.settimeout(10) # send some bytes for i in range(5): payload = bytes(str(i).encode()) print("Sending...") s.send(payload) print("Sent.") print(payload) time.sleep(30)
import socket import binascii import pycom import time from pysense import Pysense from LTR329ALS01 import LTR329ALS01 pycom.heartbeat(False) sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ4) # create a Sigfox socket s = socket.socket(socket.AF_SIGFOX, socket.SOCK_RAW) print('I am device ', binascii.hexlify(sigfox.id()), binascii.hexlify(sigfox.pac())) # send some bytes s.send("Hello World") # make the socket blocking s.setblocking(True) # configure it as uplink only s.setsockopt(socket.SOL_SIGFOX, socket.SO_RX, False) py = Pysense() lt = LTR329ALS01(py) print("Hello World Ligth")
# pycom.wdt_on_boot(False) print("sys.platform:", sys.platform) print("machine.unique_id:", ubinascii.hexlify(machine.unique_id())) print("machine.freq:", machine.freq()) print("machine.info:") machine.info() print("pycom.wifi_on_boot:", pycom.wifi_on_boot()) print("pycom.wdt_on_boot:", pycom.wdt_on_boot()) # print("pycom.smart_config_on_boot:", pycom.smart_config_on_boot()) sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=RCZ) print("sigfox version:", sigfox.version()) print("sigfox ID:", ubinascii.hexlify(sigfox.id())) print("sigfox PAC:", ubinascii.hexlify(sigfox.pac())) print("sigfox region:", RCZ) print("sigfox frequencies:", sigfox.frequencies()) print("sigfox rssi offset:", sigfox.rssi_offset()) # sigfox config rcz3_config_test_mode = ( 1, 0x2ee0, 0x100 ) # ie, 1 retry, 12sec timeout, 3rd config is always ignored in R3 # rcz3_config_default = (3, 5000, 0x100) # ie, 3 retries, 5sec timeout print("sigfox config:", sigfox.config()) # by default put the device into private key mode sigfox.public_key(False) print("sigfox public key:", sigfox.public_key()) s = socket.socket(socket.AF_SIGFOX, socket.SOCK_RAW)
from network import Sigfox import socket downlink = True print("init") sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ1) # rcz1 Europe print("id", ubinascii.hexlify(sigfox.id())) print("pac", ubinascii.hexlify(sigfox.pac())) print("socket") s = socket.socket(socket.AF_SIGFOX, socket.SOCK_RAW) s.setblocking(True) if downlink: s.setsockopt(socket.SOL_SIGFOX, socket.SO_RX, True) print("send") s.send(bytes([1, 4, 12])) x = s.recv(64) print("received", ubinascii.hexlify(x)) else: s.setsockopt(socket.SOL_SIGFOX, socket.SO_RX, False) print("send") s.send(bytes([1, 4, 3]))
# * click on Messages # * run this script # -> see a message show up in the list # -> see a "received .... " line in the REPL ################################################## # init downlink = True # downlink = False count = 100 sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ1) print("ID", hexlify(sigfox.id())) if False: # one time PAC to be used when initially registering device print("PAC", hexlify(sigfox.pac())) s = socket.socket(socket.AF_SIGFOX, socket.SOCK_RAW) s.setblocking(True) ################################################## # blink if pycom.heartbeat(): pycom.heartbeat(False) for i in range(0, 5): pycom.rgbled(0x220022) time.sleep_ms(100) pycom.rgbled(0x000000) time.sleep_ms(100) ##################################################
wdt.feed() battery_voltage = py.read_battery_voltage() if disable_low_power_on_usb == 1: if battery_voltage > 4.2: low_power_consumption_mode = 0 print("USB connection detected, disable low power mode (voltage=%s)" % (str(py.read_battery_voltage()))) sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ1) sigfox_network = socket.socket(socket.AF_SIGFOX, socket.SOCK_RAW) sigfox_network.setblocking(True) sigfox_network.setsockopt(socket.SOL_SIGFOX, socket.SO_RX, True) # true=downlink device_id = binascii.hexlify(sigfox.id()) device_pac = binascii.hexlify(sigfox.pac()) if low_power_consumption_mode == 0: print("DEVICE ID : %s" % (device_id)) print("DEVICE PAC: %s" % (device_pac)) # ################################################################ # ######## pre-check # ################################################################ low_power_mode_indicator = color_white low_power_mode_indicator_ok = color_white low_power_mode_indicator_fail = color_red for x in range(4): # indicate power mode (blue=high power, orange=low power) time.sleep(0.1)
from network import Sigfox import binascii # initalise Sigfox for RCZ1 (You may need a different RCZ Region) sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ1) # print Sigfox Device ID print('Sigfox Device ID: ', binascii.hexlify(sigfox.id())) # print Sigfox PAC number print('Sigfox PAC number: ', binascii.hexlify(sigfox.pac()))
class SigfoxLib(): ### On object creation, define zone def __init__(self, zone): # Define Zone if zone == "RCZ1": # Init Sigfox for RCZ1 (Europe, Oman, South Africa) self.sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ1) elif zone == "RCZ2": # Init Sigfox for RCZ2 (USA, Mexico, Brazil) self.sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ2) elif zone == "RCZ3": # Init Sigfox for RCZ3 (Japan) self.sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ3) elif zone == "RCZ4": # Init Sigfox for RCZ4 (Australia, New Zealand, Singapore, Taiwan, Hong Kong, Colombia, Argentina) self.sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ4) else: # Default one to RCZ1 self.sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ1) # Variables self.received_mes = "" ### Proint firmware, lib content, ID and PAC def print_device_info(self): # Print device firmware and library included on flash print("Device firmware: ", os.uname().release) print("Device /flash/lib: ", os.listdir('/flash/lib')) # Print device ID and PAC print("Device ID: ", {binascii.hexlify(self.sigfox.id())}) print("Device PAC ", {binascii.hexlify(self.sigfox.pac())}, "\n") ### Create socket and set UPLINK by default def init_com(self): # Create a Sigfox socket self.s = socket.socket(socket.AF_SIGFOX, socket.SOCK_RAW) # Make the socket blocking self.s.setblocking(True) # Default UPLINK self.s.setsockopt(socket.SOL_SIGFOX, socket.SO_RX, False) self.mode = 1 ### Close socket def close_com(self): # Close socket self.s.close() ### Set sending mode to DOWNLINK or UPLINK def set_sending_mode(self, mode): # Define sending mode UPLINK or DOWNLINK if mode == "DOWNLINK": # Configure it as DOWNLINK specified by 'True' self.s.setsockopt(socket.SOL_SIGFOX, socket.SO_RX, True) self.mode = 0 elif mode == "UPLINK": # Configure it as UPLINK only self.s.setsockopt(socket.SOL_SIGFOX, socket.SO_RX, False) self.mode = 1 else: # Default UPLINK self.s.setsockopt(socket.SOL_SIGFOX, socket.SO_RX, False) self.mode = 1 ### Send SIGFOX message def send_message(self, message): # Send message depending on mode UPLINK or DOWNLINK if self.mode == 0: # DOWNLINK message pycom.rgbled(0xFFC000) # LED yellow input = self.s.send(message) pycom.rgbled(0) # Turn off the LED print("Nb bytes sent: ", input) # Number of bytes sent # Await DOWNLINK message self.received_mes = self.s.recv(32) print("Message received: ", self.received_mes, "\n") else: # UPLINK message pycom.rgbled(0x007f00) # LED green input = self.s.send(message) pycom.rgbled(0) # Turn off the LED print("Nb bytes sent: ", input, "\n") # Number of bytes sent ### Get message received by DOWNLOAD def get_lastrcvd_message(self): return self.received_mes