Preet's Relay Controller Project

From Embedded Systems Learning Academy
Revision as of 21:17, 26 September 2012 by Preet (talk | contribs) (Implementation)

Jump to: navigation, search

This article is under construction. You may have found it because you're lucky but it's not linked anywhere, and is not complete.

Overview

The motivation of this project is to control a relay board over the network (or even over the internet). More specifically, this project is an application of the relay control in the form of a sprinkler control system (SCS). Overall, the following functionality is targeted:

  • Schedule a run-time
  • Select days to water
  • Select run-times of each sprinkler
  • Turn System On or Off
  • Set 'Rainy days' (number of days to skip a scheduled run)

The extended functionality is the Web GUI*. The Web interface (WI) not only allows to control SCS, but also view the logs in a graphical way. For example, WI can request daily water-times from the SCS and plot a bar-graph to show the user the water history.



Features

Commandline Interface

The command-line interface is an intuitive interface that is a self-documented piece of SCS. To begin using the interface, all it takes is to connect SCS to a USB port on a computer, open a COM Port at 38400bps and type "help". Below are some screenshots of the command-line interface. TODO: Attach screenshots

SCS specific commands

To begin, "relay help" command can be issued to see the command-set. To schedule the SCS to water plants everyday, the following commands can be issued:

  • relay schedule 18:00
  • relay waterday everyday
  • relay minutes 1 1 1 1 2 2 2 2 3 3 3
  • relay power on

TCP/IP Commands

The commandline interface is the same over TCP/IP. All it takes is to open up a TCP/IP port of SCS and use the same commands. SCS uses fixed port number: 5555 of TCP/IP. To open command-line interface over TCP/IP, SCS IP needs to be first determined and then the Hercules* utility can be used to interface with SCS. TODO: Attach picture

Web-based Controls

SCS exposes its interface over a TCP/IP connection. It provides a simple, human readable command-line interface. This command-line interface is encapsulated in the WI that provides an even simpler interface to an end-user. The idea is that a person without any knowledge of the controller should be able to interact with the system.

Logs

There are several forms of logs that get saved in the file system accessible by FAT* library. The SCS software saves the log in the on-board 1Mb SPI flash memory. This provides capability to save data without relying on an external SD card.

Hourly Sensor Logs

Every hour, temperature and light sensor value is logged into a file in the format:
9/16/2012, 14:00, 76.25, 100
9/16/2012, 15:00, 77.25, 150
9/16/2012, 16:00, 78.75, 130

Nightly Logs

Every night, a log message saves the number of minutes SCS turned on relays in a 24-hour period. This serves as a daily log of the number of minutes of plants were watered. 9/16/2012, 25
9/17/2012, 0
9/18/2012, 25

Debugging Logs

Debugging and informational logs also get saved on demand or if the log-buffer becomes full. This can open the gate for long-term testing and debugging to ensure that the board is operating as expected.



Implementation

Hardware

In the hardware level, the following components form SCS:

  • SJ-One Board
  • RN-XV Wifi module
  • Relays attached to GPIOs

The wifi module is a simple UART based TCP/IP solution. The SCS software initializes RN-XV, sets up a TCP/IP port to listen to, and simply waits for data or command from UART and responds back to the command.


Embedded Software

FreeRTOS template project was used to provide the software framework. This software framework was extended by adding more command-line handlers.

Tasks

Three tasks exist in the software:

  1. Terminal Task
    Handles all command inputs and routing the commands to appropriate command handlers
    SCS is configured through commands by this task
  2. Wifi Task
    Associates RN-XV to Wifi Network
    Monitors disconnect and attempts to reconnect
  3. SCS Task
    Handles servicing relays every minute
    Logs data on hourly and nightly basis

Schedule Class

There were only two modifications to the SJ-One Software Framework:

  • Additional commands
  • Scheduler class

The scheduler class is the primary modification to the SJ-One Software Framework. Note that this has nothing to do with Operating System scheduling, but it is the scheduling of the relays. Here are some useful members of this C++ class:

  • Turn On/Off Scheduler
  • Get/Set Scheduled Time
  • Get/Set Relay timings
  • Get/Set Waterdays

The modifications and status was handled through commandline commands. The schedule was saved in a binary file on the on-board SPI Flash. To minimize the number of times this file was written (avoid flash wear), a five minute delay timer was implemented. Whenever the schedule was changed, the schedule was marked dirty. After detecting no schedule change for five minutes, the schedule from RAM was saved to Flash memory.


RN-XV Initialization

The RN-XV Wifi module is initialized to obtain an IP via DHCP, and then act as a TCP/IP server on port 5555. The basic operation of RN-XV is as follows:

  • RN-XV boots with UART speed of 9600bps
  • RN-XV commands can be issued in Command Mode
    To enter command mode, send $$$
    No characters should precede or follow $$$ for 250ms.
  • To exit command mode, issue exit\r\n
  • If RN-XV is not in command mode, all data received over UART is treated as data for an established connection.

Setup IP & Associate

RN-XV can be commanded through it's UART interface to initialize it. For this project, WPA2 security is used which is an optional setting.

  • Enter command mode: $$$
  • Enable DHCP: set ip dhcp 1
  • Set SSID: set wlan ssid <Your_SSID>
  • Set WPA2 Security: set wlan auth 4
  • Set WPA2 Passphrase: set wlan phrase <My_Sec_Phrase>
  • Set all channels:
    set wlan channel 0
    set wlan mask 0x1FFF

Setup Parameters

(Continued from previous section):

  • No 'Greeting' messages upon connection:
    set comm close 0
    set comm open 0
    set comm remote 0
  • Setup Flush Parameters:
    Disable character flush: set comm match 0
    Set Flush size: set comm size 1024

To optimize data transfer, a character triggering flush is disabled and the flush size is set to 1024 bytes. When RN-XV receives no data transmission within 10ms, any outstanding data is sent from its RAM to the network (default setting).

Setup TCP/IP Server

(Continued from previous section)

  • Use TCP/IP Protocol: set ip protocol 2
  • Use your port: set ip local port 5555
  • Save and reboot:
    save
    reboot

Wait about 1-5 seconds for reboot and association to finish. In case your RN-XV doesn't connect to your wireless network, enter command mode and command it to join your SSID:

  • join <Your_SSID>

After performing these steps, RN-XV should have obtained an IP address and it is ready to communicate over TCP/IP port 5555.


Front-End Web Interface

While the SCS software can interact with a command-line interface, WI encapsulates commands with simple buttons. This provides a non-technical person the capability to control the system.



Testing

The testing was performed manually by verifying the command and expected output. The steps were:

  • Program the software
  • Test each command through UART interface
  • Test each command through Wifi interface
  • Test Web Interface

Test Cases

Simple Test Case

  • Set a schedule and run-time in minutes.
  • Enable each day as a 'waterday'
  • Wait until the system time reaches scheduled time
  • Check 'relay status' periodically to verify the results
  • Check the log file to make sure relays turned on and turned off as appropriate

Corner Case Test Cases

  • Repeat the Simple Test Case with the following:
    • Rainy days set to non-zero
    • Weekday as not a 'waterday'
    • SCS set to OFF: No system activity should exist

Logfiles test case

  • Test to make sure hourly and nightly logs are written correctly
    Verify by typing in read hourly.csv -print command

Web Control Tests

  • Repeat the Simple Test Case but through web controls.


Conclusion

A very low powered relay system was developed that has capability to be controlled over the Internet. This solution is a completely encapsulated system that can be controlled by a USB/UART interface, or over a TCP/IP interface.

The RN-XV Wifi module is an exceptional piece of hardware at a relatively great price ($35 per unit). Although I've done projects using SPI Ethernet module (by Microchip), it required integration of uIP (from Adam Dunkels). The advantage of using uIP is that you can support more than one TCP/IP or UDP port communication but the disadvantage is the huge code base that you have to integrate and test. Furthermore, RN-XV is a wireless solution which yields more flexibility.

During the project development, I enhanced the project by being able to transfer files through a command-line command. This enhancement allowed me to transfer new software binaries to the board, and then program the binary to the processor's flash memory. In other words, I was able to reprogram the board over-the-air saving enormous amount of time during development and testing while the project was working as a live-unit in the house garage.