Difference between revisions of "F18: Wireless sensor network"

From Embedded Systems Learning Academy
Jump to: navigation, search
(References Used)
(Wireless Rx Implementation)
Line 586: Line 586:
  
 
====Wireless Rx Implementation====
 
====Wireless Rx Implementation====
[[File:Master_wireless_flow.jpeg|750px|thumb|center|Wireless receiver flow]]
+
[[File:Master_wireless_flow.jpeg|500px|thumb|center|Wireless receiver flow]]
  
 
Nordic Wireless Rx and Tx implementation is done using Preet's Low Powered Mesh Network stack.Each packet is sent via an existing route, and if route has changed, a new route is automatically discovered using a special retry packet.However for our application, all the slaves nodes directly transmit data to the master node so there won't be any other nodes in the path. The minimum payload is 9 bytes, of which, 8 bytes will be the mesh header overhead which contains the network source and destination information, along with packet type and hop count information.
 
Nordic Wireless Rx and Tx implementation is done using Preet's Low Powered Mesh Network stack.Each packet is sent via an existing route, and if route has changed, a new route is automatically discovered using a special retry packet.However for our application, all the slaves nodes directly transmit data to the master node so there won't be any other nodes in the path. The minimum payload is 9 bytes, of which, 8 bytes will be the mesh header overhead which contains the network source and destination information, along with packet type and hop count information.

Revision as of 00:47, 20 December 2018

Wireless Sensor Network

SensorNetwork MasterAssy front.jpeg

Wireless Sensor Networks project displays data received from various nodes on a 16*32 LED Matrix which is connected with the Master node (SJOne Board).
Each node measure various environmental parameters it's surrounding and send those data to master in form of packets.

Abstract

A wireless sensor network (WSN) consists of collection of sensor nodes. Each individual node consists of MCU with on-board sensors and Nordic wireless mesh for communication, which are distributed to monitor physical or environmental conditions, such as temperature, ambient light, humidity etc. The SJ-One board contains an onboard light intensity sensor, and a custom PCB for the project has a combined Temperature, Pressure, and Humidity sensor and an air quality sensor. These sensor data are transmitted to a network coordinator which is the controls the wireless mesh network. The advantage of a mesh configuration is that the data flow is uninterrupted in the network even if one of the nodes fails. The coordinator board colates the data from all nodes, and displays it on a 16x32 LED matrix.

Objectives & Introduction

This project was a culmination of a semester of learning about FreeRTOS and embedded software. Our objectives were to apply what we had learned, while also using our critical thinking and problem solving to figure out how to implement parts of our project that we didn't yet know how to do, such as interfacing with an LED Matrix display.

Team Members & Responsibilities

  • Halak Vyas
    • Temperature, Pressure and Humidity Sensor Driver (BME280), RTC Config (primary), Wireless driver (secondary), .
  • Jay Parsana
    • Display driver (primary), Sensor drivers
  • Prashant Gandhi
    • PCB Assembly, PCB Design (review), Sensor drivers
  • Tristan French
    • Team lead, PCB Design (primary), PCB Assembly (secondary), Display driver (secondary)
  • Vatsal Makani
    • Wireless driver (Primary), PCB design (secondary), Wiki lead

Schedule

Week# Date Task Actual
1 10/9 Tristan
  • Order LED matrix

Everyone

  • Assign team roles
Tristan
  • Matrix ordered (10/1), Received (10/14)

Everyone

  • Team roles assigned (10/16)
2 10/16 Everyone
  • Create wiki schedule

Everyone

  • Initial schedule created(10/16).

Unforeseen issues

  • Issues with wiki login credentials (resolved)
  • Discovered antennas that we have do not match the connector, must order new ones (resolved)
3 10/23 Vatsal
  • Order antenna adapters

Vatsal

  • adapters ordered
4 10/30 Halak
  • RTC programming started

Tristan

  • PCB design requirements complete
Halak
  • RTC programming started

Tristan

  • PCB Design requirements delayed, still finalizing sensors.
5 11/6 Halak
  • task

Jay

  • Acquire power supply for LED matrix, review Adafruit library

Prashant

  • Review and update schedule for your subsystem.

Tristan

  • PCB design complete and ordered (including components)

Vatsal

  • Review nordic wireless API, and plan code
Halak
  • Completed?

Jay

  • Acquired (Tristan already had a power supply)

Prashant

  • Schedule updated

Tristan

  • Poor documentation on MQ-135, waiting for parts before releasing PCB. Design 95% complete

Vatsal

  • Investigated, have questions for Preet next week
6 11/13 Halak
  • RTC programming done

Jay

  • Initial LED matrix testing

Prashant

  • Review PCB design

Tristan

  • PCB and components Ordered

Vatsal

  • Basic wireless driver (send/receive a byte)
Halak
  • RTC code complete, need battery to test.

Jay

  • LED matrix initial test completed (able to light desired LED).

Prashant

  • Reviewed and approved

Tristan

  • PCB ordered (11/10) arriving 11/15.

Vatsal

  • Wireless not functioning yet, Prashant and Halak to provide support.
7 11/20 Halak
  • Write initial driver to read BME280 sensor values

Jay

  • Display symbols on matrix

Prashant

  • Write MQ-135 sensor driver and test

Tristan

  • PCB Soldered

Vatsal

  • Wireless driver send/receive sensor data
Halak
  • Able to read raw data values

Jay

  • Displaying letters and numbers

Prashant

  • Code written to read MQ-135, need to test

Tristan

  • One PCB soldered and initial test completed

Vatsal

  • Initial transmitter code complete, receiver mostly complete, troubleshooting
8 11/27 Halak
  • Finish BME280 Driver

Jay

  • Parse and display ASCII sensor data

Prashant

  • Finish MQ-135 driver
  • Solder remaining PCBs

Tristan

  • Solder remaining PCBs

Vatsal

  • Wireless driver code to Send and Receive Sensor Data
Halak
  • Driver working, beginning to interface with wireless

Jay

  • Displaying data values (numerical inputs, don't need ASCII)

Prashant

  • Driver working, beginning to interface with wireless
  • Scheduling conflict, planning to finish soldering Fri 11/30

Tristan

  • Scheduling conflict, planning to finish soldering Fri 11/30

Vatsal

  • Wireless test is functional, beginning to integrate sensor readings
9 12/4 Halak
  • Make BME280 data accessible by wireless module

Jay

  • Read and display Sensor display from wireless, and switch between display devices

Prashant

  • Make MQ-135 data accessible by wireless module

Tristan

  • Support integration

Vatsal

  • Make received sensor data available to display driver

Everyone

  • Integration of all drivers/modules, All sensor readings working
Halak
  • Data integrated with wireless communication

Jay

  • Having issues with connection to LED matrix, possible bad matrix (investigating)

Prashant

  • Data integrated with wireless communication

Tristan

  • Data being sent over wireless.

Vatsal

  • Sending and receiving all sensor data, waiting on LED matrix issue

Everyone

  • Partially complete, delayed by issues with LED matrix
10 12/11 Everyone
  • Debugging any major issues, Project complete
Everyone
  • All core code functionality is implemented, still need to clean up and finalize details of functionality.
11 12/19 Everyone
  • Final code cleanup and testing.
  • Project Demo and Review day
Everyone
  • Completed?

Parts List & Cost

Top level BoM

Item# Part Source Quantity Unit

Cost($)

Total

Cost($)

1 SJ One Board Preet 5 80.00 400.00
2 Adafruit RGB LED Matrix LED Matrix 1 24.95 24.95
3 LED matrix Connector board LED Matrix 1 1.00 1.00
4 Led Matrix Power Adapter N/A (Stock) 1 N/A 0.00
5 Air Quality Sensor Willwin 4 4.50 18.00
6 Custom Sensor PCB Assembly In House 4 14.72 58.87
7 CR1225 RTC Battries Amazon 10 0.7 6.99

Total (minus SJ One cost): $109.82

Custom PCB BoM

Item# Part Source Quantity Unit

Cost($)

Total

Cost($)

1 Custom Sensor PCB JLCPCB 4 0.20 0.80
2 SMD P, T, H sensor Digikey 1 6.628 6.628
3 USB mini-B SMD receptacle Digikey 1 0.87 0.87
4 2-position screw terminal, .1in pitch Digikey 1 0.595 0.595
5 2-pin male header, .1in pitch Digikey 2 0.099 0.198
6 4-pin female header, .1in pitch Digikey 1 0.45 0.45
7 17-pin female header w/ long pins, .1in pitch Digikey 2 1.719 3.438
8 Red SMD LED, 0603 Digikey 2 N/A 0.00
9 100 Ohm SMD resistor, 0603 Digikey 1 0.30 0.30
10 300 Ohm SMD resistor, 0603 Digikey 1 0.066 0.066
11 2.2 kOhm SMD resistor, 0603 Digikey 2 0.024 0.048
12 3.3 kOhm SMD resistor, 0603 Digikey 2 0.024 0.048
13 4.7 kOhm SMD resistor, 0603 Digikey 4 0.30 1.20
14 0.1uF ceramic capacitor, 0603 Digikey 2 0.038 0.076
15 SMD Air Quality sensor Digikey 1 (optional) 12.88 0.00
16 2-pin jumper, .1in pitch Digikey 1 (optional) 0.45 0.00

Total: $14.72

Design & Implementation

Wireless Sensor Network

The design section can go over your hardware and software design. Organize this section using sub-sections that go over your design and implementation.

Hardware Design

The project is based around the SJ one board, which uses an LPC1758 Arm Core M3 microcontroller. The system design consists of two main devices: a wireless master with a display and a wireless sensor node (of which there are several).

Wireless Master

The master board's role is to drive the LED matrix, and receive and log the sensor data. The majority of the functionality of the master can be handled with the default hardware on the SJ One board. The only addition necessary was an antenna for the wireless chip, and a custom connector board.

The custom connector board mounts to the 17x2 header on the SJ One board, and the pins were routed to the appropriate pins of a 2x8 pin header, which the matrix plugged into.

LED Matrix connector board

Wireless Slave Node

Each slave node has to interface with each of its sensors and send the data wirelessly. The sensors were interfaced using a custom PCB shown below.

The PCB accomplishes the following:

  1. Accept USB mini power
  2. Provide 5V output to power SJ One
  3. Robust and compact mounting to SJ One board
  4. Provide I2C connection for BME280
  5. Mounting header for MQ-135
  6. Voltage divider to convert 5V analog output from MQ-135 to 3.3V
  7. Leave unused SJ One pins available for other uses (accessible through extra long header pins)

The PCB design was completed in Eagle

PCB Schematic
Sensor PCB Layout, top
Sensor PCB Layout, bottom
Top of Sensor PCB
Bottom of Sensor PCB

Hardware Interface

The system utilizes several different interfaces to achieve its purpose. Specifically, it uses a 2.4GHz Nordic Wireless protocol, I2C to read sensors, an ADC to read sensors, and SPI to log data on an SD card.

LED Matrix

The LED matrix is controlled through a 12-pin header consisting of the following pins:

  1. An output enable (oe) which turns the LEDs on when pulled low
  2. Latch pin (lat) which prevents data being shifted into the shift registers when set high
  3. Clock pin (clk) which triggers a shift on the shift registers
  4. Three Address pins (rA, rB, and rC) to select the row
  5. Three RGB pins for the top half (R1, G1, and B1)
  6. Three RGB pins for the bottom half (R2, G2, and B2)

Nordic Wireless

Nordic SPI Hardware Interface

The Nordic nRF24L01+ wireless sensor is a highly integrated, ultra low power (ULP) 2Mbps RF transceiver IC for the 2.4GHz ISM (Industrial, Scientific and Medical) band. The nRF24L01+ is designed for operation in the frequency band of 2.400 - 2.483GHz. The high air data rate combined with two power saving modes make the nRF24L01+ very suitable for ultra-low power designs. This chip is interfaced with LPC1758 microcontroller of SJOne Board using SPI pins (MOSI, MISO, CLK, CS). It is the on-board chip in SJOne Board, so no breakout board connections are required for this project. We had to attach a RP-SMA Adapter as well as antenna to communicate between the multiple nodes and the master board.

Sensors

Light Sensor

Light Sensor is an on-board sensor in SJOne Board which interfaces with LPC1758 microcontroller at Port 0, pin 25 analog signal. This sensor shall give the raw value to the controller which will be processed by 12-bit microcontroller ADC. The output of the sensor is shown in a form of 'bulb' indication in LED Matrix

Temperature, Pressure & Humidity (BME280)
Temperature, Pressure and Humidity Sensor

BME280 supports both SPI and I2C bus communication. In our wireless mesh project we have used I2C bus over SPI as I2C can read and write registers very easily in compare to SPI and Nordic Wireless is already using SPI bus.Here we are using BME280 sensor of Bosch which has a facility to measure Temperature, Pressure and Humidity. It uses 3.6 microAmps of current at 1Hz and requires 3.3V of voltage which SJOne provides it.

Operating Range : Temperature (-40C to +85C), Pressure (300 to 1100hPa), Humidity (0% to 100%) with +- 1% of tolerance.

The sensor facilitates the user with various modes of operation which can be selected as per preferred environmental conditions. There are basically 3 modes for which range of IIR filter is varied. Those modes are - Indoor Navigation, Weather Monitoring and Gaming Mode. We have facility to switch between those modes, but here for our project we are using Indoor mode as our project is designed to test laboratory environment.

Air Quality Sensor
MQ135 Air Quality Sensor
Voltage divider

MQ135 is used for air quality measurement in this project. MQ135 is a low cost semiconductor sensor which can detect Carbon-Dioxide(CO2), Benzene, Smoke, Ammonia(NH3), Mono-nitrogen oxide. Sno2 is used as sensitive material in this sensor and it's conductivity is low in clean air. As the concentration of these sensing gases increases, sensor conductivity also increases. This module can give both analog and digital outputs. For this project we are using analog output. Analog output can go upto 5V, but our SJ-One board can support maximum 3.3V as input, so we need voltage divider circuit which can convert 5V output from module to 3.3V. As we are using analog pin output from module we will configure ADC driver of SJ-One board to read that ADC value and calculate ppm for CO2. Note that in this project we will calibrate our value such that we will get CO2 ppm.

Software Design

Master node with LED Matrix

Master node with LED Matrix

General Layout

We basically made a bit pattern on the matrix for the 4 screens and each of them displays:

Screen# Row (0:7) Row (8:15)
1 Temperature Humidity
2 Temperature(Sun) Temperature(Sun)
3 Pressure Air Quality
4 Light bulb with bars Light bulb with bars
Screen 1
Screen 2
Screen 3
|
Screen 4

It’s the layout of how the display would look for all the screens. We made use of a lookup table for units tens and hundreds positions to update the sensor value on the led matrix. For the LED matrix control signals, as mentioned above in the hardware part, we are making use of SJOne boards 12 GPIO pins.

MatrixTask:

This task that will perform functions like initializing these GPIO pins to output. Here we are scanning the led matrix at a rate of about 500Hz. We have a row_selection function that will go through each and every row one by one. The OE(Output Enable) will be set to high to keep the display off while the data is being buffered to the matrix. We have an ev_switch function inside the matrix task that will buffer the data along with different colors for all the different sensors. Also the ev_switch function when displaying the temperature will switch to temp_color task which will choose different colors for various temperature ranges. The data is being buffered with a duty cycle just like the PWM to control the brightness of the matrix. After pushing the data on to a single row we turn the OElow to turn the display ON and then set the LAT(Latch) high to mark the end of the data for the row.

SwitcherTask:

Its has a medium priority and it will cycle between four screens for about 2 seconds for each screen thereby blocking this task for 2 seconds. We have 2 pointers top and bottom that will fetch the layout from the lookup table and based on the value of the switcher variable it will update layout that it has to display. If the value of the switcher variable is 1, top and bottom will fetch temperature and humidity layout for the top and the bottom portion of LED Matrix respectively.

Matrix_Value_Update_Task

Here we have a matrix_value_update function that will parse the sensor values received from the nodes using value_extractor function and assign it to a values struct object which has 0-9 value for the struct members units, tens and hundreds. These values are then passed to a lookup_switch function which will lookup a matrix of 5x3 that matches the 0-9 digit value and assign it to a pointer. The data indicated by the pointer will overwrite the general layout for each sensor value thereby updating the sensor values on the LED Matrix. The switching between the nodes has been implemented in this task. We scan the switches and based on which switch is pressed we display the respective sensor values.

Pseudo-code

  • Initialize GPIO pin as output.
  • Set OE(Output Enable) to high and LAT(Latch) to low.
  • Select row to which the data will be shifted.
  • Enter the RBG data for a single pixel.
  • Assert the clock pin to high and then set it to low to shift the data in the pixel.
  • Deassert the clock pin.
  • DO this for 32 pixels and then set the LAT to high to indicate end of row and set OE low to display the data.
  • Introduce some delay which will be proportional to the ON time of the led i.e. set the duty cycle.
           for (int j = 0; j < no_of_rows; j++) {
               lat.setLow();
               oe.setHigh();
               row_selection(i);
               for (int j = 0; j < no_of_pixel_on_each_row; j++) {
                   input_data(i, j);
                   clk.setHigh();
                   clk.setLow();
               }
               lat.setHigh();
               oe.setLow();
               delay_us(on_time);
           }

The image below shows a logic analyzer capture of the display of one row of the matrix.

Logic Analyzer of LED matrix

Wireless Rx Implementation

Wireless receiver flow

Nordic Wireless Rx and Tx implementation is done using Preet's Low Powered Mesh Network stack.Each packet is sent via an existing route, and if route has changed, a new route is automatically discovered using a special retry packet.However for our application, all the slaves nodes directly transmit data to the master node so there won't be any other nodes in the path. The minimum payload is 9 bytes, of which, 8 bytes will be the mesh header overhead which contains the network source and destination information, along with packet type and hop count information.

For example, if the payload size is 32 bytes, then 8 bytes are used by the network header and 24 bytes are free to be used for transmitting the data.The mesh_packet_t is a structure that is used to transmit the data to understand the data of the wireless packet. It has 4 struct variables.

  • nwk : Packet network address
  • mac : packet physical address
  • info:Packet header
  • Data:Type of data to be sent which is 24 bytes.

Slave Node (Sensor reading)

Slave Node Block Diagram
Slave Node

This section includes implementation and calculations for all sensors(Temperature,Pressure,Humidity,Air Quality,Light) implemented at the software design in the slave node. These sensors reads the data and sends the data to SJOne board which processes the data and performs the desired calibrations. These calibrations are required to convert the Raw value to a readable value. These sensor readings are then combined together to form a single packet and transmitted wirelessly to master node using nordic nRF24L01+ chip.

Pseudo-code

  • Initialize ADC
  • Initialize BME280:
  • Create wireless transmit task
  • Start task scheduler
  • Start wireless transmit task
  • Initialize wireless parameters
  • Initialize Air quality sensor variables
  • Start while 1 loop
  • Start Calibrating ADC readings according to atmosphere CO2 value
  • Calculation:
    MQ_135 = adc0_get_reading(3);
   __resistance=((4095./(float)MQ_135) - 1.);
   temp = __resistance/33;
   air_quality = (PARA*pow(temp,-PARB));
  • Get temperature data in form of float from the sensor by reading 0xFA through 0xFC and using it to calculate raw temperature data.
  • Raw data is converted into degree C using some mathematical calculation as shown below
   temp = (temp * 5 + 128) >> 8;
   tempf = (float)temp;
   return (tempf/100.0f);
  • Get pressure data in form of float from the sensor by reading 0xF7 through 0xF9 and combining it to get raw pressure data.
  • This raw pressure data is converted into actual pressure data in form of kPa.
   press = (((1048576 - press_raw) - (var2 >> 12))) * 3125;
   var1 = ((int32_t)dig_P9 * ((int32_t)(((press >> 3) * (press >> 3)) >> 13))) >> 12;
   var2 = (((int32_t)(press >> 2)) * (int32_t)dig_P8) >> 13;
   press = (press + ((var1 + var2 + dig_P7) >> 4));
   return (pressf/1000.0f);
  • Get humidity data in form of float from the sensor by reading 0xFD through 0xFF and combining it to get raw humidity data.
  • This raw humidity data is converted into real value in percentage format.
   v_x1 = (v_x1 > 419430400 ? 419430400 : v_x1);
   humf = (float)(v_x1 >> 12);
   return (humf/1024.0f);

Wireless Tx Implementation

The calibrated sensor values are stored into a single int array of size 5 to store all 5 sensor readings. The following function "wireless_send()" is responsible to send the packet wherein the address of the destination node, type of mesh protocol used, pointer to the data to send, length of the data to send and number of hops is passed as a parameter. It returns true if packet is sent successfully. For our application, we are passing the below parameters to the wireless send()

  • Our Master LED Matrix node address is selected as 5
  • The mesh protocol used is mesh no-acknowledgement
  • We will send the int sensor array along with the size.
  • For our application, since all the slave nodes directly send data to master node, there shall be no intermediate node, hence the max_hops is taken as 0.

Below is the function:

 wireless_send(addr, mesh_pkt_nack, &sensor_data, sizeof(sensor_data), max_hops);

Testing & Technical Challenges

Nordic Wireless

Merging the sensor data into a single packet and transmitting the packet at Tx section. Incorrect values received at the Rx section with significant delay. Ensuring the receipt of packets from the correct node without data corruption.

Sensors

Air quality sensor calibration

To get accurate and stable data, we need to keep MQ135 constantly plug in with power supply for 12 to 24 hours and then we can calibrate the sensor reading. Also every time when we remove power supply we need to reset calibration factor.

LED Matrix

Unstable/inconsistent display on LED matrix. Suspect it is due to high frequency and poor connections with jumper cables, planning to make simple header breakout for more robust connection.

  • Header breakout did not help, investigating possibility of an issue with the matrix itself.
  • Issue resolved. The breakout board did end up fixing the connection issue, but an erroneous line of code which set the latch too early was the main problem.

Synchronizing all the task after integrating the wireless part was quite a challenge.

  • We were able synchronize the tasks pretty well by changing the priority and eliminating certain delays.

The data logger task gave a blackout while the sensor data was being displayed

RTC

Time just kept resetting on board reset, even after setting the time. We figured out a bug in implementation, where we had to store some value in "day of year" register to satisfy the if condition that goes to default if of register does shows any value.

Conclusion

Conclude your project here. You can recap your testing and problems. You should address the "so what" part here to indicate what you ultimately learnt from this project. How has this project increased your knowledge?

Project Video

Demo

Project Source Code

References

Acknowledgement

We would like to thank Prof. Preetpal Kang for teaching us this course because of which we became familiar with driver development, the knowledge of which helped us in developing this project. We would also like to thank him for motivating and supporting us during the whole journey. We would also like to thank our classmates and ISA team who helped us to solve few bugs.

References Used

https://hackaday.io/project/3475-sniffing-trinket/log/12363-mq135-arduino-library
https://www.engineeringtoolbox.com/co2-comfort-level-d_1024.html