用Python 將 adxl345(esp8266) 資料寫到 firebase

1.adxl 345 連接到 esp8266

其中

SCL 接到 D1  , SDA 接到 D2 ,VCC 接3.3V  ,GND 接 GND

2.用ampy 將ADXL345.py 匯入到 esp8266上

2.1 安裝esptool

pip install esptool,  

執行 esptool --port COM4 erase_flash (windows 環境下 用COM4)

載入esp8266-20190529-v1.1.bin 檔案

執行 esptool --port COM4 write_flash --flash_size=detect 0 esp8266-20190529-v1.1.bin

2.2 安裝 ampy

pip install  adafruit-ampy 

將ADXL345.py 匯入到 esp8266  (https://github.com/adafruit/Adafruit_Python_ADXL345/tree/master/Adafruit_ADXL345)

執行 ampy --port COM4 put ADXL345.py

檢查 ampy --port COM4 ls

   會出現 boot.py,  ADXL345.py 兩個

3.用putty 連上 esp8266 看看

* 注意  Row control 要設成None

會出現>>> python 操作畫面

測試一下  import ADXL345  如果下一行,則沒問題了

3.到firebase 建立 realtime database ,

* 新增專案,輸入名稱,建立專案,選擇資料庫  Realtime(選擇測試模式啟動) ,

路徑為  https://test-a54dd.firebase.com

4.在putty 測試程式效果,

>>>  按下 ctrl+E 會出現     ===  

這時後將程式貼上

然後按下 ctrl+D 執行

程式如下:

import urequests

import network

import time

import machine

from machine import Pin,I2C

import ADXL345

#import BlynkLib

SSID = 'cef'

PASSWORD = '0912882881'

URL = 'https://python8266.firebaseio.com/data.json'

i2c = I2C(scl=Pin(5),sda=Pin(4), freq=10000)

adx = ADXL345.ADXL345(i2c)

#BLYNK_AUTH = 'hHOqA_BzqkT4ItQiaQt3RAauPLqtUjaG'

#blynk = BlynkLib.Blynk(BLYNK_AUTH)

def do_connect():

    # Set to station mode for connecting to network

    sta_if = network.WLAN(network.STA_IF)

    if not sta_if.isconnected():

        print('Connecting to network...')

        sta_if.active(True)

        sta_if.connect(SSID, PASSWORD)

        # Wait until connected

        while not sta_if.isconnected():

            pass

        print('Network connected!')

def flash_led():

    led = machine.Pin(2, machine.Pin.OUT)

    led.value(0) # In some hardware modules, the on/1 and off/0 is reversed; so here the 0 represents on on my module

    time.sleep(0.2)

    led.value(1)

def get_data():

    # Now dummy, should be retrieved from sensors

    x=adx.xValue

    y=adx.yValue

    z=adx.zValue

    roll, pitch = adx.RP_calculate(x,y,z)

    return { 'x': x, 'y':y, 'z':z, 'roll':roll, 'pitch':pitch} 

#return { 'temperature': 25.6 }

def send_data(data):

    print('Sending data...')

    res = urequests.put(URL, json=data)

    print('Response: {}'.format(res.text))

    flash_led() # Indicate successful data transmission

def main():

    # Connect to network

    do_connect()

    # Keep posting sensor data at a certain interval

    while True:

        send_data(get_data())

        time.sleep(0.5)

main()

5.這時候可以看到輸出結果


你可能感兴趣的:(用Python 將 adxl345(esp8266) 資料寫到 firebase)