def slave(): csn = Pin(cfg['csn'], mode=Pin.OUT, value=1) ce = Pin(cfg['ce'], mode=Pin.OUT, value=0) if cfg['spi'] == -1: spi = SPI(-1, sck=Pin(cfg['sck']), mosi=Pin(cfg['mosi']), miso=Pin(cfg['miso'])) nrf = NRF24L01(spi, csn, ce, payload_size=32, channel=80) else: nrf = NRF24L01(SPI(cfg['spi']), csn, ce, payload_size=32, channel=80) nrf.open_tx_pipe(pipes[1]) nrf.open_rx_pipe(1, pipes[0]) nrf.start_listening() print('NRF24L01 slave mode, waiting for packets... (ctrl-C to stop)') while True: global printcount global foot_l global foot_lcount global count_udp if nrf.any(): while nrf.any(): buf = nrf.recv() lorr, datasend, count = struct.unpack('iii', buf) if lorr == 1: ##left is 1 foot_l[count] = datasend foot_lcount[count] += 1 printcount = (printcount + 1) % 16 # print("data:",datasend,"count:",printcount) utime.sleep_ms(_RX_POLL_DELAY) nrf.stop_listening() if printcount == 0: print('ready to send to server') foot_l = foot_l[1:] + [foot_l[0]] dictsend = {'sign': 'l', 'data': foot_l} sent = json.dumps(dictsend) try: # slaveserver.sendto(sent.encode('utf-8'),(addr,port)) #count_udp += 1 print('start send') r = urequests.post('http://184.72.70.209:5000/leftdata', data=sent, headers=iot_headers) print('sent') # count_udp+=1 except: print('send failed') # count_udp += 1 utime.sleep(0.01) nrf.start_listening()
def master(): csn = Pin(cfg['csn'], mode=Pin.OUT, value=1) ce = Pin(cfg['ce'], mode=Pin.OUT, value=0) if cfg['spi'] == -1: spi = SPI(-1, sck=Pin(cfg['sck']), mosi=Pin(cfg['mosi']), miso=Pin(cfg['miso'])) nrf = NRF24L01(spi, csn, ce, payload_size=8) else: nrf = NRF24L01(SPI(cfg['spi']), csn, ce, payload_size=8) nrf.open_tx_pipe(pipes[0]) nrf.open_rx_pipe(1, pipes[1]) nrf.start_listening() num_needed = 16 num_successes = 0 num_failures = 0 led_state = 0 print('NRF24L01 master mode, sending %d packets...' % num_needed) while num_successes < num_needed and num_failures < num_needed: # stop listening and send packet nrf.stop_listening() millis = utime.ticks_ms() led_state = max(1, (led_state << 1) & 0x0f) print('sending:', millis, led_state) try: nrf.send(struct.pack('ii', millis, led_state)) except OSError: pass # start listening again nrf.start_listening() # wait for response, with 250ms timeout start_time = utime.ticks_ms() timeout = False while not nrf.any() and not timeout: if utime.ticks_diff(utime.ticks_ms(), start_time) > 250: timeout = True if timeout: print('failed, response timed out') num_failures += 1 else: # recv packet got_millis, = struct.unpack('i', nrf.recv()) # print response and round-trip delay print('got response:', got_millis, '(delay', utime.ticks_diff(utime.ticks_ms(), got_millis), 'ms)') num_successes += 1 # delay then loop utime.sleep_ms(250) print('master finished sending; successes=%d, failures=%d' % (num_successes, num_failures))
def slave(): nrf = NRF24L01(SPI(2), Pin('Y5'), Pin('Y4'), payload_size=8) nrf.open_tx_pipe(pipes[1]) nrf.open_rx_pipe(1, pipes[0]) nrf.start_listening() print('NRF24L01 slave mode, waiting for packets... (ctrl-C to stop)') while True: pyb.wfi() if nrf.any(): while nrf.any(): buf = nrf.recv() millis, led_state = struct.unpack('ii', buf) print('received:', millis, led_state) for i in range(4): if led_state & (1 << i): pyb.LED(i + 1).on() else: pyb.LED(i + 1).off() pyb.delay(15) nrf.stop_listening() try: nrf.send(struct.pack('i', millis)) except OSError: pass print('sent response') nrf.start_listening()
def slave(): nrf = NRF24L01(SPI(2), Pin('Y5'), Pin('Y4'), payload_size=8) nrf.open_tx_pipe(pipes[1]) nrf.open_rx_pipe(1, pipes[0]) nrf.start_listening() print('NRF24L01 slave mode, waiting for packets... (ctrl-C to stop)') while True: machine.idle() if nrf.any(): while nrf.any(): buf = nrf.recv() millis, led_state = struct.unpack('ii', buf) print('received:', millis, led_state) for led in leds: if led_state & 1: led.on() else: led.off() led_state >>= 1 utime.sleep_ms(15) nrf.stop_listening() try: nrf.send(struct.pack('i', millis)) except OSError: pass print('sent response') nrf.start_listening()
def setup(): print("Initialising the nRF24L0+ Module") nrf = NRF24L01(SPI(0), csn, ce, payload_size=payload_size) nrf.open_tx_pipe(send_pipe) nrf.open_rx_pipe(1, receive_pipe) nrf.start_listening() return nrf
def setup(): """Setup radio transiever to recieve commands and transmit verification.""" nrf = NRF24L01(SPI(0), csn, ce, payload_size=10) nrf.open_tx_pipe(pipes[0]) nrf.open_rx_pipe(1, pipes[1]) nrf.start_listening() led.value(0) return nrf
def slave(spi): if spi==1: cfg = {'spi': 1, 'miso': 'X7', 'mosi': 'X8', 'sck': 'X6', 'csn': 'X5', 'ce': 'X4'} else : cfg = {'spi': 2, 'miso': 'Y7', 'mosi': 'Y8', 'sck': 'Y6', 'csn': 'Y5', 'ce': 'Y4'} csn = Pin(cfg['csn'], mode=Pin.OUT, value=1) ce = Pin(cfg['ce'], mode=Pin.OUT, value=0) if cfg['spi'] == -1: spi = SPI(-1, sck=Pin(cfg['sck']), mosi=Pin(cfg['mosi']), miso=Pin(cfg['miso'])) nrf = NRF24L01(spi, csn, ce, payload_size=8) else: nrf = NRF24L01(SPI(cfg['spi']), csn, ce, payload_size=8) nrf.open_tx_pipe(pipes[1]) nrf.open_rx_pipe(1, pipes[0]) nrf.start_listening() # print('NRF24L01 slave mode, waiting for packets... (ctrl-C to stop)') while True: if nrf.any(): while nrf.any(): buf = nrf.recv() #接收内容 millis, led_state = struct.unpack('ii', buf)#解析包unpack为解析包函数 # print('received:', millis, led_state) for led in leds: if led_state & 1: led.on() else: led.off() led_state >>= 1 utime.sleep_ms(_RX_POLL_DELAY) # Give master time to get into receive mode. utime.sleep_ms(_SLAVE_SEND_DELAY) nrf.stop_listening() try: nrf.send(struct.pack('i', millis)) except OSError: pass print('sent response') nrf.start_listening() break return millis
def slave(): csn = Pin(cfg["csn"], mode=Pin.OUT, value=1) ce = Pin(cfg["ce"], mode=Pin.OUT, value=0) if cfg["spi"] == -1: spi = SPI(-1, sck=Pin(cfg["sck"]), mosi=Pin(cfg["mosi"]), miso=Pin(cfg["miso"])) nrf = NRF24L01(spi, csn, ce, payload_size=8) else: nrf = NRF24L01(SPI(cfg["spi"]), csn, ce, payload_size=8) nrf.open_tx_pipe(pipes[1]) nrf.open_rx_pipe(1, pipes[0]) nrf.start_listening() print("NRF24L01 slave mode, waiting for packets... (ctrl-C to stop)") while True: if nrf.any(): while nrf.any(): buf = nrf.recv() millis, led_state = struct.unpack("ii", buf) print("received:", millis, led_state) for led in leds: if led_state & 1: led.on() else: led.off() led_state >>= 1 utime.sleep_ms(_RX_POLL_DELAY) # Give master time to get into receive mode. utime.sleep_ms(_SLAVE_SEND_DELAY) nrf.stop_listening() try: nrf.send(struct.pack("i", millis)) except OSError: pass print("sent response") nrf.start_listening()
def master(): nrf = NRF24L01(SPI(2), Pin('Y5'), Pin('Y4'), payload_size=8) nrf.open_tx_pipe(pipes[0]) nrf.open_rx_pipe(1, pipes[1]) nrf.start_listening() num_needed = 16 num_successes = 0 num_failures = 0 led_state = 0 print('NRF24L01 master mode, sending %d packets...' % num_needed) while num_successes < num_needed and num_failures < num_needed: # stop listening and send packet nrf.stop_listening() millis = pyb.millis() led_state = max(1, (led_state << 1) & 0x0f) print('sending:', millis, led_state) try: nrf.send(struct.pack('ii', millis, led_state)) except OSError: pass # start listening again nrf.start_listening() # wait for response, with 250ms timeout start_time = pyb.millis() timeout = False while not nrf.any() and not timeout: if pyb.elapsed_millis(start_time) > 250: timeout = True if timeout: print('failed, response timed out') num_failures += 1 else: # recv packet got_millis, = struct.unpack('i', nrf.recv()) # print response and round-trip delay print('got response:', got_millis, '(delay', pyb.millis() - got_millis, 'ms)') num_successes += 1 # delay then loop pyb.delay(250) print('master finished sending; successes=%d, failures=%d' % (num_successes, num_failures))
def setUp(self): super().setUp() self.radio = NRF24L01(0, 0, 22, 18, pri_mode_rx=False, incoming_data_cb=data_callback) self.radio.address_width = 5 self.radio.crc = NRF24L01.CRC_ENABLED self.radio.crc_length = NRF24L01.CRC_8 self.radio.pa_level = NRF24L01.PA_LOW self.radio.data_rate = NRF24L01.BR_1MBPS self.radio.channel = 76 self.radio.retries = 5 self.radio.delay = 1000 self.radio.enable_interrupt(NRF24L01.RX_DR | NRF24L01.TX_DS | NRF24L01.MAX_RT) self.assertEqual(self.radio.state, 'standby_i', 'Standby Mode was not SET')
def master(): csn = Pin(cfg["csn"], mode=Pin.OUT, value=1) ce = Pin(cfg["ce"], mode=Pin.OUT, value=0) if cfg["spi"] == -1: spi = SPI(-1, sck=Pin(cfg["sck"]), mosi=Pin(cfg["mosi"]), miso=Pin(cfg["miso"])) nrf = NRF24L01(spi, csn, ce, payload_size=8) else: nrf = NRF24L01(SPI(cfg["spi"]), csn, ce, payload_size=8) nrf.open_tx_pipe(pipes[0]) nrf.open_rx_pipe(1, pipes[1]) nrf.start_listening() num_needed = 16 num_successes = 0 num_failures = 0 led_state = 0 print("NRF24L01 master mode, sending %d packets..." % num_needed) while num_successes < num_needed and num_failures < num_needed: # stop listening and send packet nrf.stop_listening() millis = utime.ticks_ms() led_state = max(1, (led_state << 1) & 0x0F) print("sending:", millis, led_state) try: nrf.send(struct.pack("ii", millis, led_state)) except OSError: pass # start listening again nrf.start_listening() # wait for response, with 250ms timeout start_time = utime.ticks_ms() timeout = False while not nrf.any() and not timeout: if utime.ticks_diff(utime.ticks_ms(), start_time) > 250: timeout = True if timeout: print("failed, response timed out") num_failures += 1 else: # recv packet (got_millis, ) = struct.unpack("i", nrf.recv()) # print response and round-trip delay print( "got response:", got_millis, "(delay", utime.ticks_diff(utime.ticks_ms(), got_millis), "ms)", ) num_successes += 1 # delay then loop utime.sleep_ms(250) print("master finished sending; successes=%d, failures=%d" % (num_successes, num_failures))
import json import time import threading import RPi.GPIO as GPIO from mqtt_service import MqttService from nrf24l01 import NRF24L01 from arm_controller import ArmController from config import * rf24 = NRF24L01() mqtt = MqttService() arm = ArmController() robot = False trashbin = False def button_press(): global trashbin trashbin = not trashbin if trashbin: for _ in range(3): rf24.write(b'11') rf24.write(b'21') rf24.write(b'31') time.sleep(0.1) else:
ping_mqtt = 0 ping_fail = 0 water = False wdt = WDT() # Init MQTT client = MQTTClient(config.CONFIG['MQTT_CLIENT'], config.CONFIG['MQTT_BROKER'], user=config.CONFIG['MQTT_USER'], password=config.CONFIG['MQTT_PASSWORD'], port=config.CONFIG['MQTT_PORT']) # Init NRF24L01 csn = Pin(config.NRFCFG["csn"], mode=Pin.OUT, value=1) ce = Pin(config.NRFCFG["ce"], mode=Pin.OUT, value=0) nrf = NRF24L01(SPI(config.NRFCFG["spi"]), csn, ce, channel=76, payload_size=32) nrf.open_rx_pipe(1, config.PIPES[1]) nrf.start_listening() def time_now(): ntp_query = bytearray(48) ntp_query[0] = 0x1b try: addr = socket.getaddrinfo(config.host, 123)[0][-1] s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.settimeout(1) s.sendto(ntp_query, addr) msg = s.recv(48) s.close() val = struct.unpack("!I", msg[40:44])[0]
def master(): csn = Pin(cfg['csn'], mode=Pin.OUT, value=1) ce = Pin(cfg['ce'], mode=Pin.OUT, value=0) if cfg['spi'] == -1: spi = SPI(-1, sck=Pin(cfg['sck']), mosi=Pin(cfg['mosi']), miso=Pin(cfg['miso'])) nrf = NRF24L01(spi, csn, ce, payload_size= 32,channel = 80) else: nrf = NRF24L01(SPI(cfg['spi']), csn, ce, payload_size=32,channel = 80) adc = machine.ADC(machine.Pin(39)) adc.atten(adc.ATTN_11DB) row_0 = machine.Pin(13,machine.Pin.OUT) row_1= machine.Pin(12,machine.Pin.OUT) row_2 = machine.Pin(27,machine.Pin.OUT) column_0 = machine.Pin(33,machine.Pin.OUT) column_1 = machine.Pin(15,machine.Pin.OUT) foot_map =[(1,0,1,0,0),(1,0,1,0,1),(1,0,1,1,0),(1,0,1,1,1),(0,0,0,0,0),(0,0,0,0,1),(0,0,0,1,0),(0,0,0,1,1),(1,0,0,1,1),(0,0,1,0,1),(0,0,1,1,0),(0,0,1,1,1),(0,1,1,0,1),(0,1,1,1,0),(0,1,0,0,1),(0,1,0,1,0)] nrf.open_tx_pipe(pipes[0]) nrf.open_rx_pipe(1, pipes[1]) nrf.start_listening() lorr = 1#left sing is 1 count = 0 print('NRF24L01 master mode, sending ') while True : # stop listening and send packet nrf.stop_listening() row_2_val,row_1_val,row_0_val,column_1_val,column_0_val = foot_map[count] row_2.value(row_2_val) row_1.value(row_1_val) row_0.value(row_0_val) column_1.value(column_1_val) column_0.value(column_0_val) result = adc.read() datasend = result try: nrf.send(struct.pack('iii',lorr,datasend,count)) print('sending:', lorr, datasend) except OSError: pass count = (1+count) % 16 # start listening again nrf.start_listening() # wait for response, with 50ms timeout # start_time = utime.ticks_ms() # timeout = False # while not nrf.any() and not timeout: # if utime.ticks_diff(utime.ticks_ms(), start_time) > 50: # timeout = True # if timeout: # print('failed, response timed out') # else: # # recv packet # got_millis = struct.unpack('i', nrf.recv()) # # print response and round-trip delay # print('got response:', got_millis) # delay then loop utime.sleep_ms(15)
import gc gc.collect() SCK = 5 MOSI = 4 MISO = 0 CE = 2 CSN = 14 from nrf24l01 import NRF24L01 nrf = NRF24L01(ce=2, csn=14) nrf.set_channel(90) nrf.set_speed(0x08) nrf.set_crc(2) nrf.open_tx_pipe('e7e7e7e7e7') nrf.send(bytes([0x01, 0x02])) nrf.read_reg(0x07)
def __init__(self,spi,csn,ce): csn0 = Pin(csn, mode=Pin.OUT, value=1) ce0 = Pin(ce, mode=Pin.OUT, value=0) self.nrf = NRF24L01(SPI(spi), csn0, ce0, payload_size=8)