Skip to content

Instantly share code, notes, and snippets.

@widoyo
Created October 20, 2020 11:20
Show Gist options
  • Save widoyo/e90d682fc36311e7a9633d1a0884d984 to your computer and use it in GitHub Desktop.
Save widoyo/e90d682fc36311e7a9633d1a0884d984 to your computer and use it in GitHub Desktop.
/**************************************************************
*
* primabot Davis Sensor Reader
*
* target prosesor: Atmega 328p
*
* Widoyo
* 20 Okt 2020
*
**************************************************************/
#include <sensirion.h>
#define BUCKET_PIN 2
#define SOLAR_RAD_PIN A3
#define MEASURE_PERIODE 300
#define WIND_DIRECTION_PIN A2
#define WIND_SPEED_PIN 3
#define BOUNCE_LIMIT 1000
sensirion sht(A4, A5);
volatile byte tick = 0; // Tipping Bucket TICK
volatile int windrot = 0; // tick putaran wind speed
int wind_speed = 0;
void setup() {
Serial.begin(9600);
// put your setup code here, to run once:
attachInterrupt(digitalPinToInterrupt(BUCKET_PIN), ticking, CHANGE);
attachInterrupt(digitalPinToInterrupt(WIND_SPEED_PIN), wind_rot, FALLING);
}
String line = "";
void loop() {
// put your main code here, to run repeatedly:
static bool request = false;
while (Serial.available()) {
char c = Serial.read();
Serial.print(c);
if (c == 10) {
Serial.print("line");
if (line == ":#") request = true;
line = "";
} else {
line += c;
}
Serial.print(line);
}
if (request){
Serial.print(F("SHT Status: "));
Serial.println(sht.readStatus());
String data = "tick="+tick;
data += ";temp=";
data += readTemp();
data += ";humi=";
data += readHumi();
data += ";wind_speed=";
data += readWindSpeed();
data += ";wind_dir=";
data += readWindDirection(readWindVane());
data += ";solar_rad=";
data += readSunRadiation();
request = false;
Serial.println(data.c_str());
Serial.println("Requested.");
tick = 0;
windrot = 0;
}
}
void ticking() {
static unsigned long last_millis = 0;
unsigned long m = millis();
if (m - last_millis < BOUNCE_LIMIT) {
// terjadi bounce
} else {
if (digitalRead(BUCKET_PIN) == LOW) {
tick++;
}
}
last_millis = millis();
}
void wind_rot() {
// ISR wind rotation
static unsigned long last_millis = 0;
unsigned long m = millis();
if (millis() - last_millis > 15) {
windrot++;
last_millis = m;
}
}
int readSunRadiation() {
int rad = analogRead(SOLAR_RAD_PIN);
int solar_rad = (int)(rad * 1000 / 1.67);
return rad; // mW/M2
}
int readWindSpeed() {
float t = 2.25 / MEASURE_PERIODE;
int ws = (int)(windrot * t);
return windrot;
}
int readWindVane() {
int vane_value = analogRead(WIND_DIRECTION_PIN);
int dir_degree = map(vane_value, 0, 4095, 0, 360);
return vane_value;
}
// Converts compass direction to heading
String readWindDirection(int direction) {
//
String dir;
if(direction < 22)
dir = "U";
else if (direction < 67)
dir = "TL";
else if (direction < 112)
dir = "T";
else if (direction < 157)
dir = "Te";
else if (direction < 212)
dir = "S";
else if (direction < 247)
dir = "BD";
else if (direction < 292)
dir = "B";
else if (direction < 337)
dir = "BL";
else
dir = "U";
return dir;
}
float readTemp() {
return sht.readTemperatureC();
}
float readHumi() {
return sht.readHumidity();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment