<commit_before># Author : Kowonsik, github.com/kowonsik
# Author : Jeonghoonkang, github.com/jeonghoonkang

## This code for T100, ELT CO2 sensor

import serial,os,time
import sys
import RPi.GPIO as GPIO
import logging 
import logging.handlers 

import json
import requests
import fcntl, socket, struct

sys.path.append("/home/pi/devel/BerePi/apps/BereCO2post/lib")
from co2led import *

DEBUG_PRINT = 0 
SERIAL_READ_BYTE = 12
FILEMAXBYTE = 1024 * 1024 * 100 #100MB
LOG_PATH = '/home/pi/log_tos.log'

CO2LED_BLUE_PIN = 17
CO2LED_GREEN_PIN = 22
CO2LED_RED_PIN = 27

# important, sensorname shuould be pre-defined, unique sensorname
sensorname = "co2.test"

#url = "http://127.0.0.1:4242/api/put"
url = "http://172.16.58.3:4242/api/put"

def getHwAddr(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', ifname[:15]))
    return ':'.join(['%02x' %ord(char) for char in info[18:24]])

macAddr = getHwAddr('eth0')
macAddr = macAddr.replace(':','.')

level = 0
ppm = 0

# check length, alignment of incoming packet string
def syncfind():
    index = 0
    alignment = 0
    while 1:
        in_byte = serial_in_device.read(1)
# packet[8] should be 'm'
# end of packet is packet[10]
        if in_byte is 'm' :
            #print 'idx =', index, in_byte
            alignment = 8
        if alignment is 10 : 
            alignment = 1
            index = 0
            break
        elif alignment > 0 :
            alignment += 1
        index += 1

def checkAlignment(incoming):
    idxNum = incoming.find('m')
    # idxNum is 9, correct
    offset = idxNum - 9 
    if offset > 0 :
        new_str = incoming[offset:]
        new_str = new_str + incoming[:offset]
    if offset < 0 :
        offset = 12 + offset 
        new_str = incoming[offset:]
        new_str = new_str + incoming[:offset]
    return new_str
    
def init_process():
    print " "
    print "MSG - [S100, T110 CO2 Sensor Driver on RASPI2, Please check log file : ", LOG_PATH
    print "MSG - now starting to read SERIAL PORT"
    print " "
    ledall_off()


def insertPost(t,ppm,macAddr, sensorname):
    data = {
        "metric": "rc1.co2.ppm",
        "timestamp": t,
        "value": ppm,
        "tags": {
            "eth0": macAddr,
            "hw": "raspberrypi2" ,
            "sensor" : "co2.t110",
            "name" : sensorname,
            "floor_room": "5fl_512",
            "building": "keti",
            "owner": "kang",
            "country": "kor"
        }
        #tags should be less than 9, 8 is alright, 9 returns http error
    }

    try:
        ret = requests.post(url, data=json.dumps(data))
    except :
        pass

    time.sleep(1)

    return
<commit_msg>Update BereCO2post.py<commit_after># Author : Kowonsik, github.com/kowonsik
# Author : Jeonghoonkang, github.com/jeonghoonkang

## This code for T100, ELT CO2 sensor

import serial,os,time
import sys
import RPi.GPIO as GPIO
import logging 
import logging.handlers 

import json
import requests
import fcntl, socket, struct

sys.path.append("/home/pi/devel/BerePi/apps/BereCO2post/lib")
from co2led import *

from socket import gethostname
hostname =  gethostname()


DEBUG_PRINT = 0 
SERIAL_READ_BYTE = 12
FILEMAXBYTE = 1024 * 1024 * 100 #100MB
LOG_PATH = '/home/pi/log_tos.log'

CO2LED_BLUE_PIN = 17
CO2LED_GREEN_PIN = 22
CO2LED_RED_PIN = 27

# important, sensorname shuould be pre-defined, unique sensorname
sensorname = "co2.test"

#url = "http://127.0.0.1:4242/api/put"
url = "http://172.16.58.3:4242/api/put"

def getHwAddr(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', ifname[:15]))
    return ':'.join(['%02x' %ord(char) for char in info[18:24]])

macAddr = getHwAddr('eth0')
macAddr = macAddr.replace(':','.')

level = 0
ppm = 0

# check length, alignment of incoming packet string
def syncfind():
    index = 0
    alignment = 0
    while 1:
        in_byte = serial_in_device.read(1)
# packet[8] should be 'm'
# end of packet is packet[10]
        if in_byte is 'm' :
            #print 'idx =', index, in_byte
            alignment = 8
        if alignment is 10 : 
            alignment = 1
            index = 0
            break
        elif alignment > 0 :
            alignment += 1
        index += 1

def checkAlignment(incoming):
    idxNum = incoming.find('m')
    # idxNum is 9, correct
    offset = idxNum - 9 
    if offset > 0 :
        new_str = incoming[offset:]
        new_str = new_str + incoming[:offset]
    if offset < 0 :
        offset = 12 + offset 
        new_str = incoming[offset:]
        new_str = new_str + incoming[:offset]
    return new_str
    
def init_process():
    print " "
    print "MSG - [S100, T110 CO2 Sensor Driver on RASPI2, Please check log file : ", LOG_PATH
    print "MSG - now starting to read SERIAL PORT"
    print " "
    ledall_off()


def insertPost(t,ppm,macAddr, sensorname):
    data = {
        "metric": hostname,
        "timestamp": t,
        "value": ppm,
        "tags": {
            "eth0": macAddr,
            "hw": "raspberrypi2" ,
            "sensor" : "co2.t110"
        }
        #tags should be less than 9, 8 is alright, 9 returns http error
    }

    try:
        ret = requests.post(url, data=json.dumps(data))
    except :
        pass

    time.sleep(1)

    return

{'fraction_non_alphanumeric': 0.1192200557103064, 'fraction_numerical': 0.04326833797585887, 'mean_word_length': 2.434948979591837, 'pattern_counts': {'":': 16, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4987706', 'n_tokens_mistral': 2025, 'n_tokens_neox': 1856, 'n_words': 501}