Shop OBEX P1 Docs P2 Docs Learn Events
Live logger for home power usage. — Parallax Forums

Live logger for home power usage.

eagletalontimeagletalontim Posts: 1,399
edited 2012-08-25 22:43 in General Discussion
This is an interesting idea and I know it has already been done but..... I want to build a logger for how much energy I use based on what my power meter says. I have one of those "smart meters" which allows the electric company to read / monitor the usage any time of the day. My lovely power company has now put into affect this new system which charges more per kWh for 8 hours a day. The time the cost goes up is determined by the summer or winter. In the summer, the time is from 11AM to 7PM, and in the winter it is from 11PM to 7AM. They are able to read this usage due to the smart meter.

Interestingly enough, there is an IR light on the very top of this meter which flashes for every time a watt hour used. I tested this by using the camera on my phone to see the light blink and sure enough, it did. The flashes were closer together when the AC was running and when it turned off, the flashing was slower.

What I am hoping to do is to have an IR sensor pick up these flashes and send the information to one of my computers. Since either of my 2 computers are about 20 feet away, I am not sure if I should use wireless or wired communication from the sensor circuit to the computer. I am thinking wired since it would be less hassle in the long run, but I don't like climbing under my house :p Also, I need to figure out which chip would be best to use to read the IR pulses and send them via USB or Serial to my computer, SX28 or Prop. I am thinking the Prop would be better since it can handle more than 1 process at a time without possible timing issues and missing a pulse here and there while sending data to the computer. Any feedback on this?

I do have some VB6 programming knowledge and have built a computer control wired car a long time ago using the SX28. I am hoping to have a VB program that runs in the background which reads the information from the chip and send that information to my web server which will then store the information into my mysql database for later viewing with possible graphs and more.

A few questions I have.....

1) Is the SX28 capable of counting the IR pulses while sending information to a computer via Serial or USB at the same time?

2) Would the Prop be a better chip for this application or is there another "less powerful" chip?

3) Does VB6 recognize USB devices that don't have a specific driver? I am not familiar with RS232 or any type of USB communications.

4) Would Serial communication be easier, effective, and cheaper?
«13

Comments

  • JLockeJLocke Posts: 354
    edited 2012-08-12 19:23
    I don't have any experience with the SX28, so I'll leave someone else to comment on that. I would use a Prop combined with an XBee module to transmit to your PC. You can use something like the XBee Explorer from Sparkfun at the PC end. You'll have to write some serial communication code in VB6, but to the PC, the XBee dongle will look like an RS232 port. Eventually you may want to design a dedicated board to monitor and save the data to SD card and free up your PC.
  • SRLMSRLM Posts: 5,045
    edited 2012-08-12 22:00
    I would use a Prop: if you run into problems, then you're much more likely to receive forum support.

    You'll also need to do some more research into the IR. It will almost certainly be regulated at a frequency (38KHz, 54KHz, etc.), and it probably includes some sort of data in the transmission. My guess is that each time it turns on it transmits the total. If you do find out more, please post. That's interesting (and useful) information.

    The computer would recognize the port as a serial port. There are a number of examples here on the forums of using VB for serial port reading. In my work, I've used PySerial to do the same with Python.

    I'm also curious, but on an ethical note: while it's unethical to trick the meter into thinking it's the opposite season or time of day (to get the lower rates), is it illegal? And is it technically possible?
  • kwinnkwinn Posts: 8,697
    edited 2012-08-13 00:03
    I would also suggest using a prop, but would suggest using the prop with an SD card to log the data. This will avoid avoid tying up a PC. Store the accumulated data in a file on the SD card and once a month take the card, transfer the data to your PC, and empty the file on the SD card.

    A prop with an SD card could be the beginning of a home monitoring/automation system.
  • eagletalontimeagletalontim Posts: 1,399
    edited 2012-08-13 04:34
    I thought about using an SD card for storage but I need to be able to keep track the of the date and time accurately with which ever chip I use. I am leaning more towards the Prop due to it's capabilities. Can the prop accurately keep track of the date and time even with a possible power failure?
  • HumanoidoHumanoido Posts: 5,770
    edited 2012-08-13 06:24
    When I first read the title of the post, I thought it said "live longer for..." and that was a real eye catcher. I was ready to buy into this project. :) The Prop in many examples can do a good job of keeping track of the date and time and handle power failures with a few extra components. Serial communication is very effective on the prop when baud rates are matched to the project. One idea to keep date and time going is the use of a backup battery so even if the main prop circuit loses power, the clock will continue, and after the prop reboots, it can read the time again. A small UPS can handle the power failures, or look into battery operations with a warning circuit to indicate (or switch) batteries when low voltage occurs etc..
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2012-08-13 07:26
    1) Is the SX28 capable of counting the IR pulses while sending information to a computer via Serial or USB at the same time?

    2) Would the Prop be a better chip for this application or is there another "less powerful" chip?

    3) Does VB6 recognize USB devices that don't have a specific driver? I am not familiar with RS232 or any type of USB communications.

    4) Would Serial communication be easier, effective, and cheaper?

    In general terms, I think the Propeller is the best chip. This project is just a bit too complex for arduino or picaxe, and Basic stamp. But it is overkill to run a PC all the time with the 30-100W it consumes.

    So 1) I don't know about other chips, but I know the propeller with all those cogs is perfect for multitasking. You will end up with cogs to spare but who cares, at least you know you will never miss a pulse.

    2) With some super smart coding maybe there is a "better" chip but with the prop you don't have to be super smart. Just plug in lots of obex code and a tiny amount of glue code you write. Which the forum will gladly help with as this is a cool project :)

    3) vb6 and vb.net and C# can easily talk via RS232. USB is a little more complex, but I'd stick with RS232. It is boring and slow but safe and easy to debug. $2 will buy you a USB to serial adaptor on ebay, and at the propeller end, a max3232 will set you back about $1.

    4) Yes I like serial comms. I've got a pile of code in vb6 and vb.net for serial and I can send you this if you like. I've also got free serial port sniffer software which is great because you can see the individual bytes going back and forth. Much easier to debug if you can see things.

    If you don't like climbing under the house, there are lots of modules around that convert RS232 to wireless. Usually around $30-$40 a pair. I think they are that price because in China they are mass produced for remote monitoring of power use. Which is kind of appropriate considering what you want to do. So - at the PC end, a $2 USB to serial adaptor, then convert that to wireless serial at 1200 to 19200 baud. Then at the prop end, one of those wireless adapters going straight into the prop. A IR phototransistor and a RTC chip on the I2C bus of the propeller and you are set. Maybe an SD card if you want to be able to turn the PC off.
  • MicksterMickster Posts: 2,721
    edited 2012-08-13 07:53
    I wonder if a old Android-based phone could be put to use for this(?) Built-in camera, WiFi, Bluetooth, battery, etc. Basic 4 Android is quite similar to VB.

    Just a thought.

    Mickster
  • eagletalontimeagletalontim Posts: 1,399
    edited 2012-08-13 17:11
    I have a computer that is on all the time that also has a VB program which reads my IP address and sends that information to my website so I can easily link to my house from anywhere that has web access. I run a DVR system on that computer so it is always on. I plan on using that computer for the project.

    I decided due to cost to go with serial communication since I have tinkered with it in the past and I know there is no extra circuitry I would have to purchase to communicate with my computer. I also bought 50 ft of shielded 2 wire cable which will run to the IR sensor. The prop will be located close to the computer to be within range of the serial cable I have. If anyone has source code on how to communicate via serial to a computer using the Prop, please feel free to post it :) I am hoping to have my air conditioning replaced with the new R410 unit from my old R22 soon and would like to be able to monitor how much of a difference it is in power consumption. Hopefully this change will happen next week so I really got to get something working. I am doing my own research now, but any working code that I can use will be very helpful!
  • kwinnkwinn Posts: 8,697
    edited 2012-08-13 19:52
    Since you are already running a PC 24/7 it makes sense to use that for data logging as well. I would still suggest using the prop to interface between the meter and the PC. The programming required for this application would be a bit of spin code to tie together existing objects. Once you have a prop there with all those unused pins I am sure you will find uses for them.
  • eagletalontimeagletalontim Posts: 1,399
    edited 2012-08-13 19:56
    I have a Prop set up in front of me on a breadboard and I am tinkering with how to get the infrared phototransistor to work properly with the prop. I am using a TV remote as my test IR signal :) If the IR sensor picks up any signal, an LED turns on for 1 second. Still can't get it figured out on how to wire it up. I got the LED to turn on by manually tripping the pin, but the sensor is giving me issues...
  • kwinnkwinn Posts: 8,697
    edited 2012-08-13 20:52
    The phototransistor has to be sensitive to IR and should be shielded from stray light. Typical connection for the phototransistor is emitter to ground, base floating, and collector to a resistor to +V. Signal is taken at the collector/resistor junction. Use a high value resistor ( 10 - 50K ) between the collector and V+. Another option is to use a 100K potentiometer so you can determine the optimum resistor value. A phototransistor may not be sensitive enough. If that is the case you can use a photodarlington or add a transistor to the phototransistor.
  • eagletalontimeagletalontim Posts: 1,399
    edited 2012-08-13 21:11
    I was just able to get it figured out but I need to block incoming light to the sensor. I connected the phototransistor to 5V+ and pulled down the other side with a 100K resistor. The Prop pin has a 1K resistor connected before the 100K resistor. It is very sensitive to light, but if I shield it enough from the light, I can see the LED blinking when the IR blinks on the power meter :)

    Here is my code so far :
    CON
      _CLKMODE = XTAL1 + pll16x
      _XINFREQ = 5_000_000
      LED = 0
      Sensor = 1
      CountDelay = 10
    
    OBJ
      Ser     :       "FullDuplexSerial"
    
    PUB Main(gear)
      Ser.start(31, 30, 0, 9600)
      repeat
        dira[LED]~~
        outa[LED] := 0
        
        if ina[Sensor] == 1
          outa[LED] := 1
        
    
        waitcnt(clkfreq / 1000 * CountDelay + cnt)
    

    I am not using the serial just yet as I am still learning about this.

    There is something very interesting about the IR LED on the power meter... It is not a flash, it is a series of quick flashes. This is probably some sort of coded output that is used by the electric company. Not quite sure how to use it, but I may just add a delay that is long enough to pick up the first flash and ignore the rest. Not sure how accurate that will be, but I am sure I can figure something out.
  • kwinnkwinn Posts: 8,697
    edited 2012-08-13 22:50
    For counting flashes simply counting the first flash and waiting for some fixed length of time before looking for another flash would work.

    For discovering the code (if any) the simplest would be to see if you can find the info on the meter manufacturer or your utility's web site. If it is not available you could try to decode it by using a quick and dirty 1 channel logic analyzer. That is once you detect the first flash you shift in bits one at a time at a fixed rate until there is no flash for a reasonable period of time. The fixed rate and reasonable time would have to be determined. The code could be almost anything, RS232 async ascii, IR remote, PWM232, one of the barcodes, or even morse code (yes, I saw that once in a circuit designed by an old ham radio enthusiast).
  • eagletalontimeagletalontim Posts: 1,399
    edited 2012-08-14 18:24
    For an update on things.... I have the wire run under my house and the IR sensor fixed to my power meter. I have the Prop next to my computer and the LED blinks every time the IR light blinks on the meter. I think I was previously picking up interference which was causing the LED to blink multiple times per flash. Now, the LED blinks once and that's all :) I am about to start learning how to have the Prop communicate with my computer using serial communication! Here we go!
  • eagletalontimeagletalontim Posts: 1,399
    edited 2012-08-14 19:55
    Ok, I am not getting any communication between the Prop and the computer :( I am not sure where I have the problem, but here is what I have set up....

    For the serial connection to the prop, I am using Figure 2.3.2 on page 5 of this document : http://www.parallax.com/Portals/0/Downloads/docs/prod/prop/PropellerDatasheet-v1.1.pdf

    Here is the Prop Code :
    CON
      _CLKMODE = XTAL1 + pll16x
      _XINFREQ = 5_000_000
      LED = 0
      Sensor = 1
      CountDelay = 10
    
    OBJ
      Ser     :       "FullDuplexSerial"
    
    PUB Main(gear)
      Ser.start(17, 16, 0, 9600)
      repeat
        dira[LED]~~
        outa[LED] := 0
        
        if ina[Sensor] == 1
          outa[LED] := 1
          Ser.str(string("testing"))
        
    
        waitcnt(clkfreq / 1000 * CountDelay + cnt)
    

    And the VB6 Code :
    Dim incomingdata As String
    
    Private Sub Form_Load()
        Dim myport As String
        If MSComm1.PortOpen = False Then
        On Error GoTo No_Connect
        'myport = InputBox("Which COM Port?", "COM Port Selection", "1")
        With MSComm1
          .CommPort = 1
          .Settings = "9600,N,8,1"
          .PortOpen = True
          constatus.Caption = "Connected to Port " + myport
        End With
      Else
        If (MSComm1.PortOpen = True) Then
          MSComm1.PortOpen = False
        End If
        constatus.Caption = "Disconnected"
      End If
     
      On Error GoTo 0
      Exit Sub
       
    No_Connect:
      Dim response
      response = MsgBox("Error: Could not connect.", vbExclamation + vbOKOnly)
      On Error GoTo 0
    End Sub
    
    Private Sub MSComm1_OnComm()
        Dim sData As String
        Dim mcard As String
        If MSComm1.CommEvent = comEvReceive Then
               incomingdata = MSComm1.Input
               Text1.Text = incomingdata
        End If
    End Sub
    

    It has been a long time since I have messed with VB6, but I am sure I have everything correct. Is there anything you might see wrong?


    EDIT : VB6 Code changed. Still not working :(
  • SRLMSRLM Posts: 5,045
    edited 2012-08-14 20:30
    Why do you have the serial connection on pins 16 and 17? Usually, they are on 30 and 31. Do you have an FTDI chip (Propplug would work) on pins 16-17?

    Also, try not using the VB6 code just yet and just see if you can get the string on a regular terminal such as the Parallax Serial Terminal or the like.
  • eagletalontimeagletalontim Posts: 1,399
    edited 2012-08-14 20:40
    I used the separate pins since I am programming the chip over and over and did not know if the circuit shown in the above post would cause issues when programming or trying to send serial data. I know there is serial data going through each serial connection when it is activate. I have watched this in the PST on both COM ports. For some reason, VB is not recognizing it :(
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2012-08-14 21:24
    I never had much luck with comevreceive. What worked for me was
    If MSComm1.InBufferCount >0 then...
    
    and then keep looping with
    incomingdata = MScomm1.input   
    
    until the inbuffercount is down to zero again.

    I presume you have the polarities correct, max232 etc chips. Can you get data into the prop terminal to check the prop is sending?
  • eagletalontimeagletalontim Posts: 1,399
    edited 2012-08-14 21:30
    Like this?
    Private Sub MSComm1_OnComm()
        If MSComm1.InBufferCount > 0 Then
            Text1.Text = "Reading..."
            While MSComm1.InBufferCount > 0
                incomingdata = incomingdata + "" + MSComm1.Input
            Wend
            Text1.Text = incomingdata
        End If
    End Sub
    

    Still not getting anything :(
  • eagletalontimeagletalontim Posts: 1,399
    edited 2012-08-14 21:37
    Yes, I can get the correct data into the serial terminal from either the prop plug or the direct serial connection to the back of the computer. This one has me stumped!
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2012-08-14 21:45
    Maybe leave out the oncomm bit. This is some stripped down code - should exit the loop when one byte arrives. I presume this is for port 1 (ie the same as the number the proptool uses)
    MSComm1.CommPort = 1
     MSComm1.Settings = "9600,n,8,1"
     MSComm1.RThreshold = 1
     MSComm1.InputLen = 1
     MSComm1.PortOpen = True
     Do
     If MSComm1.InBufferCount = 1 Then Exit Do: ' got enough bytes
    MSComm1.PortOpen = False
    
  • eagletalontimeagletalontim Posts: 1,399
    edited 2012-08-14 21:51
    Private Sub MSComm1_OnComm()
        Text1.Text = "Picked up something...about to process it"
        Do
            incomingdata = incomingdata + "" + MSComm1.Input
            Text1.Text = "Reading..."
        If MSComm1.InBufferCount = 1 Then Exit Do
        Loop
        Text1.Text = incomingdata
    End Sub
    

    This hangs the program :(
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2012-08-14 21:57
    Try the one in post #22 without the oncomm. I never got oncomm to work. This way does hang the program, but there are workarounds for that by reading the system clock (datediff) and then having a timeout. But for the moment we want it to hang until a byte comes in. What port number is the proptool using?
  • eagletalontimeagletalontim Posts: 1,399
    edited 2012-08-14 22:17
    That gets something on in the text field :) But....the way i have it coded, it hangs the program till the next transmission comes though.

    Here is my code so far :
    Dim incomingdata As String
    
    Private Sub Form_Load()
        Dim myport As String
        If MSComm1.PortOpen = False Then
        On Error GoTo No_Connect
        'myport = InputBox("Which COM Port?", "COM Port Selection", "1")
        With MSComm1
          .CommPort = 4
          .Settings = "9600,n,8,1"
          .RThreshold = 1
          .InputLen = 1
          .PortOpen = True
        End With
        Timer2.Interval = 500
        Timer2.Enabled = True
        constatus.Caption = "Connected to Port " + myport
      Else
        If (MSComm1.PortOpen = True) Then
          MSComm1.PortOpen = False
        End If
        constatus.Caption = "Disconnected"
      End If
    
    
        Text1.Text = incomingdata
     
      On Error GoTo 0
      Exit Sub
       
    No_Connect:
      Dim response
      response = MsgBox("Error: Could not connect.", vbExclamation + vbOKOnly)
      On Error GoTo 0
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
    End Sub
    
    Private Sub Timer1_Timer()
        Do
            incomingdata = incomingdata + "" + MSComm1.Input
        If MSComm1.InBufferCount = 1 Then Exit Do
        Loop
    End Sub
    
    Private Sub Timer2_Timer()
        Text1.Text = incomingdata
        incomingdata = ""
    End Sub
    

    It also seems to cut off some of the text, but I think that has to do with a timing issue. Any way to not hang the program while waiting for a serial communication?
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2012-08-14 22:56
    Sounds like a start.

    For hanging, you could set up a loop with a timer, check the number of bytes in the buffer say once every second, then when there are enough, read them in. Also DoEvents is useful for things that hang because if you have multiple threads and one is in a tight loop you can put delays in that loop and DoEvents and then most of the time the program can go off and run other threads.
  • eagletalontimeagletalontim Posts: 1,399
    edited 2012-08-15 16:54
    I have gotten the prop plug to work with the serial terminal and my VB program, but the serial connection to the serial port does not work in the VB program, but it does in the serial terminal. I am only outputting the string "Test" at the moment. Is the "Alternative serial port connection" located on page 5 of this document the right wiring for what I am doing?

    http://www.parallax.com/Portals/0/Downloads/docs/prod/prop/PropellerDatasheet-v1.1.pdf

    I don't think I need the DTR connection since I am not programming though this port, but it is wired up this way on my breadboard. I have checked every connection and it matches the schematic exactly. Same transistors used, Same resistor values used, and pins are not close to any "static" signals. Why would this work through the USB prop plug, but not through a straight serial connection?
  • SRLMSRLM Posts: 5,045
    edited 2012-08-15 17:08
    I don't think I need the DTR connection since I am not programming though this port, but it is wired up this way on my breadboard. I have checked every connection and it matches the schematic exactly. Same transistors used, Same resistor values used, and pins are not close to any "static" signals. Why would this work through the USB prop plug, but not through a straight serial connection?

    You probably don't want to have the DTR line hooked up at all. Some COM programs I've used (Python, I'm looking at you!) always toggle the DTR line. This puts the Propeller into a reset, so you wouldn't see any serial data for a while, if ever.
  • eagletalontimeagletalontim Posts: 1,399
    edited 2012-08-15 17:22
    I just disconnected it and voila! I have serial communication :) Now to figure the best way to store the usage....

    Maybe this will make sense to someone, but here is my idea on how to do this. First, the Prop will time the interval between pulses and store the result in an array(?). After 1 minute or 30 seconds or even after X number of pulses, depending on which one will dump the array before it gets too big, it will then send that array to the VB program which will process the data. The data should be stored something like array('1000', '1500', '1500', ....). The VB program will count how many stored values there are and add up the values to get an over all time. If each pulse = 100 watt hour, then I can use a math formula to calculate how much power was used during that time period the Array stored. I am going to see if I can figure out this formula. If there is a better / easier way, please let me know!
  • kwinnkwinn Posts: 8,697
    edited 2012-08-15 18:05
    In your first post you state

    "Interestingly enough, there is an IR light on the very top of this meter which flashes for every time a watt hour used. I tested this by using the camera on my phone to see the light blink and sure enough, it did. The flashes were closer together when the AC was running and when it turned off, the flashing was slower."

    If each blink represents a fixed number of watt hours why not let the prop do a little preprocessing by having it count the number of pulses and sending a count at a fixed interval (say once per minute) or when the pulse rate changes by more than some amount. The PC can add the time and date to the count so you can graph power usage and identify power hogs or appliances that run during peak rate times.
  • eagletalontimeagletalontim Posts: 1,399
    edited 2012-08-15 18:16
    I thought about counting pulses over a certain period and sending that result to the computer, but... If a pulse happens right when the information is sent and the count is restarted, I will lose that pulse. I know it may not seem like much, but say 10 pulses per day lost, that is ~300 pulses per month which is 3000 watt hours lost.
Sign In or Register to comment.