Monday, January 11, 2016

IoT: Raspberry Pi2 and Wireless conectivity losses

As I have described elsewhere, I have setup my Pi2 with a temperature sensor which posts the information to an Azure Event Hub. I’ve made the code more robust to handle different errors, but eventually the Pi2 – every 3 to 4 days - would just stop sending readings and responding to pings.
The problem is apparently related to the Wifi dongle, so (with a little help from a friend) I found the commands that restart the wlan, and after adding the code, the problem seems to be fixed. Here’s the relevant C# code:

public static void RestartWifi()
{
    Process proc = new Process
    {
        StartInfo = new ProcessStartInfo
        {
            FileName = "bash",
            Arguments = "-c '" + @"/sbin/ifdown \'wlan0\'" + "'",
            UseShellExecute = false,
            RedirectStandardOutput = true,
            CreateNoWindow = true
        }
    };

    proc.Start();
    Thread.Sleep(5000);
    proc = new Process
    {
        StartInfo = new ProcessStartInfo
        {
            FileName = "bash",
            Arguments = "-c '" + @"/sbin/ifup --force \'wlan0\'" + "'",
            UseShellExecute = false,
            RedirectStandardOutput = true,
            CreateNoWindow = true
        }
    };

    proc.Start();
}

No comments:

Post a Comment