Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1251

Python • Re: Arduino-Pi Weather Station issue

$
0
0
Good morning, there is no readline in the arduino code...but for curiosity sake, here is my arduino code, if this will get someone started. I must note it is unfinished as I am currently waiting on my second encoder to arrive...my anemometer is finished...currently 3d printing the mount and housings for the vane and anemometer at time of this post.

Code:

//most code are snippets from others work...#include <DFRobot_VEML6075.h>#include <Adafruit_BMP085.h>#include <Wire.h>#include <Adafruit_Sensor.h>#include <DHT.h>#include <OneWire.h>#include <DallasTemperature.h>#include <Arduino.h>#define DHTTYPE DHT22Adafruit_BMP085 bmp;#define VEML6075_ADDR 0x10#define DEBOUNCE_TIME 80#define CALC_INTERVAL 1000DFRobot_VEML6075_IIC VEML6075(&Wire, VEML6075_ADDR);const int e_Pin_1 = 2;const int e_Pin_2 = 3;const int e_Pin_3 = 4;const int e_Pin_4 = 5;const int t_pin = 22;const int h_pin = 24;const int l_pin = A1;const int rain_pin = 25;volatile int lastEncoded_1 = 0;volatile long encoderValue_1 = 0;volatile int lastEncoded_2 = 0;volatile long encoderValue_2 = 0;volatile unsigned int trigger = 0;volatile unsigned long last_micros_rg;long lastencoderValue_1 = 0;long lastencoderValue_2 = 0;int lastMSB_1 = 0;int lastMSB_2 = 0;int lastLSB_1 = 0;int lastLSB_2 = 0;int state = 0;OneWire oneWire(t_pin);DallasTemperature sensors(&oneWire);DHT dht(h_pin, DHTTYPE);float p = 0;float press = 0;float temp_f = 0;float old = 0;float newp = 0;float rainfall = 0;float amt = 0;unsigned long nextCalc;unsigned long timer;void setup() {  Serial.begin (9600);  pinMode(e_Pin_1, INPUT);   pinMode(e_Pin_2, INPUT);  pinMode(e_Pin_3, INPUT);  pinMode(e_Pin_4, INPUT);  pinMode(rain_pin, INPUT);  digitalWrite(e_Pin_1, HIGH);  //turn pullup resistor on  digitalWrite(e_Pin_2, HIGH);  //turn pullup resistor on  digitalWrite(e_Pin_3, HIGH);  //turn pullup resistor on  digitalWrite(e_Pin_4, HIGH);  //turn pullup resistor on  attachInterrupt(0, updateEncoder, CHANGE);   attachInterrupt(1, updateEncoder, CHANGE);  attachInterrupt(2, updateEncoder, CHANGE);  attachInterrupt(3, updateEncoder, CHANGE);  sensors.begin();  dht.begin();  if (!bmp.begin()) {    Serial.println("Could not find the BMP180 sensor...");    while(1) {}  }  while(!Serial);  while(VEML6075.begin() != true) {    Serial.println("UV sensor failed to start");    delay(2000);  }  //Serial.println("UV sensor connected");  attachInterrupt(digitalPinToInterrupt(rain_pin), rain, RISING);  nextCalc = millis() + CALC_INTERVAL;}void loop() {    timer = millis();  if(timer > nextCalc) {    nextCalc = timer + CALC_INTERVAL;  }  //Serial.println(encoderValue_1);  //Serial.println(encoderValue_2);  //anny();  //vane();  temp();  humid();  baro();  rain();  uv();  light();  delay(30100);}void updateEncoder() {  int MSB_1 = digitalRead(e_Pin_1); //MSB = most significant bit for encoder 1  int LSB_1 = digitalRead(e_Pin_2); //LSB = least significant bit for encoder 1  int MSB_2 = digitalRead(e_Pin_3); //MSB = most significant bit for encoder 2  int LSB_2 = digitalRead(e_Pin_4); //LSB = least significant bit for encoder 2  int encoded_1 = (MSB_1 << 1) |LSB_1; //converting the 2 pin value to single numberv  int encoded_2 = (MSB_2 << 1) |LSB_2; //converting the 2 pin value to single numberv  int sum_1  = (lastEncoded_1 << 2) | encoded_1; //adding it to the previous encoded value  int sum_2  = (lastEncoded_2 << 2) | encoded_2; //adding it to the previous encoded value  if(sum_1 == 0b1101 || sum_1 == 0b0100 || sum_1 == 0b0010 || sum_1 == 0b1011) encoderValue_1 ++;  if(sum_1 == 0b1110 || sum_1 == 0b0111 || sum_1 == 0b0001 || sum_1 == 0b1000) encoderValue_1 --;  if(sum_2 == 0b1101 || sum_2 == 0b0100 || sum_2 == 0b0010 || sum_2 == 0b1011) encoderValue_2 ++;  if(sum_2 == 0b1110 || sum_2 == 0b0111 || sum_2 == 0b0001 || sum_2 == 0b1000) encoderValue_2 --;  lastEncoded_1 = encoded_1; //store this value for next time  lastEncoded_2 = encoded_2; //store this value for next time}void temp() {  sensors.requestTemperatures();  temp_f = sensors.getTempFByIndex(0);  Serial.print("Temp = ");  Serial.print(temp_f);  Serial.println(" F");  return temp_f;}void humid() {  float h = dht.readHumidity();  if (isnan(h)) {    Serial.println("Failed to read from the DHT");    return;  }  Serial.print("Humidity = ");  Serial.print(h);  Serial.println("%");  return h;}void baro() {  p = bmp.readPressure();  press = (p / 3.2435) / 1000;  Serial.print("Barometer @ ");  Serial.print(press);  Serial.print(" inHg");  newp = press;  if (old < newp) {    Serial.println(" and rising");  }  else if (old > newp) {    Serial.println(" and dropping");  }  else {    Serial.println(" and is stable");  }  old = newp;  newp = 0;  return press;}void uv() {  uint16_t  UvaRaw = VEML6075.readUvaRaw();  uint16_t  UvbRaw = VEML6075.readUvbRaw();  uint16_t comp1Raw = VEML6075.readUvComp1Raw();  uint16_t comp2Raw = VEML6075.readUvComp2Raw();  float Uva = VEML6075.getUva();  float Uvb = VEML6075.getUvb();  float Uvi = VEML6075.getUvi(Uva, Uvb);  Serial.print("UVA: ");  Serial.println(Uva, 2);  Serial.print("UVB: ");  Serial.println(Uvb, 2);  Serial.print("UV Index: ");  Serial.print(Uvi, 4);  if(Uvi < UVI_LOW) {    Serial.println(" which is low");  }  else if(Uvi < UVI_MODERATE) {    Serial.println(" which is moderate");  }    else if(Uvi < UVI_HIGH) {    Serial.println(" which is high");  }  else if(Uvi < UVI_VERY_HIGH) {    Serial.println(" which is very high");  }  else {    Serial.println(" which is EXTREME, use caution!");  }  Serial.print("Amount of power -> ");  float Uvi2 = (104 * Uvi - 18.365) /1000;  Serial.print(Uvi2, 4);  Serial.println(" kW/M2");  return Uva, Uvb, Uvi, Uvi2;}void light() {  float lit = analogRead(l_pin);  float li = lit;  Serial.print("Light intensity @ ");  Serial.println(li);  return li;}void rain() {  state = digitalRead(rain_pin);  if (state == LOW) { //magnet present    if((long)(micros() - last_micros_rg) >= DEBOUNCE_TIME) {    trigger += 1;    last_micros_rg = micros();    rainfall = trigger * .03730;    }  }  Serial.print("Rainfall = ");  Serial.print(rainfall);  Serial.println(" inches");  return rainfall;}void anny() {  // calculate the wind speed  Serial.println("Calculating wind speed...");}void vane() {  // calculate the degree position of the weather vane  Serial.println("Calculating wind direction...");}
Proof it's sending data to the Pi...
Image

This is still a work in progress...Next challenge is to stop the rainfall from continuously adding. It is currently in the "away from switch" position and therefore is adding unnecessarily...steps lol

Statistics: Posted by Osprey72 — Tue Apr 15, 2025 12:01 pm



Viewing all articles
Browse latest Browse all 1251

Trending Articles