Read DS18B20 temperature with .NET 5.0 IoT throught TCP on Raspberry Pi 3B+

These days I was thinking about to  make a PID controller use my Raspberry Pi Which has been layed in the trash corner for a long time.

At firsh I planned to do it with Labview only,but I found it is kind of difficult to make it come true.Thought there is a strong LINX tool with which you can operate the GPIO of  Raspberry Pi directly, it didn't offer the One-Wire sensor like DS18B20. And with unknow reason the LINX don't have the custom command for Raspberry Pi, so I have to abandent.

Fortunately, I found a good another way  to do it.

 

That is .NET Window IoT.

 Microsoft also creates some tutorials. You can use .net 5.0 operate the GPIO directly.

And  the .NET IoT Libraries 1.3.0 has lots of libraries that is very convenient to use.

docs.microsoft.com

 

 

R1: 4.7K Ohm or 10K Ohm resistor

f:id:sesnail:20210206134325p:plain



 

CODE:

using System;
using System.Net;
using System.Net.Sockets;
using System.Device.Gpio;
using System.Threading;
using Iot.Device.Pwm;
using Iot.Device.OneWire;
using System.Text;

namespace ConsoleApp1
{
class Program
{

static void Main(string args)
{
OneWireThermometerDevice theDs = null;
GpioController controller = new GpioController();
int pin=18;
bool ledOn = true;
Console.WriteLine("Blinking LED. Press Ctrl+C to end.");
TcpListener server = null;
DeviceInitialize(ref controller, pin, ref theDs);
#region TCP
try
{
// Set the TcpListener on port 13000.
int port = 1300;
IPAddress serverAddr = IPAddress.Parse("192.168.11.10");

// TcpListener server = new TcpListener(port);
server = new TcpListener(serverAddr, port);

// Start listening for client requests.
server.Start();

// Enter the listening loop.
while (true)
{
Console.Write("Waiting for a connection... ");
// Perform a blocking call to accept requests.
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connected!");

// Get a stream object for reading and writing
NetworkStream stream = client.GetStream();
// Loop to receive all the data sent by the client.
while (true)
{
ReadTemperature(ref stream);
}

//// Shutdown and end connection
//client.Close();
}
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
// Stop listening for new clients.
server.Stop();

}


#endregion
void ReadTemperature(ref NetworkStream networkStream)
{
var temperature = theDs.ReadTemperature().ToString();
Byte sendBytes = Encoding.ASCII.GetBytes(temperature);
networkStream.Write(sendBytes, 0, sendBytes.Length);
Console.WriteLine(temperature);
controller.Write(pin, *1;
ledOn = !ledOn;
}
void DeviceInitialize(ref GpioController theController, int ControlPin, ref OneWireThermometerDevice theDal)
{
string busID = "", devieID = "";
theController.OpenPin(ControlPin, PinMode.Output);
foreach (var device in OneWireThermometerDevice.EnumerateDevices())
{
busID = device.BusId;
devieID = device.DeviceId;
Console.WriteLine(busID + "," + devieID + ","+device.Family);
}
theDal = new OneWireThermometerDevice(busID, devieID);

}
}

}
}

 

SCREENSHOOT:

left: recieved data right: sent data

f:id:sesnail:20210206133758p:plain



 

 

*1:ledOn) ? PinValue.High : PinValue.Low