Relay Controller
This documentation includes installation guidelines and sample code for your hardware.
RelayController library is responsible for controlling the solid state relays.
To use the library, add the following using statement to the top of your C# code.
using TurtaIoTHAT;
Then, create an instance of the RelayController class.
RelayController relayController
Now you're ready to access the library by calling the relayController instance.
Initiates the solid state relays.
RelayController()
Parameters
None
Controls the relay state.
void SetRelay(int ch, bool state)
Parameters
Int ch: Relay channel. 1 or 2. Bool st: Relay state. True for enable, false for disable. Returns
None
Reads the relay state.
bool ReadRelayState(int ch)
Parameters
Int ch: Relay channel. 1 or 2. Returns
Bool: Relay state. True if relay is on, false if relay is off.
To clean up the resources used by the class, call the Dispose() method it when the application is closing.
void Dispose()
This method cleans the resources used by the class.
You can copy the example code from https://github.com/Turta-io/IoTHAT/tree/master/Samples/Windows10IoTCore address. There is one example of this library.
This application demonstrates turning relays on and off.
To run the example:
- 1.Open the sample project you downloaded.
- 2.Make sure "ARM" is selected from the solution platforms selection.
- 3.Deploy the sample to the Raspberry Pi using the "F5" key.
Sample Code
MainPage.xaml.cs
using Windows.UI.Xaml.Controls;
using System.Threading;
using TurtaIoTHAT;
namespace RelayControllerSampleApp
{
public sealed partial class MainPage : Page
{
// Relay Controller
static RelayController relayController;
// Relay Timer
Timer relayTimer;
public MainPage()
{
this.InitializeComponent();
// Initialize Relay Controller and timer
Initialize();
}
private void Initialize()
{
// Initialize Relay Controller
relayController = new RelayController();
// Configure timer to 2000ms delayed start and 10000ms interval
relayTimer = new Timer(new TimerCallback(RelayTimerTick), null, 2000, 10000);
}
private static void RelayTimerTick(object state)
{
// Set Relay 1's state
relayController.SetRelay(1, !relayController.ReadRelayState(1));
// Set Relay 2's state
relayController.SetRelay(2, !relayController.ReadRelayState(2));
}
}
}
Result
When you run the sample, it turns on and off the relays repeatedly.
To stop the application, use the "Stop" button on the Visual Studio.
Last modified 4yr ago