0

$USD

$USD
TUTORIALS MicroPythonESP32

MicroPython Tutorial: 
Lighting LED with UDP 


DFRobot Jun 21 2017 854

What is uPyCraft?

uPyCraft is an IDE runs on Windows and Mac, designed with simple interface and it is convenient to use.
To provide an easy and practical platform for fans of MicroPython, uPyCraft has many internal libraries.  



The Primer posters:

A brief Introduction of interface




Lighting LED with UDP (A Step by Step Study of Micro-Python)

Hardware list


Hardware list

FireBeetle ESP32 IOT Microcontroller (Supports Wi-Fi & Bluetooth) X1

FireBeetle Covers-Gravity I/O Expansion Shield X1

Ssd1306 oled Display (I2C interface) X1

LED X1

DuPont wires X6


Software list


Step1: Connecting FireBeetle Board-ESP32 to PC to prepare for uploading.





If the application is new or took from examples, you should choose download and run in the menu of tools after editing; but if it is already in your PC, you can just run it.


Step2: Code for FireBeetle Board-ESP32

import socket
import network
import time
from machine import Pin,I2C
import ssd1306
 
i2c = I2C(scl=Pin(22), sda=Pin(21), freq=100000)  
lcd=ssd1306.SSD1306_I2C(128,64,i2c)
led = Pin(27,Pin.OUT)    # configura gpio27──to D4
led.value(0) # Initialize to turn off the LED light.
 
port = 10000 #Interface 10000
SSID="*************" #yourssid
PASSWORD="****************" #your psw
wlan=None
s=None
 
def connectWifi(ssid,passwd): #Configure Wi-Fi
  global wlan
  wlan=network.WLAN(network.STA_IF)
  wlan.active(True)
  wlan.disconnect()
  wlan.connect(ssid,passwd)
 
  while(wlan.ifconfig()[0]=='0.0.0.0'):
    time.sleep(1)
  return True
 
try:
  if(connectWifi(SSID, PASSWORD) == True): #Sucessful connection
    s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)  
    ip=wlan.ifconfig()[0]
    s.bind((ip,port))
    print('waiting...')
    lcd.text("Connected...",0,0) #Display Connected in oled screen...
    while True:
      data,addr=s.recvfrom(1024)
      print('received:',data,'from',addr)
      lcd.clear()
      lcd.text("Received:",0,16) # Display text received in oled screen
      lcd.text(data,24,32)
      lcd.show()
      s.sendto(data+"***",addr) # Send to client side of udp, adding ‘***’
      if int(data)==1: #Character is 1
        led.value(1)    #Turn on LED light
      else:#Not 1
        led.value(0) #Turn off the LED light.
except:
  if (s):
    s.close()
  wlan.disconnect()
  wlan.active(False)


Step3: Connect hardware and allocate with UDP client side.

Connection list

Step4: When UDP client side send 1 or not send 1 and observe the change LED light.





Sum-up:

1. UpyCraft lower the barrier to play micro-python for ESP32, you need not to worry about hardware, tools, parameter (such as address allocation) or the board crash, break down.

Features: 


(1) The editing box is visually, supporting transmission between temp files and equipment files, convenient maintenance and click and download temp files for ESP32.

(2) The self-repairing of hardware which is as important as Arduino IDE comes into being.

(3) Just focus on the application and intention, you need not to worry about tools preparation, materials. The choice of UpyCraft save time and guarantee efficiency at the same time. Of course, your satisfaction will also be improved a lot.
      
2. ssd1306.py is a great library, but the original version do not have the function to clear the screen,  it is so lucky to found a modified version in the powerful github.com, the author is so nice to ass the function, I am sincerely suggest DFRobot groups can adopt it.


3. I have searched many UDP client side applications in the Google pay, I tried one which with send and feedback. Surely, you can also use what you accustomed to.


4. When we can control the LED light, then we can drive a small car. Obviously, we can achieve it by APP and ESP32 as a client side, ESP32-a controls ESP32-b directly.  


5. Arduino is a DIY toy, the same goes for micro-python for ESP32, we are waiting for more interesting toys from each play!


6. Thank you all again! Dear Jiang, Wang, Andy, we are really appreciate your efforts for such a fantastic IDE. Your efforts really impress me a lot!


P.s Please remember to click stop after the running of program.
The attachment is the new library: ss1306 and apk of UDP client side.