Giter VIP home page Giter VIP logo

micropython-rshell's Introduction

micropython-rshell

how to connect to ESP32/DHT11 with rshell

Connect to ESP32 with DHT11 with terminal.

pi@raspberrypi:~ $ ls /dev/*USB*
/dev/ttyUSB0

pi@raspberrypi:~ $ rshell -p /dev/ttyUSB0

Using buffer-size of 32
Connecting to /dev/ttyUSB0 (buffer-size 32)...
Trying to connect to REPL  connected
Testing if ubinascii.unhexlify exists ... Y
Retrieving root directories ... /DHT11_pub.py/ /DHT11_pub2.py/ /boot.py/ /ftp.py/ /main.old/ /main.py/ /main.tmp/ /main2.py/ /main_moulin.py/ /main_old.py/ /microWebSocket.py/ /microWebSrv.py/ /microWebSrv2.png/ /microWebTemplate.py/ /min.py/ /start.py/ /start2.py/ /start2b.py/ /umqtt/ /uping.py/ /www/
Setting time ... Apr 24, 2022 08:57:36
Evaluating board_name ... pyboard
Retrieving time epoch ... Jan 01, 2000
Welcome to rshell. Use Control-D (or the exit command) to exit rshell.


/home/pi> ls /pyboard
umqtt/              main.py             microWebTemplate.py main.old           
www/                main2.py            min.py              main.tmp           
DHT11_pub.py        main_moulin.py      start.py            microWebSrv2.png   
DHT11_pub2.py       main_old.py         start2.py          
boot.py             microWebSocket.py   start2b.py         
ftp.py              microWebSrv.py      uping.py           

# main.py is launched when ESP32 reboots.
/home/pi> cat /pyboard/main.py

# python START

import network
from time import sleep
from umqtt.simple import MQTTClient
from machine import Pin
import uping
from dht import DHT11

led = Pin(2, Pin.OUT)
sensor = DHT11(Pin(15, Pin.IN, Pin.PULL_UP))

station = network.WLAN(network.STA_IF)
station.active(True)

# IP of broker server
SERVER = '192.168.0.184'
station.connect(SSID,PASSWORD)
while station.isconnected() == False:
	pass

print('Connection successful')
print(station.ifconfig())

#flash once
led.value(1)
sleep(0.1)
led.value(0)
sleep(1)

#SERVER = 'node02.myqtthub.com' #MQTT Server address ip = 185.195.98.58 using ping to get

CLIENT_ID = 'ESP8266_sensor'
TOPIC = b'temp_humidity'
USER = 'sensor'
PASSW = 'sensor01'
PORT = 0

#client = MQTTClient(CLIENT_ID, SERVER) #, PORT, USER, PASSW)
client = MQTTClient(CLIENT_ID, SERVER, PORT, USER, PASSW)
#print(client)
state = client.connect()
#print(state)

#flash once
led.value(1)
sleep(0.1)
led.value(0)
sleep(1)

#msg = (b'DH11 speaking ....')
#client.publish(TOPIC, msg)

while True:
    try:
        sensor.measure()
        sleep(0.1)
        t = sensor.temperature()
        h = sensor.humidity()
        if isinstance(t, int) and isinstance(h, int):
            #msg = (b'0, 0')
            #client.publish(TOPIC, msg)
            msg = (b'{0:3.1f}, {1:3.1f}'.format(t,h))
            client.publish(TOPIC, msg)
            #flash twice
            led.value(1)
            sleep(0.1)
            led.value(0)
            sleep(0.1)
            led.value(1)
            sleep(0.1)
            led.value(0)
            print(msg)
            sleep(20)
        else:
            print('Invalid sensor readings.')
    except OSError:
        print('Failed to read sensor.')
        try:
            client.connect()
        except:
            led.value(1)
            sleep(0.1)
            led.value(0)
# python END

/home/pi> 

CTRL-D to quit.

# REPL

# Enter REPL from rshell
# You can execute directly python programs

/home/pi> repl
Entering REPL. Use Control-X to exit.
>
MicroPython v1.13 on 2020-09-02; ESP32 module with ESP32
Type "help()" for more information.
>>> 
>>> import main.py
I (48681) phy: phy_version: 4180, cb3948e, Sep 12 2019, 16:39:13, 0, 0
Connection successful

CTRL-X to exit REPL.

# Return to rshell
/home/pi> 

CTRL-D to exit rshell.

micropython-rshell's People

Contributors

jdavid54 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.