Simple Server Controlled LED -- MicroPython With Ameba

by xidameng in Circuits > Microcontrollers

13 Views, 0 Favorites, 0 Comments

Simple Server Controlled LED -- MicroPython With Ameba

Simple Server controlled LED -- MicroPython with Ameba

This is MicroPython project developed using Ameba RTL8722 from Realtek.

MicroPython is offically supported on Ameba RTL8722 WiFi microcontroller, we will see, in this demo, 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 an example client socket code from online that sends a 'Hello, world' string via the WiFi network, Ameba receives it and if it is indeed 'Hello, world' then it will blink the LED.

Supplies

Ameba RTL8722 x1

Connectino and Code

Screenshot 2020-11-24 184101.jpg
socketServerLED.jpg

Check the picture above for connections between ameba and LED. After connecting everything together, let's check out the code below,

#!/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

Hope You Enjoy!

Screenshot 2020-11-24 210032.jpg

Now follow steps in the video to run python code on both PC and ameba and see the LED blinking!

Hope you enjoy and happy coding~