6

I have a simple Python code that takes readings from a temperature sensor. I wish to send these readings (each time one is recorded in the code's while loop) to my laptop so the values can be used in real time in an application I have created on my laptop using Visual C#.

Therefore, I would like to be able to add code into my existing Python code that instead of simply printing the sensor readings on the Pi, sends the values to my laptop in a way that can be read by a C# app.

I don't have wireless internet on the Pi, so I intend to send the data via a cable such as an Ethernet cable.

Any advice on how I could achieve this would be very appreciated, thanks.

3

You can use UDP fairly easily in both languages. Something like this on the Pi sets up sending packets:

import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
message = "data you want to send"
sock.sendto(message, ("dns.or.ip.of.server", pick_a_port_number))

On the C# side it's a little bit more complex, but there's an example here, which I'll reproduce modified below:

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class UdpSrvrSample
{
   public static void Main()
   {
      byte[] data = new byte[1024];
      IPEndPoint ipep = new IPEndPoint(IPAddress.Any, pick_a_port_number);
      UdpClient newsock = new UdpClient(ipep);

      Console.WriteLine("Waiting for a client...");

      IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);

      data = newsock.Receive(ref sender);

      Console.WriteLine("Message received from {0}:", sender.ToString());
      Console.WriteLine(Encoding.ASCII.GetString(data, 0, data.Length)); // this will be "data you want to send"
   }
}

This page might also be useful.

1

I would like to be able to add code into my existing Python code that instead of simply printing the sensor readings on the Pi, sends the values to my laptop in a way that can be read by a C# app.

You want to use a use a TCP server/client model. Either end can be the server, the other is the client; the client connects to the server and establishes a connection, then information can be sent back and forth. Skeleton code for a basic client or server is probably only a dozen or so lines in python. Start by developing an echo server -- something that simply sends back the same message it is sent -- and then work from there.

You will get better help with this on a dedicated python or C# forum -- nothing about the task is specific to the pi. You should first look around for TCP networking tutorials involving either language and work through those. This way you can save yourself some time by coming to the table with specific questions and code demonstrating what you have tried/what problems you are having.

  • Thank you for your advice. I have followed a TCP server/client tutorial for python, which uses the local host (127.0.0.1). If I run both the server and client on the Pi, it works, however if I run the client code on my laptop which is connected to the Pi via an Ethernet cable, the client code fails with the error: [Errno 10061] No connection could be made because the target machine actively refused it. Do you have any idea as to why the Pi is refusing my laptop? Thanks again. – Aaron H Mar 18 '14 at 17:58
  • 127.0.0.1 is localhost on every machine, including your laptop. So if you are trying to connect using that address, the laptop is trying to connect to itself (and if the corresponding port is not open, it will refuse). You need to use the pi's network address. – goldilocks Mar 18 '14 at 19:34
  • Thank you, I have changed the client code the now have the Pi's correct address, however it now waits for a while and then throws an error saying "[Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time or established connection failed because connected host has failed to respond." Could this be due to the port on the Pi not allowing the laptop to communicate? – Aaron H Mar 18 '14 at 21:23
  • It could if you have a firewall (iptables) configured. This isn't activated by default on raspbian though. How do you have this connection (the direct ethernet cable) set-up? It's kind of unusual. – goldilocks Mar 18 '14 at 21:30
  • 1
    It would be more normal to put them both on the same network, and communicate via a router. I bet you'll get the same [Errno] if you unplug the cable. If you don't have a network to use (i.e., you are totally offline somewhere, which is odd since you posted here...), it is possible to connect the two w/ a cross-over cable. The specifics of that are a whole other question (you could ask on U&L, there may be other methods too). – goldilocks Mar 19 '14 at 15:30
1

Using a Samba Server on your Raspberry Pi you could share a directory over the network. By putting your files in that directory you will have access to them from your laptop. By doing this you don't have to edit any code, more than changing what directory the Python program should write to.

Here you can read more about how to set up a Samba server and share folders.

0

As an alternate, post your readings to a web service, so your laptop need not be on to collect the information. You can fetch the readings on demand from your laptop or any other connected machine.Or

As per @goldilocks answer, this is not specific to pi so much as it is a general computing / programming question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.