Skip to content

Instantly share code, notes, and snippets.

@rpj
Created June 28, 2019 16:48
Show Gist options
  • Save rpj/01a018751c5493cbc8d29094e067d7fd to your computer and use it in GitHub Desktop.
Save rpj/01a018751c5493cbc8d29094e067d7fd to your computer and use it in GitHub Desktop.
US EPA PM2.5 (24-hour average) MATLAB code -- https://thingspeak.com/channels/668594
readChannelID = 655525;
pm2p5FieldId = 2;
pm10p0FieldId = 4;
pm2p5 = thingSpeakRead(readChannelID,'Fields',pm2p5FieldId,'NumMinutes',1440,'ReadKey',readAPIKey);
pm10p0 = thingSpeakRead(readChannelID,'Fields',pm10p0FieldId,'NumMinutes',1440,'ReadKey',readAPIKey);
avgPm2p5 = mean(pm2p5);
avgPm10p0 = mean(pm10p0);
display(avgPm2p5,'avgPm2p5');
display(avgPm10p0,'avgPm10p0');
pm2p5Low = 0
pm2p5Hi = 50
c2p5Low = 0.0
c2p5Hi = 12.0
if (avgPm2p5 > 350.4)
c2p5Low = 350.4
c2p5Hi = 500.5
pm2p5Low = 401
pm2p5Hi = 500
elseif (avgPm2p5 > 250.4)
c2p5Low = 250.4
c2p5Hi = 350.4
pm2p5Low = 301
pm2p5Hi = 400
elseif (avgPm2p5 > 105.4)
c2p5Low = 105.4
c2p5Hi = 250.4
pm2p5Low = 201
pm2p5Hi = 300
elseif (avgPm2p5 > 55.4)
c2p5Low = 55.4
c2p5Hi = 105.4
pm2p5Low = 151
pm2p5Hi = 200
elseif (avgPm2p5 > 35.4)
c2p5Low = 35.4
c2p5Hi = 55.4
pm2p5Low = 101
pm2p5Hi = 150
elseif (avgPm2p5 > 12.0)
c2p5Low = 12.0
c2p5Hi = 35.4
pm2p5Low = 51
pm2p5Hi = 100
end
display(pm2p5Low,'pm2p5Low');
display(pm2p5Hi,'pm2p5Hi');
display(c2p5Low,'c2p5Low');
display(c2p5Hi,'c2p5Hi');
aqiPm2p5 = (((pm2p5Hi - pm2p5Low) / (c2p5Hi - c2p5Low)) * (avgPm2p5 - c2p5Low)) + pm2p5Low;
display(aqiPm2p5,'aqiPm2p5');
pm10p0Low = 0
pm10p0Hi = 50
c10p0Low = 0.0
c10p0Hi = 54.0
if (avgPm10p0 > 504.0)
c10p0Low = 504.0
c10p0Hi = 605.0
pm10p0Low = 401
pm10p0Hi = 500
elseif (avgPm10p0 > 424.0)
c10p0Low = 424.0
c10p0Hi = 504.4
pm10p0Low = 301
pm10p0Hi = 400
elseif (avgPm10p0 > 354.0)
c10p0Low = 354.0
c10p0Hi = 424.0
pm10p0Low = 201
pm10p0Hi = 300
elseif (avgPm10p0 > 254.0)
c10p0Low = 254.0
c10p0Hi = 354.0
pm10p0Low = 151
pm10p0Hi = 200
elseif (avgPm10p0 > 154.0)
c10p0Low = 154.0
c10p0Hi = 254.0
pm10p0Low = 101
pm10p0Hi = 150
elseif (avgPm10p0 > 54.0)
c10p0Low = 54.0
c10p0Hi = 154.0
pm10p0Low = 51
pm10p0Hi = 100
end
display(pm10p0Low,'pm10p0Low');
display(pm10p0Hi,'pm10p0Hi');
display(c10p0Low,'c10p0Low');
display(c10p0Hi,'c10p0Hi');
aqiPm10p0 = (((pm10p0Hi - pm10p0Low) / (c10p0Hi - c10p0Low)) * (avgPm10p0 - c10p0Low)) + pm10p0Low;
display(aqiPm10p0,'aqiPm10p0');
writeChannelID = []; # fill in!
writeAPIKey = ''; # fill in!
thingSpeakWrite(writeChannelID,[avgPm2p5,aqiPm2p5,avgPm10p0,aqiPm10p0],'WriteKey',writeAPIKey);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment