Simple Server controlled LED -- MicroPython with Ameba

MENG XI

Mar 18, 2020
52
Joined
Mar 18, 2020
Messages
52
[SIZE=.9375rem]This is [/SIZE]MicroPython project developed using Ameba RTL8722.
[SIZE=.9375rem]MicroPython is offically supported on Ameba RTL8722, so through this demo video, we will see how easy and fast it is to develop a simple server socket on Ameba, which would then control other peripheral to perform other tasks.[/SIZE]
[SIZE=.9375rem]Here we are using a client socket code running on PC to send a 'Hello, world' string via the WiFi network, Ameba receives it and if it is indeed 'Hello, world' then it will blink the LED.[/SIZE]
 
[SIZE=.9375rem]Check out the demo video down below,[/SIZE]
[SIZE=.9375rem]https://youtu.be/pEMkwvw-r18[/SIZE]
 
Code used:
Code:
#!/usr/bin/env python3

#PC Client code

import socket

HOST = '192.168.1.152'  # The server's hostname or IP address
PORT = 80        # The port used by the server

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((HOST, PORT))
    s.send(bytes('Hello, world','utf8'))
Code:
#Ameba Code

from machine import Pin
from wireless import WLAN
import time
import socket

wifi = WLAN(WLAN.STA)
wifi.connect("MPSSID","upyameba")
led = Pin("PB_4",Pin.OUT)

s = socket.SOCK()
port = 80
data = ' '

s.bind(port)
s.listen()

conn, addr = s.accept()

while True:
	data = conn.recv(1024)
	print(data)
	if data == b'Hello, world':
		print('turn on LED 1')
		for i in range(6):
			led.toggle()
			time.sleep_ms(200)
	if not data:
		break
 
 
Top