Genestic12
Posts: 9
Joined: Fri Jul 20, 2018 2:05 am

I need help to send time stamp to PC

Tue Aug 14, 2018 5:43 am

Hello.

I've currently set up my Pi and a simple Python program so that when I press a button, an LED blinks and Python prints a timestamp of when the button was pressed.

Now, I'm trying to extend this so that when I press the button, the Pi sends a message with the timestamp to my laptop as a notification(or not, as long as the message gets sent in some method it's good).

What do I have to do to do this, and how would the code need to be changed?

Code: Select all

import RPi.GPIO as GPIO
import time
import datatime

SW=7

LED=11

GPIO.setmode(GPIO.BOARD)

GPIO.setup(SW, GPIO.IN, pull_up_down=GPIO.PUD_UP)

GPIO.setup(LED, GPIO.OUT, initial=GPIO.LOW)

try:
     while True:
           a=GPIO.input(SW)
           if a==0:
               ts=time.time()
               readable=datetime.datetime.frofromtimestamp(ts).isoformat()
               print 'Button pressed at', readable
               for i in range(0,10):
                    GPIO.output(LED,GPIO.HIGH)
                    time.sleep(0.5)
                    GPIO.output(LED,GPIO.LOW)
                    time.sleep(0.5)

except KeyboardInterrupt:
     pass

GPIO.cleanup()

bertlea
Posts: 164
Joined: Wed Dec 07, 2016 6:33 am
Location: Hong Kong

Re: I need help with a personal project

Tue Aug 14, 2018 6:24 am

Assume you didn’t mean send “email” to your PC….

So you need to have some kind of server running at your PC to wait for the message and display it. Then in your Raspberry Pi, your Python code need to send the ¬message that contains the “time-stamps” to the PC. This can be done in different ways and your Raspberry Pi and PC need to be connected, say, by the public Internet or LAN.

The simplest way for Raspberry Pi to send out message is using the “socket” package to send UDP datagram to another system by specifying its IP address and port number. Notes, UDP is not guaranteed delivery protocol so if you need reliable communication, you need to use better (higher level) protocol. At the PC side, you need a server running listen to the port you specified. If your PC is running Linux, then you can simply use nc commond to listen to the port and it will show what it got in the terminal.

However, a more common way is that your Raspberry running your Python code is also act as a web server serving a page for PC to open and polling webpage in order to monitor the button press status. Then when your Python code detects the button event, modify the webpage to reflect the latest status. A even better way is to use WebSocket instead of a polling webpage so you can push the update immediately to the PC’s browser.

Genestic12
Posts: 9
Joined: Fri Jul 20, 2018 2:05 am

Re: I need help with a personal project

Tue Aug 14, 2018 9:10 am

bertlea wrote:
Tue Aug 14, 2018 6:24 am
Assume you didn’t mean send “email” to your PC….

So you need to have some kind of server running at your PC to wait for the message and display it. Then in your Raspberry Pi, your Python code need to send the ¬message that contains the “time-stamps” to the PC. This can be done in different ways and your Raspberry Pi and PC need to be connected, say, by the public Internet or LAN.

The simplest way for Raspberry Pi to send out message is using the “socket” package to send UDP datagram to another system by specifying its IP address and port number. Notes, UDP is not guaranteed delivery protocol so if you need reliable communication, you need to use better (higher level) protocol. At the PC side, you need a server running listen to the port you specified. If your PC is running Linux, then you can simply use nc commond to listen to the port and it will show what it got in the terminal.

However, a more common way is that your Raspberry running your Python code is also act as a web server serving a page for PC to open and polling webpage in order to monitor the button press status. Then when your Python code detects the button event, modify the webpage to reflect the latest status. A even better way is to use WebSocket instead of a polling webpage so you can push the update immediately to the PC’s browser.
Thank you very much for the advice.

However I am not very skilled in computer terminology so I don't really know what you mean by 'UDP datagram', 'socket package' etc.

About the python code acting as a web server serving a page and modifying it whenever it detects the button press (and WebSocket), could you elaborate a bit more on how to do this?

bertlea
Posts: 164
Joined: Wed Dec 07, 2016 6:33 am
Location: Hong Kong

Re: I need help with a personal project

Tue Aug 14, 2018 10:01 am

Maybe we can try some simple way to use the UDP first. I noticed you are using Pyhon 2 which I never tried, so the code may not even run.... :?

Anyway, on the Pi, you can try the slightly modified code that I just send out the message to port 4567 to the local system, you need to change the IP in the code from "127.0.0.1" to the IP of your PC.

Code: Select all

import RPi.GPIO as GPIO
import time
import datatime
import socket

SW=7

LED=11

GPIO.setmode(GPIO.BOARD)

GPIO.setup(SW, GPIO.IN, pull_up_down=GPIO.PUD_UP)

GPIO.setup(LED, GPIO.OUT, initial=GPIO.LOW)

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

try:
     while True:
           a=GPIO.input(SW)
           if a==0:
               ts=time.time()
               readable=datetime.datetime.frofromtimestamp(ts).isoformat()
               print 'Button pressed at', readable
			   sock.sendto('button pressed at ' + readable, ("127.0.0.1", 4567))
               for i in range(0,10):
                    GPIO.output(LED,GPIO.HIGH)
                    time.sleep(0.5)
                    GPIO.output(LED,GPIO.LOW)
                    time.sleep(0.5)

except KeyboardInterrupt:
     pass

GPIO.cleanup()
Then on your PC, I assume you also got Python 2, then you can run the server: (* keep the IP "127.0.0.1" in this server code, don't change it)

Code: Select all

import time
import socket

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(("127.0.0.1", 4567))

while True:
    data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
    print "received message:", data
    time.sleep(0.1)
After you started the server on the PC, then you can run and try your button on the Pi. Assume your Pi and the PC are connected via network already.

*** Just the concept, as I am also learning Python (3), so the code may not even run, but you can try to see what I am doing there. I am not sure how the server can exit, you may need to try Ctrl-C or even close the terminal (cmd) window to stop it.

Genestic12
Posts: 9
Joined: Fri Jul 20, 2018 2:05 am

Re: I need help to send time stamp to PC

Tue Aug 14, 2018 10:58 am

Thank you very much.

Side note, I'm currently trying to get my Pi to connect to my school's wifi (which, yes, is a WPA2 Enterprise) so that it can stay connected to the internet even when outside the lab I have set up my Pi in. The lab has a personal wifi of its own (WPA2 Personal) and the Pi can connect to that but not the school wifi; when I open the wifi tab the school wifi is grayed out.

I've been using this guide https://gist.github.com/chatchavan/3c58511e3d48f478b0c2, in which Step 3 it says to edit /etc/network/interface, except in my /network folder it says interfaces, and that file is empty except for this:
#interfaces(5_ file used by ifup(8) and ifdown(8)

#Please note that this file is written to be used with dhcpcd.
#For static TP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'

#Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

I've tried simply adding

Code: Select all

network={
ssid="YOUR_NETWORK_NAME"
proto=RSN
key_mgmt=WPA-EAP
pairwise=CCMP TKIP
group=CCMP TKIP
identity="YOUR_USER_NAME"
password=hash:YOUR_PASSWORD_HASH
phase1="peaplabel=0"
phase2="auth=MSCHAPV2"
}
(yes, I filled out the stuff I'm supposed to change) to the bottom and saving, however when I try doing 'sudo ifdown wlan0' it says unkown interface wlan0. I don't know why that file is empty and why this isn't working, any advice?

bertlea
Posts: 164
Joined: Wed Dec 07, 2016 6:33 am
Location: Hong Kong

Re: I need help to send time stamp to PC

Tue Aug 14, 2018 11:31 am

I suggest you make a new post to focus on your wifi network issue, so that someone got good knowledge on that area can help you.

hippy
Posts: 3590
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: I need help to send time stamp to PC

Tue Aug 14, 2018 11:31 am

I imagine MQTT or similar could be used so there's no need to mess about with low-level stuff like UDP datagrams and sockets.

Genestic12
Posts: 9
Joined: Fri Jul 20, 2018 2:05 am

Re: I need help to send time stamp to PC

Tue Aug 14, 2018 11:50 am

Image
It caused an error which I don't understand because I don't know how to use socket.

bertlea
Posts: 164
Joined: Wed Dec 07, 2016 6:33 am
Location: Hong Kong

Re: I need help to send time stamp to PC

Wed Aug 15, 2018 2:22 am

Sorry, I don't know what's wrong with the Python 2.7 on Windows, I don't have Python 2.7 on Windows to test. I suspect Windows 10 may not allow your code to listen to that port. I tested that "server" running on my Raspberry Pi Zero W and it is working fine. Do you have another Pi to connect to the network and use that as the "server" instead of your Windows 10 PC?

Genestic12
Posts: 9
Joined: Fri Jul 20, 2018 2:05 am

Re: I need help to send time stamp to PC

Wed Aug 15, 2018 3:23 am

Unfortunately I don't. I only have my Pi and my PC.

Would running a webpage from my Pi via Node.js be a viable option?

bertlea
Posts: 164
Joined: Wed Dec 07, 2016 6:33 am
Location: Hong Kong

Re: I need help to send time stamp to PC

Wed Aug 15, 2018 4:16 am

Would running a webpage from my Pi via Node.js be a viable option?
Yes, use a web page is a viable option but that needs more effort. If your goal is to learn, yes, that should be a more meaningful path. However, why Node.js? Which part you want to use the Node.js?

If you know Node.js, already then I got an idea. Just use Node.js on your PC to listen to the UDP message while your Pi keep using the sample Python to send out the UDP message to your PC. The Node.js code should be:

Code: Select all

const dgram = require('dgram');
const server = dgram.createSocket('udp4');

server.on('error', (err) => {
  console.log(`server error:\n${err.stack}`);
  server.close();
});

server.on('message', (msg, rinfo) => {
  console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`);
});

server.on('listening', () => {
  const address = server.address();
  console.log(`server listening ${address.address}:${address.port}`);
});

server.bind(4567);

Genestic12
Posts: 9
Joined: Fri Jul 20, 2018 2:05 am

Re: I need help to send time stamp to PC

Wed Aug 15, 2018 4:43 am

Well I searched up WebSocket after your first answer and I found this
https://www.w3schools.com/nodejs/nodejs ... socket.asp and then I noticed 'Node.js Tutorial' on the list on the left. I thought I'd give it a try since it seemed to be able to help me out with this.

I don't actually know Node.js; I was currently following the tutorials and setting up demo modules.

Return to “Beginners”

Who is online

Users browsing this forum: Karathos and 21 guests