Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Louis_m
Published

WizFi360 :: Message Transfer [ MicroPython ] ::

A project that receives a message through the WizFi 360 and outputs it to the OLED display

BeginnerFull instructions provided244
WizFi360 :: Message Transfer [ MicroPython ] ::

Things used in this project

Story

Read more

Code

main.py

Python
from machine import Pin, I2C, UART, ADC
from ssd1306 import SSD1306_I2C
import os, sys
import utime


#Functions
def sendCMD_waitResp(cmd, timeout=1500):
    print("---> " + cmd)
    uart.write(cmd.encode('utf-8'))
    prvMills = utime.ticks_ms()
    response = b""
    while (utime.ticks_ms()-prvMills) < timeout:
        if uart.any():
            response = b"".join([response, uart.read(1)])
    
    if "AT+CIPSNTPTIME?\r\n" in cmd:
        return response
    print("<---", response)

# init UART #
uart = machine.UART(1, baudrate=115200, tx=Pin(4), rx=Pin(5))

# init I2C #
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
oled = SSD1306_I2C(128, 64, i2c)

# Temperature #
#sensor_temp = machine.ADC(4)
#conversion_factor = 3.3/(65535)

# AT + Cmd #
sendCMD_waitResp("AT+RST\r\n")
sendCMD_waitResp("AT\r\n")

utime.sleep(0.5)
sendCMD_waitResp("AT+CWMODE_CUR=1\r\n")
sendCMD_waitResp("AT+CWDHCP_CUR=1,1\r\n")
sendCMD_waitResp('AT+CWJAP="Louis_2g","louiswifi"\r\n') #AP connecting
sendCMD_waitResp("AT+CIPSTA_CUR?\r\n") #network CHK

sendCMD_waitResp("AT+CIFSR\r\n")
sendCMD_waitResp("AT+CIPSNTPCFG=1,9\r\n")
utime.sleep(3)

sendCMD_waitResp("AT+CIPMUX=1\r\n")
sendCMD_waitResp('AT+CIPSTART=0,"TCP","192.168.11.4",5000\r\n') #Server IP address

Data = bytes()
chat = bytes()

while True:
    oled.text("Start WizFi Chat", 0, 0)
    oled.show()
    if uart.any()> 0:
        Data = b"".join([Data, uart.read(100)])
        utime.sleep(0.5)
        r_data = Data.decode('utf-8')
        time_data = sendCMD_waitResp("AT+CIPSNTPTIME?\r\n")
        Data = b""
        oled.fill(0)
        chat = "[RECV]%s" %r_data[11:80]
        print(chat)
        oled.text(chat[:16], 0, 12)
        oled.text(chat[16:32], 0, 22)
        oled.text(chat[32:48], 0, 32)
        oled.text(chat[48:62], 0, 42)
        oled.text(time_data[34:50], 0, 54)
        
        oled.show()

Credits

Louis_m
19 projects • 9 followers
Contact

Comments

Please log in or sign up to comment.