Showing wi-fi connection status real time i.e db's and packets received as opposed to lost on xively

Hi Guys.

Completely new to this. Undertaking my final year uni project and am way out of my comfort zone!! Im a marine engineer lol.

Anyway i have got a simple thermocouple up and running and am studying the possibility of condition based monitoring wirelessly and in real time on ships and offshore installations etc in a cheap and simple manner.

The main aim is not the sensor readings but the gaining of info for connection strength over time i.e. graphed on xively like my temp sensor is so would correlate accordingly. and maybe a packet graph which would show dopped packets? dont know if this can be done?

Any help would be gratefully received.

Regards

Glenn

You can get RSSI data right from the Imp. I display that on the LCD screen of my temp monitor project. It would be really easy to send that up to xively at an interval.

imp.rssi()

I don’t know if the imp monitors any other network connection data…

Thankyou for your timely reply…

I did find that code segment on the wiki page but have no idea where to put it or how to set up an interval for like once a minute

// pin 9 is the middle of the voltage divider formed by the NTC - read the analog voltage to determine temperature
hardware.pin9.configure(ANALOG_IN);

// all calculations are done in Kelvin

const b_therm = 3988;
const t0_therm = 298.15;

// Output structure for sending temperature to server
local tempOut = OutputPort(“Temperature ©”, “number”);
local tempOutStr = OutputPort(“Temperature ©”, “string”);

function getTemp() {
// scale the ADC reading to a voltage by dividing by the full-scale value and multiplying by the supply voltage
local v_therm = 3.3 * hardware.pin9.read() / 65535.0;
// calculate the resistance of the thermistor at the current temperature
local r_therm = 10000.0 / ( (3.3 / v_therm) - 1);
local ln_therm = math.log(10000.0 / r_therm);

local t_therm = (t0_therm * b_therm) / (b_therm - t0_therm * ln_therm) - 273.15;

// convert to fahrenheit for the less-scientific among us
local f = (t_therm) + 32.8 ;
// format into a string for the string output port
local f_str = format("%.01f", f)
server.log("Current temp is "+f_str+" C");

// emit values to our output ports
tempOut.set(f);
tempOutStr.set(f_str);

imp.wakeup(30, getTemp);

}

// Configure on planner and register with imp server
imp.configure(“Thermocouple MT Kent”, [], [tempOut, tempOutStr]);
getTemp();
server.log(format(“rssi %ddBm”,imp.rssi()));

i added the above line and it gives me the data i wanted but i would like this every 30 seconds like my temps?? and upped to xively.

any help would be amazing.

thanks

It looks like you are using the old planner style for HTTP… take at look at the Open Beta sticky at the top of discussions. I would highly recommend asking to be put into the open beta… its so much easier to code for HTTP when you use agents.

When you get into the beta, take a look at this code. I put it together for a tutorial that I have on the Adafruit learning system. There is a file with code for the agent, which you will get access to, and for the device, which you are programming now.

https://github.com/joel-wehr/Tutorial_Electric_Imp_MAX31855

This code reads a thermocouple amplifier and then posts the temps to Xively every 30 seconds. It doesn’t post RSSI data, but that is easy to add to another channel. It might help to read through the tutorial.

http://learn.adafruit.com/connecting-the-max31855-thermocouple-amplifier-breakout-to-an-electric-imp

Take a look at that, and I’ll post some more later on…

Hi

Thanks for all your help i have this setup but it didnt work for me at all? i copied it to the tee!!

ill try it again and see what happens.

i sent the open beta form off as you requested but i think i am on it already.

i have an agent and device section and the code window is white?

I can get the rssi data along with the temp but the temp as in my code is every 30seconds but the rssi data just sends one when first compiled.

can you show me how to get it every 30 seconds the same as i get my temps at present must be pretty easy but am really lost. and then how to add the rssi so that it does the same as my temps to xively.

Thanks once again your builds are inspiring!!

Glenn

Sounds like you are in the beta… The code that I mentioned won’t work for your circuit, but it should be good example code for what you want to do.

Since you are in the beta, you want to write code differently that what you have, for example…
// Output structure for sending temperature to server local tempOut = OutputPort("Temperature (C)", "number"); local tempOutStr = OutputPort("Temperature (C)", "string"); and, imp.configure("Thermocouple MT Kent", [], [tempOut, tempOutStr]);

this code isn’t used with agents.To send information back and forth between the agent and device you use agent.send/agent.on, and device.send/device.on.

So, your original device code was fine, except that you need to modify it to send to the agent, something like this…

agent.send("FarenheitToXively", f);

and then in your agent code, you need a handler for “FarenheitToXively” that sends to Xively. (You also need all the Xively code, and your Xively feed information configured correctly like it is show in the tutorial.)

device.on("FarenheitToXively", function(v) { channel1 <- Xively.Channel(Channel_ID); channel1.Set(v); feed1 <- Xively.Feed(Feed_ID, [channel1]); client.Put(feed1); });

So that would be for sending your temperature. To send rssi is very similar, you just insert that instead of your fahrenheit reading.

agent.send("RSSIToXively", imp.rssi()); and then create a handler for that in your agent.

If I get some more time this evening, I’ll try to throw together some working example code.

A read through the Agent and Device sections of the API should make things more understandable:

https://electricimp.com/docs/api/agent/

thanks very much really want to get this going?
i have been using the API in the add nodes for xively and has been working ok?? just put the xively details like FEED ID in the 3 boxes and hey presto!

was this the wrong way? all i have is a simple thermistor on pin 9 of my sparkfun breakout and just need to send temps and rssi data on 2 seperate streams to xively to monitor over a period.

didnt think would be this difficult i found the arduino code easier but there wifi shiels was useless!

Hope you can help i know time is a commodity.

Regards.

Glenn

Ok, this should work:

Agent Code:

`//BEGIN XIVELY

API_Key <- “”; //Type your Xively API Key
Feed_ID <- “” //Type your Feed ID
Channel_ID <- “”; //Type your Channel ID for RSSI
Channel_ID2 <- “”; //Type your Channel ID for Temp

Xively <- {}; // this makes a 'namespace’
class Xively.Client {
ApiKey = null;
triggers = [];

constructor(apiKey) {
	this.ApiKey = apiKey;
}

/*****************************************
 * method: PUT
 * IN:
 *   feed: a XivelyFeed we are pushing to
 *   ApiKey: Your Xively API Key
 * OUT:
 *   HttpResponse object from Xively
 *   200 and no body is success
 *****************************************/
function Put(feed){
	local url = "https://api.xively.com/v2/feeds/" + feed.FeedID + ".json";
	local headers = { "X-ApiKey" : ApiKey, "Content-Type":"application/json", "User-Agent" : "Xively-Imp-Lib/1.0" };
	local request = http.put(url, headers, feed.ToJson());

	return request.sendsync();
}

/*****************************************
 * method: GET
 * IN:
 *   feed: a XivelyFeed we fulling from
 *   ApiKey: Your Xively API Key
 * OUT:
 *   An updated XivelyFeed object on success
 *   null on failure
 *****************************************/
function Get(feed){
	local url = "https://api.xively.com/v2/feeds/" + feed.FeedID + ".json";
	local headers = { "X-ApiKey" : ApiKey, "User-Agent" : "xively-Imp-Lib/1.0" };
	local request = http.get(url, headers);
	local response = request.sendsync();
	if(response.statuscode != 200) {
		server.log("error sending message: " + response.body);
		return null;
	}

	local channel = http.jsondecode(response.body);
	for (local i = 0; i < channel.datastreams.len(); i++)
	{
		for (local j = 0; j < feed.Channels.len(); j++)
		{
			if (channel.datastreams[i].id == feed.Channels[j].id)
			{
				feed.Channels[j].current_value = channel.datastreams[i].current_value;
				break;
			}
		}
	}

	return feed;
}

}

class Xively.Feed{
FeedID = null;
Channels = null;

constructor(feedID, channels)
{
    this.FeedID = feedID;
    this.Channels = channels;
}

function GetFeedID() { return FeedID; }

function ToJson()
{
    local json = "{ \"datastreams\": [";
    for (local i = 0; i < this.Channels.len(); i++)
    {
        json += this.Channels[i].ToJson();
        if (i < this.Channels.len() - 1) json += ",";
    }
    json += "] }";
    return json;
}

}

class Xively.Channel {
id = null;
current_value = null;

constructor(_id)
{
    this.id = _id;
}

function Set(value) { 
	this.current_value = value; 
}

function Get() { 
	return this.current_value; 
}

function ToJson() { 
	local json = http.jsonencode({id = this.id, current_value = this.current_value });
    server.log(json);
    return json;
}

}
temp <- 0;
device.on (“temp”, function(v) {
temp = v;
});
client <- Xively.Client(API_Key);
device.on(“rssi”, function(v) {
channel1 <- Xively.Channel(Channel_ID);
channel1.Set(v);
channel2 <- Xively.Channel(Channel_ID2);
channel2.Set(temp);
feed1 <- Xively.Feed(Feed_ID, [channel1, channel2]);
client.Put(feed1);
});

//END XIVELY
`

Device Code:

`// pin 9 is the middle of the voltage divider formed by the NTC - read the analog voltage to determine temperature
hardware.pin9.configure(ANALOG_IN);

// all calculations are done in Kelvin

const b_therm = 3988;
const t0_therm = 298.15;

function getTemp() {
// scale the ADC reading to a voltage by dividing by the full-scale value and multiplying by the supply voltage
local v_therm = 3.3 * hardware.pin9.read() / 65535.0;
// calculate the resistance of the thermistor at the current temperature
local r_therm = 10000.0 / ( (3.3 / v_therm) - 1);
local ln_therm = math.log(10000.0 / r_therm);

local t_therm = (t0_therm * b_therm) / (b_therm - t0_therm * ln_therm) - 273.15;

// convert to fahrenheit for the less-scientific among us
local f = (t_therm) + 32.8 ;
// format into a string for the string output port
local f_str = format("%.01f", f)
server.log(“Current temp is “+f_str+” C”);

agent.send(“temp”, f);
agent.send(“rssi”, imp.rssi());

imp.wakeup(30, getTemp);
}

imp.configure(“Thermocouple MT Kent”, [], []);
server.log(format(“rssi %ddBm”,imp.rssi()));
getTemp();
`

… and lets all say thank you to @beardedinventor for writing the Xively code.

Couple of notes:
You need to make a channel for Temp and for RSSI in xively, and fill on all those fields.

I’m sure there is a more elegant way to pass the two variables to the agent, but it works. Maybe someone can suggest a better way.

I posted the entire Xively code so you can get and put if you want to, though in this example you are only putting. That code is available from the Electric Imp GitHub page, and you’ll see it re-used in many projects that I have posted as well.

thanks ever so much.

local url = “https://api.xively.com/v2/feeds/” + feed.FeedID + “.json”;

the above comes up as an error?

i copied it like for like and just added my xively credentials i.e.api key etc as stated i ran it and the above flicked up an error? any thoughts?

thanks

Glenn

Its getting messed up when I paste it into the forum… should be:

local url = “https://api.xively.com/v2/feeds/” + feed.FeedID + “.json”;