Arduino
Arduino’s sketch performs the following steps:
[checklist]
- measures the room temperature using a DS18B20 sensor
- sends the value to the PC through a network connection
[/checklist]
I chose the Dallas Temperature library by Miles Burton to interface Arduino to the temperature sensor; thanks to that library is very easy to read the temperature value:
IPAddress listeningIp = IPAddress.Any; IPEndPoint listeningEndPoint = new IPEndPoint(listeningIp, listeningPort); listeningSocket = new Socket(listeningIp.AddressFamily, SocketType.Stream, ProtocolType.Tcp); listeningSocket.Bind(listeningEndPoint); listeningSocket.Listen(10); |
Before sending the value, you have to convert it into a string value using the dtostrf function:
SocketHandler socketHandler = new SocketHandler(listeningSocket, this); serverThread = new Thread(new ThreadStart(socketHandler.startListening)); serverThread.Start(); |
Then, prepare the TCP packet and send it on the network:
try { // Wait for incoming connection handlerSocket = listeningSocket.Accept(); handlerSocket.ReceiveTimeout = 5000; IPAddress clientAddress = ((IPEndPoint)handlerSocket.RemoteEndPoint).Address; |
I used a temporary Stash object where I saved (using the print method) the temperature value followed by the “carriage return” character (= end of the message).
My sketch is available on Github too.
Demo
Temperature sensor connected to Arduino:
SocketServer application running:
Pages: 1 2
Ciao Luca,
non riesco a trovare lo sketch puoi darmi maggiori indiaczioni? Puoi anche fornire l’eseguibile c#?
Grazie
ciao, trovi tutto nel Repository su GitHub.
Domanda, non ho capito a cosa serve:
>> Stash::prepare(PSTR(“$H”), sd);
?
ciao, serve a preparare il pacchetto che poi invierai con tcpSend(). Il contenuto del pacchetto è soltanto “sd”, ovvero l’oggetto stash creato prima che contiene la temperatura e il terminatore di riga \r. $H è un “segnaposto” per la variabile che specifichi dopo (“sd”). Se guardi gli altri esempi vedrai pacchetti più complessi, con una parte fissa (“GET…”) e alcune variabili ($H, $F…).
Hi Luca.
nice tutorial, do the job very well.
I’m write the PC side (your C#) in Perl. the work is the same, the look and feel not.
Reading your tutorial, analizing the TCP packages, working with my perl socket script, I can see how the TCP port open and close for every temperature send by the arduino.
It is possible (I don’t know how) open permanently the port and send the text over and over the same open port?
It is possible open the port and do something bidirectinally (receive and send data over TCP to the arduino)
anyway, Thanks a lot for every tutorial in our webpage. it’s rocks
hi Luca
I had download you code. I replace the sensor data by code: float float_temp = 27.5; because i don’t have the sensor and the library.
But when i build the code in arduino and C#, nothing happen. it’s not connect between the arduino and pc.
Can you help me?
Hi, are you sure the IP address of Arduino and of your PC (running the demo app) are in the same network? Are you able to ping the Arduino address from your PC?
hi Luca,
I want to send data from C# to arduino. i try function :
ether.tcpReply() but the arduino don’t receive anything. can you help me?
if you have any example can you post it to your website?
thank so much!
Hello!
you use the tcpReply() to send data FROM Arduino… to receive data (sent from a C# app) to Arduino you can follow the examples as it was a webserver, like this one.
Hi! Nice work! 😉
I am working with Arduino and C# but I need to work in a asyncronous way… do you have any example? Do you know if there is some web site or book to find some good information? Thank you very much!
Hi Maximiliano,
what do you mean for “asyncronous” way? c# side, using the non-blocking socket methods?
Hi Luca i’m working on RFID with LAN, i downloaded your program, and c# code works fine when i test i with a c# client (on my LAN using different PC ), but my Arduino does not send any data, but i can ping to it, this setup is on LAN no internet is that a problem? Thanx in advance
Hi Salinda,
no, it doesn’t need an Internet access to work… did you use the exact Arduino sketch that is in my repository? Which ethernet shield are you using?
no i change it litte, coz i dont have a dhcp so i use Static setup function, and for the float i give fixed value. my problem is pings works, but there is no coming data to c# , this is the shield i use
http://dereenigne.org/wp-content/uploads/ENC28J60.jpg
Hi Luca, i got it solved, it’s the Ether Card new Library, i use the old library form here https://github.com/vworp/OldEthercard
i test it with both dhcp and static works fine, new library is not working for both(at the moment),
can you please help me on how to send a data back, like Acknowledgmentt back to Arduino when i recive a data.(ex RFID recived and display a username on lcd)
Good! Very strange the new library doesn’t work… anyway I’m working on a bidirectional example!
Hi luca,
I have a propblem. I send data from arduino to C# app. I use ether.tcpSend() function like you example. It work. But it just work about 5 minute or send about 32 times and then it doesn’t work. it don’t send data from arduino(ENC28j60) to C# app any more. I don’t know how?
thank so much!
I think the buffer is full. can you tell me how to clear the buffer?
thanks!
Hello Lucas. I am working on a project where the Arduino is a client and have to send and receive data received by the serial port to ethernet ENC28J60 like the example you have in C #. Could you please help me?
Hi Rafael, what help do you need?
I just need to send and receive against a set of strings through an ethernet connection client, already tried several examples but none worked so they send the data but not receive anything.
Can you please put libraries you used on github or just send them to my email. nmarbm@gmail.com. I im using W5100 ethernet shield and cant really just upload your sketch to arduino adn I dont really understand code that well to change your code to work on my shield.
Hi! The library I use for my shield is the Ethercard one from Jeelabs: https://github.com/jcw/ethercard
Buon pomeriggio, nel codice di c # non riesco a trovare dove configurare su quale porta il software PC ascolta. È il PC che funge da server e Arduino come client, giusto?
corretto, la porta è configurabile da GUI (nel mio esempio è la 1000)