「IoT」カテゴリーアーカイブ

pusherテスト

アプリ内メッセンジャーの制作で、リアルタイム性を出すためにいろいろ検討した。Node.jsはなんかめんどそう。WebSocketも面倒そう。

で、サンプルコードが理解できたpusherにする。

<テスト環境の構築>

MacのXAMPPでPHPのComposerがいるらしい

$ composer
-bash: composer: command not found

入ってないらしい。

https://blog.fire-sign.info/686/

を参考にインストする

$ curl -sS https://getcomposer.org/installer | php

インスト始まる

ll settings correct for using Composer
Downloading...

Composer (version 1.10.9) successfully installed to: /Users/ユーザ名/composer.phar

どうも composer.pahr を特定のディレクトリにいれなきゃらしい

$ sudo mv composer.phar /usr/local/bin/composer

移動できたのでバージョンチェック

$ composer --version
Composer version 1.10.9 2020-07-16 12:57:00

このバージョンが入りましたとさ

肝心のpusherを入れる

$ composer require pusher/pusher-php-server
Using version ^4.1 for pusher/pusher-php-server
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files

でインスト終わる。

うまくいかない

$ sudo nano .bash_profile

export PATH=$PATH:/Applications/XAMPP/bin

を追加(Mac)

source ~/.bash_profile

して実行

 

支援デバイス Ver6コード

支援デバイスVer6コード

# ODEKAKE Beacon 2018ver4
from microbit import *
#import microbit
import music
import radio
import os

display.scroll('RadV6')
radio.config(channel=10, power=5, length =64)#BLE init  length
radio.on()#BLE ON

btnChk = False
myID = 1
grCount  = 1
grChk = False
oneTime = False
missing = False
timerA = 0
timerB = 0
config = '0,0'#myID,grCount,
tune1 = ["D4:1"]
tune2 = ["E4:1"]
tune3 = ["F4:1"]
timB = 30000
interval = 3000#change to 1sec Feb.16
#IDlist = []
#IDlist2 = []
idDic = {}#ID + time dictionary

#File read
if 'config.txt' in os.listdir():
    with open('config.txt') as my_config:
        conftxt = my_config.read()
        conf = conftxt.split(',')#split by commma to list
        myID = int(conf[0])#myID read and set
        grCount  = int(conf[1])#my grCount read and set

while True:
    if button_a.is_pressed() and button_b.is_pressed():
        ##setingMode
        if btnChk is False:
            btnChk = True
            display.scroll('SET')
            display.scroll('ID=' + str(myID))
        elif btnChk is True:
            btnChk = False #end set mode
            shakeChk = False #end shake mode
            display.show(Image.YES)
    elif button_a.is_pressed():
        if btnChk is True and grChk is False:#ID count mode
            if myID < 20:
                myID += 1
            else:
                myID = 0
            display.scroll('ID=' + str(myID) ,delay = 80)
            #sleep(200)
            #display.clear()
        elif btnChk is False and grChk is True:#group count mode
            if grCount < 20:
               grCount += 1
            else:
                grCount = 0
            display.scroll('GR=' + str(grCount) ,delay = 80)
        else:#normal mode
            if(idDic):
                mList2 = []#initialize and clear
                for k, v in idDic.items():
                    sabun2 = timA - v
                    if sabun2 < 10000:#if 20sec no access
                        mList2.append(k)
                nagasa = int(len(mList2)) + 1#own ID add
                display.scroll(str(nagasa), delay = 80)
                if nagasa == int(grCount):
                    display.scroll('OK!', delay = 80)
                    missing = False
                elif nagasa < int(grCount):
                    display.show(Image.SAD)
                else:
                    display.scroll('??', delay = 100)
                    ##music.play(tune2)
                radio.send(str(myID) + ',' +  str(grCount) + ',nohelp,noset')
            else:
                display.show(Image.DUCK)
            sleep(500)
            display.clear()
            
    elif button_b.is_pressed():
        if btnChk is False and grChk is False:#HELP SEND
            radio.config(power=7)#HelpMode
            radio.send(str(myID) + ',' +  str(grCount) + ',HELP' + ',noset')
            radio.config(power=5)#HelpMode
            display.scroll('HELP')
            sleep(800)
            display.clear()
        elif btnChk is False and grChk is True:#end gr set mode
            btnChk = False #end set mode
            grChk = False #end shake mode
            radio.send(str(myID) + ',' +  str(grCount) + ',nohelp' + ',set')
            display.show(Image.YES)
            sleep(500)
            display.clear()
            #File Save
            saveConfig = str(myID) + ',' + str(grCount)
            with open('config.txt', 'w') as my_conf:#save list my_conf
                my_conf.write(saveConfig)
            #with open('gr.txt', 'w') as gr_id:
            #    gr_id.write(str(grCount))
        elif btnChk is True and grChk is False:#end id set mode
            btnChk = False
            grChk = True#to Group set mode
            display.show(Image.YES)
            sleep(500)
            display.clear()
            display.scroll('GR=' + str(grCount))
    #else:
        #display.scroll('')
        
    #radio message 'myID,grCount,HELP'
    try:
        s = radio.receive()
        if(s):
            sList = s.split(',')#split by , to list
            if sList[2] == 'HELP':
                display.scroll('!')
                music.play(music.BADDY)
                display.show(Image.SAD)
                sleep(3000)
                display.clear()
            elif btnChk is False and grChk is False:
                if sList[3] == 'set':
                    grCount = sList[1]
                    saveConfig = str(myID) + ',' + str(grCount)
                    display.scroll('GR=' + str(grCount))
                    with open('config.txt', 'w') as gr_id:
                        gr_id.write(saveConfig)
                #elif sList[2] == 'nohelp' and sList[3] == 'noset':
                else:#recieve ID check
                    if(not myID == sList[0]):#without own device
                        #time check
                        nowTime = running_time()
                        if(not idDic.get(sList[0])):#not in dictionary add ID and time
                            updValue = {sList[0] : nowTime} 
                            idDic.update(updValue)
                            #music.play(tune3)
                        else:
                            updValue = {sList[0]:nowTime} 
                            idDic.update(updValue)
    except ValueError:#PacketError
        display.show(Image.DUCK)
        radio.reset()
        sleep(200)
        radio.config(channel=10, power=5, length =64)
        display.clear()
        
    #send Beacon interval time            
    timA = running_time()
    if timA > timB:
        #del IDlist[:]#initialaize list
        if btnChk is False and grChk is False:
            radio.send(str(myID) + ',' +  str(grCount) + ',nohelp,noset')
        #display.scroll(str(timA))
            timB = timA + interval
            #display.show(Image.PITCHFORK)
            #sleep(100)
            #display.clear()
            #music.play(tune1)
        
    if(idDic):
        mList = []#initialize and clear
        #mList.clear()#need()?
        for k, v in idDic.items():
            sabun2 = timA - v
            if sabun2 > 200000:#200sec
                mList.append(k)
        
        if(mList):
            
            ids  = ''
            for t in mList:
                ids += t + '_'
            #display.scroll('ID=' + ids + 'missing', delay = 200)
            if missing is False:
                music.play(music.POWER_DOWN)
                missing = True
            display.scroll('ID' + ids + 'missing')
            sleep(5000)

    #sleep(100)

 

micro:bitでDictionary

micro:bitでDictionaryつかえるやん,,,数時間無駄にした

from microbit import *
myDict = {
    'id':1000,
    'id2':2000
}

while True:
    if button_a.is_pressed():
        cat = myDict.get('id')
        display.scroll(str(cat))
        #display.show(Image.HEART)
        sleep(2000)
    if button_b.is_pressed():
        cat = myDict.get('id2')
        display.scroll(str(cat))
        #display.show(Image.HEART)
        sleep(2000)

値を更新と追加は

inu = {'key2':400}
myDict.update(inu)

Dictionaryの中身をチェックするには(Key,Valueの両方)

for k, v in myDict.items():

これでk にkeyがvにValueが入る


スクロール速度速く

display.scroll('cat', delay = 100)