Tuesday 30 July 2019

Installing MySQL on the Raspberry Pi - part 3: Connecting from .Net

Installing the Connector

Visual Studio is not supplied with a default MySQL/MariaDB connector.
Use the MySQL connector.
https://dev.mysql.com/doc/connector-net/en/connector-net-installation.html
Run the installer.
 image
Select Typical installation
 image
In Visual Studio, to add the connector go to the Project/Add Reference. Select Assemblies and search for MySQL. Remember you will need to do this for each project (even within a solution).
 image
Tick MySQL.Data and click on OK.

Example program in VB.Net

The program uses a simple form.
One vertical Split Container, with one button in one panel, and a multi-line, docked text box with vertical scroll bar in the other panel.

Code

Public Class Form1
    ' *** Test application - 
    ' *** to a remote MariDB database, insert a row and then display all rows
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim myDB As New MySql.Data.MySqlClient.MySqlConnection("Server=192.168.1.191;Database=datastorage;Uid=datawriter;Pwd=*******;")
        Dim myCommand As New MySql.Data.MySqlClient.MySqlCommand()
        Dim myReader As MySql.Data.MySqlClient.MySqlDataReader
        Dim sSQL As String
        addtext("Connecting")
        myDB.Open()
        addtext("Connected")

        addtext("Inserting")

        sSQL = "insert into datavalues(user,title,value) values(CURRENT_USER(),'VBtest',23);"

        With myCommand
            .Connection = myDB
            .CommandText = sSQL
            .ExecuteNonQuery()
        End With
        addtext("Inserted")


        sSQL = "SELECT * FROM datavalues"
        addtext("Selecting")

        With myCommand
            .CommandText = sSQL
            myReader = .ExecuteReader()
        End With
        addtext("Selected")

        addtext("Reading")

        While myReader.Read()
            Dim sLine As String = ""
            For ix As Integer = 0 To myReader.FieldCount - 1
                If sLine <> "" Then
                    sLine = sLine & ", " & myReader.GetString(ix)
                Else
                    sLine = myReader.GetString(ix)
                End If
            Next
            addtext(sLine)
        End While
        addtext("Reading")
        addtext("Closing")
        myReader.Close()
        myDB.Close()
        addtext("Closed")
    End Sub
    ''' <summary>
    ''' Add string parameter to current content of text box
    ''' </summary>
    ''' <param name="aString">Text to be added to text box </param>
    Private Sub addtext(aString As String)
        TextBox1.Text = TextBox1.Text & aString & vbCrLf
    End Sub
End Class

Output

Connecting
Connected
Inserting
Inserted
Selecting
Selected
Reading
1, root@localhost, test, 1, 27/02/2019 11:06:09
2, root@localhost, test, 2, 27/02/2019 11:06:16
3, root@localhost, test, 3, 27/02/2019 11:06:23
5, datawriter@192.168.1.%, test, 30, 28/02/2019 13:13:07
6, datawriter@192.168.1.%, test, 30, 28/02/2019 13:13:23
7, datawriter@192.168.1.%, test, 30, 30/07/2019 08:06:21
8, datawriter@192.168.1.%, VBtest, 23, 30/07/2019 10:49:39
9, datawriter@192.168.1.%, VBtest, 23, 30/07/2019 10:58:02
Reading
Closing
Closed

Example Code in C#

This is a console application.

Code

using System;

using MySql.Data.MySqlClient;

public class MySQLTest
{
    public static void Main()
    {
        string connStr = "Server=192.168.1.191;Database=datastorage;Uid=datawriter;Pwd=********;";
        MySqlConnection myDB = new MySqlConnection(connStr);
        try
        {
            Console.WriteLine("Connecting");
            myDB.Open();
            Console.WriteLine("Connected");
            Console.WriteLine("Inserting");

            string sql = "insert into datavalues(user,title,value) values(CURRENT_USER(),'C#test',54);";
            MySqlCommand myCommand = new MySqlCommand(sql, myDB);

            myCommand.ExecuteNonQuery();
            Console.WriteLine("Inserted");

            Console.WriteLine("Selecting");
            sql = "SELECT * FROM datavalues";
            myCommand.CommandText = sql;

            MySqlDataReader myReader = myCommand.ExecuteReader();
            Console.WriteLine("Selected");

            Console.WriteLine("Reading");

            while (myReader.Read())
            {
                string rowtext = "";
                for (int ix = 0; ix < myReader.FieldCount; ix += 1)
                {
                    if (rowtext == "")
                    {
                        rowtext = string.Concat("", myReader[ix]);
                    }
                    else
                    {
                        rowtext = string.Concat(rowtext,", ", myReader[ix]);
                    }
                }
                    Console.WriteLine(rowtext);              
            }
            myReader.Close();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
        myDB.Close();
        Console.WriteLine("Done.");
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}

Output

Connecting
Connected
Inserting
Inserted
Selecting
Selected
Reading
1, root@localhost, test, 1, 27/02/2019 11:06:09
2, root@localhost, test, 2, 27/02/2019 11:06:16
3, root@localhost, test, 3, 27/02/2019 11:06:23
5, datawriter@192.168.1.%, test, 30, 28/02/2019 13:13:07
6, datawriter@192.168.1.%, test, 30, 28/02/2019 13:13:23
7, datawriter@192.168.1.%, test, 30, 30/07/2019 08:06:21
8, datawriter@192.168.1.%, VBtest, 23, 30/07/2019 10:49:39
9, datawriter@192.168.1.%, VBtest, 23, 30/07/2019 10:58:02
10, datawriter@192.168.1.%, test, 30, 30/07/2019 13:28:56
11, datawriter@192.168.1.%, C#test, 54, 30/07/2019 13:42:00
12, datawriter@192.168.1.%, test, 30, 30/07/2019 13:43:13
13, datawriter@192.168.1.%, C#test, 54, 30/07/2019 13:45:37
14, datawriter@192.168.1.%, C#test, 54, 30/07/2019 13:47:51
15, datawriter@192.168.1.%, C#test, 54, 30/07/2019 13:48:04
16, datawriter@192.168.1.%, C#test, 54, 30/07/2019 13:52:03
17, datawriter@192.168.1.%, C#test, 54, 30/07/2019 13:56:07
Done.
Press any key to exit.

Wednesday 24 July 2019

GoPro Wifi connection - part one

Background

The GoPro series of cameras have become synonymous with action cameras in general. Earlier versions have had a WiFi backpack, later versions have the WiFi option built in.

I have an elderly GoPro Hero2, that has survived three leaks - thankfully all in fresh water.


They are rust marks on the shutter button and surroundings.


Unfortunately, the only official way of connecting to a GoPro is either via their Android application, or via their PC or Mac applications. If you are using older hardware, or Linux based operating systems, there appears to be no official connection.
However, a search of the internet found the first referenced article. Once the IP address was discovered, it was a lot easier to find the other references, and more importantly the GoProWifiHack on GitHub.

Connecting to the GoPro webserver

If the GoPro has an external WiFi backpack, connect it to the back of the GoPro.

It is worth plugging in the power to the backpack, as you might spend more time on it than you expect.

Switch on the WiFi. The Wifi device name should appear on the list of available networks on your computer.

Now my GoPro Hero 2 had been set up previously, so I was able to select the connection, enter the password and connect to the device.

Now the key piece of information is the availability of a web interface.

This is located at 10.5.5.9:8080. From there you can access the videos and stills on the device.


Next - sending commands.

References

http://aikiwolfie.blogspot.com/2014/12/ubuntu-tip-connecting-to-gopro-hero4.html
https://github.com/KonradIT/goprowifihack
https://github.com/KonradIT/goprowifihack/blob/master/HERO2/README.md
https://github.com/KonradIT/goprowifihack/blob/master/HERO2/WifiCommands.md
https://github.com/KonradIT/goprowifihack/blob/master/HERO2/Mediabrowsing.md