Home

Welcome! Log in or Join us

Forums SEGmeter Arduino with WiFly... so close!

Subscribe to Arduino with WiFly... so close! 22 posts, 4 voices , Tagged: arduino WiFly

Reply to topic
 

 
Icon_missing_medium
Jun 6, 2011 2:05am
7 posts

Before I discovered this site I built my own hard wired current meter based on this:

http://www.panucatt.com/files/datasheets/cs100a...

(I wish I had know about this place and your non splicing current meters, that would have saved me some time and effort). 

 

Never the less, my Arduino is now measuring Current on A0 and Voltage on A1, and I'd like to send that data to SEG.

I can measure my data accurately and in realtime and I can connect to my SEG stream, but I can't figure out how to PUT or GET or whatever my data.  Here is some spliced and diced non working code that I feel like is close (and no I don't know what all this code is doing):

 

// (Based on Ethernet's WebClient Example (and others))

#include "WiFly.h"

#include <AikoEvents.h>

#include "Credentials.h"

using namespace Aiko;

//byte server[] = { 209, 20, 81, 241 }; // api.smartenergygroups.com

 

//Client client(server, 80);

 

Client client("api.smartenergygroups.com", 80); //  /api_sites/stream", 80);  //site_1ad7ad6edccf064

 

float voltage() {

  float voltageSum;

  float voltageArray[50];

  for (int i =0; i < 50; i++) {

    voltageArray[i] = (analogRead(A1));

    voltageSum = (voltageSum+voltageArray[i]);

    delay(1);

  }

  float voltageAve = (voltageSum / 50)*0.121;  //multiplier to convert arbitrary number to real voltages

//  v = voltageAve;

  Serial.print("Voltage = ");

  Serial.println(voltageAve);

  return voltageAve;

}

 

 

float current() {

  float currentSum;

  float currentArray[50];

  for (int i =0; i < 50; i++) {

    currentArray[i] = ((-1*((analogRead(A0)*(5.00/1024))- 2.5)/ .006)*0.305);

    currentSum = (currentSum+currentArray[i]);

    delay(1);

  }

  float currentAve = (currentSum / 50); //*0.1203125;  //multiplier to convert arbitrary number to real voltages

  Serial.print("Current = ");

  Serial.println(currentAve);

  return currentAve;

}

 

void senddata(){

  WiFly.begin();

 

  if (!WiFly.join(ssid, passphrase)) {

    Serial.println("Association failed.");

    while (1) {

      // Hang on failure.

    }

  }  

 

  Serial.println("connecting...");

 

  if (client.connect()) {

    Serial.println("connected");

    client.println //Figure out how to PUT data_post = "(site site_<site> (node <node> ? (w_1 2002.8)))");

    client.println();

  } else {

    Serial.println("connection failed");

  }

 

}

 

void setup() {

 

  Serial.begin(9600);

  Events.addHandler(senddata, 10000);

//   Events.addHandler(voltage,   200);

//   Events.addHandler(current,   100);

 

void loop() {

  if (client.available()) {

    char c = client.read();

    Serial.print(c);

  }

 

  if (!client.connected()) {

    Serial.println();

    Serial.println("disconnecting.");

    client.stop();

    for(;;)

      ;

  }

}

 

 
Samotage_300_medium
Jun 6, 2011 7:45am
555 posts

Hey Jeremy,

Great to have you here!

We haven't experimented with the WiFly devices yet, but it would seem to me that the first thing you perhaps need to do is get the requests running.

E.g. try and get it to pull down a GET request from SEG, then work on constructing the PUT request.  

Kaynerichens here wrote a simple curl program on his linux machine, perhaps this may work to get some wins?

http://smartenergygroups.com/kaynerichens/posts...

Keep at it!

Sam.

@samotage

 
Samotage_300_medium
Jun 7, 2011 12:22am
555 posts

Hey Jeremy,

I was thinking about this last night, and this morning I remembered some work we did with John Oxer @jonoxer some time back when he was developing the freetronics wifi shield.  To support this, we made a special API, that was a simple GET request to the stream resource that send the value in the parameter string, e.g.

http://api.smartenergygroups.com/api_streams/&l...

Check out the documentation at the end of the API for details.

http://smartenergygroups.com/api

This may be more approriate for what you are trying to do :D

Let me know how you get on,

Sam.

 
Icon_missing_medium
Jun 7, 2011 4:23am
7 posts

Oooh... I think that's going to work.  I'll play around tonight and report back my results.

Thanks for your help!

 
Samotage_300_medium
Jun 7, 2011 4:34am
555 posts

Awesome!  I look forward to hearing about your updates.  There are a few things that may work a little funky, i.e. device online/offline etc, but let me know when you are hooked up, and we will work through these when it's running steady.

Sam.

 
Icon_missing_medium
Jun 7, 2011 4:06pm
7 posts

Woot... success!  I worked until 2am, and was able to send some fake data from my Arduino via Wifly via GET last night.  Now I just need to tweak the code so that it sends real data periodically.  

What is an ideal frequency?  I'd like as granular of data as is reasonable. (I love seeing the sun break through the clouds and watch the numbers fly up, in near real time.)  

Once I get watts data flowing then I'll have to enable some additional streams like, volts, current, temperature, light levels, battery levels, overall home & individual device usage.  This is gonna be fun!

Then my next project is a location aware thermostat and home security system that detects whether either my wife or myself are home (by checking to see if our iPhones have checked into our router) and adjust thermostat and security system appropriately.

 
Samotage_300_medium
Jun 8, 2011 6:45am
555 posts

Fantastic!

Typically we send every 45 seconds to a minute.  SEG then aggregates and does other magic.  Any finer than this there is too much raw data for any sense, greater than this there is detail lost.

Just hit up the API!

Sam.

 
Icon_missing_medium
Jun 12, 2011 7:11pm
7 posts

Woot! Got it working and looping well.  Here is my source in case it helps anyone, keep in mind I'm a bad programmer and an even worse C programmer, so be nice and offer any suggestions:

http://redhelp.net/misc/Solar_Current_Meter_sou...

There are some multipliers in there that are very specific to my hardware, those would need to be tweaked as needed.

 
Icon_missing_medium
Jun 12, 2011 7:45pm
7 posts

Well I may have spoken too soon.  For some reason the Arduino konks out after a while and just sits there.  I'll look into solutions (like perhaps a soft reset every so often).  

In the mean time, I have a question.  How do I convert my raw Watts measurement into energy (watts over time), so I can get some determine my daily totals etc?  I see there are some dashboard items that respond to energy (Wh), but I don't understand how to set them up properly.  Do I need another stream, or can I do with the my watts stream?

 
Samotage_300_medium
Jun 12, 2011 10:02pm
555 posts

Hey Jeremy,

It is possible your wifi buffers are being popped, causing fail in the arduino. John Oxer once implemented a 555 watchdog timer, that when it didn't get a signal from the arduino in normal functioning, would then initiate a reset which would re-initialise everything and restore envy win.

For the energy the best way is to do the time based integration of your watts measured on the arduino by working out the eleapsed time between measurements. Then send the watts hours for the period in another data stream up to SEG.

Sam.

 

 
Icon_missing_medium
Jun 15, 2011 5:20pm
7 posts

Thanks for the tips.  I ended up using the built in watch dog on my Uno and it seems to be working great, either that or my code doesn't crash anymore.  Either way, I've been successfully streaming watts and watt hour data for more than 24 hours!  Yeah!  

Now I have a simple question, for some reason, with the exception of my line charts, most of my dashboard widgets are reporting zeros (and the gauge is always pointing straight down and doesn't have any numbers).  What am I doing wrong?  

Here's a screenshot:

mydash.gif created on: 06/15/11

 
Samotage_300_medium
Jun 15, 2011 11:02pm
555 posts

Hey Jeremy,

All looks good, the problem you have is an issue with the new API you are using, it's not recognising your devices are online!  Hence the aggreations aren't running.  We will get this fixed ASAP, likely today, then you will have lots of dashboarding goodness.

Sam.

 

 
Icon_missing_medium
Jun 15, 2011 11:20pm
7 posts

Sweet! I look forward to it.  Thanks for all your help.

While you are poking around, I'd love to have one more tab on the Performance Summary widget... In addition to: day, week, month, quarter, year, I'd like "Lifetime".  Possible? No rush though.

 
Samotage_300_medium
Jun 15, 2011 11:47pm
555 posts

Great idea!

Though there is one interesting problem. How to compare this lifetime with the last ;)

BTW your data is aggregating and your device is ONLINE! When it stops working SEG will now be sending you messages (check your account settings).

Also, did you know the dashboard gadgets are drag and drop?  Drag em around to arrange them just so.

Thanks!

Sam.

 
Samotage_300_medium
Jun 16, 2011 7:58am
555 posts

Now haz lifetime.  :D

Sam.

 
Icon_missing_medium
Aug 30, 2011 12:18am
48 posts

I thought I would update this topic with my experiences with a WiFly shield.

I ran the sample terminal programs from the WiFly library and they worked fine. There is a little trick to sending the $$$ to get into command mode. You need to select no line endings when you send $$$ and click send to get into command mode. When the WiFly responds with CMD, change the line ending to send carriage return and then issue a VER command to get the release level of the WiFly firmware.

Now the fun starts.

One of the WiFly commands is SCAN - this reports all the access points it can see and some other details such as the type of encryption on the link. I could see my access point (Wireless N using WPA2 PSK AES), and it also gives the signal strength as well.

You then need to issue a few SET commands to set up the authorisation mode, DHCP parameters, SSIS and passphrase.

As hard as I tried I could not connect and associate to the access point, so I  read through all the forum posts I could find and found other people had similar problems but there was not a clear understanding of the problem and solution. I was about to give up on it and look for an easier solution when I found an interesting solution.

A few weeks ago I bought an Android smartphone. One of its features is that it can create a WiFi hotspot. My phone is using 2.3.3 (Gingerbread) so it is built in - previous versions I think there were applications that you could download from the market to do the same thing.

Anyway, I set the phone up as a hotspot, first with Open (no) encryption and was able to immediately connect. So I now knew the WiFly could work. I then wandered around the house at different distances from the WiFly module to check range and was able to still connect from most places, including next to my router/access point, so I now figured range wasn't the problem. I then set the hotspot up with WPA2 encryption and repeated the test, and found I could still connect. One thing I did notice was that the SCAN command reported different characteristics for my access point to the hotspot, which were supposedly configured the same. Didn't understand that. I could connect to the hotspot but not the access point that were both supposedly set up with similar characteristics in terms of authorisation.

One thing that I had noticed on Roving Networks website is that the user manual for the WiFly showed firmware level 2.23, and mine was 2.21. One of the WiFly commands is that it can update its firmware over the air by FTP back to Roving Networks (FTP U command). When that was done, reboot the WiFly (REBOOT command, and then the $$$ trick from above again) and did a rescan - both access points now reported the same type of encryption signature whereas before they didn't.

I then set up the commnds to connect to my access point (SSID and passphrase) and immediate success - WiFly was connect and the router alloacted an IP address and the connection showed up in the router's DHCP table. This was looking good!

Next step, modify the WebClient sketch which just issues a search request against Google - yep, this worked too, so modified it again to tickle the smartenergygroups API with some static dummy data - success again.

So, now I can move on and get my sensors sorted out.

The key to solving this problem seemed to be getting the latest firmware (2.23) for the WiFly, and the hotspot on the smartphone helped here immensely.

The other thing I found out (not related to the WiFly) is that my sensors using the analog input channels were giving readings that weren't quite what I expected. I was converting the raw A/D value into a voltage by multiplying by 5V/1024. However, as I was still powering the Arduino from USB, I measured the voltage and found it was around 4.8V. So, some more research reveals that the USB standard allows a USB voltage from 4.5 to 5.0 volts, so this was causing the reading to be a little bit off. So, I need to rework the voltage reference to either use an external power source (which I would get to eventually), or use the 3.3v regulator as the reference and not "5V".

A few lessons learnt, so I hope it might help others.

Cheers,

Grant

 

 
Samotage_300_medium
Aug 30, 2011 3:28am
555 posts

Hey Grant,

What a journey!  Glad you are now up and running again, and have your WiFi shield working.  Loooking forward to seening how it goes!

We tend to use the internal aRef on the Arduino, which is at 1.1v.  This solves the 5V USB power issue you are having, as the 5V regulator drops a couple of volts when fed with just 5, which is the scenario you are observing!

Great stuff,

Sam.

 
Icon_missing_medium
Sep 25, 2011 11:57am
48 posts

Hi Sam,

Continuing on I did have a GET command working with the simple API until the middle of last week. When I was trying it FRiday night I was getting a HTML response of 301 Moved Permanently whereas before it adding data.

Here is the code:

  Client client("api.smartenergygroups.com", 80);


  client.print("GET /api_streams/c49de053b4ac672/add_point?value=");
  client.println(ave_val1);
  client.println();

Has there been any recent changes in the API on the server that would stop the above from working.

I was also trying to get the HTTP PUT to work so that I can send multiple data items in one transmission instead of sending each channel separately, but I get a no data sent reply. Here is the code for this:

  String tsdata = "(site fc28fee48952e70 (node test ? (p_2 123.45)(p_3 567.89)))";
  

  client.println("PUT /sites/fc28fee48952e70 HTTP/1.1");
  client.println("Host: api.smartenergygroups.com");
  client.println("Connection: close");
  client.println("Content-type: application/x-www-form-urlencoded");
  client.print("Content-Length: ");
  client.print(tsdata.length());
  client.println(" ");
  client.print(tsdata);

I have been trying all sorts of variations on the PUT statement in the last week without success, so I thought I would put this out there for some-one more knowledgeable in these things to provide some feedback on either of the above 2 code snippets.

Any help gratefully received.

Thanks,

Grant

 
Samotage_300_medium
Sep 26, 2011 12:43am
555 posts

Hey Grant,

It's possible you send while we have some server issues - why you got the 301.  There are no redirects for this service inplace now!

have you tried changing the following to:

 String tsdata = "data_post = (site fc28fee48952e70 (node test ? (p_2 123.45)(p_3 567.89)))";

This may work, as the API is expecting a multipart form like variable data_post.

Let me know how you get on!

Sam.

 
Icon_missing_medium
Sep 26, 2011 12:24pm
48 posts

Thanks Sam.

The suggestion for prepending data_post= to the string for PUT worked well. I can now push out multiple values directly, and currently logging random test data every 50 seconds. I"ll let that run for a few hours tonight.

The GET statement still returns a 301 error! Not too concerned as I can use the PUT statement now.

Thanks for the help

Grant

 

 
Cyclingmike-lr_medium
Aug 29, 2016 6:18am
1 post

Sambo

How do I reboot the wi fi on my Crimea SEG meter? Cheers Mike

 
Samotage_300_medium
Aug 30, 2016 1:23am
555 posts

Hi Mike,

Follow along with this

https://smartenergygroups.com/samotage/posts/109-Connect-your-V2-SEGmeter-to-the-WiFi

Sam, @samotage

Log in to reply to this topic

Forums SEGmeter Arduino with WiFly... so close!

copyright © 2008 Smart Energy Groups pty ltd