Jump to content
Electronics-Lab.com Community

Simple Server controlled LED -- MicroPython with Ameba


MENG XI
 Share

Recommended Posts

This is MicroPython project developed using Ameba RTL8722.
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.
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.
 
Check out the demo video down below,
 
Code used:
#!/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'))

 

#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

 

 
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.