I’m trying to control a Kopykake cake decorating turntable (http://www.kopykake.com/store/karousel-turntable-variable-speed-0-10rpm-t1000.html ) with Gadgeteer.
The actual project is more complex than the listing here (the rotation commands are gleaned from a web server, it can turn clockwise or anti-clockwise, …) but I’ve cut it right down to demonstrate my problem.
I have replaced the on/off switch of the rotating cake stand with one of the relays on the Seed relays module.
The following code works, using it I can turn the rotating stand on and off with a GHI Gadgeteer button module.
using Microsoft.SPOT;
using Gadgeteer.Modules.GHIElectronics;
namespace RelayACMainsTest
{
public partial class Program
{
private bool _turning;
private bool _Turning
{
get
{
return _turning;
}
set
{
if (value != _turning)
{
if (value)
{
_relays.Relay1 = true;
_turning = true;
}
else
{
_relays.Relay1 = false;
_turning = false;
}
Debug.Print("_relays.Relay1 = " + _relays.Relay1);
}
}
}
void ProgramStarted()
{
_button.ButtonReleased += ButtonReleased;
Debug.Print("Program Started");
}
void ButtonReleased(Button sender, Button.ButtonState state)
{
Debug.Print("ButtonReleased");
_Turning = !_Turning;
}
}
}
However when I add in the network connection and use it to poll for commands the button operating the turntable fails, and the debug print statements stop. Here’s the code with the network:
using System;
using System.Threading;
using GHI.Premium.Net;
using Gadgeteer.Networking;
using Microsoft.SPOT;
using Gadgeteer.Modules.GHIElectronics;
using GT = Gadgeteer;
namespace RelayACMainsTestWithWifi
{
public partial class Program
{
private const int _CommandScanPeriond = 500;
private const string _RequestString = "http://www.microsoft.com/";
private GT.Timer _commandRequestTimer;
private bool _turning;
private bool _Turning
{
get
{
return _turning;
}
set
{
if (value != _turning)
{
if (value)
{
_relays.Relay1 = true;
_turning = true;
}
else
{
_relays.Relay1 = false;
_turning = false;
}
Debug.Print("_relays.Relay1 = " + _relays.Relay1);
}
}
}
static Program()
{
Debug.Print("Startup pause.");
Thread.Sleep(1000);
}
void ProgramStarted()
{
_button.ButtonReleased += ButtonReleased;
_commandRequestTimer = new GT.Timer(_CommandScanPeriond, GT.Timer.BehaviorType.RunOnce);
_commandRequestTimer.Tick += CommandRequestTimerTick;
_wifiRS21.Interface.NetworkInterface.EnableDhcp();
_wifiRS21.Interface.WirelessConnectivityChanged += WirelessConnectivityChanged;
_wifiRS21.Interface.NetworkAddressChanged += NetworkAddressChanged;
Debug.Print("Starting wifi");
var scanResults = _wifiRS21.Interface.Scan("HTC Windows Phone 8X by HTC8858");
if (scanResults != null && scanResults.Length > 0)
{
Debug.Print("Joining HTC Windows Phone 8X by HTC8858");
_wifiRS21.Interface.Join(scanResults[0], "****");
}
else
{
Debug.Print("Network HTC Windows Phone 8X by HTC8858 not found");
}
Debug.Print("Program Started");
}
void ButtonReleased(Button sender, Button.ButtonState state)
{
Debug.Print("ButtonReleased");
_Turning = !_Turning;
}
private void NetworkAddressChanged(object sender, EventArgs eventArgs)
{
Debug.Print("NetworkAddressChanged to " + _wifiRS21.Interface.NetworkInterface.IPAddress);
_commandRequestTimer.Start();
}
private void WirelessConnectivityChanged(object sender, WiFiRS9110.WirelessConnectivityEventArgs e)
{
Debug.Print("WirelessConnectivityChanged");
if (e.IsConnected)
{
Debug.Print("Wifi connected, with IP address " + _wifiRS21.Interface.NetworkInterface.IPAddress);
if (_wifiRS21.Interface.NetworkInterface.IPAddress == "0.0.0.0")
{
Debug.Print("Waiting for DHCP address assignment");
}
else
{
_commandRequestTimer.Start();
}
}
else
{
Debug.Print("Wifi Disconnected");
}
}
private void CommandRequestTimerTick(GT.Timer timer)
{
var request = HttpHelper.CreateHttpGetRequest(_RequestString);
request.ResponseReceived += ResponseReceived;
try
{
request.SendRequest();
Debug.Print("Sent request " + _RequestString);
}
catch (Exception)
{
Debug.Print("Socket Exception");
}
}
private void ResponseReceived(HttpRequest sender, HttpResponse response)
{
Debug.Print("ResponseReceived, response.Text.Length = " + response.Text.Length);
_commandRequestTimer.Start();
}
}
}
Here is an example of the output window in Visual Studio up to the point it stopped listing and the button stopped responding a few button presses later.
The relay interrupts the live AC power on route from the plug to the transformer in the cake turntable (in place of the original on/off switch). If the turntable is unplugged the Gadgeteer code works fine (i.e. I can see the relay switch LED and hear the relay switch and the debug prints continue to be printed in the output window.) But powered up it fails and stops responding after a few button presses. Any ideas why?