projects:two_arduino_yun_to_display_temperature_and_humidity_remotely:start

Using two Arduino Yun to display temperature and humidity remotely

Principle

  • One Arduino Yun with a sensor for humidity and temperature
  • One Arduino Yun with a LCD displaying the values

Components used

Working

Code

Arduino with LCD (main board)

/*
 * Read from a remote Arduino Yun the values (timestamp, temperature, humidity) of a DHT22 sensor
 * and display them on a LCD
 */
#include <Bridge.h>
#include <HttpClient.h>
#include <FileIO.h>
 
// LCD I2C TWI LCD2004 Module (SDA : pin 2 / SCL : pin 3)
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
 
String remoteYunUrl;
 
// Set the LCD address to 0x20 for a 20 chars and 4 line display
LiquidCrystal_I2C lcd(0x20, 20, 4);
 
void setup() {
  // Bridge startup
  Bridge.begin();
 
  lcd.init();
  lcd.backlight();
 
  // Reading configuration file
  FileSystem.begin();
 
  // Read IP of remote YUN on the SD cart
  File dataFile = FileSystem.open("/mnt/sd/arduino/config.txt", FILE_READ);
 
  lcd.setCursor(0, 3);
 
  if (dataFile) {
    String param1 = dataFile.readStringUntil('\n');
    param1.indexOf('=', 0);
    String remoteIp = param1.substring(param1.indexOf('=', 0) + 1);
    remoteYunUrl = "http://" + remoteIp + "/arduino/dht22/";
    lcd.print(remote_ip);
    dataFile.close();
  }
  else {
    lcd.print("SD card error");
  }
}
 
void loop() {
  char * i;
  char s[29];
  // Will get a response of this type: 09/09/11-10:28:48;28.00;56.80
  String response = getRemoteYunResponse();
  // Split the values and print them on the LCD
  response.toCharArray(s, 29) ;
  char * res = strtok_r(s,";",&i);
  lcd.setCursor(0, 0);
  lcd.print(res); // date
  res = strtok_r(NULL,";",&i);
  lcd.setCursor(0, 1);
  lcd.print("Temp:");
  lcd.print(res); // temperature
  lcd.print(" deg.");
  res = strtok_r(NULL,";",&i);
  lcd.setCursor(0, 2);
  lcd.print("Hum:");
  lcd.print(res); // humidity
  lcd.print(" %");
  delay(2000);
}
 
String getRemoteYunResponse() {
  HttpClient client;
  client.get(remoteYunUrl);
  String s = "";
  while (client.available()) {
    char c = client.read();
    s += (char)c;
  }
  return s;
}

Arduino with DHT22

/*
 * Arduino Yun board with DHT22 sensor
 * To get the value send this request:
 *   http://<yun_ip>/arduino/dht22/
 * You will get three values separated by a semi-column in this format:
 * <timestamp>;<temperature>;<humidity>
 */
 
// https://github.com/adafruit/DHT-sensor-library
#include "DHT.h"
 
#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>
 
// Listen on default port 5555, the webserver on the Yun
// will forward there all the HTTP requests for us.
YunServer server;
 
const byte DHT22_PIN = 2;
 
DHT dht(DHT22_PIN, DHT22);
 
long lastTimeDht22Read = 0;
// The sensor can only be read from every 1-2s, and requires a minimum
// 2s warm-up after power-on.
const int DHT22_READ_INTERVAL = 2000;
 
void setup(void) {
  // Bridge startup
  Bridge.begin();
  dht.begin();
  // Listen for incoming connection only from localhost
  // (no one from the external network could connect)
  //server.listenOnLocalhost();
  server.begin();
  lastTimeDht22Read = millis();
}
 
void loop(void) {
  // Get clients coming from server
  YunClient client = server.accept();
 
  if (client) {
    while ((millis() - lastTimeDht22Read) < DHT22_READ_INTERVAL);
 
    // Process request
    processReq(client);
 
    lastTimeDht22Read = millis();
    // Close connection and free resources.
    client.stop();
  }
  delay(50);
}
 
void processReq(YunClient client) {
  String temperature, humidity;
  float sensorValue;
  // read the command
  String sensor = client.readStringUntil('\r');
  if (sensor == "dht22") {
    String timeStamp = getTimeStamp();
    sensorValue = dht.readHumidity(); // in %
    if (isnan(sensorValue)) {
      humidity = "DHT sensor error";
    }
    else {
      humidity = String(sensorValue);
    }
 
    sensorValue = dht.readTemperature(); // in °C
    if (isnan(sensorValue)) {
      temperature = "DHT sensor error";
    }
    else {
      temperature = String(sensorValue);
    }
    client.print(timeStamp + ";" + temperature + ";" + humidity);
  }
}
 
String getTimeStamp() {
  String result;
  Process time;
  time.begin("date");
  time.addParameter("+%d/%m/%y-%T");  
  time.run();
 
  while(time.available()>0) {
    char c = time.read();
    if(c != '\n')
      result += c;
  }
  return result;
}
/var/www/tech_mirelsol/data/pages/projects/two_arduino_yun_to_display_temperature_and_humidity_remotely/start.txt · Last modified: 22/07/2014 17:20 by Mirelsol