Main Site Documentation

Controling a cake decorating turntable with a relay - WiFi problems


#1

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?


#2

Does it matter if you use other (than www.microsoft.com) web site?


#3

The one thing I don’t quite get is why things work when not connected to the relay’s downstream feed. That’s often a sign that there’s a power, ground, interference type issue at play. But I assume the only difference is when you don’t have a wireless connection, which is unlikely to physically change this.

Does the behaviour change if you don’t connect to wifi? Leave your 8X off and see what happens for longer than the usual failure time.


#4

https://www.ghielectronics.com/community/forum/topic?id=13132&page=1
This thread might be of interest where a capacitor is added to an inductive load to reduce EMI


#5

Thanks all.

Valentin — no, the real code uses my own WCF server on Azure and parses the resulting JSON, but I swapped it to http://www.microsoft.com/ in this test code to try to keep it as generic and short as I could.

Brett — I’m not sure I understand some of the terminology in your answer (e.g. “downstream feed”.) It works without the Wi-Fi (that’s the first code sample), and it works with the cake turntable unplugged from the mains, but it fails with Wi-Fi on and the cake turntable plugged in to the mains.

Rob — that thread is a great find. I don’t have the electronics skill to know which capacitor to put into a live 240v AC wire (or even if that would work for AC). What capacitor would you suggest?


#6

@ dumbledad - The circuit used to handle this issue is called a snubber network.

Please see this links… it explains what it is and how to connect one and another.

http://www.bpesolutions.com/bpemanuals/Snubber.pdf

Beside adding a snubber network you could also add a diode over the coil of the relay that connects to your netmf board. the cathode of the diode goes to the output port pin and the anode to the ground pin. place the diode as close as possible to the relay.