Protocol: TCP/İP Socket
Scheme:
[Client]Maixduino AiOT(ESP32) ---LiveStreaming--> [Server] Computer
Micropython Language Maixduino Board(in ESP32):
The k210 take sensor image. Esp32 sending data with tcp/ip protocol.
Code: Select all
'''
SSID = "MW40V_DE9B"
PASW = "XR608obv57"
import sensor, image, time
#Nz9kYLvX
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
# This file is part of MaixPY
# Copyright (c) sipeed.com
#
# Licensed under the MIT license:
# http://www.opensource.org/licenses/mit-license.php
#
#network_wiznet5k()
def enable_esp32():
from network_esp32 import wifi
if wifi.isconnected() == False:
for i in range(5):
try:
# Running within 3 seconds of power-up can cause an SD load error
# wifi.reset(is_hard=False)
wifi.reset(is_hard=True)
print('try AT connect wifi...')
wifi.connect(SSID, PASW)
if wifi.isconnected():
break
except Exception as e:
print(e)
print('network state:', wifi.isconnected(), wifi.ifconfig())
enable_esp32()
def enable_espat():
from network_espat import wifi
if wifi.isconnected() == False:
for i in range(5):
try:
# Running within 3 seconds of power-up can cause an SD load error
# wifi.reset(is_hard=False)
wifi.reset()
print('try AT connect wifi...')
wifi.connect(SSID, PASW)
if wifi.isconnected():
break
except Exception as e:
print(e)
print('network state:', wifi.isconnected(), wifi.ifconfig())
#enable_espat()
def network_wiznet5k():
from network_wiznet5k import lan
from machine import SPI
from Maix import GPIO
if lan.isconnected() == False:
WIZNET5K_SPI_SCK = 21
WIZNET5K_SPI_MOSI = 8
WIZNET5K_SPI_MISO = 15
WIZNET5K_SPI_CS = 20
spi1 = SPI(4, mode=SPI.MODE_MASTER, baudrate=600 * 1000,
polarity=0, phase=0, bits=8, firstbit=SPI.MSB, sck=WIZNET5K_SPI_SCK, mosi=WIZNET5K_SPI_MOSI, miso=WIZNET5K_SPI_MISO)
for i in range(5):
try:
lan.reset(spi1, WIZNET5K_SPI_CS)
print('try connect lan...')
if lan.isconnected():
break
except Exception as e:
print(e)
print('network state:', lan.isconnected(), lan.ifconfig())
# network_wiznet5k()
#time.sleep(10)
import socket
ADDR = ("192.168.1.108", 23456)
sock = socket.socket()
sock.connect(ADDR)
sock.settimeout(1)
while 1:
'''
--> Bellleğe görüntüyü yazmaya çalış..
'''
#data = image.compressed(20)
#img = image.compressed([quality=50])
img = sensor.snapshot()
sock.send(img)
#İMG type is <İMGCLASS>
#img to bytes
#print(img.size())
#sock.send()
#data = sock.recv(10) # old maxipy have bug (recv timeout no return last data)
#print(data) # fix
try:
data = b""
while True:
tmp = sock.recv(262144)
print(tmp)
if len(tmp) == 0:
raise Exception('timeout or disconnected')
data += tmp
except Exception as e:
print("rcv:", len(data), data)
#time.sleep(2)
sock.close()
Python3 Language My Computer code:
Code: Select all
import socket, cv2, pickle
import numpy as np
from io import BytesIO
HOST = "192.168.1.108" # Standard loopback interface address (localhost)
PORT = 23456 # Port to listen on (non-privileged ports are > 1023)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
print(f"Connected by {addr}")
while True:
data = conn.recv(262144)
#data=pickle.loads(data)
print(type(data))
data = cv2.imdecode(data, cv2.IMREAD_COLOR)
print(type(data))
cv2.imshow('server', data) #to open image