Herramientas de usuario

Herramientas del sitio


proyectos:monitorclima_i2c_raspi

Esquema:

Codigo Arduino

#include <LiquidCrystal.h>
#include <Adafruit_Sensor.h>
#include <DHT_U.h>
#include <DHT.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
 
 
// Definimos el pin digital donde se conecta el sensor
#define DHTPIN 7
// Dependiendo del tipo de sensor
#define DHTTYPE DHT11
 
// Inicializamos el sensor DHT11
DHT dht(DHTPIN, DHTTYPE);
 
// para i2c
#define SLAVE_ADDRESS 0x04 // Define the I2C address to Communicate to Uno
 
char number[50];
int state = 0;
 
// para LCD sin I2C
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
 
// Lo que queremos leer
float t=0; // temperatura en C
float f=0; // temperatura en F
float h=0; // humedad en %
 
#define FLOATS_SENT 2 // Los datos que queremos enviar via I2C
float data[FLOATS_SENT]; // arreglo de los datos a enviar
 
// 32 caracteres que vamos a recibir: Pj. Temperatura:28.80 Humedad:26.00
char buffer[32]=" ";
 
char datah[] = "hello";
int index = 0;
 
void setup() {
  // Comenzamos el sensor DHT
  dht.begin();
 
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Listo!");
 
  Serial.begin(9600);
  Wire.begin(SLAVE_ADDRESS);
  Wire.onReceive(receiveData);
  Wire.onRequest(sendData);
  Serial.println("Listo!");
}
 
void loop() {
    // Esperamos 5 segundos entre medidas
    delay(5000);
    char* formato="Temperatura:%s Humedad:%s";
    char ttemp [6]; 
    char thum [6]; 
 
  // Leemos la humedad relativa
  h = dht.readHumidity();
  // Leemos la temperatura en grados centígrados (por defecto)
  t = dht.readTemperature();
  // Leemos la temperatura en grados Fahrenheit
  f = dht.readTemperature(true);
 
  // Comprobamos si ha habido algún error en la lectura
  if (isnan(h) || isnan(t) || isnan(f)) {
    //Serial.println("Error obteniendo los datos del sensor DHT11");
     lcd.setCursor(0, 0);
     lcd.print("Error obteniendo los datos del sensor DHT11");
    return;
  }
 
  // Calcular el índice de calor en Fahrenheit
  float hif = dht.computeHeatIndex(f, h);
  // Calcular el índice de calor en grados centígrados
  float hic = dht.computeHeatIndex(t, h, false);
 
  // Ubicamos el cursor en la primera posición(columna:0) de la segunda línea(fila:1)
  lcd.setCursor(0, 0);
  // Escribimos el número de segundos trascurridos
  lcd.print("Humedad: ");
  lcd.print(h); 
  lcd.print("%  ");
 
  lcd.setCursor(0, 1);
  lcd.print("Tmp:");
  lcd.print(t);
  lcd.print("C ");
  lcd.print(f);
  lcd.print("F         ");
  delay(5000);
  lcd.setCursor(0, 0);
  lcd.print("Indice de Calor ");
  lcd.setCursor(0, 1);
  lcd.print(hic);
  lcd.print("C ");
  lcd.print(hif);
  lcd.print("F         ");
 
  data[0] = t; 
  data[1] = h;
//  data[2] = h;
   dtostrf(t,3,2,ttemp); // guardamos el valor float en un string con precision de 3.2
   dtostrf(h,3,2,thum);  // guardamos el valor float en un string con precision de 3.2
   sprintf(buffer,formato,ttemp,thum); //Concatenamos los valores  para enviarlos via I2C
 
}
 
 
// callback for received data
void receiveData(int byteCount) {
  int i = 0;
  while (Wire.available()) {
    number[i] = Wire.read();
    i++;
  }
  number[i] = '\0';
  Serial.print(number);
 
 
}  // end while
 
 
// callback for sending data
void sendData() { 
    Wire.write(buffer[index]);
    ++index;
    Serial.print(buffer[index]);
    if (index >= 33) {
         index = 0;
    }
 }

Codigo en la raspberry PI

#!/usr/bin/python
 
import smbus
import time
bus = smbus.SMBus(1)
address = 0x04
 
while True:
    data = ""
    for i in range(0, 33):
            data += chr(bus.read_byte(address));
    print data
    time.sleep(3);

Referencias:

proyectos/monitorclima_i2c_raspi.txt · Última modificación: por manuel.floresv