625 views|5 replies

280

Posts

7

Resources
The OP
 

[Digi-Key Follow me Issue 2] + Task 2: Use of network functions [Copy link]

 

1. Create a WiFi hotspot

Circuitpython is really good. The function of creating a WiFi hotspot only requires this one line of code:

wifi.radio.start_ap(HOT_POINT_SSID, HOT_POINT_PASSWORD)

Code:

import wifi

wifi.radio.start_ap(ssid='hahaha', password='12345678')

Figure 1. Creating a WiFi hotspot

2. Connect to WiFi

Connecting to wifi is also very simple, just this line of code:

wifi.radio.connect(secrets["ssid"],secrets["password"])

Figure 2. Connecting to WiFi

3. Connect to computer to send and receive messages

The following is a TCP communication test. The development board is connected to the TCP server on the computer. The computer sends a message to the development board. After the development board receives it, it prints it out in the terminal and sends it back to the computer. The program is as follows.

import wifi

wifi.radio.connect('xxxxxxxxxxxxxx', 'xxxxxxxxxxxxxx')
print(f"My IP address: {wifi.radio.ipv4_address}")

import socketpool
import time

HOST = "192.168.31.228"
PORT = 7777
TIMEOUT = 30


pool = socketpool.SocketPool(wifi.radio)
print("Creating Socket")
sock=pool.socket(pool.AF_INET, pool.SOCK_STREAM)
sock.settimeout(TIMEOUT)
sock.connect((HOST, PORT))
sent = sock.send(b"start\n")
while  True:    
    buff = bytearray(5)
    numbytes = sock.recv_into(buff)
    if(numbytes > 0):
        print(buff)
        sent = sock.send(buff)
    time.sleep(1)

The operating effect is as follows.

Figure 3: Transmitting and receiving test

The wheels made on Circuitpython are very complete, and most common functions can be implemented with just a few lines of code, making it relatively easy to get started.

This post is from DigiKey Technology Zone

Latest reply

Python is indeed convenient, but how do you design the various firmware yourself? The while part in wifi is indispensable.   Details Published on 2023-9-1 09:06
 
 

6748

Posts

2

Resources
2
 

If the computer's TCP service is not enabled, and the computer's TCP service is enabled only after the microcontroller is started, can I still connect to the computer's TCP service?

This post is from DigiKey Technology Zone

Comments

Not tested, will try again later  Details Published on 2023-8-31 17:09
 
 
 

280

Posts

7

Resources
3
 
wangerxian posted on 2023-8-31 14:12 If the computer's TCP service is not enabled, and the computer's TCP service is enabled only after the microcontroller is started, can I still connect to the computer's TCP service?

Not tested, will try again later

This post is from DigiKey Technology Zone

Comments

sock.connect((HOST, PORT)) If the PC server is not turned on for about 20 seconds after running this line of instruction, an error will be reported. If it is turned on during this period, the connection will be established.  Details Published on 2023-8-31 18:34
 
 
 

280

Posts

7

Resources
4
 
sipower posted on 2023-8-31 17:09 Not tested, will try later

sock.connect((HOST, PORT))

About 20 seconds after this line of command is run, if the PC server is not opened, an error will be reported. If it is opened during this period, the connection will be possible.

This post is from DigiKey Technology Zone
 
 
 

6027

Posts

6

Resources
5
 

Python is indeed convenient, but how do you design the various firmware yourself? The while part in wifi is indispensable.

This post is from DigiKey Technology Zone

Comments

It seems that microPython supports multitasking, but I haven’t studied it that deeply yet.  Details Published on 2023-9-1 09:19
Personal signature

在爱好的道路上不断前进,在生活的迷雾中播撒光引

 
 
 

280

Posts

7

Resources
6
 
Qintianqintian0303 posted on 2023-9-1 09:06 Python is indeed convenient, but how can I design various firmwares by myself? The while part in wifi is indispensable

It seems that microPython supports multitasking, but I haven’t studied it that deeply yet.

This post is from DigiKey Technology Zone
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list