private void deviceList_SelectionChanged(object sender, SelectionChangedEventArgs e) { try { dev_info dev = (dev_info)deviceList.SelectedItem; lastitem = dev; scrolltext.Text = "Device ID:" + dev.device_address.ToString() + ", Device Name:" + dev.Name.ToString() + "," + dev.board_type.ToString(); switch (dev.board_type) { case (byte)device_type.Triac_Device: frame.Navigate(typeof(TriacDevice)); break; case (byte)device_type.Relay_Device: frame.Navigate(typeof(RelayBoard)); break; case (byte)device_type.Motor_Device: frame.Navigate(typeof(motor)); break; default: frame.Navigate(typeof(TestPage)); break; } } catch (Exception ex) { statusbar_string(ex.ToString(), true); } // button.Content = dev.id.ToString(); }
/// <summary> /// Receive data and remote address here + pass to controls /// </summary> /// <param name="addrs"></param> /// <param name="msg_string"></param> private void do_job(string addrs, string msg_string, string port_, string local_addr_) { //do jobs here :D raspberry or windows //deviceList.Items.Add(addrs + ":" + data_rx); //list view and datagram test : DONE :) //to clear network log textblock //otherwise system may become slow due to large amount of log information if (netlog.Text.Length > 500) netlog.Text = ""; //add network activity/log info to textblock netlog.Text = "Message: " + msg_string + "\n" + addrs + ", Group: " + local_addr_ + ", Port: " + port_ + "\n" + netlog.Text; char[] chararray = msg_string.ToCharArray(); string n_id; byte n_deviceID, devicelist_index = 0; //dev_info object dev_info dev_class = new dev_info(); try { ///wrong length of device ID may cause exception ///extract device ID from received byte n_id = new String(new char[] { chararray[2], chararray[3] }); n_deviceID = byte.Parse(n_id); statusbar_string(n_id, true); } catch { statusbar_string("Unknown Network Packet/Command", true); return; } bool flag = false; ///match received id with devices present. if failed 'return' foreach (dev_info dev in deviceList.Items) { if (dev.device_address == n_deviceID) { flag = true; //device found dev_class = dev; break; } devicelist_index++; } if(!flag) { //No Device Found statusbar_string("Network Request Received, But wrong Device ID", true); return; } byte[] n_tx_packet = new byte[6]; if (chararray[0] == '!') //checks whether it is control packet or other message { ///this switch statemnt will be clear if you look at the Android program ///each charactor of chararray[1] represents a control function /// eg. on/off relay, buzzer, triac etc switch (chararray[1]) { case 'A': dev_class.device1_state = 1; break; case 'B': dev_class.device2_state = 1; break; case 'C': dev_class.device3_state = 1; break; case 'D': dev_class.device4_state = 1; break; case 'E': dev_class.device1_state = 0; break; case 'F': dev_class.device2_state = 0; break; case 'G': dev_class.device3_state = 0; break; case 'H': dev_class.device4_state = 0; break; case 'I': dev_class.device2_state = 1; break; case 'J': dev_class.device2_state = 0; break; case 'K': dev_class.device3_state = 1; break; case 'L': dev_class.device3_state = 0; break; case 'M': byte trigger_value = byte.Parse(new String(new char[] { chararray[4], chararray[5] })); trigger_value = (byte)((trigger_value * 140.0) / 99.0); dev_class.device1_state = trigger_value; break; case 'N': dev_class.device1_state = 1; dev_class.device2_state = 0; dev_class.device3_state = 1; dev_class.device4_state = 0; break; case 'O': dev_class.device1_state = 0; dev_class.device2_state = 1; dev_class.device3_state = 0; dev_class.device4_state = 1; break; case 'P': dev_class.device1_state = 1; dev_class.device2_state = 0; dev_class.device3_state = 0; dev_class.device4_state = 1; break; case 'Q': dev_class.device1_state = 0; dev_class.device2_state = 1; dev_class.device3_state = 1; dev_class.device4_state = 0; break; case 'R': dev_class.device1_state = 0; dev_class.device2_state = 0; dev_class.device3_state = 0; dev_class.device4_state = 0; break; default: return; } //construct data byte from dev_class //by this way we made switch statement much simple byte[] n_tx_byte = {dev_class.device1_state, dev_class.device2_state, dev_class.device3_state, dev_class.device4_state,dev_class.device4_state,0x00}; ///Transmit data byte nrf_send_data(n_tx_byte, dev_class.device_address, packet_type.data_packet); //Update chnages deviceList.Items[devicelist_index] = dev_class; } }
/// <summary> /// Update Device list with latest value /// </summary> /// <param name="dev">dev_info object</param> public void update_devicelist(dev_info dev) { deviceList.SelectedItem = dev; }
private void deviceList_SelectionChanged(object sender, SelectionChangedEventArgs e) { try { dev_info dev = (dev_info)deviceList.SelectedItem; lastitem = dev; scrolltext.Text = "Device ID:" + dev.device_address.ToString() + ", Device Name:" + dev.Name.ToString() + "," + dev.board_type.ToString(); switch (dev.board_type) { case (byte)device_type.Triac_Device: frame.Navigate(typeof(TriacDevice)); break; case (byte)device_type.Relay_Device: frame.Navigate(typeof(RelayBoard)); break; case (byte)device_type.Motor_Device: frame.Navigate(typeof(motor)); break; default: frame.Navigate(typeof(TestPage)); break; } } catch(Exception ex) { statusbar_string(ex.ToString(), true); } // button.Content = dev.id.ToString(); }
/* nRF TX packet count != Raspbverryy Pi RX packet Raspberry Pi receives an extra byte at the begining (STATUS Register of nRF module) We don't need status register values in below function. data_byte[0] can be discarded. To know more about STATUS register and nRF module operation refer its datasheets */ public void data_received(byte[] data_byte) { last_received = data_byte; //Check Received Packet Type switch (data_byte[1]) { /*Device initialization packet every nRF slave device need to send an "add me" packet before starting to send an "add me" packet hold the "Switch 1(sw1)" during power on this will add the device into the list view item. */ case (byte)packet_type.add_me_packet: //foreach (byte dev in data_byte) //check all list view items //{ //for duplicate entry // textBox.Text += dev.ToString(); //} foreach (dev_info dev in deviceList.Items) //check all list view items { //for duplicate entry if (dev.device_address == data_byte[2]) { statusbar_string("Duplicate item found...", true); return; } } statusbar_string("New Slave board found... ID:" + data_byte[2].ToString(), false); //Get Device Type from tha packet 3rd byte string list_device_string; if (data_byte[3] < device_type_string.Length) list_device_string = device_type_string[data_byte[3]]; else list_device_string = "Unknown Type/New Device"; //Add new item to the listview dev_info new_device = new dev_info(list_device_string); new_device.board_type = data_byte[3]; //3rd byte = Board type (Triac, Relay, Buzzer etc new_device.device_address = data_byte[2]; //2ndByte = nRF slave ID\ //Device 1 to 4 state (on/off, trigger angle), any number of relays/buzzers/sensors can be added (need to modify the code) //Either using multiple packets or extracting bits (On/Off states require only 1 bit, multible device states can be added in a single byte new_device.device1_state = data_byte[5]; new_device.device2_state = data_byte[6]; new_device.device3_state = data_byte[7]; new_device.device4_state = data_byte[8]; // statusbar_string(new_device.device1_state.ToString() // + new_device.device2_state.ToString() + new_device.device3_state.ToString() + // new_device.device4_state.ToString(), false); //Sensor readings (No sensor limitation, the code can be modified for upto n number of sensors new_device.sensor1_value = data_byte[9]; new_device.sensor2_value = data_byte[10]; //add to listview deviceList.Items.Add(new_device); break; /* Data packet contains sensor informations, solid state relay(s) status, relay(s) status, buzzer on/of condition and other slave specific informations. */ case (byte)packet_type.update_packet: statusbar_string("Update Packet Received..." + data_byte[5], false); //If you are using different sensors then you could make necessary calibration below //Before displaying the value. //you will get 1.29 by the following math (3.3/255) * 100 //i'm using 8bit adc (even though it's a 10bit i discarded 2 lsb bits) //I have no idea how to calibrate light intensity double temp; if((data_byte[3] == (byte)device_type.Triac_Device) || (data_byte[3] == (byte)device_type.Motor_Device)) temp = ((data_byte[7] * 1.29)) ; else temp = ((data_byte[7] * 1.29)) ; Sensors.Text ="PIR: " + data_byte[8] + ", " + "lIght Intensity:" + data_byte[6].ToString() + ", Temp: " + temp.ToString() + "'C"; //using temp variable for light intnsity control if (databyte[6] < 70) temp = 0; else temp = databyte[6] - 70; //Send intensity and buzzer on/off commands based light intensity and PIR sensors if ((bool)autocontrol.IsChecked) { byte[] bb = {0x00,0x00, 0x00 , 0x00 , 0x00 , 0x00 }; if (temp < 140) bb[0] = (byte)(temp); else bb[0] = 140; bb[1] = lastitem.device2_state; bb[2] = lastitem.device3_state; bb[3] = lastitem.device4_state; nrf_send_data(bb, lastitem.device_address, packet_type.data_packet); } break; case (byte)packet_type.remove_packet: //foreach (byte dev in data_byte) //check all list view items //{ //for duplicate entry // textBox.Text += dev.ToString(); //} int itemnumber = 0; foreach (dev_info dev in deviceList.Items) //check all list view items { itemnumber++; if (dev.device_address == data_byte[2]) { ///Accidental removal of motor controller devices may cause danger /// so motor controller boards cannot be removed once added /// you need to restart the application for removing motor controller devices if (dev.board_type == (byte)device_type.Motor_Device) { statusbar_string("Motor Device cannot be removed... ID:" + data_byte[1].ToString(), true); return; } deviceList.Items.Remove(dev); statusbar_string("item Removed... ID:" + data_byte[1].ToString(), false); return; } } statusbar_string("No Device found with ID:" + data_byte[1].ToString(), true); break; } }
/// <summary> /// Receive data and remote address here + pass to controls /// </summary> /// <param name="addrs"></param> /// <param name="msg_string"></param> private void do_job(string addrs, string msg_string, string port_, string local_addr_) { //do jobs here :D raspberry or windows //deviceList.Items.Add(addrs + ":" + data_rx); //list view and datagram test : DONE :) //to clear network log textblock //otherwise system may become slow due to large amount of log information if (netlog.Text.Length > 500) { netlog.Text = ""; } //add network activity/log info to textblock netlog.Text = "Message: " + msg_string + "\n" + addrs + ", Group: " + local_addr_ + ", Port: " + port_ + "\n" + netlog.Text; char[] chararray = msg_string.ToCharArray(); string n_id; byte n_deviceID, devicelist_index = 0; //dev_info object dev_info dev_class = new dev_info(); try { ///wrong length of device ID may cause exception ///extract device ID from received byte n_id = new String(new char[] { chararray[2], chararray[3] }); n_deviceID = byte.Parse(n_id); statusbar_string(n_id, true); } catch { statusbar_string("Unknown Network Packet/Command", true); return; } bool flag = false; ///match received id with devices present. if failed 'return' foreach (dev_info dev in deviceList.Items) { if (dev.device_address == n_deviceID) { flag = true; //device found dev_class = dev; break; } devicelist_index++; } if (!flag) { //No Device Found statusbar_string("Network Request Received, But wrong Device ID", true); return; } byte[] n_tx_packet = new byte[6]; if (chararray[0] == '!') //checks whether it is control packet or other message { ///this switch statemnt will be clear if you look at the Android program ///each charactor of chararray[1] represents a control function /// eg. on/off relay, buzzer, triac etc switch (chararray[1]) { case 'A': dev_class.device1_state = 1; break; case 'B': dev_class.device2_state = 1; break; case 'C': dev_class.device3_state = 1; break; case 'D': dev_class.device4_state = 1; break; case 'E': dev_class.device1_state = 0; break; case 'F': dev_class.device2_state = 0; break; case 'G': dev_class.device3_state = 0; break; case 'H': dev_class.device4_state = 0; break; case 'I': dev_class.device2_state = 1; break; case 'J': dev_class.device2_state = 0; break; case 'K': dev_class.device3_state = 1; break; case 'L': dev_class.device3_state = 0; break; case 'M': byte trigger_value = byte.Parse(new String(new char[] { chararray[4], chararray[5] })); trigger_value = (byte)((trigger_value * 140.0) / 99.0); dev_class.device1_state = trigger_value; break; case 'N': dev_class.device1_state = 1; dev_class.device2_state = 0; dev_class.device3_state = 1; dev_class.device4_state = 0; break; case 'O': dev_class.device1_state = 0; dev_class.device2_state = 1; dev_class.device3_state = 0; dev_class.device4_state = 1; break; case 'P': dev_class.device1_state = 1; dev_class.device2_state = 0; dev_class.device3_state = 0; dev_class.device4_state = 1; break; case 'Q': dev_class.device1_state = 0; dev_class.device2_state = 1; dev_class.device3_state = 1; dev_class.device4_state = 0; break; case 'R': dev_class.device1_state = 0; dev_class.device2_state = 0; dev_class.device3_state = 0; dev_class.device4_state = 0; break; default: return; } //construct data byte from dev_class //by this way we made switch statement much simple byte[] n_tx_byte = { dev_class.device1_state, dev_class.device2_state, dev_class.device3_state, dev_class.device4_state,dev_class.device4_state, 0x00 }; ///Transmit data byte nrf_send_data(n_tx_byte, dev_class.device_address, packet_type.data_packet); //Update chnages deviceList.Items[devicelist_index] = dev_class; } }
/* * nRF TX packet count != Raspbverryy Pi RX packet * Raspberry Pi receives an extra byte at the begining (STATUS Register of nRF module) * We don't need status register values in below function. * data_byte[0] can be discarded. * To know more about STATUS register and nRF module operation refer its datasheets */ public void data_received(byte[] data_byte) { last_received = data_byte; //Check Received Packet Type switch (data_byte[1]) { /*Device initialization packet * every nRF slave device need to send an "add me" packet before starting * to send an "add me" packet hold the "Switch 1(sw1)" during power on * this will add the device into the list view item. */ case (byte)packet_type.add_me_packet: //foreach (byte dev in data_byte) //check all list view items //{ //for duplicate entry // textBox.Text += dev.ToString(); //} foreach (dev_info dev in deviceList.Items) //check all list view items { //for duplicate entry if (dev.device_address == data_byte[2]) { statusbar_string("Duplicate item found...", true); return; } } statusbar_string("New Slave board found... ID:" + data_byte[2].ToString(), false); //Get Device Type from tha packet 3rd byte string list_device_string; if (data_byte[3] < device_type_string.Length) { list_device_string = device_type_string[data_byte[3]]; } else { list_device_string = "Unknown Type/New Device"; } //Add new item to the listview dev_info new_device = new dev_info(list_device_string); new_device.board_type = data_byte[3]; //3rd byte = Board type (Triac, Relay, Buzzer etc new_device.device_address = data_byte[2]; //2ndByte = nRF slave ID\ //Device 1 to 4 state (on/off, trigger angle), any number of relays/buzzers/sensors can be added (need to modify the code) //Either using multiple packets or extracting bits (On/Off states require only 1 bit, multible device states can be added in a single byte new_device.device1_state = data_byte[5]; new_device.device2_state = data_byte[6]; new_device.device3_state = data_byte[7]; new_device.device4_state = data_byte[8]; // statusbar_string(new_device.device1_state.ToString() // + new_device.device2_state.ToString() + new_device.device3_state.ToString() + // new_device.device4_state.ToString(), false); //Sensor readings (No sensor limitation, the code can be modified for upto n number of sensors new_device.sensor1_value = data_byte[9]; new_device.sensor2_value = data_byte[10]; //add to listview deviceList.Items.Add(new_device); break; /* * Data packet contains sensor informations, solid state relay(s) status, relay(s) status, buzzer on/of condition * and other slave specific informations. */ case (byte)packet_type.update_packet: statusbar_string("Update Packet Received..." + data_byte[5], false); //If you are using different sensors then you could make necessary calibration below //Before displaying the value. //you will get 1.29 by the following math (3.3/255) * 100 //i'm using 8bit adc (even though it's a 10bit i discarded 2 lsb bits) //I have no idea how to calibrate light intensity double temp; if ((data_byte[3] == (byte)device_type.Triac_Device) || (data_byte[3] == (byte)device_type.Motor_Device)) { temp = ((data_byte[7] * 1.29)); } else { temp = ((data_byte[7] * 1.29)); } Sensors.Text = "PIR: " + data_byte[8] + ", " + "lIght Intensity:" + data_byte[6].ToString() + ", Temp: " + temp.ToString() + "'C"; //using temp variable for light intnsity control if (databyte[6] < 70) { temp = 0; } else { temp = databyte[6] - 70; } //Send intensity and buzzer on/off commands based light intensity and PIR sensors if ((bool)autocontrol.IsChecked) { byte[] bb = { 0x00, 0x00, 0x00 , 0x00,0x00, 0x00 }; if (temp < 140) { bb[0] = (byte)(temp); } else { bb[0] = 140; } bb[1] = lastitem.device2_state; bb[2] = lastitem.device3_state; bb[3] = lastitem.device4_state; nrf_send_data(bb, lastitem.device_address, packet_type.data_packet); } break; case (byte)packet_type.remove_packet: //foreach (byte dev in data_byte) //check all list view items //{ //for duplicate entry // textBox.Text += dev.ToString(); //} int itemnumber = 0; foreach (dev_info dev in deviceList.Items) //check all list view items { itemnumber++; if (dev.device_address == data_byte[2]) { ///Accidental removal of motor controller devices may cause danger /// so motor controller boards cannot be removed once added /// you need to restart the application for removing motor controller devices if (dev.board_type == (byte)device_type.Motor_Device) { statusbar_string("Motor Device cannot be removed... ID:" + data_byte[1].ToString(), true); return; } deviceList.Items.Remove(dev); statusbar_string("item Removed... ID:" + data_byte[1].ToString(), false); return; } } statusbar_string("No Device found with ID:" + data_byte[1].ToString(), true); break; } }