0

I need to build an app that will display on browser data from 3 sensors. My 3 sensors are : Humidity and temperature( DHT22), vibration sensor () and distance sensor ( HC-SR04). I want to connect these 3 sensors to my Wemos d1 r1 and to send the data to be displayed on a webpage. Until now, i can locally( from Serial monitor of Arduino IDE) read the sensors one by one - i mean, i upload the code for each one...dont know how to combine the 3 codes to works.

So my main question is how to combine these 3 codes into one and access the data from a webpage?

My codes are : vibration( i want to get rid of the LED, just to display the vibration value)

int LED_Pin = D13;
int vibr_Pin = D2;

void setup(){
  pinMode(LED_Pin, OUTPUT);
  pinMode(vibr_Pin, INPUT); //set vibr_Pin input for measurment
  Serial.begin(9600); //init serial 9600
 // Serial.println("----------------------Vibration demo------------------------");
}
void loop(){
  long measurement =TP_init();
  delay(50);
 // Serial.print("measurment = ");
  Serial.println(measurement);
  if (measurement > 4000){
    digitalWrite(LED_Pin, HIGH);
  }
  else{
    digitalWrite(LED_Pin, LOW); 
  }
}

long TP_init(){
  delay(10);
  long measurement=pulseIn (vibr_Pin, HIGH);  //wait for the pin to get HIGH and returns measurement
  return measurement;
}

DHT22 code :

#include <SimpleDHT.h>

// for DHT22, 
//      VCC: 5V or 3V
//      GND: GND
//      DATA: 2
int pinDHT22 = D8;
SimpleDHT22 dht22;

void setup() {
  Serial.begin(115200);
}

void loop() {
  // start working...
  Serial.println("=================================");
  Serial.println("Sample DHT22...");

  // read without samples.
  // @remark We use read2 to get a float data, such as 10.1*C
  //    if user doesn't care about the accurate data, use read to get a byte data, such as 10*C.
  float temperature = 0;
  float humidity = 0;
  int err = SimpleDHTErrSuccess;
  if ((err = dht22.read2(pinDHT22, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
    Serial.print("Read DHT22 failed, err="); Serial.println(err);delay(2000);
    return;
  }

  Serial.print("Sample OK: ");
  Serial.print((float)temperature); Serial.print(" *C, ");
  Serial.print((float)humidity); Serial.println(" RH%");

  // DHT22 sampling rate is 0.5HZ.
  delay(2500);
}

HC-SR04 CODE :

// defines pins numbers
const int trigPin = D5;
const int echoPin = D6;
// defines variables
long duration;
int distance_cm;
int distance_in;
void setup() {
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  Serial.begin(9600); // Starts the serial communication
}
void loop() {
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(500);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(500);
  digitalWrite(trigPin, LOW);
  /* Reads the echoPin, returns the sound wave 
     travel time in microseconds */
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance_cm = duration / 58;
  distance_in = duration / 148;
  /* Prints the distance on the Serial Monitor
     Serial.print("Distance: ");
     Serial.print(distance_in);
     Serial.println(" inches"); */
  Serial.print("Distance: ");
  Serial.print(distance_cm);
  Serial.println(" centimeters");
  /* uncomment the 3 Serial.print lines
     to display measure in inches */
}
  • thinger.io – Majenko May 25 '18 at 13:21
  • I don't know if you can understand the code of my project, but there is a WebServer with html and js files you could modify for your use and the project is build up from multiple ino files for different sensors and actuators with setup and loop in every ino file github.com/jandrassy/Regulator/tree/master/Regulator – Juraj May 25 '18 at 13:25
  • i will have a look and that link. my main problem is that my coding skills are basic and its hard for me to create/understand more complex structures. i need to do this application i've posted here to integrate it into a bachelors degree diploma project and i have only a few days left :( – Andrei Opris May 25 '18 at 13:29
0

I would suggest using thinger.io. It has a library that is insanely simple to use.

This is code for a greenhouse monitor that I wrote using their library. It exposes the temperature and humidity from a DHT22, the time the unit has been running, and when it was last programmed. And all with OTA support as well.

#include <ESP8266WiFi.h>
#include <dht.h>
#include <ThingerESP8266.h>
#include <ArduinoOTA.h>
#include <Average.h>

dht dht22;

Average<float> temperature(10);
Average<float> humidity(10);

ThingerESP8266 thing("myaccount", "thisunit", "somekey");

void getSample() {
    static uint32_t ts = 0;
    if (millis() - ts < 10000) {
        return;
    }
    ts = millis();

    dht22.read22(D6);
    int samples = 0;
    while (
        (dht22.temperature < -100) ||
        (dht22.temperature > 200) ||
        (dht22.humidity < -100) ||
        (dht22.humidity > 200))
    {
        delay(10);
        dht22.read22(D6);
        samples++;
        if (samples > 10) {
            return;
        }
    }
    temperature.push(dht22.temperature);
    humidity.push(dht22.humidity);
}

void setup() {
    WiFi.hostname("Greenhouse");
    thing.add_wifi("MyWiFiSSID", "MyWiFiPSK");

    thing["temperature"] >> [](pson& out) {
        getSample();
        out = temperature.mean();
    };
    thing["humidity"] >> [](pson& out) {
        getSample();
        out = humidity.mean();
    };
    thing["uptime"] >> [](pson& out) {
        out = millis() / 1000.0;
    };
    thing["compiled"] >> [](pson& out) {
        out = __DATE__ " " __TIME__;
    };

    ArduinoOTA.setHostname("Greenhouse");
    ArduinoOTA.setPassword("MyOTAPassword");
    ArduinoOTA.begin();
}

void loop() {
    thing.handle();
    ArduinoOTA.handle();
}

It's a little more complex that you may need, since I do averaging and filtering of the sampled data to clean it up a little. All the magic of when to take samples and how to deal with the data is done on the thinger.io website. But the code, as you can see, is insanely simple.

0

i managed to merge the 3 codes into 1. I still need to adapt it to send the information on a webpage. Up to now, the code for 3 sensors is:

  #include <SimpleDHT.h>

// for DHT22, 
//      VCC: 5V or 3V
//      GND: GND
//      DATA: 2
const int trigPin = D5;
const int echoPin = D6;
long duration;
int distance_cm;
int distance_in;
int vibr_Pin = D8;
int pinDHT22 = D2;
SimpleDHT22 dht22;

void setup() {
  Serial.begin(9600);
  pinMode(vibr_Pin, INPUT);
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
}

void loop() {
  // start working...
  Serial.println("=================================");
  Serial.println("TEMPERATURE & HUMIDITY");

  // read without samples.
  // @remark We use read2 to get a float data, such as 10.1*C
  //    if user doesn't care about the accurate data, use read to get a byte data, such as 10*C.
  float temperature = 0;
  float humidity = 0;
 int err = SimpleDHTErrSuccess;
           if ((err = dht22.read2(pinDHT22, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
             Serial.print("Read DHT22 failed, err="); Serial.println(err);delay(2000);
              return; }

  Serial.print("Temperature & Humidity: ");
  Serial.print((float)temperature); Serial.print(" *C, ");
  Serial.print((float)humidity); Serial.println(" RH%");

  // DHT22 sampling rate is 0.5HZ.
  delay(2500);
  Serial.println("=================================");
  Serial.println("VIBRATION");
  long measurement =TP_init();
  delay(50);
  Serial.print("measurment = ");
  Serial.println(measurement);
    Serial.println("=================================");
    Serial.println("DISTANCE");
    // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(500);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(500);
  digitalWrite(trigPin, LOW);
  /* Reads the echoPin, returns the sound wave 
     travel time in microseconds */
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance_cm = duration / 58;
  distance_in = duration / 148;
  /* Prints the distance on the Serial Monitor
     Serial.print("Distance: ");
     Serial.print(distance_in);
     Serial.println(" inches"); */
  Serial.print("Distance: ");
  Serial.print(distance_cm);
  Serial.println(" centimeters");
  /* uncomment the 3 Serial.print lines
     to display measure in inches */                                                                              


  }
  long TP_init(){
  delay(10);
  long measurement=pulseIn (vibr_Pin, HIGH);  //wait for the pin to get HIGH and returns measurement
  return measurement;

}

so, in the serial monitor of Arduino IDE, we will see the values displayed for our 3 sensors. I would really appreciate if somebody can help me to integrate this code to an IoT platform.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.