Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am trying to write a code that communicate between relay node and gateway Jul 18, 2018 01:59 I'm trying to implement some code that

I am trying to write a code that communicate between relay node and gateway

Jul 18, 2018 01:59

I'm trying to implement some code that takes the data described in the 2 attachment below and passes it to the gateway node. This is done by opening reading and writing pipes between the radios. The relay node has to keep reading pipes open, except when writing to the gateway. This is because the transceiver can only transmit or receive at one time, not simultaneously. but the code that switches between reading, writing and storage of the data array is most important.

1. The following is the transmitted data structure from the sensor node data recieved from sensor node: (MAX 32 BYTES PER TRANSMISSION) 1st byte = int node_address (ints take 1 byte) *If unsure of data size, reference the fundamentals of C++ and check how the variables were declared. 2nd byte = float temp sensor 1 (floats take 2 bytes) 4rd byte = float temp sensor 2 6th byte = float temp sensor 3 8th byte = float temp sensor 4 10th byte = float temp sensor 5 12th byte = int reed switch 1 13th byte = int reed switch 2 14th byte = int reed switch 3 Transmitter address: (should be in setup) const uint8_t txaddr[] = { 0xE7, 0xD3, 0xF0, 0x35, 0xF0 }; Sensor Node 1 Radio Address const uint8_t txaddr[] = { 0xE7, 0xD3, 0xF0, 0x35, 0xF1 }; Sensor Node 2 Radio Address const uint8_t txaddr[] = { 0xE7, 0xD3, 0xF0, 0x35, 0xF2 }; Sensor Node 3 Radio Address Relay node example: When parsing data, the first byte that comes in will determine the end node (sensor node) address. The node can be determined with the reading pipes on the relay node, but by the time it gets to the gateway, an address in the transmitted data is useful to track which end node it came from and can't be done with the pipes (man in the middle). Relay Reciving pipe addresses should match the above transmitting address. Each address should be a different pipe. Relay Transmitting address should match the gateways recieving address. The relay nodes should have a transmitting address than its reading address.

2. #include #include #include #include #include //STRUCTURES //FUNCTIONS void dump_radio_status_to_serialport(uint8_t); //list of all functions used in REV2.1 void get_address(); void get_temp(); void disp_address(); void reed_switch(); //DECLARE PINS OneWire ds(6); //declare all pins used Enrf24 radio(P2_0, P2_1, P2_2); // P2.0=CE, P2.1=CSN, P2.2=IRQ const int ledPin = P1_0; const int buttonPin = P1_3; const int reed1 = P2_3; const int reed2 = P2_4; const int reed3 = P2_5; //PROGRAM COUNTER bool got_address = FALSE; //will be set to TRUE once the address finder function runs bool display_address = FALSE; // will be set to TRUE once the address is displayed in the serial monitor //DECLARE SETUP int num_temp = 0; //number of temperature sensors (will be replaced with a function to auto detect the number of temperature sensors on the bus) int num_reed = 3; //number of reed switches present in the circuit int node_address = 1; float temp[5]; // array of floats to store the temperatures that are read from the sensors. Allocates 5 float positions (4bytes x 5sensors = 20 bytes) in memory for 5 sensors per node int reed[3]; //array of bools to store the status of the reed sensors. Allocates three (1byte x 3reeds) //DECLARE ADDRESS byte sensor_addr[5][8]; //DECLARE RADIO const uint8_t txaddr[] = { 0xE7, 0xD3, 0xF0, 0x35, 0xF0 }; //SETUP void setup() { Serial.begin(9600); pinMode(buttonPin, INPUT_PULLUP); //setup tactile switch pin as internal pull-up pinMode(reed1, INPUT_PULLUP); //setup reed switch pin as internal pull-up pinMode(reed2, INPUT_PULLUP); //setup reed switch pin as internal pull-up pinMode(reed3, INPUT_PULLUP); //setup reed switch pin as internal pull-up pinMode(ledPin, OUTPUT); //setup of sinking LED (will be inversly related to state in code) digitalWrite(ledPin, LOW); SPI.begin(); SPI.setDataMode(SPI_MODE0); //setup SPI bus SPI.setBitOrder(MSBFIRST); //setup SPI bus radio.begin( 1000000, 88 ); radio.setCRC( 1, 1 ); dump_radio_status_to_serialport(radio.radioState()); //function below code radio.setTXaddress((void*)txaddr); //setup radio address from address in array } //MAIN LOOP void loop() { // if (!digitalRead(buttonPin)) //need to write function to call // { // TX_setup(); // } if (!got_address) //checks bool state to determine if this should be displayed again { get_address(); //refrence void function below } if(!display_address){ disp_address(); //refrence void function below } reed_switch(); //refrence void function below get_temp(); radio.write(&temp, sizeof(temp)); radio.flush(); for(int i = 0; i < num_temp; i++) { Serial.println(temp[i]); } // dump_radio_status_to_serialport(radio.radioState()); // Should report IDLE delay(1000); } //*******************************************FUNCTIONS*********************************************** //RADIO STATUS void dump_radio_status_to_serialport(uint8_t status) { Serial.print("Enrf24 radio transceiver status: "); switch (status) { case ENRF24_STATE_NOTPRESENT: Serial.println("NO TRANSCEIVER PRESENT"); break; case ENRF24_STATE_DEEPSLEEP: Serial.println("DEEP SLEEP <1ua power consumption"); break; case enrf24_state_idle: serial.println("idle module powered up w> #include #include #include Enrf24 radio(P2_0, P2_1, P2_2); // P2.0=CE, P2.1=CSN, P2.2=IRQ const uint8_t txaddr[] = { 0xE7, 0xD3, 0xF0, 0x35, 0xF0 }; const char *str_on = "ON"; const char *str_off = "OFF"; void dump_radio_status_to_serialport(uint8_t); void setup() { Serial.begin(9600); SPI.begin(); SPI.setDataMode(SPI_MODE0); SPI.setBitOrder(MSBFIRST); radio.begin(); // Defaults 1Mbps, channel 0, max TX power dump_radio_status_to_serialport(radio.radioState()); radio.setTXaddress((void*)txaddr); radio.enableRX(); // Start listening } void loop() { Serial.print("Sending packet: "); Serial.println(str_on); radio.print(str_on); radio.flush(); // Force transmit (don't wait for any more data) dump_radio_status_to_serialport(radio.radioState()); // Should report IDLE delay(1000); Serial.print("Sending packet: "); Serial.println(str_off); radio.print(str_off); radio.flush(); // dump_radio_status_to_serialport(radio.radioState()); // Should report IDLE delay(1000); } void dump_radio_status_to_serialport(uint8_t status) { Serial.print("Enrf24 radio transceiver status: "); switch (status) { case ENRF24_STATE_NOTPRESENT: Serial.println("NO TRANSCEIVER PRESENT"); break; case ENRF24_STATE_DEEPSLEEP: Serial.println("DEEP SLEEP <1ua power consumption"); break; case enrf24_state_idle: serial.println("idle module powered up w>

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions