Send to Azure IoT Hub Rest Endpoint with C# without library

Problem

  • Send Device to Cloud messages to Azure IoT Hub without libraries only using REST Endpoint
  • Integrate Proxy Support
  • Getting SharedAccessSignature

Solution

private static void sendToIOTHUB(string s)
{
string iotHub = “<<IoTHubName>>”;
string deviceId = “<<DevideID>>”;
string api = “2016-02-03”;

string restUri = String.Format(“https://{0}.azure-devices.net/devices/{1}/messages/events?api-version={2}”, iotHub, deviceId, api);

using (WebClient client = new WebClient())
{
client.Headers.Set(“Content-Type”, “application/json”);
//Proxy – can be deleted
WebProxy wp = new WebProxy(“<<PROXY>>:<<PORT>>”);
client.Proxy = wp;
client.Headers.Set(“Authorization”, “<<SharedAccessSignature>>”);
string res = client.UploadString(new Uri(restUri), s);
Console.WriteLine(res);
}
}

Getting SharedAccessSignature

First, you have to download the Device Explorer from here.

Got to Management and SAS Token.

device_explorer

Generate the SASToken. You need something like this:

SharedAccessSignature sr=<<IOTHUBNAME>>.azure-devices.net%2Fdevices%2F<<DEVIDEID>>&sig=<<SIG>>%3D&se=<<SOMENUMBER>>”

sastoken

 

Advertisements

Convert .NET Dictionary to JSON without Libraries in C#

Problem

  • You want to convert a C# Dictionary to JSON
  • You do not want use external libraries

Solution

private string DictionaryToJson(Dictionary<string, object> dict)
{
string entries = “”;
foreach (KeyValuePair<string, object> entry in dict)
{
if(entries.Length>0)
entries = entries + “,” + “\”” + entry.Key + “\”:\”” + entry.Value.ToString() + “\””;
else
entries = “\”” + entry.Key + “\”:\”” + entry.Value.ToString() + “\””;
}
return “{” + entries + “}”;
}

Missing api-ms-win-crt-runtime-l1-1-0.dll

Problem

error

  • The api-ms-win-crt-runtime-l1-1-0.dll is missing
  • Sub problem: you can not install Visual C++ Redistributable for Visual Studio because Windows Update, especially on Windows Vista, doesn’t work. (it runs hour for hour and exits with an error)

Solution

Normally, you only have to install the VC++ Redistributable for your computer:

Visual C++ Redistributable for Visual Studio 2017 (64-bit)

Visual C++ Redistributable for Visual Studio 2017 (32-bit)

Windows Update

If you can not install VC++ it is most likely because of the broken Windows Update process:

Here is what i did:

  1. Uninstall VC++, by starting the installer and select Uninstall.
  2. Restart computer
  3. Open cmd as administrator
  4. Type: net stop wuauserv
  5. Install VC++

This installs VC++ without problems. You can restart Windows Update with net start wuauserv 

Where to find Windows system log files

Problem

  • You are looking for the system / security / application log file, which are displayed by the Windows Event Viewer.
  • You need to copy the log files to another computer.

Solution

The log files can be found under

C:\Windows\System32\winevt\Logs

Just copy the files ending with “evtx“. You can open them with the Windows Event Viewer on every other computer, even those, you have no access on the original computer (no permission to watch; in that case, boot with linux, copy files …).

Fin

 

Your java it out of date

Dialog “Your Java version is out of date”

Problem

Your java it out of date
Your Java version is out of date
  • If you start a Java application on Windows this dialog shows up
  • Especially on servers this can be annoying, because it prevents the start of the application
  • You want to get rid of this dialog
  • (But you should update Java periodically)

Solution

1. Create the following path:

C:\Windows\Sun\Java\Deployment

2. Create in this path a file named Deployment.config

3. Paste the following content

deployment.system.config=file\:C\:/Windows/Sun/Java/Deployment/deployment.properties
deployment.system.config.mandatory=true

deployment.system.config.mandatory forces java to load the property file set in deployment.system.config.

4. Create the file deployment.properties

5. Paste the following content

deployment.javaws.autodownload=never
deployment.javaws.autodownload.locked
deployment.expiration.check.enabled=false
deployment.expiration.check.enabled.locked
deployment.expiration.decision=never
deployment.expiration.decision.locked
deployment.expiration.decision.suppression=true
deployment.expiration.decision.suppression.locked

You will find description of the parameter here.

deployment.expiration.check.enabled is set to “false” to suppress the prompt. deployment.expiration.check.enabled.locked will lock the value, so a non root user cannot change it.

Optional:

  • deployment.javaws.autodownload – Prevent autodownloadin in java web start
  • deployment.expiration.decision – Remember the decision and suppress them

Fin

Raspberry PI – Boot from USB drive or stick

Problem

  • You want to use a USB drive or stick to store the OS and user data because it is more reliable than a normal SD card.
  • You only have a Windows based computer

Solution

The Raspberry can not boot from USB, but it is possible to transfer the root to the USB drive.

The solution consists of 4 steps:

  1. Extract the image from the SD-Card
  2. Copy image on USB drive/stick
  3. Change boot.txt
  4. Delete old root partition

Step 1

Install and configure the OS (Raspbian). After you finished this, remove the SD card from the Raspberry and insert it into your windows computer.

To extract the image from the SD, i used Win32 Disk Imager (Download). It can extract and write images. Just store the image on your hard disk.

disk imager

Step 2

Use the Win32 Disk Imager to write the image to the USB drive. After that, you have two different drives with the identical partition layout.

Step 3

Insert SD card and plug the USB drive in the Raspberry PI. With blkid you see the two identical layout of both disks. mmcblk0p is the SD card, sda the USB drive.

/dev/mmcblk0p1: SEC_TYPE=”msdos” LABEL=”RECOVERY” UUID=”47BE-8134″ TYPE=”vfat” PARTUUID=”000709f6-01″
/dev/mmcblk0p5: LABEL=”SETTINGS” UUID=”4b288679-4f9e-47bc-b72e-5989629d5e7f” TYPE=”ext4″ PARTUUID=”000709f6-05″
/dev/mmcblk0p6: SEC_TYPE=”msdos” LABEL=”boot” UUID=”67B4-B99A” TYPE=”vfat” PARTUUID=”000709f6-06″
/dev/mmcblk0p7: LABEL=”root” UUID=”0b00e3d6-6726-4db3-845d-335e3c5b548e” TYPE=”ext4″ PARTUUID=”000709f6-07″
/dev/sda1: SEC_TYPE=”msdos” LABEL=”RECOVERY” UUID=”47BE-8134″ TYPE=”vfat” PARTUUID=”000709f6-01″
/dev/sda5: LABEL=”SETTINGS” UUID=”4b288679-4f9e-47bc-b72e-5989629d5e7f” TYPE=”ext4″ PARTUUID=”000709f6-05″
/dev/sda6: SEC_TYPE=”msdos” LABEL=”boot” UUID=”67B4-B99A” TYPE=”vfat” PARTUUID=”000709f6-06″
/dev/sda7: LABEL=”root” UUID=”0b00e3d6-6726-4db3-845d-335e3c5b548e” TYPE=”ext4″ PARTUUID=”000709f6-07″

The interesting partition is the root partition. We have to tell the boot system, where the new root partition is located. Get the PARTUUID from the root partition (here from sda7): PARTUUID=”000709f6-07″

Open the /boot/cmdline.txt and change the root like this:

dwc_otg.lpm_enable=0 console=ttyAMA0,115200 console=tty1 root=PARTUUID=000709f6-07 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait

Step 4

If you would reboot now, the Raspberry would mount the SD card root to / because it’s the first partition he finds with the given ID. Thus you have to delete the old root partition.

For this i used gparted. You also can use fdisk like in this tutorial.

Step 5

Reboot

Fin

 

 

PowerShell – Check TCP Port

Problem

  • You want to check if a TCP port is open.
  • You want to use the PowerShell.

Solution

This solution checks, if port 80 on 192.168.2.1 is open.

try{
    $tcp=new-object System.Net.Sockets.TcpClient
    $tcp.Connect("192.168.2.1",80)
    If($tcp.Connected)
    {
        Write-Host "It works"
    }
    else
    {
        Write-Host "It doesnt work"
    }
    $tcp.Close()
}
catch {
  Write-Host "Error"  
}