Combining Arduino Mega 2560 and Raspberry Pi 3B

Sensors

  • Waveshare Dust Sensor
  • MQ-2
  • MQ-135
  • DHT11
  • BMP180

Computers

  • Arduino Mega 2560
  • Raspberry Pi 3 B

Wiring

Sensor Arduino
 Waveshare Dust Sensor  
 ILED  Not used
AOUT A0
GND GND
VCC VCC (5V)
MQ-2  
Output A2
GND GND
VCC VCC (5V)
MQ-135  
Output A3
GND GND
VCC VCC (5V)
DHT11  
VCC VCC (5V)
GND GND
Data 2
BMP180  
VCC VCC (3.3 V)
GND GND
SCL SCL (21)
SDA SDA (20)

The following Arduino sketch reads all sensors and writes their data to the serial console:

#include 

// ---------- Temeperature and humidity sensor:
#include 
#include 
dht11 DHT11;
#define DHT11PIN 2

// ---------- MQ2:
int smokeQ2 = A2;

// ---------- MQ135:
int smokeQ135 = A3;

// ----------- Dust sensor:
#define        COV_RATIO                       0.2            //ug/mmm / mv
#define        NO_DUST_VOLTAGE                 400            //mv
#define        SYS_VOLTAGE                     5000

// ----------- BMP180:

#include 
#include 
#include 

Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085);

const int iled = 7;                                            //drive the led of sensor
const int vout = 0;                                            //analog input

float dust, voltage;
int   adcvalue;

int Filter(int m)
{
  static int flag_first = 0, _buff[10], sum;
  const int _buff_max = 10;
  int i;

  if (flag_first == 0)
  {
    flag_first = 1;

    for (i = 0, sum = 0; i < _buff_max; i++)
    {
      _buff[i] = m;
      sum += _buff[i];
    }
    return m;
  }
  else
  {
    sum -= _buff[0];
    for (i = 0; i < (_buff_max - 1); i++)
    {
      _buff[i] = _buff[i + 1];
    }
    _buff[9] = m;
    sum += _buff[9];

    i = sum / 10.0;
    return i;
  }
}
// ###############################################
void setup() {
  Serial.begin(9600);

  // -----MQ2:
  pinMode(smokeQ2, INPUT);

  // -----MQ135:
  pinMode(smokeQ135, INPUT);

  // ------ Dust sensor:
  pinMode(iled, OUTPUT);
  digitalWrite(iled, LOW);

  // ------ BMP180:
  if (!bmp.begin())
  {
    Serial.print("BMP180 doesn't work");
    while (1);
  }


}

void loop() {
  // -------- MQ2:
  int mq2 = analogRead(smokeQ2);

  // -------- MQ135:
  int mq135 = analogRead(smokeQ135);


  // -------- Dust sensor:

  digitalWrite(iled, HIGH);
  delayMicroseconds(280);
  adcvalue = analogRead(vout);
  digitalWrite(iled, LOW);
  adcvalue = Filter(adcvalue);
  voltage = (SYS_VOLTAGE / 1024.0) * adcvalue * 11;

  if (voltage >= NO_DUST_VOLTAGE)
  {
    voltage -= NO_DUST_VOLTAGE;

    dust = voltage * COV_RATIO;
  }
  else
    dust = 0;

  // -----Read DHT11:  

  DHT11.read(DHT11PIN);

  Serial.print(dust);
  Serial.print("\t");
  Serial.print(mq2);
  Serial.print("\t");
  Serial.print(mq135);
  Serial.print("\t");
  Serial.print((float)DHT11.temperature);
  Serial.print("\t");
  Serial.print((float)DHT11.humidity);
  Serial.print("\t");

  //----- BMP180:
  sensors_event_t event;
  bmp.getEvent(&event);
  Serial.print(event.pressure);
  Serial.print("\t");
  float temperature;
  bmp.getTemperature(&temperature);
  Serial.print(temperature);

  float seaLevelPressure = SENSORS_PRESSURE_SEALEVELHPA;
  Serial.print("\t");
  Serial.print(bmp.pressureToAltitude(seaLevelPressure, event.pressure));

  Serial.println("");
  delay(2000);

}

Download here the Sketch.

After connecting the Arduino with the Raspberry Pi via USB wire, it is possible to read the data from the Arduino on the Raspi with a Python script. The Python script writes the data a) into a file called “sensors.dat” and b) stores the data in an Influxdb (credentials needs to be adjusted):

#!/usr/bin/python
import serial
import time
from influxdb import InfluxDBClient

client = InfluxDBClient("localhost", 8086, "USER", "PASSWORD", "DATABASE")

s = serial.Serial('/dev/ttyACM0', 9600) # Devide  anpassen

time.sleep(5) 

file = open("sensors.dat", "a")

try:
        while True:
                response = s.readline().strip()
                rec = response.split("\t")
                if(len(rec) == 8):
                        dust = float(rec[0])
                        mq2 = float(rec[1])
                        mq135 = float(rec[2])
                        temperature = float(rec[3])
                        humidity = float(rec[4])
                        pressure = float(rec[5])
                        bmp180temp = float(rec[6])
                        alt = float(rec[7])
                        json_body = [
                        {
                        "measurement": "sensors",
                        "tags": {
                        "source": "arduino"
                        },

                        "fields": {
                        "dust" : dust,
                        "mq2" : mq2,
                        "mq135" : mq135,
                        "temperature" : temperature,
                        "humidity" : humidity,
                        "pressure" : pressure,
                        "bmp180temp" : bmp180temp,
                        "altitude" : alt
                        }
                        }
                        ]
                        print json_body
                        print "-------------"
                        client.write_points(json_body)


                file.write(response)
                print(response),
except KeyboardInterrupt:
        file.close()
        s.close()

Finally, the data can be visualized with Grafana or similar tools:



Click to enlarge