Sending data over wifi from Raspberry Pi Pico W to local PC
I sent data from Raspberry Pi Pico W over wifi to local PC.
1. Start listener program on PC and then program on Pico
- local PC script must be running first, as its listening for the data to come in on all IP addresses but on a specific port
- Fire up the pico code to send data to the local PC
2. Send data with identifiers as to which Pico sending, as all data goes to same file (at this time)
I want to develop to be able to have multiple Raspberry Pi Pico W’s send data to local PC, on same port , but it needs to differentiate between them- it can do this:
Firstly- by IP address on wifi
Secondly -We need to be able to send the PicoID across as well, so we know which device data is coming form
Turdly, we need to stream relevant data from Sensor, as well as correct datetime to local PC
3. Local PC has to manage the data – we will write it to a single .txt file
First step, write it all to a file
so far, have been able to write – IP address and DateTime to a file
('192.168.1.5', 57865),b'DateTime: 2024-01-10 9:08:07'
('192.168.1.5', 57866),b'DateTime: 2024-01-10 9:08:11'
('192.168.1.5', 57867),b'DateTime: 2024-01-10 9:08:15'
('192.168.1.5', 57868),b'DateTime: 2024-01-10 9:08:19'
('192.168.1.5', 57869),b'DateTime: 2024-01-10 9:08:23'
('192.168.1.5', 57870),b'DateTime: 2024-01-10 9:08:27'
can be modified to
('192.168.1.5', 57896),b'Pico2,DateTime: 2024-01-10 9:36:56'
('192.168.1.5', 57897),b'Pico2,DateTime: 2024-01-10 9:37:00'
('192.168.1.5', 57898),b'Pico2,DateTime: 2024-01-10 9:37:04'
('192.168.1.5', 57899),b'Pico2,DateTime: 2024-01-10 9:37:08'
('192.168.1.5', 57900),b'Pico2,DateTime: 2024-01-10 9:37:12'
So code for Raspberry Pi Pico W:
# GetTimePico2LocalPC_v2.py
import socket
import network
import time
# from time import strftime, localtimeimport os
import ntptime
import os
from machine import Pin, RTC
# Set variables
PicoID='Pico2'
ip4LocalPC= "192.168.1.10" # get IPAddress from PC from Wifi connect - It is IP4 address
port4PC=12345 # make sure same on Pico & PC code
timeAdj =1
ssid = 'WIFI_NAME'
password = 'WIFI_PASSWORD'
dataFileSize=30000 #30,000 bytes (30KB)
dataReadInterval=3 #1 second between readings (in 1 second intervals) so 10 minutes =600
blinkX=.5 #1 second between readings (in 1 second intervals) so 10 minutes =600
# Set things
machine.RTC().datetime()
led = machine.Pin('LED', machine.Pin.OUT) #configure LED Pin as an output pin and create and led object for Pin class
# Connect to Wifi
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
max_wait = 10
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
print('waiting for connection...')
time.sleep(1)
if wlan.status() != 3:
raise RuntimeError('network connection failed')
else:
print('connected')
status = wlan.ifconfig()
print( 'ip = ' + status[0] )
print('DeviceID= ' + PicoID )
## Update Time
print("Local time before synchronization: %s" % str(time.localtime()))
# Synchronize the time with the NTP server
ntptime.settime()
# Check the local time after synchronization
print("Local time after synchronization: %s" % str(time.localtime()))
## Update Time End
def timeNow():
now = time.localtime()
if now[1]<10: #month
m1=str(now[1])
m2=("0{}".format(m1))
#
if now[2]<10: #day
d1=str(now[2])
d2=("0{}".format(d1))
else:
d2=now[2]
if(now[3]+ timeAdj) <10: #hr
h1=str(now[3]+ timeAdj)
h2=("0{}".format(h1))
else:
h2=now[3]
if now[4]<10:
mm1=str(now[4])
mm2=("0{}".format(mm1))
else:
mm2=now[4]
if now[5]<10:
s1=str(now[5])
s2=("0{}".format(s1))
else:
s2=now[5]
timeStamp="DateTime: {}-{}-{} {}:{}:{}".format(now[0], m2, d2, h2, mm2, s2)
return timeStamp
with open('savedata.txt', 'a') as f:
f.write('Test Data Logging on ' + PicoID)
f.write('\n')
def logData():
# https://electrocredible.com/raspberry-pi-pico-w-data-logger-temperature-micropython/
file_size=os.stat('/savedata.txt')
if(file_size[6]<dataFileSize):
try:
with open('savedata.txt', 'a') as f:
f.write(timeNow())
# f.write('Temperature='+ str(temperature_celcius))
f.write(','+'\n')
except:
print("Error! Could not save")
def dataToLocal(IpLocal, port1, SensData):
# Create a TCP socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect to the PC server (replace with your PC's IP and port)
s.connect((IpLocal, port1))
# s.connect(("192.168.1.10", 12345))
# Read sensor data (replace with your specific sensor logic)
sensor_data = SensData # 'Tuesday 9 January 2024- The rain in spain falls mainly on the plain '
# Send sensor data to the PC
s.sendall(sensor_data.encode())
# Close the socket connection
s.close()
print('Data Sent closed')
def blinkWrite(dataReadInterval):
led.value(True) #turn on the LED
time.sleep(1) #wait for one second
led.value(False) #turn off the LED
time.sleep(1) #wait for one second
def infoData():
inf1= PicoID + ','+ timeNow()
return inf1
x=0
while True:
led.value(True) #turn on the LED
time.sleep(blinkX) #wait for one second
led.value(False) #turn off the LED
time.sleep(blinkX) #wait for one second
x= x+1
print(x)
logData()
# dataToLocal(ip4LocalPC, port4PC, timeNow())
dataToLocal(ip4LocalPC, port4PC, infoData())
print(timeNow())
time.sleep(dataReadInterval)
And code for Local PC , win10 (in python):
# ConnectPico2.py
# v2 - try writing data from listener to file
import time
import socket
# Create a TCP/IP socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to a specific IP address and port
server_address = ("0.0.0.0", 12345) # Listen on all available IPs, port 12345
server_socket.bind(server_address)
# Listen for incoming connections
server_socket.listen(1)
# Accept incoming connections
while True:
client_socket, client_address = server_socket.accept()
print("Connected by", client_address)
try:
# Receive data in chunks
while True:
data = client_socket.recv(1024) # Receive up to 1024 bytes at a time
# print("y",data)
if not data:
break
# Process the received data (e.g., print, store, analyze)
print("Received data:", data.decode())
with open('output_data.txt', 'a') as f: # Open file in append mode
# try:
print("1", data)
f.write(f"{client_address},{data}\n")
time.sleep(1) # Adjust interval as needed
except Exception as e:
print(f"Error receiving or processing data: {e}")
finally:
# Close the client socket
client_socket.close()
# Close the server socket when finished
server_socket.close()
End comment
I sent this from Obsidian to WordPress using the WordPress add-in in Obsidian. Which did an OK job but I still needed to do a bit of a tidy up.
Actual code took me a while to get the output to file that I wanted, that took quite a bit of fiddling and testing until I got roughly some of the variables in the output string (eg pico ID). But now that is in the string I can manipulate with something like pandas to tidy up the data.
It might be good to read the stream and then send it to different files based on the ID, so that only data relevant to specific sensors will be sent to a specific file.
you need the Sensor ID as the IP Addree of these devices can change, depending on when they are started and stopped.