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 sigfox_config(RCZ): # init Sigfox with the right RCZ sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=RCZ) print(sigfox.id()) # create a Sigfox socket s = socket.socket(socket.AF_SIGFOX, socket.SOCK_RAW) # make the socket blocking s.setblocking(True) # configure it as uplink only s.setsockopt(socket.SOL_SIGFOX, socket.SO_RX, False) return s
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()))
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
def sigfox_setup(): # setup sigfox sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ1) sigfox_socket = socket.socket(socket.AF_SIGFOX, socket.SOCK_RAW) sigfox_socket.setblocking(True) sigfox_socket.setsockopt(socket.SOL_SIGFOX, socket.SO_RX, False) return sigfox_socket
def connect_sigfox(self): if (self.__connection_status != constants.__CONNECTION_STATUS_DISCONNECTED): # noqa print("Error: Connection already exists. Disconnect First") pass try: from network import Sigfox except Exception: print("This device does not support Sigfox connections") return sigfox_config = self.__conf.get('sigfox', {}) if sigfox_config is None or sigfox_config.get('RCZ') is None: print(constants.__SIGFOX_WARNING) try: sf_rcz = int(sigfox_config.get('RCZ', 1)) - 1 if sf_rcz >= 0 and sf_rcz <= 3: Sigfox(mode=Sigfox.SIGFOX, rcz=sf_rcz) self.__sigfox_socket = socket.socket(socket.AF_SIGFOX, socket.SOCK_RAW) # noqa self.__sigfox_socket.setblocking(True) self.__sigfox_socket.setsockopt(socket.SOL_SIGFOX, socket.SO_RX, False) # noqa self.__network_type = constants.__NETWORK_TYPE_SIGFOX self.__connection_status = constants.__CONNECTION_STATUS_CONNECTED_SIGFOX # noqa self.__pybytes_protocol.start_Sigfox(self) print( "Connected using Sigfox. Only upload stream is supported" ) return True else: print('Invalid Sigfox RCZ specified in config!') return False except Exception as e: print('Exception in connect_sigfox: {}'.format(e)) return False
def initialize(): # init Sigfox for RCZ1 (Europe) cls.sg = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ1) # create a Sigfox socket cls.the_socket = socket.socket(socket.AF_SIGFOX, socket.SOCK_RAW) # make the socket blocking cls.the_socket.setblocking(True)
def initialize(chrono): ComInterface.chrono = chrono # init Sigfox for RCZ1 (Europe) ComInterface.sg = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ1) # create a Sigfox socket ComInterface.the_socket = socket.socket(socket.AF_SIGFOX, socket.SOCK_RAW) # make the socket blocking ComInterface.the_socket.setblocking(True)
def __init__(self): self._sigfox= Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ2) s = usocket.socket(usocket.AF_SIGFOX, usocket.SOCK_RAW) s.setblocking(True) s.setsockopt(usocket.SOL_SIGFOX, usocket.SO_RX, False) s.send(bytes([0x01, 0x02, 0x03]))
def sendMessage(): #Sigfox sfx = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ2) # init Sigfox for RCZ2 (USA) s = socket.socket(socket.AF_SIGFOX, socket.SOCK_RAW) # create a Sigfox Socket s.setsockopt(socket.SOL_SIGFOX, socket.SO_RX, False) # configure it as uplink only s.send(str(massaHex)) print("Sigfox Message Sent")
def start_test_ce(): global sigfox global lora global start_freq global end_freq global hop_num sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ1) lora = LoRa(mode=LoRa.LORA, region=LoRa.EU868) start_freq = 863000000 end_freq = 870000000 hop_num = 68
def start_test_rcm(): global sigfox global lora global start_freq global end_freq global hop_num sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ4) lora = LoRa(mode=LoRa.LORA, region=LoRa.AU915) start_freq = 915000000 end_freq = 928000000 hop_num = 128
def start_test_fcc(): global sigfox global lora global start_freq global end_freq global hop_num sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ2) lora = LoRa(mode=LoRa.LORA, region=LoRa.US915) start_freq = 902000000 end_freq = 928000000 hop_num = 258
def __init__(self): self.debug('doing state init') #load (or create) a local file sd = SD() os.mount(sd, '/sd') #sigfox setup sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ1) self.sig = socket.socket(socket.AF_SIGFOX, socket.SOCK_RAW) self.sig.setblocking(False) self.sig.setsockopt(socket.SOL_SIGFOX, socket.SO_RX, False) #py = Pytrack() #self.gps = L76GNSS(py) self.debug('trying to get state from net...') try: data = {} data["order"] = '-createdAt' data["limit"] = '1' headers = { 'X-Parse-Application-Id': PARSE_APPLICATION_ID, 'X-Parse-REST-API-Key': PARSE_REST_API_KEY, 'Content-Type': 'application/json' } self.debug('sending request.') resp = urequests.get(PARSE_BASE_URL + '/classes/ConfigObject', headers=headers, data=ujson.dumps(data)) config = resp.json() resp.close() self.debug('got state from net:') self.debug(ujson.dumps(config)) self.write_config(config["results"][0]) except: self.debug('could not get state from net.') try: f = open('/sd/config.json', 'r') config = f.read() f.close() if config == '': self.debug('found invalid local json file, writing...') config = {} config["state"] = 0 self.write_config(config) else: self.debug('found valid local json file:') self.debug(config) self.config = ujson.loads(config) except: self.debug('ERROR - could not get/create a valid config file!') pass
def __init__(self, ctrlbyte): 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) self.ctrlbyte = ctrlbyte self.buffer = bytearray(0)
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)
def sendSigfoxMessage(payload): global sigfoxBroadcastStatus sigfoxBroadcastStatus = 0 # init Sigfox for RCZ1 (Europe) sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ1) # create a Sigfox socket s = socket.socket(socket.AF_SIGFOX, socket.SOCK_RAW) # make the socket blocking s.setblocking(True) # configure it as uplink only s.setsockopt(socket.SOL_SIGFOX, socket.SO_RX, False) # send some bytes s.send(payload) print("Message {} sent successfully!".format(payload)) sigfoxBroadcastStatus = 1
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 = ""
def sigfoxConnect(humidity): # init Sigfox for RCZ1 (Europe) sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ1) # create a Sigfox socket s = socket.socket(socket.AF_SIGFOX, socket.SOCK_RAW) # make the socket blocking s.setblocking(True) # configure it as uplink only s.setsockopt(socket.SOL_SIGFOX, socket.SO_RX, False) print("connected") # send some bytes s.send(bytes([humidity])) print("send done")
def setup_sigfox_and_send(self, lijstBytes): pycom.heartbeat(False) # init Sigfox for RCZ1 (Europe) sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ1) pycom.rgbled(0xf4eb42) # geel # create a Sigfox socket s = socket.socket(socket.AF_SIGFOX, socket.SOCK_RAW) pycom.rgbled(0x00007f) # blauw # make the socket blocking s.setblocking(True) pycom.rgbled(0x9242f4) # paars # configure it as uplink only s.setsockopt(socket.SOL_SIGFOX, socket.SO_RX, True) pycom.rgbled(0xffffff) # wit print("Door de setup gelopen, bezig met verzenden...") #Hieronder wijzigingen verstuurd = False if (len(lijstBytes) > 12): #controleren als de lengte niet meer is dan 12 bytes error = "Maximum aantal bytes overschreden. Maximum is 12 bytes, er zijn %d bytes proberen te verzenden." % ( len(lijstBytes)) print(error) raise ValueError(error) verstuurd = False else: try: verstuurd = s.send(lijstBytes) #Bytes sturen time.sleep( 2 ) #Even wachten omdat het programma anders vastloopt. Er wordt dan meteen overgegaan naar de volgende regel code, terwijl de data nog niet verstuurd is. antwoord = s.recv(32) #Wachten op downlink message print("Verstuurd: " + '{}'.format(verstuurd) + " antwoord: " + '{}'.format(binascii.hexlify(antwoord))) print("VERZONDEN & DOWNLINK OK") except OSError as e: #Error opvangen if e.args[0] == 100: print("Probleem met OSError: [Errno 100] ENETDOWN") print("VERZONDEN, DOWNLINK NIET OK") verstuurd = True s.close() #Sluiten van de socket. return (verstuurd)
def send_data_Sigfox(data): """ Function to send data to Sigfox backend. Parameters ---------- data: bytes list of bytes to send Returns ------- bool True if data has been sent to Sigfox backend. Otherwise returns False """ # init Sigfox for RCZ1 (Europe) sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ1) # create a Sigfox socket s = socket.socket(socket.AF_SIGFOX, socket.SOCK_RAW) # make the socket blocking s.setblocking(True) # configure it as uplink only s.setsockopt(socket.SOL_SIGFOX, socket.SO_RX, False) bytes_data = len(data) data_sent = 0 # wait until the module has joined the network start = time.ticks_ms() while data_sent < bytes_data: # send bytes data_sent = s.send(data) # print(time.ticks_diff(start, time.ticks_ms())) # counter to send data to Sigfox. time_searching_Sigfox in seconds if time.ticks_diff(start, time.ticks_ms()) > \ (time_searching_Sigfox * 1000): print("possible timeout") return False return True
def send(humidity, distance, temperature): # init Sigfox for RCZ1 (Europe) sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ1) # create a Sigfox socket s = socket.socket(socket.AF_SIGFOX, socket.SOCK_RAW) # make the socket blocking s.setblocking(True) # configure it as uplink only s.setsockopt(socket.SOL_SIGFOX, socket.SO_RX, False) distance = 1800 factor = floor(distance / 255) rest = distance % 255 print(factor) print(rest) # send some bytes #s.send(string) s.send(bytes([humidity, factor, rest, temperature]))
def sigfox_send(): sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ1) # init Sigfox for RCZ1 (Europe) s = socket.socket(socket.AF_SIGFOX, socket.SOCK_RAW) # create a Sigfox socket s.setblocking(True) # make the socket blocking s.setsockopt(socket.SOL_SIGFOX, socket.SO_RX, False) try: print('Sigfox sending...') ret = s.send(_sigfox_payload()) print('Sigfox done', ret) pycom.rgbled(0x003300) idle() sleep(0.3) except Exception as e: print('Sigfox error', e.args[0]) pycom.rgbled(0x330000) idle() sleep(0.3) return 0 s.setblocking(False) s.close() return ret
def SendLevelSigfox(level): ''' data = b"" #string data = Msg.encode() #bytes #data = b"" #bytes print(Msg) print(len(data)) ''' # Battery Level BatteryLV, RealVolt = GetBattVolt() BatteryLV = BatteryLV << 4 RealVolt = int(RealVolt * 100) # Multiply 100 RVLow = RealVolt & 0xFF RVHigh = (RealVolt >> 8) & 0xFF # init Sigfox for RCZ4 sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ4) # create a Sigfox socket s = socket.socket(socket.AF_SIGFOX, socket.SOCK_RAW) # make the socket blocking s.setblocking(True) # configure it as uplink only s.setsockopt(socket.SOL_SIGFOX, socket.SO_RX, False) lowbyte = level & 0xFF highbyte = (level >> 8) & 0xFF # send some bytes s.send(bytes([BatteryLV, 0x00, 0x00, highbyte, lowbyte, RVHigh, RVLow, 0x00, 0x00, 0x00, 0x00, 0x00])) # 12 bytes payload s.close()
from network import Sigfox import socket # init Sigfox for RCZ1 (Europe) sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ1) # create a Sigfox socket s = socket.socket(socket.AF_SIGFOX, socket.SOCK_RAW) # make the socket blocking s.setblocking(True) # configure it as uplink only s.setsockopt(socket.SOL_SIGFOX, socket.SO_RX, False) # send some bytes s.send(bytes([0x01, 0x02, 0x03])
from network import Sigfox import socket import time import pycom pycom.heartbeat(False) sigfox = Sigfox(mode=Sigfox.FSK, frequency=920800000) s = socket.socket(socket.AF_SIGFOX, socket.SOCK_RAW) s.setblocking(True) num = 0 while True: s.send('Device-1') time.sleep(10) print("Message ID: " + str(num) + " receiving from " + str(s.recv(64))) num = num + 1
from network import Sigfox import socket import pycom import time # setup led pycom.heartbeat(False) pycom.rgbled(0x007f00) # init Sigfox for RCZ1 (Europe) sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ1) # create a Sigfox socket s = socket.socket(socket.AF_SIGFOX, socket.SOCK_RAW) # make the socket blocking s.setblocking(True) # wait for a downlink after sending the uplink packet s.setsockopt(socket.SOL_SIGFOX, socket.SO_RX, True) # send some bytes pycom.rgbled(0x7f7f00) # yellow try: s.send(bytes([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])) pycom.rgbled(0x007f00) # green time.sleep(1) except: pycom.rgbled(0x7f0000) # red time.sleep(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: {}'.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, Wifi import socket import binascii import time from machine import Pin from onewire import DS18X20 from onewire import OneWire # init Sigfox for RCZ1 (Europe) sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ1) # create a Sigfox socket s = socket.socket(socket.AF_SIGFOX, socket.SOCK_RAW) print('I am device ', binascii.hexlify(sigfox.id()) ) # make the socket blocking s.setblocking(True) # configure it as uplink only s.setsockopt(socket.SOL_SIGFOX, socket.SO_RX, False) ow = OneWire(Pin('G17')) temp = DS18X20(ow) while True: temp.start_convertion() time.sleep(1) result = temp.read_temp_async() print(result) s.send(str(result)) time.sleep(50)
import json # Chronometers for testing from machine import Timer def zfill(string, width): if len(string) < width: return ("0" * (width - len(string))) + string else: return string # init Sigfox for RCZ4 (Chile) # sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ4) # init Sigfox for RCZ1 (Europe) sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ1) s = socket.socket(socket.AF_SIGFOX, socket.SOCK_RAW) s.setblocking(True) # set False for only uplink, true for BIDIR s.setsockopt(socket.SOL_SIGFOX, socket.SO_RX, True) timeout = 45 s.settimeout(timeout) c = 10 submerged_time = 0 n = 20 # Wait for the beacon to be submerged time.sleep(submerged_time)