Net core uses socket to communicate with raspberry pie

Time:2021-4-24

preface

Last year’s raspberry pie has been put in the drawer to eat ash, Debian 9 released a few days ago, and it’s no surprise that it supports raspberry pie.

So I took out the card reader again and installed the Debian desktop system again.

introduce

Now the current level of this thing is just to understand the communication between Python and. Net. The best version would be to remotely execute raspberry pie commands through the server.

We all know the reason for this. Many broadband networks don’t provide external IP now. As a result, families don’t have public IP, and they can’t control raspberry pie remotely. What I want to do is like penetrating ngrok through the internal network.

effect

Python side


#!/usr/bin/env python2
#-*- coding: utf-8 -*-
import socket
import threading
import os

host = '192.168.31.7'
port = 5001

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))

def Start():
 a = 0
 
 while True:
 a = a+1
 data = str(a).encode('utf8') + getCPUtemperature().encode('utf8')
 #s.send(str(a).encode('utf8'))
 print(data)
 #print(data)
 threading._sleep(10)

def getCPUtemperature():
 res = os.popen('vcgencmd measure_temp').readline()
 return(res.replace("temp=","").replace("'C\n",""))


if __name__ == '__main__':
 Start()

A very simple code fragment. We should be able to write a general prototype by this time next week. Now we will test the stability of the server on the server.

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.