C programming on Raspberry Pi: an example chapter from Elektor
The Raspberry Pi is traditionally programmed in Python. Although Python is a very powerful language, C is probably the most commonly used among programmers. Here’s a taste of C on the Raspberry Pi.
Editor’s Note: This is a sample of the delivered C programming on Raspberry Pi – Develop innovative projects based on C material (D. Ibrahim, Elektor 2021).
UDP and TCP / IP
Communication via a Wi-Fi link takes place in the form of client and server. Sockets are used to send and receive data packets. The server-side device usually waits for a connection from clients and once it is established, two-way communication can start. Two protocols are mainly used to send and receive data packets over a Wi-Fi link: UDP and TCP. TCP is a connection-based protocol that guarantees the delivery of packets. The packets are assigned sequence numbers and the receipt of all packets is acknowledged to prevent them from arriving in the wrong order. As a result of this confirmation, TCP is generally slow but reliable because it guarantees the delivery of packets. UDP, on the other hand, is not connection-based. The packets do not have sequence numbers and therefore there is no guarantee that they will reach their destination. UDP has less overhead than TCP and, therefore, is faster. Table 1 lists some of the differences between TCP and UDP protocols.
1. Create a UDP socket.
2. Bind the socket to the server address.
3. Wait for the datagram packet to arrive from the client.
4. Process the datagram packet.
5. Send a response to the client or close the socket.
6. Go back to step 3 (if it is not closed).
1. Create a UDP socket.
2. Send a message to the server.
3. Wait for a response from the server to be received.
4. Process the response.
5. Return to step 2 or close the socket.
Project 1: Communicate with an Android smartphone via UDP (Raspberry Pi is the server)
In this project, UDP communication is used to receive and send data to / from an Android smartphone. In this project, Raspberry Pi is the server and the Android smartphone is the client.
Goal: This project aims to show how UDP communication can be established between a Raspberry Pi and an Android smartphone.
List of programs: The program MyServer.c is shown in List 1 and can be retrieved from the software support package for the book. Head to , scroll to Downloads, then click Software_C programming on Raspberry Pi. Store the .zip file locally, then extract all files or just MyServer.c.
At the beginning of MyServer.c, the required header files are included in the program. The message Hello from Raspberry Pi is stored in an array of characters message. A UDP-type socket is then created by calling the socket function and storing the handle in the sock variable. The details of the server computer (Raspberry Pi) are then given where the address is set to INADDR_ANY So any other computer on the same network with the port number set to 5000 will establish communication with the Raspberry Pi. The details of the server computer are then linked to the specified port by calling the function bind.
The rest of the program runs in a loop. Inside this loop, the function recvde is called to wait to receive a data packet from the client computer (Android smartphone). Note that this is a blocking call and the function will wait until data is received from the client. The program ends if the character X is received from the client computer. A NO character is added to the received data and displayed on the Raspberry Pi PC screen as a string using the to print function. An integer variable called to count is converted to a character and appended to the end of the character array message. This is sent to the client computer. The first time in the loop, the client computer will display Hello from Raspberry Pi 1. The second time, the client computer will display Hello from Raspberry Pi 2, etc. The socket is closed just before the end of the program. The program can be compiled and run as follows:
gcc –o MyServer MyServer.c
Many UDP apps are available for free on the Play Store. In this project the UDP terminal from couldyIT is used (see Figure 3). Enter the port number and IP address of your Android smartphone and click Start the terminal as shown on the Figure 4. An example of program execution is shown in Figure 5, where the smartphone and the PC screen are shown.
Note: You can find the IP address of your Raspberry Pi by using the ifconfig order.