# Communication between RPi and PC

#### Pushkar1

Joined Apr 5, 2021
416
I have a one misunderstanding that needs to be cleared

For Example :

Assume
MySQL database installed on the PC and Temprature sensor is connected to raspberry Pi and Pi and PC are connected to the Wi-Fi router in the room.

The Pi takes the temperature data and sends it to a database running on the PC.

I want to know about the following

1) Is there direct communication happens between RPi and PC or through Wi-Fi router?

2) What communication protocol is being used in this project?

3) Is TCP/IP being used in this project?

#### LesJones

Joined Jan 8, 2017
3,644
The data will not be transferred unless you write software on the transmitting device to format the data, transfer the data (With a protocol that you decide to use.) and on the receiving device receive the data (possibly with error checking.) transfer the data to the software ( MySQL ). I have only ever done this by creating a file that could be shared over the network.

Les.

#### BobTPH

Joined Jun 5, 2013
4,244
If each of them is connected to WIFi, it would be silly to use anything but TCP/IP over the WIFI network. TCP is a very reliable protocol that will guarantee that the data is transferred without error, and the software for it exists already on both sides.

Bob

Last edited:

#### Yaakov

Joined Jan 27, 2019
3,938
What would probably be done in a case like this (though it is very poorly specified, so this is largely speculative) is a program running on the RPi would use an HTTP post to an httpd on the PC. That server would either be part of, or would execute a program that would insert the data in the MySQL database. The PC program could be written in a variety of languages like perl, PHP, javascript (node.is) or others.

Alternatively, the RPi could run an httpd and PC could poll periodically to get the data. The problem with that approach is the potential race condition of polling before the update so you would have to be careful to poll between RPi readings according to clock time while if the RPi pushes the data it will always be correct.

#### Yaakov

Joined Jan 27, 2019
3,938
I should add, though it is implied, that TCP/IP would be used to make these connections since HTTP runs over TCP/IP.

#### Pushkar1

Joined Apr 5, 2021
416
The data will not be transferred unless you write software on the transmitting device to format the data, transfer the data (With a protocol that you decide to use.) and on the receiving device receive the data (possibly with error checking.) transfer the data to the software ( MySQL ). I have only ever done this by creating a file that could be shared over the network.

Les.
Obviously the program will be run on the Pi to transfer the temperature data to the database

So this is a sample code that should be run on the Pi.

Python:
# Author: Tony DiCola
import MySQLdb
import time
import datetime

# Note you can change the I2C address from its default (0x48), and/or the I2C
# bus by passing in these optional parameters:

# Choose a gain of 1 for reading voltages from 0 to 4.09V.
# Or pick a different gain to change the range of voltages that are read:
#  - 2/3 = +/-6.144V
#  -   1 = +/-4.096V
#  -   2 = +/-2.048V
#  -   4 = +/-1.024V
#  -   8 = +/-0.512V
#  -  16 = +/-0.256V
GAIN = 1

time_sensor = time.time()
# Main loop.
x = [1]*4
y = [2]*4
z = [3]*4
for i in range(4):
# Read the specified ADC channel using the previously set gain value.
# Once continuous ADC conversions are started you can call get_last_result() to
db = MySQLdb.connect("192.168.1.10", "root", "raspber", "temp")
curs=db.cursor()

while True:
try:
curs=db.cursor()
curs.execute("""INSERT INTO table_temp(time, out_x, out_y, out_z)
values(%s,%s,%s,%s)""",(time_sensor,x[i],y[i],z[i]))
db.commit()
curs.close()
except:
print "Error"
db.rollback()
time.sleep(1)
So this is just a sample code, haven't tested it

Is TCP/IP being used in this code?

@Yaakov

I think there is no need to write any program for PC. When the database running on PC is connected with Pi. temperature value will store directly the in the database

Last edited:

#### BobTPH

Joined Jun 5, 2013
4,244

#### Pushkar1

Joined Apr 5, 2021
416
Almost certainly, yes.

Bob
@BobTPH Why do you think tcp-ip may have been used?

The reason for the confusion is that the PC, router and the Pi are all three Wi-Fi devices. The confusion is whether WiFi is used or the TCP-IP protocol is used.

#### BobTPH

Joined Jun 5, 2013
4,244
The fact that you connect via an IP address.

Bob

#### Yaakov

Joined Jan 27, 2019
3,938
That program uses the native MySQL client to talk to the PC which is fine. It does use TCP/IP as you can see because of this statement:

C++:
db = MySQLdb.connect("192.168.1.10", "root", "raspber", "temp")
This instantiates an object that has a connection (via TCP/IP) to the PC's MySQL server.

#### Pushkar1

Joined Apr 5, 2021
416
The fact that you connect via an IP address.
That program uses the native MySQL client to talk to the PC which is fine. It does use TCP/IP.
I'm having trouble to understanding communication of complet system. I assume that there is a tcp-ip protocol being used between Pi and PC.

PC has a IP address and a PI has a IP address. Router works to connect both the devices.

What protocol is being used if there is communication happening between the Pi and the Wi-Fi router or Wi-Fi router to PC.

#### Yaakov

Joined Jan 27, 2019
3,938
I am having a hard time understanding your question. It might be because you don’t understand network protocols. Here’s a very basic description:

The network uses layers. The very lowest is the physical layer, it’s the radio part of WiFi. Above that, there is a layer to deal with traffic in your collision domain, that is roughly, things that can hear and interfere with each other. This is tied pretty closely to the physical and handles local delivery of frames, that is packets of data. The MAC address (Media Access Control) is used for this. The clue is in the name “meda”, in this case the medium is radio.

The problem with this protocol,is it has no reliable delivery and no way to address something not on your LAN. TCP/IP is on top of the Ethernet protocol to provide these things among others. The IP Address is the Internet Protocol address, it has two parts: a network portion and a host portion. The netmask is used to separate them. A router can use the network portion to send the traffic to a network the client cannot hear, and route the return packets to it.

IP addresses are converted to MAC addresses by a protocol called ARP or Address Resolution Protocol so the Ethernet frames can be delivered locally.

Other protocols can be built on top of TCP/IP such as the MySQL native protocol.

#### BobTPH

Joined Jun 5, 2013
4,244

#### Pushkar1

Joined Apr 5, 2021
416
Other protocols can be built on top of TCP/IP such as the MySQL native protocol.

Is a wifi a protocol? Yes/No

I think it's protocol like if i want to share my mobile internet i enable my wi-fi then other people can also use my internet if they know the password

It has come to the conclusion that the temperature example I have taken uses the tcp-ip protocol and does not use wifi.

#### nsaspook

Joined Aug 27, 2009
9,049
https://www.techopedia.com/definition/25705/communication-protocol
WiFi is an entire family of protocols that use radio waves as a communications media.
https://www.techopedia.com/definition/14462/communication-media

Weak analogy time: The tcp-ip protocol is like expressing a specific human language using a computer symbol code like ASCII. Here, we are using the human language protocol of English to communicate back and forth about this topic via a ASCII compatible 'communications' channel (that might use WiFi or dozens of other communications systems with their own protocols and media). That 'communications' channel will work exactly the same way if we switch to the human Spanish language protocol to transfer the same information on that channel.

#### Yaakov

Joined Jan 27, 2019
3,938
Is a wifi a protocol? Yes/No

I think it's protocol like if i want to share my mobile internet i enable my wi-fi then other people can also use my internet if they know the password

It has come to the conclusion that the temperature example I have taken uses the tcp-ip protocol and does not use wifi.
Your conclusion is incorrect. WiFi is the name for a set of tests that determine if wireless networking equipment is compatible. We use it as an inaccurate shorthand. The protocol used by WIFi compatible equipment is 802.11, wireless Ethernet. It is a protocol that specifies PHY (Physical) and MAC (Media Access Control) to allow transmission of data wirelessly. It is the basic platform that all the other protocols devices can use depend on.

If a device uses WiFi, that device depends on it for all other transactions.

On top of WiFi, we can find a number of protocols each more abstract than the last. In general, we use TCP/IP to manage connections between devices and create a channel that can be used by applications. So if two devices which operate using WiFi hardware connect to exchange data, they are both using WiFi. If the are on the same physical Access Point/Router, they don't need the IP (Internet Protocol) because the are on the same LAN (Local Area Network) and their routing tables will cause them to ARP (use address resolution protocol) to determine the MAC (Media Access Control, or hardware address) of the target device and communicate directly.

If they are on different networks, they will depending on a gateway, specified in the routing tables, to act as a router and use its routing table to hand off the traffic to a router it can talk to that is closer to the target network.

So, if things use wireless, they use WiFi. If they are on the same LAN, they talk as directly as possible though if they are not in an ad hoc configuration they won't talk directly but through an AP. If they are not on the same LAN they will use a router they can talk to from their LAN as a gateway and count on any number of routers in between to hand off the traffic until it reached the destination network, and then that router will talk to the device.

The TCP (Transmission Control Protocol) packets that pass back and forth (or in some cases UDP (User Datagram Protocol)) contain payloads of data that can be other, higher level protocols such as http, smtp, imap, or even MySQL.

#### nsaspook

Joined Aug 27, 2009
9,049
One day all of this simple networking will be history.

https://datatracker.ietf.org/doc/html/rfc1606
The vast number space of the IPv9 protocol has also allowed
allocation to be done in a straight forward manner. Typically, most
high street commercial internet providers issue a range of 1 billion
partitioned into subnet hierarchies allowing groups of a million
addresses to be allocated for each discreet unit (e.g., room/floor
etc.) The allocation of sub groups then to controllers such as light
switches, mains sockets and similar is then done from each pool.

The allocation process is again done in a hierarchical zoned way,
with each major application requesting a block of addresses from its
controller. In this way the light bulb requests an address block from
the light switch, the light switch in turn from the electrical system
which in turn requests one from the room/floor controller. This has
been found to be successful due to the enormous range of addresses
available, and contention for the address space being without
problems typically.

Whilst there are still many addresses unallocated the available space
has been sharply decreased. The discovery of intelligent life on
other solar systems with the parallel discovery of a faster-than-
light transport stack is the main cause. This enables real time
communication with them, and has made the allocation of world-size
address spaces necessary, at the level 3 routing hierarchy. There is
still only 1 global (spatial) level 2 galaxy wide network required
for this galaxy, although the establishment of permanent space
stations in deep space may start to exhaust this. This allows level 1
to be used for inter-galaxy routing. The most pressing problem now is
the case of parallel universes. Of course there is the danger of
assuming that there is no higher extrapolation than parallel
universes...

#### BobTPH

Joined Jun 5, 2013
4,244
Gosh, I can’t wait until each wall of my house is a high res screen and each pixel on it has an IP address. That is when the 256 bit wide processors will start to gain traction.

Bob