Send info to a different field | MATLAB | Forum

Avatar

Please consider registering
Guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed sp_TopicIcon
Send info to a different field
No permission to create posts
September 20, 2015
12:12 am
Avatar
hmslouis
Australia

Silver
Members
Forum Posts: 6
Member Since:
September 18, 2015
sp_UserOfflineSmall Offline

Hi all,
My first post and am new to matlab and thingspeak so hope my code is easy to fix.
Basically I need to read from fields 1 & 2 then send the difference to field 3.

When I run my code below I keep getting this error message

Error using thingSpeakWrite
Wait for 15 seconds since the last update was completed before requesting another.

My code
% Channel ID to read data from
ChannelID = 48915;
% Field index ID
PortFieldID = 1;
StbdFieldID = 2;
ListFieldID = 3;
% API key
writeKey = 'OQQMWYDW2J5WLAJQ';

% Read Port and stbd

PortVal = thingSpeakRead(ChannelID, 'Fields', PortFieldID);
StbdVal = thingSpeakRead(ChannelID, 'Fields', StbdFieldID);
% Calculate difference
List = PortVal - StbdVal;

thingSpeakWrite(ChannelID, List, 'Fields', 3, 'WriteKey', writeKey)

September 21, 2015
7:53 am
Avatar
Adarsh_Murthy

MathWorks
Members
Forum Posts: 62
Member Since:
August 25, 2015
sp_UserOfflineSmall Offline

Hi hmslouis,

ThingSpeak implements a 15 sec limit between consecutive updates to a channel. Therefore, if your fields 1 and 2 on channel 48915 are being updated once every 15 seconds, then the update to field 3 from your MATLAB code will be within 15 seconds of the last update to (fields 1 & 2 on) channel 48915. Therefore, thingSpeakWrite function generates an error. In such a case, I would create a new channel to store the difference between field 1 and field 2. This would avoid the update rate limit.

Example code - I have renamed channelID to ReadChannelID and added WriteChannelID to store the ID of the new channel that the difference data needs to be written to. I have also set the difference data to be written to field 1 of the new channel instead of field 3:

% Channel ID to read data from
ReadChannelID = 48915;
WriteChannelID =
% Field index ID
PortFieldID = 1;
StbdFieldID = 2;
ListFieldID = 3;
% API key
writeKey = ;
% Read Port and stbd
PortVal = thingSpeakRead(ReadChannelID , 'Fields', PortFieldID);
StbdVal = thingSpeakRead(ReadChannelID , 'Fields', StbdFieldID);
% Calculate difference
List = PortVal - StbdVal;
thingSpeakWrite(WriteChannelID , List, 'Fields', 1, 'WriteKey', writeKey)

- Adarsh

September 21, 2015
10:51 pm
Avatar
hmslouis
Australia

Silver
Members
Forum Posts: 6
Member Since:
September 18, 2015
sp_UserOfflineSmall Offline

Hi Adarsh,

Thanks so much for your explanation - that make perfect sense now.
I set up a new channel and send the data there now and the error is gone, which is great.
When I check the data in the new channel however there is nothing there.
Do I need to do something for this code to loop so that the new data will show up in the new channel?

Thanks heaps

Louis

September 21, 2015
11:00 pm
Avatar
lee

Admin
Forum Posts: 457
Member Since:
March 14, 2011
sp_UserOfflineSmall Offline

Hi Louis,

The easiest way to do this would be to create a React ( https://thingspeak.com/docs/react ) which is connected to your MATLAB analysis. Every time new data is written to your original channel, React will automatically run the MATLAB code and write to your derived channel via thingSpeakWrite.

lee

September 22, 2015
1:22 am
Avatar
hmslouis
Australia

Silver
Members
Forum Posts: 6
Member Since:
September 18, 2015
sp_UserOfflineSmall Offline

Thanks Lee,

I'll do some more reading and give it as go.
If I'm right my process flows like this;

1. Photon 1 reads pressure to field 1 on my data collection channel
2. Photon 2 reads pressure to field 2 on the same data collection channel
3. React senses a change in values of field 1 above and runs Matlab analysis
4. Matlab analysis reads in the 2 values above and sends them to a new channel
5. The new channel data is posted via a google gauge plugin

Does this make sense.

Cheers - thanks again for your help.
Louis

September 22, 2015
2:33 am
Avatar
hmslouis
Australia

Silver
Members
Forum Posts: 6
Member Since:
September 18, 2015
sp_UserOfflineSmall Offline

Hi all,

I think the whole loop works better for me by using separate channels as opposed to sending info to different fields within the same channel.
I'll test further but for my application I need more frequent feeds and less storage of data.
Is there a paid version or equivalent service that reads every 5 seconds but stores much less data ( maybe the previous 5 - 10 readings)?
For my current application I only need to know what is happening right now and the past is not so relevant any more.

Am loving the learning curve and the flexibility of thingspeak and can't wait to set my little project up.
Cheers,
Louis

September 22, 2015
9:29 am
Avatar
lee

Admin
Forum Posts: 457
Member Since:
March 14, 2011
sp_UserOfflineSmall Offline

Hi Louis,

Yes you've got the workflow correct, I'm glad that you seem to have things working!

No, we don't have a paid version that would allow updates to a single channel at a more frequent rate, but it is something we're considering for the future.

lee

September 22, 2015
5:25 pm
Avatar
hmslouis
Australia

Silver
Members
Forum Posts: 6
Member Since:
September 18, 2015
sp_UserOfflineSmall Offline

Thanks Lee,

All working well now.
The other way of achieving a more frequent output for my project would be to alter the React to include multiple parameters.
For example having either of two conditions met (e.g. if channel X field>0 or if channel Y field>0).
I tried to make 2 separate Reacts but it wouldn't allow it.
Currently I can setup the 2 feed channels to update (every 15 seconds) but have them set up to alternate thereby sending a value every 7-8 seconds.
This frequency of data would be ideal for me. But the React on only one channel gets me back to reading only every 15 seconds.

Is my understanding correct and if so might there be another work around for me?
Cheers,
Louis

September 22, 2015
10:25 pm
Avatar
lee

Admin
Forum Posts: 457
Member Since:
March 14, 2011
sp_UserOfflineSmall Offline

Hi Louis,

An alternative could be to set React to update on every data insert, and run a MATLAB analyses each time. You could check both conditions in MATLAB and then write to the derived channel if necessary.

If you're ok with delayed data, you can also set a TimeControl to run every 5 minutes and perform the MATLAB analysis.

lee

September 23, 2015
4:00 am
Avatar
hmslouis
Australia

Silver
Members
Forum Posts: 6
Member Since:
September 18, 2015
sp_UserOfflineSmall Offline

Thanks again Lee,

Yes I've set it up to react on every data insert.
All working well.

Cheers,
Louis

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online:
27 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 261

Vinod: 196

piajola: 85

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

Newest Members:

James181, kimberlypg2, JamesAdorb, waynesmith, paulettebw60, ashleytb4

Administrators: Hans: 387, lee: 457