Work out sunrise and sunset for data collected from light sensor | ThingSpeak API | Forum

Avatar

Please consider registering
Guest

Search

— Forum Scope —






— Match —





— Forum Options —





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

Register Lost password?
sp_Feed sp_TopicIcon
Work out sunrise and sunset for data collected from light sensor
Avatar
viiartz

Silver
Forum Posts: 9
sp_UserOfflineSmall Offline
1
September 1, 2017 - 9:04 pm
sp_Permalink sp_Print

Hi All, I have a curly question, maybe! Confused

I have a light sensor (LDR) monitoring the day light and I've worked out that between the values 50 and 60 is when it's either getting dark or getting light, basically sunrise or sunset.

So my theory is to retrieve values in the range between 50 and 60 that happened during AM giving me an approximate sunrise time and then use the same range during PM giving me the sunset time. Because I live in QLD, Australia the sunrise and sunset are around the same time all year round (QLD does not implement day light saving at all) so how would I retrieve values recorded between say 5am to 7am and values between 5pm and 7pm or the lowest value that occurred in the morning time and the lowest value that occurred in the evening time. How would I be able to do this in MATLAB to retrieve the data from my channel?

Has anyone done anything similar, nothing came up during my research hence the question!

Avatar
rw950431

Forum Posts: 250
sp_UserOfflineSmall Offline
2
September 2, 2017 - 3:35 am
sp_Permalink sp_Print

I would just get the official times from somewhere like https://sunrise-sunset.org/api  using the webread() function rather than attempt to guess them from ambient light data..  

But it is possible to retrieve data from a channel for a specific time see https://au.mathworks.com/help/thingspeak/get-channel-field-feed.html particularly the 'start' and 'end' parameters.    Hopefully one of the matlab experts who lurk here might be able to help you with the syntax.

Avatar
piajola

Gold
Forum Posts: 75
sp_UserOfflineSmall Offline
3
September 2, 2017 - 8:14 pm
sp_Permalink sp_Print

Hi viiartz,

As rw950431 says look at the internet of people for a second and get the canonical hours Mr Sun visit us Cool

I saw your channel (I think) where lux is overloading half-day. See the code below, please copy/paste and blindily run it; see the output box, copy results to Excel to see if that make sense, now erase/change at your pleasure all/any pzrt

 

% MATLAB Code below
readChannelID = 312667;
disp(['Working in channel number ',num2str(readChannelID)]); % you compose the text

%% Read Data - your choice on what you want %%
% one way to Read %
%lux = thingSpeakRead(readChannelID, 'OutputFormat','table' , 'NumDays', 1)
lux = thingSpeakRead(readChannelID, 'OutputFormat','table' , 'Numpoints', 5); % without ; ending to display
disp(lux.Lux); % case sensitive
% another way %
[luxx,timestamps] = thingSpeakRead(readChannelID, 'fields',[2] , 'Numpoints', 5);
disp(luxx);

%% selecting time lapse %%
t = datetime('now','TimeZone','local','Format','HH:mm:ss');
disp(['right now ' datestr(t)]);

lorita = datetime('now','TimeZone','America/Lima'); % datestr(datetime('now'),30)
yy=year(lorita); % it is "the today" every time this runs
mm=month(lorita);
dd=day(lorita);
% ho=hour(lorita); you will put your time
% mi=minute(lorita);
% se=second(lorita);

%% Mr.sun is coming ... Mr.sun is coming ... Mr.sun is coming ... Mr.sun is coming ...

tini1=datetime([yy,mm,dd,5,30,0]); %,'TimeZone','UTC') whatever you estimate/wish
tfin1=datetime([yy,mm,dd,6,30,0]); %,'TimeZone','UTC') here hour 6 min 30 secs 0 [06:30:00]
disp(['Mr.sun coming from ' datestr(tini1) ' to ' datestr(tfin1)]);

%% Mr.sun is leaving ... Mr.sun is leaving ... Mr.sun is leaving ... Mr.sun is leaving ...

tini2=datetime([yy,mm,dd,17,30,0]); %,'TimeZone','UTC') whatever you estimate/wish
tfin2=datetime([yy,mm,dd,18,30,0]); %,'TimeZone','UTC') here hour 6 min 30 secs 0 [06:30:00]
disp(['Mr.sun leaving from ' datestr(tini2) ' to ' datestr(tfin2)]);

[lux1,timestamp1] = thingSpeakRead(readChannelID, 'fields',[2] , 'DateRange',[tini1,tfin1]);
[lin1,col1]=size(lux1);
[lux2,timestamp2] = thingSpeakRead(readChannelID, 'fields',[2] , 'DateRange',[tini2,tfin2]);
[lin2,col2]=size(lux2);

disp(['coming ' num2str(lin1) 'x' num2str(col1) ' leaving ' num2str(lin2) 'x' num2str(col2)]);

%% nuclear/fision/fusion intergalactic calculations %%
[maxLux1, maxLuxIdx] = max(lux1);
[minLux1, minLuxIdx] = min(lux1);
[maxLux2, maxLux2Idx] = max(lux2);
[minLux2, minLux2Idx] = min(lux2);

% Choose the timestamp at which the maximum/minimum lux was measured
timeLuxTemp1 = timestamp1(maxLuxIdx);
timeLuxTemp2 = timestamp1(minLuxIdx);
timeLux2Temp1 = timestamp2(maxLux2Idx);
timeLux2Temp2 = timestamp2(minLux2Idx);

display(['Maximum lux Mr.sun coming is ' num2str(maxLux1) '->' datestr(timestamp1(maxLuxIdx))]);
display(['Minimum lux Mr.sun coming is ' num2str(minLux1) '->' datestr(timestamp1(minLuxIdx))]);
display(['Maximum lux Mr.sun leaving is ' num2str(maxLux2) '->' datestr(timestamp2(maxLux2Idx))]);
display(['Minimum lux Mr.sun leaving is ' num2str(minLux2) '->' datestr(timestamp2(minLux2Idx))]);

display('- - - - - - - - - - - -')
for c = 1:length(timestamp1)
disp([num2str(lux1(c)) '--' datestr(timestamp1(c),'HH:MM:SS')])
end
display('- - - - - - - - - - - -')
for c = 1:length(timestamp2)
disp([num2str(lux2(c)) '--' datestr(timestamp2(c),'HH:MM:SS')])
end

 

If it is of some help and you solve your issue post your code so others can use it Smile

Avatar
rw950431

Forum Posts: 250
sp_UserOfflineSmall Offline
4
September 3, 2017 - 7:54 am
sp_Permalink sp_Print

Awesome work @piajola..   Do you happen to know if parameters like  'start' and 'end' can be added to the thingSpeakRead() function- there is a vague reference to 'other parameters' in the docco but no real examples.

Avatar
piajola

Gold
Forum Posts: 75
sp_UserOfflineSmall Offline
5
September 3, 2017 - 5:29 pm
sp_Permalink sp_Print

Hi rw950431,

To my knowledge using thingspeakread the way is 'Daterange' with 2 dates/time as parameters
In a string to be used for retrieval of records you use 'start''end', i.e.
http://api.thingspeak.com/channels/xXx/feeds.json?start=2017-1-8 00:00:00&end=2017-1-8 23:59:59

That is the way I use this, it is enough for me

I hope this is some help Smile

Avatar
viiartz

Silver
Forum Posts: 9
sp_UserOfflineSmall Offline
6
September 7, 2017 - 10:00 am
sp_Permalink sp_Print

piajola said
Hi viiartz,

As rw950431 says look at the internet of people for a second and get the canonical hours Mr Sun visit us Cool

I saw your channel (I think) where lux is overloading half-day. See the code below, please copy/paste and blindily run it; see the output box, copy results to Excel to see if that make sense, now erase/change at your pleasure all/any pzrt

 

% MATLAB Code below
readChannelID = 312667;
disp(['Working in channel number ',num2str(readChannelID)]); % you compose the text

%% Read Data - your choice on what you want %%
% one way to Read %
%lux = thingSpeakRead(readChannelID, 'OutputFormat','table' , 'NumDays', 1)
lux = thingSpeakRead(readChannelID, 'OutputFormat','table' , 'Numpoints', 5); % without ; ending to display
disp(lux.Lux); % case sensitive
% another way %
[luxx,timestamps] = thingSpeakRead(readChannelID, 'fields',[2] , 'Numpoints', 5);
disp(luxx);

%% selecting time lapse %%
t = datetime('now','TimeZone','local','Format','HH:mm:ss');
disp(['right now ' datestr(t)]);

lorita = datetime('now','TimeZone','America/Lima'); % datestr(datetime('now'),30)
yy=year(lorita); % it is "the today" every time this runs
mm=month(lorita);
dd=day(lorita);
% ho=hour(lorita); you will put your time
% mi=minute(lorita);
% se=second(lorita);

%% Mr.sun is coming ... Mr.sun is coming ... Mr.sun is coming ... Mr.sun is coming ...

tini1=datetime([yy,mm,dd,5,30,0]); %,'TimeZone','UTC') whatever you estimate/wish
tfin1=datetime([yy,mm,dd,6,30,0]); %,'TimeZone','UTC') here hour 6 min 30 secs 0 [06:30:00]
disp(['Mr.sun coming from ' datestr(tini1) ' to ' datestr(tfin1)]);

%% Mr.sun is leaving ... Mr.sun is leaving ... Mr.sun is leaving ... Mr.sun is leaving ...

tini2=datetime([yy,mm,dd,17,30,0]); %,'TimeZone','UTC') whatever you estimate/wish
tfin2=datetime([yy,mm,dd,18,30,0]); %,'TimeZone','UTC') here hour 6 min 30 secs 0 [06:30:00]
disp(['Mr.sun leaving from ' datestr(tini2) ' to ' datestr(tfin2)]);

[lux1,timestamp1] = thingSpeakRead(readChannelID, 'fields',[2] , 'DateRange',[tini1,tfin1]);
[lin1,col1]=size(lux1);
[lux2,timestamp2] = thingSpeakRead(readChannelID, 'fields',[2] , 'DateRange',[tini2,tfin2]);
[lin2,col2]=size(lux2);

disp(['coming ' num2str(lin1) 'x' num2str(col1) ' leaving ' num2str(lin2) 'x' num2str(col2)]);

%% nuclear/fision/fusion intergalactic calculations %%
[maxLux1, maxLuxIdx] = max(lux1);
[minLux1, minLuxIdx] = min(lux1);
[maxLux2, maxLux2Idx] = max(lux2);
[minLux2, minLux2Idx] = min(lux2);

% Choose the timestamp at which the maximum/minimum lux was measured
timeLuxTemp1 = timestamp1(maxLuxIdx);
timeLuxTemp2 = timestamp1(minLuxIdx);
timeLux2Temp1 = timestamp2(maxLux2Idx);
timeLux2Temp2 = timestamp2(minLux2Idx);

display(['Maximum lux Mr.sun coming is ' num2str(maxLux1) '->' datestr(timestamp1(maxLuxIdx))]);
display(['Minimum lux Mr.sun coming is ' num2str(minLux1) '->' datestr(timestamp1(minLuxIdx))]);
display(['Maximum lux Mr.sun leaving is ' num2str(maxLux2) '->' datestr(timestamp2(maxLux2Idx))]);
display(['Minimum lux Mr.sun leaving is ' num2str(minLux2) '->' datestr(timestamp2(minLux2Idx))]);

display('- - - - - - - - - - - -')
for c = 1:length(timestamp1)
disp([num2str(lux1(c)) '--' datestr(timestamp1(c),'HH:MM:SS')])
end
display('- - - - - - - - - - - -')
for c = 1:length(timestamp2)
disp([num2str(lux2(c)) '--' datestr(timestamp2(c),'HH:MM:SS')])
end

 

If it is of some help and you solve your issue post your code so others can use it Smile  

Wow piajola, thank you for taking the time to answer my question and writing code, amazing what the code is able to pull out of the data collected. That is exactly or very similar to what I was imagining I wanted to do. Yes, I could get the info from the net but what fun would that be! I'll need to really take a close look at and try to decipher what the code does, I mean I have an idea, I'm just not familiar with Matlab code or functions. I always try and post any code related to a question where possible. Again thank you so much! 

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online:
21 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 250

Vinod: 154

cstapels: 83

piajola: 75

vespapierre: 63

Adarsh_Murthy: 59

Newest Members:

webone

KMVp

aamir786

sugengprayogi

vickysam@gmail.com

farmerkjs

Forum Stats:

Groups: 4

Forums: 17

Topics: 1289

Posts: 4481

 

Member Stats:

Guest Posters: 1

Members: 5652

Moderators: 0

Admins: 2

Administrators: Hans, lee