From 619107a303fc62a0b288871def788924a768c541 Mon Sep 17 00:00:00 2001 From: PCOG sites Date: Tue, 15 Jul 2014 23:35:41 +0200 Subject: [PATCH] Write data to socket for EmonHub --- app/helpers/InSyncState.rb | 22 +++++++++++++++++++++- app/helpers/Synchronizer.rb | 2 +- smartmeter.rb | 1 + 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/app/helpers/InSyncState.rb b/app/helpers/InSyncState.rb index c7fa7cb..9ebc52a 100644 --- a/app/helpers/InSyncState.rb +++ b/app/helpers/InSyncState.rb @@ -1,7 +1,17 @@ +require "socket" + class InSyncState < StatePattern::State END_OF_FRAME = "!\n" +# def initialize(stateful, previous_state) +# # open socket to EmonHub +# @hub = TCPSocket.new 'raspberrypi.lan', 5050 +# @hub.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true) +# super(stateful, previous_state) +# end + + def handle_byte_stream(bytes) idx = 0 #sync_pattern_length = Synchronizer::SYNC_PATTERN.length @@ -84,7 +94,17 @@ class InSyncState < StatePattern::State else reading.save end + + # Write to EmonHub + begin + TCPSocket.open("raspberrypi.lan",5050){|s| + s.write(sprintf("8 %d %d\r\n", reading.current_kw_consumed*1000, reading.current_kw_produced*1000)) + } + rescue + p "Socket problem." + end + # Result return reading end -end \ No newline at end of file +end diff --git a/app/helpers/Synchronizer.rb b/app/helpers/Synchronizer.rb index 1eed956..df24f98 100644 --- a/app/helpers/Synchronizer.rb +++ b/app/helpers/Synchronizer.rb @@ -2,7 +2,7 @@ class Synchronizer include StatePattern SYNC_PATTERN = "\n/ISk5\\2ME382-1003\n\n" - + set_initial_state ::SearchingForSyncState end diff --git a/smartmeter.rb b/smartmeter.rb index 6547b11..21521d3 100644 --- a/smartmeter.rb +++ b/smartmeter.rb @@ -34,6 +34,7 @@ def read_from(source) end if __FILE__ == $0 + # open serial port and read first bytes source = open_device buffer = [] buffer = read_from(source)