Things used in this project

Schematics

Tide Sensor
Pic06 1024x625 lec2d0pf7z

Code

Detect Tidal ThresholdsMATLAB
% This code reads the past 30 minutes tide range data from ThingSpeak
% channel 22641. Calculates the median of the tide level 5 minutes ago and
% 25 minutes ago.  It then uses these values to check if either a) the tide
% is going down and has passed a defined 'falling threshold' or b) is going
% up and has passed a defined 'rising threshold'. When either of these
% thresholds is passed the tide height when that happens is written to a
% ThingSpeak Falling Tide or Rising Tide channel as appropriate. The code
% also checks to make sure that sufficient time has past before writing a
% new value to avoid multiple triggers for the same threshold.

% Read the last 30 minutes of tide data
range = thingSpeakRead(22641,'Fields',[1],'NumMinutes',30, ...
    'URL','http://thingspeak.com/');

% Convert range from sensor to water in mm to depth from mud in inches
depth = (3449 - range)/24.5; %depth from mud in inches

% Define threshold level in inches
fallingthreshold = 20;
risingthreshold = 15;

% Define minimum time in minutes between alerts
alertGap = 60; % minutes
alertGapNum = alertGap/60/24; % Convert to datenum

% Set up index to divide data into roughly 10 minute periods
i = round(length(depth)/3);

% Make sure we are working with enough data
if i>8

    % Calculate the median of each 10 minute period
    depth25minago = median(depth(1:i));
    depth5minago = round(median(depth(2*i+1:end)),1);

    % Check time since last change used below to avoid double triggers
    timeNow = datetime('now');


    % Read the last falling time
    [lastFallingHeight,lastFallingTime] = thingSpeakRead(50192,...
        'ReadKey', '9QSZ88F42HE2BA4H',...
        'URL','https://api.thingspeak.com/');
    if isempty(lastFallingTime) % at the start this will be empty
        sinceLastFall = 1;
    else
        sinceLastFall = datenum(timeNow)-datenum(lastFallingTime);
    end
    % Read the last rising time
    [lastRisingHeight,lastRisingTime] = thingSpeakRead(50193,...
        'ReadKey', '9AL2W9R0Y07GFR02',...
        'URL','https://api.thingspeak.com/');
    if isempty(lastRisingTime) % at the start this will be empty
        sinceLastRise = 1;
    else
        sinceLastRise = datenum(timeNow)-datenum(lastRisingTime);
    end

        % If the tide has fallen below the falling threshold write to the
        % ThingSpeak falling tide channel
        if (depth25minago > fallingthreshold)...
            && (depth5minago <= fallingthreshold)...
            && (sinceLastFall > alertGapNum)
            thingSpeakWrite(50192, depth5minago, ...
                'WriteKey', '928PKIJS64PXECSL',...
                'URL','https://api.thingspeak.com/');
        end

        % If the tide has fallen below the rising threshold write to the
        % ThingSpeak rising tide channel
        if (depth25minago < risingthreshold)...
            && (depth5minago >= risingthreshold)...
            && (sinceLastRise > alertGapNum)
            thingSpeakWrite(50193, depth5minago,...
                'WriteKey', 'WYVR5N22N4NABWR6',...
                'URL','https://api.thingspeak.com/');
        end

end
Tide Alerts on GitHub

Credits

83e164e22a0b069f4807cac094df28aa
Hans Scharler
10 projects • 32 followers
IoT Engineer, Maker - I have a toaster that has been tweeting since 2008.
Contact
Face 2
Robert Mawrey
0 projects • 12 followers
Founder and CEO of Sentient Things, Inc. Former CEO of IoT startup ioBridge.com.
Contact

Comments