Sending DHT data through USB to MySQL

For our semester project we had to realise something with Arduino that uses:

  • USB (serial) communication
  • Python to get the data from the serial port
  • MySQL to store the data
  • HTML (or some other accepted Web tech) to display the data

We were planning to use 2 different DHT sensors (DHT-11 and DHT-22) along with a MQ2 Smoke/Gas detector and the idea was that the DHT-11 would be placed in simulated room A and the DHT-22 and MQ2 in room B.

Maybe I'm getting this all wrong but the Arduino just spews out a long string of 4 digit numbers I'm not sure would make any sense when send to a SQL table and I'm also unsure if there is a possibility to differentiate what number comes from which sensor?

#include <DHT.h>
#include <DHT_U.h>
#include <MQUnifiedsensor.h>

#define DHT1PIN          2                                    
#define DHT2PIN          3
#define DHT1TYPE        DHT11 
#define DHT2TYPE        DHT22 
#define MQ2pin            (A2)
int sensorInput;
int senslmt         =          200;
int incomingData; 
float sensorValue;
DHT dht1                (DHT1PIN, DHT1TYPE);
DHT dht2                (DHT2PIN, DHT2TYPE);


void setup() { 
    Serial.begin                          (9600);                  
    dht1.begin();
    dht2.begin();                  
    pinMode(MQ2pin,              INPUT);  
    delay(22000);                                          
}

void loop() {
    sensorValue                      = analogRead      (MQ2pin);
    unsigned long time          = millis                ();          

    if (time            % 2000 == 0) {                                
        float h1        = dht1.readHumidity();                               
        float t1         = dht1.readTemperature();
        Serial.print (h1);
        Serial.print (t1);
    }
    if (time            % 4000 == 2000) {                             
        float h2        = dht2.readHumidity();
        float t2         = dht2.readTemperature();
        Serial.print (h2);
        Serial.print (t2);
    }
    if (time            % 500  == 0) {                               
        sensorValue             = analogRead      (MQ2pin);              
        Serial.print(sensorValue);
    }
}


I apologise for the messy code but it's my second project overall and none of us has ever worked with C++, SQL or Python before.

It won't. Your python code, running on the PC, will need to read those numbers and format them into SQL INSERT or UPDATE statements and send them to the database.

I would say that is a necessity! Your Arduino code will need to print something before the numbers to indicate which sensor they are coming from.

Your Arduino code seems to be using some potentially unreliable method of sending the sensor values from the different sensors every 2s, or 4s(?) or 0.5s. Why not simplify this and send the values from all 5 sensors together every 5s?

1 Like

For starters, just add another print() statement before the values that specifies the room/sensor. Also look at the println() function which will add a newline after the data.

    if (time            % 2000 == 0) {                                
        float h1        = dht1.readHumidity();                               
        float t1         = dht1.readTemperature();
        Serial.print("Room A:");
        Serial.print(h1);
        Serial.print(",");
        Serial.println (t1);
    }
   ...
1 Like

It won't. Your python code, running on the PC, will need to read those numbers and format them into SQL INSERT or UPDATE statements and send them to the database.

Ok, need to check my Python script again then.

Your Arduino code seems to be using some potentially unreliable method of sending the sensor values from the different sensors every 2s, or 4s(?) or 0.5s. Why not simplify this and send the values from all 5 sensors together every 5s?

I was trying to get some separation between the data blocks which obviously didn't work.
I'll simplify the Code as every 5 or so seconds is more than adequate.

Thaks for the Input!

Check out the Blink Without Delay example in the IDE (File->examples->02.digital->Blink Without Delay) to learn how you typically keep track of elapsed time so you can do things every X seconds rather than hoping you hit millis() at the exact right time.

1 Like

so I've changed the code to this:

if (time            % 5000 == 0) {                                

        float h1 = dht1.readHumidity();                               

        float t1 = dht1.readTemperature();

        Serial.print("Office:");

        Serial.print (h1);

        Serial.print(",");

        Serial.print (t1);

        float h2 = dht2.readHumidity();

        float t2 = dht2.readTemperature();

        Serial.print("Server:");

        Serial.print (h2);

        Serial.print(",");

        Serial.print (t2);

        Serial.print(",");

        sensorValue             = analogRead      (MQ2pin);  

        Serial.print(sensorValue);

    }

}

making the output look way better, thanks a lot.

I will spend more time on the Tutorials and examples once this semester project is done but unfortunately I'm just running out of time fast.

if (time            % 5000 == 0)

That is the unreliable thing that I and @blh64 mentioned. Reading sensors can be slow, and could cause your sketch to miss or skip the exact millisecond that is a multiple of 5000. Follow advice from @blh64 for a reliable way to do the same thing.

Also, while having the words "Office:" and "Server:" appear on the serial monitor is helpful for us humans, it is unnecessary for your python script and will make parsing the text from the Arduino more difficult. Just separate each value with a comma and keep the values in the same sequence so that your python script knows which value is which by its position in the sequence.

1 Like

And throw in a newline at the end of the sequence so you can detect the beginning of a new sequence.

...
Serial.print(sensorValue);
Serial.print("\n");
// or
Serial.println(sensorValue);
...
1 Like

I'll try to implement the more reliable timing method, thanks.

That bit where it says "Server" and "Office" will be marked as a comment it's just in place now so we can make sense of it right now while testing.

I'll use that newline method then, thank you

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.