File size: 1,841 Bytes
8f59bb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# -*- encoding: utf-8 -*-
import time
import math
import sephiroth
import argparse
import Adafruit_BBIO.ADC as ADC
import socket
from datetime import datetime
import logging
logging.basicConfig()

log = logging.getLogger('client_bbb')
log.setLevel(logging.INFO)

ADC.setup()
# This indicates how many times the signal is read
READ_FREQUENCY = 50 # Frequency in Hz
RFQ            = float(1)/float(READ_FREQUENCY) # In seconds

def get_signal():
    return ADC.read('P9_38')

def send_data(client, signal):
    data = '{time:%Y-%m-%dT%H:%M:%S.%fZ};{signal:07.2f}' \
            .format(time=datetime.now(), signal=signal)
    client.send(data)

def start(client_id, host, port):

    def connect():
        while True:
            try:
                client = sephiroth.endpoint(client_id)
                client.connect(host=host, port=port)
                log.info('Client %s connected' % client_id)
                return client
            except socket.error as e:
                log.error(str(e))
                log.info('Attempt to reconnect in 3 seconds...')
                time.sleep(3)
    
    client = connect()
    while True:
        try:
            signal = get_signal()
            send_data(client, signal)
            time.sleep(RFQ) # Read frequency
        except socket.error:
            log.info('Client disconnected...')
            client = connect()


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('-c', '--client', help='client id')
    parser.add_argument('-o', '--host', help='host name or ip address')
    parser.add_argument('-p', '--port', help='port number', type=int)

    args = parser.parse_args()
    client_id = args.client or '000000000000000'
    host      = args.host or '192.168.7.1'
    port      = args.port or 7777
    start(client_id, host, port)