Skip to content

Instantly share code, notes, and snippets.

@Nekodigi
Created February 18, 2021 08:16
Show Gist options
  • Select an option

  • Save Nekodigi/52a9d9e67e18ba1e941c65b12813f87c to your computer and use it in GitHub Desktop.

Select an option

Save Nekodigi/52a9d9e67e18ba1e941c65b12813f87c to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Pi Camera image transport to PC using socket.\n",
"## Server-side program"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Socket created.\n",
"Socket bind comlete.\n",
"Connected to: 192.168.0.7:45852\n",
"Store command received. Time to save a picture\n",
"Opened the file.\n",
"Time:0.025022268295288086\n",
"closed\n",
"Connected to: 192.168.0.7:45854\n",
"Store command received. Time to save a picture\n",
"Opened the file.\n",
"Time:0.026022672653198242\n",
"closed\n",
"Connected to: 192.168.0.7:45856\n",
"Store command received. Time to save a picture\n",
"Opened the file.\n",
"Time:0.0\n",
"closed\n",
"Connected to: 192.168.0.7:45858\n",
"Store command received. Time to save a picture\n",
"Opened the file.\n",
"Time:0.0\n",
"closed\n",
"Connected to: 192.168.0.7:45860\n",
"Store command received. Time to save a picture\n",
"Opened the file.\n",
"Time:0.001001119613647461\n",
"closed\n",
"Connected to: 192.168.0.7:45862\n",
"Store command received. Time to save a picture\n",
"Opened the file.\n",
"Time:0.01601433753967285\n",
"closed\n",
"Connected to: 192.168.0.7:45864\n",
"Store command received. Time to save a picture\n",
"Opened the file.\n",
"Time:0.017015457153320312\n",
"closed\n",
"Connected to: 192.168.0.7:45866\n"
]
},
{
"ename": "UnicodeDecodeError",
"evalue": "'utf-8' codec can't decode byte 0xff in position 6: invalid start byte",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mUnicodeDecodeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-12-a0ba29a765fc>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 64\u001b[0m \u001b[1;31m#try:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 65\u001b[0m \u001b[0mconn\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0msetupConnection\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 66\u001b[1;33m \u001b[0mdataTransfer\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mconn\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 67\u001b[0m \u001b[1;31m#except:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 68\u001b[0m \u001b[1;31m#break\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32m<ipython-input-12-a0ba29a765fc>\u001b[0m in \u001b[0;36mdataTransfer\u001b[1;34m(conn)\u001b[0m\n\u001b[0;32m 40\u001b[0m \u001b[1;31m# Receive the data\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 41\u001b[0m \u001b[0mdata\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mconn\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrecv\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1024\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# receive the data\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 42\u001b[1;33m \u001b[0mdata\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdata\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdecode\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'utf-8'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 43\u001b[0m \u001b[1;31m# Split the data such that you separate the command\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 44\u001b[0m \u001b[1;31m# from the rest of the data.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mUnicodeDecodeError\u001b[0m: 'utf-8' codec can't decode byte 0xff in position 6: invalid start byte"
]
}
],
"source": [
"#based on this site http://thezanshow.com/electronics-tutorials/raspberry-pi/tutorial-30\n",
"import socket\n",
"import time\n",
"import traceback\n",
"\n",
"host = 'XXX.XXX.XXX.XXX'#please set this PC ip address\n",
"port = 5560\n",
"\n",
"def setupServer():\n",
" s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n",
" print(\"Socket created.\")\n",
" try:\n",
" s.bind((host, port))\n",
" except socket.error as msg:\n",
" print(msg)\n",
" print(\"Socket bind comlete.\")\n",
" return s\n",
"\n",
"def setupConnection():\n",
" s.listen(1) # Allows one connection at a time.\n",
" conn, address = s.accept()\n",
" print(\"Connected to: \" + address[0] + \":\" + str(address[1]))\n",
" return conn\n",
"\n",
"def storeFile():\n",
" picFile = open(\"image.png\", 'wb')\n",
" print(\"Opened the file.\")\n",
" startTime = time.time()\n",
" pic = conn.recv(1024)\n",
" while pic:\n",
" #print(\"Receiving picture still.\")\n",
" picFile.write(pic)\n",
" pic = conn.recv(1024)\n",
" picFile.close()\n",
" print(\"Time:{}\".format((time.time()-startTime)))\n",
"\n",
"def dataTransfer(conn):\n",
" # A big loop that sends/receives data until told not to.\n",
" while True:\n",
" # Receive the data\n",
" data = conn.recv(1024) # receive the data\n",
" data = data.decode('utf-8')\n",
" # Split the data such that you separate the command\n",
" # from the rest of the data.\n",
" dataMessage = data.split(' ', 1)\n",
" command = dataMessage[0]\n",
" if command == \"STORE\":\n",
" print(\"Store command received. Time to save a picture\")\n",
" storeFile()\n",
" reply = \"File stored.\"\n",
" break\n",
" else:\n",
" #print(\"unknown\")\n",
" reply = 'Unknown Command'\n",
" # Send the reply back to the client\n",
" conn.sendall(str.encode(reply))\n",
" #print(\"Data has been sent!\")\n",
" conn.close() \n",
" print(\"closed\")\n",
"\n",
"s = setupServer()\n",
"\n",
"while True:\n",
" #try:\n",
" conn = setupConnection()\n",
" dataTransfer(conn)\n",
" #except:\n",
" #break\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment