JNie257
Posts: 4
Joined: Tue Oct 03, 2017 9:46 pm

Re: DHT11 readings onto a web page!

Thu Oct 05, 2017 7:43 pm

I have two things in crontab... readTemp.py every 5 minutes and something like "@reboot pigpiod". for some reason pigpiod would not automatically start even after setting up the service so i put that in there and now readTemp.py script runs without complaining about pigpiod not being started. refreshing my browser on "RASPBERRY IP"/webdata/CurrentData.txt, I see that there is indeed changes every 5 minutes.

User avatar
DougieLawson
Posts: 33787
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: DHT11 readings onto a web page!

Fri Oct 06, 2017 6:07 am

Don't put pigpiod in any crontab.

If you need pigpiod to always start at boot
sudo systemctl enable pigpiod.service

If you want to start it now
sudo systemctl start pigpiod.service
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

devtapra
Posts: 1
Joined: Thu Sep 28, 2017 11:51 pm

Re: DHT11 readings onto a web page + an led

Wed Oct 18, 2017 8:29 am

danjperron wrote:
Fri Jan 16, 2015 4:41 am
There is a small differences between the DHT11 and the DHT22

The DHT11 is way less precise and don't go below 0 Celsius.

The data stream output of the DHT22 is 16 bit big endian with the bit 15 for negative indicator.
The DHT11 is 16 bit little endian and both MSB byte are set to zero.

This is a problem when you decode the data.
On the DHT22 the formula is ( (MSB * 256) + LSB) /10
On the DHT11 it is simply the LSB data which is in a swapped position from the DHT22

To have Joan code behave correctly , I just modify it by adding a variable to check if the sensor is a DHT11.

Then If you want to use Joan method with PIGPIO use this step by step method,

1 - update

Code: Select all

sudo apt-get update
2 - Get pigpio

Code: Select all

cd ~
wget abyz.co.uk/rpi/pigpio/pigpio.zip
unzip pigpio.zip
cd PIGPIO
make
sudo make install
3 - Create pigpio service. This way we don’t need to start it all the times


let’s load my pigpio script version

Code: Select all

cd /etc/init.d
sudo wget -O pigpiod \
https://googledrive.com/host/0B9H9nJFy3HJcWVhsY2xUel9uUk0
sudo chmod 755 /etc/init.d/pigpiod
sudo update-rc.d pigpiod defaults 9
complete -W "$(ls /etc/init.d/)" service
sudo service pigpiod start
ref: http://blog.scphillips.com/2013/07/gett ... e-on-boot/

4 - Let’s download my modified version of Joan’s DHT22 code

Code: Select all

cd ~
wget https://www.dropbox.com/s/326si04qiheqfw1/DHT22.py


Be sure that you are using GPIO22 (pin 15) otherwise you will have to change the pin information in the code.


if everything is ok just type

Code: Select all

sudo python DHT22.py
and it should returns

Code: Select all

[email protected] ~ $ sudo python DHT22.py
1 33 23 0.19 0 0 0 0
2 34 23 0.19 0 0 0 0
3 34 23 0.19 0 0 0 0
Temperature 23 Celsius humidity 34%

I will continue tomorrow with the script to store into rrdtool

Daniel
i get

1 -999 -999 -999.00 0 0 0 0
2 -999 -999 -999.00 0 0 0 0
3 -999 -999 -999.00 0 0 0 0
4 -999 -999 -999.00 0 0 0 0
5 -999 -999 -999.00 0 0 0 0
6 -999 -999 -999.00 0 0 0 0
7 -999 -999 -999.00 0 0 0 0
8 -999 -999 -999.00 0 0 0 0

can you help me ?

danjperron
Posts: 3031
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: DHT11 readings onto a web page!

Wed Oct 18, 2017 1:45 pm

can you help me
@devtapra Yes I do have a big crystal ball but I only see some strutures magnification of the table.

Well If you need help you need to give more hints.

1- How is your sensor connected. Which pins? Do you add the 4k7 pull-up resistor ?
2- I hope you connect 3.3V on it and not 5V!
3- Is it a DHT22 or a DHT11 ?
4- Did you check if pigpio is working. Did you try to toggle some GPIO with pigpio and check if it toggles?
5- Also what kind of Raspberry Pi do you have ?

The DHT22.py script has the DHT11=True. if you are using a DHT22 it should be set to False. (line 265). And the GPIO pin is set to GPIO22.

A picture of your setting will help. We need to see the wires connection from the sensor to the GPIO connector of the Raspberry Pi.

Daniel

danjperron
Posts: 3031
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: DHT11 readings onto a web page!

Wed Oct 18, 2017 1:59 pm

@devtapra the step 3 is different with systemd

sudo systemctl enable pigpiod
sudo systemctl start pigpiod


B.T.W. Readings with -99 -999 -999.00 means that the sensor is not detected at all.

jzxpacky
Posts: 16
Joined: Tue Dec 15, 2015 1:47 pm

Re: DHT11 readings onto a web page!

Tue Dec 12, 2017 12:51 am

Howdy guys, me again :roll:

So I've moved back Australia and have modified my setup to suit, but am now having issues where the Sunrise and Sunset plot bands are around the wrong way. I'm not sure if it has something to do with moving from the northern hemisphere to the southern but for some reason I can't get it to work properly.

I've adjusted my latitude and longitude and also the offset from UTC in the JS function which is giving me the correct times, just plotting yellow bands when the sun has set..

Any thing else I should look at?

danjperron
Posts: 3031
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: DHT11 readings onto a web page!

Tue Dec 12, 2017 12:53 am

What about the computer clock ?

jzxpacky
Posts: 16
Joined: Tue Dec 15, 2015 1:47 pm

Re: DHT11 readings onto a web page!

Tue Dec 12, 2017 12:55 am

Changed the timezone in the Pi so it reflects the current. The weird part is that the times are correct, just plotting yellow where it should be white and vice versa.

EDIT: Here's the function.

Code: Select all

function computeSunrise(day, sunrise) {

    /*Sunrise/Sunset Algorithm taken from
     http://williams.best.vwh.net/sunrise_sunset_algorithm.htm
     inputs:
     day = day of the year
     sunrise = true for sunrise, false for sunset
     output:
     time of sunrise/sunset in hours */

    //lat, lon for Gold Coast, Qld, Australia
    var longitude = 153.423471;
    var latitude = -27.990119;
    var zenith = 90.83333333333333;
    var D2R = Math.PI / 180;
    var R2D = 180 / Math.PI;

    // convert the longitude to hour value and calculate an approximate time
    var lnHour = longitude / 15;
    var t;
    if (sunrise) {
        t = day + ((6 - lnHour) / 24);
    } else {
        t = day + ((18 - lnHour) / 24);
    }
    //calculate the Sun's mean anomaly
    var M = (0.9856 * t) - 3.289;

    //calculate the Sun's true longitude
    var L = M + (1.916 * Math.sin(M * D2R)) + (0.020 * Math.sin(2 * M * D2R)) + 282.634;
    if (L > 360) {
        L = L - 360;
    } else if (L < 0) {
        L = L + 360;
    }
    //calculate the Sun's right ascension
    var RA = R2D * Math.atan(0.91764 * Math.tan(L * D2R));
    if (RA > 360) {
        RA = RA - 360;
    } else if (RA < 0) {
        RA = RA + 360;
    }
    //right ascension value needs to be in the same qua
    var Lquadrant = (Math.floor(L / (90))) * 90;
    var RAquadrant = (Math.floor(RA / 90)) * 90;
    RA = RA + (Lquadrant - RAquadrant);

    //right ascension value needs to be converted into hours
    RA = RA / 15;

    //calculate the Sun's declination
    var sinDec = 0.39782 * Math.sin(L * D2R);
    var cosDec = Math.cos(Math.asin(sinDec));

    //calculate the Sun's local hour angle
    var cosH = (Math.cos(zenith * D2R) - (sinDec * Math.sin(latitude * D2R))) / (cosDec * Math.cos(latitude * D2R));
    var H;
    if (sunrise) {
        H = 360 - R2D * Math.acos(cosH)
    } else {
        H = R2D * Math.acos(cosH)
    }
    H = H / 15;

    //calculate local mean time of rising/setting
    var T = H + RA - (0.06571 * t) - 6.622;

    //adjust back to UTC
    var UT = T - lnHour;
    if (UT > 24) {
        UT = UT - 24;
    } else if (UT < 0) {
        UT = UT + 24;
    }

    //convert UT value to local time zone of latitude/longitude
    var localT = UT + 10;

    //convert to Milliseconds
    return localT * 3600 * 1000;
}

danjperron
Posts: 3031
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: DHT11 readings onto a web page!

Tue Dec 12, 2017 1:54 am

I think that I figure it out.

The computeSunrise return the UTC time in millisecond. The quick fix will be to set the longitude to 0. then the function will return the local time. That should work. The error will be so small that you won't notice it.

The other approach will be to modify the data table to use UTC time instead of local time on the chart x axe. or to calculate the sunrise/sunset by converting the time in local by adding the UTC offset time at your longitude from the return value.

jzxpacky
Posts: 16
Joined: Tue Dec 15, 2015 1:47 pm

Re: DHT11 readings onto a web page!

Tue Dec 12, 2017 2:14 am

Ok so I tried the first option and it kind of works, although I have to set the option useUTC to true in my config.

Here's the full JS file with the yellow plot bands around the wrong way.

Code: Select all

Highcharts.setOptions({
    global: {
        useUTC: false
    }
});

options = {
    chart: {
        renderTo: 'content',
        type: 'spline'
    },
    title: {
        text: 'Temperatures of the last 24h'
    },
    subtitle: {
        text: ''
    },
    colors: ['#4572A7', '#AA4643', '#89A54E', '#80699B', '#3D96AE', '#DB843D', '#92A8CD', '#A47D7C', '#B5CA92'],
        xAxis: {
            type: 'datetime',
            dateTimeLabelFormats: {
            hour: '%H. %M',
        }
        },

    yAxis: {
        title: {
        text: 'Temperature'
        },
        labels: {
            formatter: function () {
                return this.value + '°';
            }
        }
    },

    tooltip: {
            crosshairs: true,
            shared: true,
            valueDecimals: 1,
            useHTML: true,
            headerFormat: '<small>{point.key}</small><table>',
            pointFormat: '<tr><td style="color: {series.color}">{series.name}: </td>' + '<td style="text-align: right"><b>{point.y}</b></td></tr>',
            footerFormat: '</table>',
            valueSuffix: ' °C',
    },

    plotOptions: {
            spline: {
            marker: {
                radius: 0,
                lineColor: '#666666',
                lineWidth: 1
            }
        }
    },
        legend: {
            layout: 'vertical',
            align: 'left',
            x: 80,
            verticalAlign: 'top',
            y: 55,
            floating: true,
            backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'
    },
    lineWidth: 1,

    name: GetSerieName($(this).text()),

    series: []
}

function computeSunrise(day, sunrise) {

    /*Sunrise/Sunset Algorithm taken from
     http://williams.best.vwh.net/sunrise_sunset_algorithm.htm
     inputs:
     day = day of the year
     sunrise = true for sunrise, false for sunset
     output:
     time of sunrise/sunset in hours */

    //lat, lon for Gold Coast, Qld, Australia
    var longitude = 153.423471;
//    var longitude = 0;
    var latitude = -27.990119;
    var zenith = 90.83333333333333;
    var D2R = Math.PI / 180;
    var R2D = 180 / Math.PI;

    // convert the longitude to hour value and calculate an approximate time
    var lnHour = longitude / 15;
    var t;
    if (sunrise) {
        t = day + ((6 - lnHour) / 24);
    } else {
        t = day + ((18 - lnHour) / 24);
    }
    //calculate the Sun's mean anomaly
    var M = (0.9856 * t) - 3.289;

    //calculate the Sun's true longitude
    var L = M + (1.916 * Math.sin(M * D2R)) + (0.020 * Math.sin(2 * M * D2R)) + 282.634;
    if (L > 360) {
        L = L - 360;
    } else if (L < 0) {
        L = L + 360;
    }
    //calculate the Sun's right ascension
    var RA = R2D * Math.atan(0.91764 * Math.tan(L * D2R));
    if (RA > 360) {
        RA = RA - 360;
    } else if (RA < 0) {
        RA = RA + 360;
    }
    //right ascension value needs to be in the same qua
    var Lquadrant = (Math.floor(L / (90))) * 90;
    var RAquadrant = (Math.floor(RA / 90)) * 90;
    RA = RA + (Lquadrant - RAquadrant);

    //right ascension value needs to be converted into hours
    RA = RA / 15;

    //calculate the Sun's declination
    var sinDec = 0.39782 * Math.sin(L * D2R);
    var cosDec = Math.cos(Math.asin(sinDec));

    //calculate the Sun's local hour angle
    var cosH = (Math.cos(zenith * D2R) - (sinDec * Math.sin(latitude * D2R))) / (cosDec * Math.cos(latitude * D2R));
    var H;
    if (sunrise) {
        H = 360 - R2D * Math.acos(cosH)
    } else {
        H = R2D * Math.acos(cosH)
    }
    H = H / 15;

    //calculate local mean time of rising/setting
    var T = H + RA - (0.06571 * t) - 6.622;

    //adjust back to UTC
    var UT = T - lnHour;
    if (UT > 24) {
        UT = UT - 24;
    } else if (UT < 0) {
        UT = UT + 24;
    }

    //convert UT value to local time zone of latitude/longitude
    var localT = UT + 10;

    //convert to Milliseconds
    return localT * 3600 * 1000;
}

function dayOfYear() {
    var yearFirstDay = Math.floor(new Date().setFullYear(new Date().getFullYear(), 0, 1) / 86400000);
    var today = Math.ceil((new Date().getTime()) / 86400000);
    return today - yearFirstDay;

I'm just baffled as to why this doesn't work. It would help if the original authors site of the function was around.

danjperron
Posts: 3031
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: DHT11 readings onto a web page!

Tue Dec 12, 2017 2:44 am

Ok the bug is here in the function

//convert UT value to local time zone of latitude/longitude
// was -> localT = UT + 1; // this is the UTC offset at Berlin


localT = UT + UTC_offset
if(localT >24)
{
localT = localT - 24;
}


Don't forget to add the constant UTC_offset in the beginning of the function.
Brisbane is 10 hours.

var UTC_offset = 10.0 ;

Now it should work


I compare the result with the sunwait application using your timezone and I got the same values.

jzxpacky
Posts: 16
Joined: Tue Dec 15, 2015 1:47 pm

Re: DHT11 readings onto a web page!

Tue Dec 12, 2017 3:03 am

Perfect! That worked just right!

I really need to learn more about this hehe.

Thanks again for your help Dan!

chzurich
Posts: 1
Joined: Tue Jan 02, 2018 6:37 pm

Re: DHT11 readings onto a web page!

Tue Jan 02, 2018 6:50 pm

Hi Dan,

I've tried to implement this cool project, and thanks to your excellent and detailed instructions got things working up to the point where I was accessing temperature.html on my Pi (2B) running Stretch. I faced the same issue as JNie257 above with not having access to the Charts directory, and solved this by linking Charts in the /var/www/html directory. It appears that Raspbian Stretch uses /var/www/html/ as the home directory for Apache, instead of /var/www/ on previous versions.

With that said, I currently still face the issue of empty charts! The data in the XML files is updated, and I can see the latest reading in CurrentData.txt, but the data is not plotted (there is just an empty x,y-axis with legend items (CPU, temp, humidity) that I can toggle).

I suspect this has something to do with the new version of the OS, and possibly with permissions of directories. I'd greatly appreciate any inputs you might have on this, and more specifically:
1) Do you think rolling back to Jessie would help solve things, and is there anything I should keep in mind (such as not updating certain packages) if I do this? What OS do you have on your system?
2) For the sake of comparison, I'd be very curious to see one of your data files, e.g. temperature1h.xml, if you'd have the chance to post that. :)

danjperron
Posts: 3031
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: DHT11 readings onto a web page!

Wed Jan 03, 2018 8:07 am

I will check my step by step using stretch when I return from my vacation.

I think that's an access problem.

danjperron
Posts: 3031
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: DHT11 readings onto a web page!

Tue Jan 09, 2018 2:32 pm

Ok I found why the rrdtool didn't work!

The new release of rrdtool needs a switch to send the time data.

In my python script readTemp.py , function rrdExport(), line 125 please add the "-t" switch.

Code: Select all

 texte = "rrdtool xport -t -s {0} -e now --step {1} ".format(start, step)
Wait 5 min and the data point should be on the chart!


Please note that pigpiod is in the distribution. No need to install but you need to start it!
The web browser apache2 html root page is /var/www/html instead of /var/www.
Set the webdata and the highcharts folder into /var/www/html.

Also don't forget to change your latitude and longitude inside temperature.html.

Bee78000
Posts: 8
Joined: Sun Mar 22, 2015 7:12 pm

Re: DHT11 readings onto a web page!

Sat Jan 20, 2018 4:18 pm

Hello Daniel,
I wrote you a few years ago because I used your very nice project with Rrdtool and Highcharts for a couple of years , but after a crash I needed to recreate everything from scratch because BackUp didn't work.. Now almost everything is working, except I have got no lines in the Highchart
I searched for days, and cannot find my mistake. I am not able to find why data are not displayed the graph is empty. RRDTOOL is working and generating XML files but it seems they are not used ...

Have you got any idea?

Thanks a lot,
sT2PHANE

danjperron
Posts: 3031
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: DHT11 readings onto a web page!

Sat Jan 20, 2018 10:41 pm

Hi sT2PHANE,

rrdtool change since then! You need to add the -t argument to export the time.

Just check the post just previous of yours!

Daniel

Bee78000
Posts: 8
Joined: Sun Mar 22, 2015 7:12 pm

Re: DHT11 readings onto a web page!

Sun Jan 21, 2018 10:07 pm

Bonsoir Daniel,
Merci, MERCI, merci, :D
You were right, it was just this new parameter -t which was missing in the rrdool line!
I just add it in the line, and all dots appeared like a miracle
texte = "rrdtool xport -t -s {0} -e now --step {1} ".format(debut, step)

I can now continue my project, and will take more time to test my backup!

Thank you very much!
Stéphane

danjperron
Posts: 3031
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: DHT11 readings onto a web page!

Mon Jan 22, 2018 12:42 am

Il n'y a pas de problème Stéphane! Je suis du Québec alors je parle très bien francais!

Bonne continuation dans ton projet!

Don't forget to do your backup!

Daniel

Bee78000
Posts: 8
Joined: Sun Mar 22, 2015 7:12 pm

Re: DHT11 readings onto a web page!

Mon Jan 22, 2018 9:41 pm

Daniel,
En effet ce sera plus simple en français ;-)
Mon back up a été fait hier soir par WIN32DISK Imager, mais je vais doubler la piste par DD, j'ai vu des posts à ce sujet

Dans cette nouvelle partie de projet, je souhaite envoyer des petites photos par MMS.
Je maîtrise maintenant l'envoi par SMS, mais je butte sur l'envoi par MMS.
Aurais-tu quelquechose à ce sujet(ou des liens intéressants), car les posts ne sont pas trop nombreux et pas trop clairs, mais je vais pouvoir m' yconsacrer maintenant que tout remarche...
Merci pour ton document de synthèse qui m'a permis de tout reconstruire

Stéphane

danjperron
Posts: 3031
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: DHT11 readings onto a web page!

Tue Jan 23, 2018 1:31 pm

Bonjour Stéphane,

Et non je n'y connais rien sur les réseaux cellulaires. C'est très dispendieux au Canada. C'est un grand pays avec seulement 30 millions de population.


Maybe somebody knows more about sending pictures or video using MMS.

Bonne chance,

Daniel

akwing12
Posts: 3
Joined: Sat Feb 17, 2018 2:24 am

Re: DHT11 readings onto a web page!

Sat Feb 17, 2018 2:41 am

Dan, I am having a small issue.

I have gotten everything working and I added the -t to get the values to appear on the webpage, but it would appear that something is keeping the majority of my readings from being written into the database. I am using 3 ds18b20 sensors and can get some data, but mostly nan gets put into the database each time readTemp.py runs.

Here is the database

Code: Select all

[email protected]:~ $ rrdtool fetch data_DS18B20.rrd LAST
                           TDS1                TDS2                TDS3
1518822900: nan nan nan
1518823200: nan nan nan
1518823500: nan nan nan
1518823800: nan nan nan
1518824100: nan nan nan
1518824400: nan nan nan
1518824700: nan nan nan
1518825000: nan nan nan
1518825300: 3.2061181861e+01 4.5497778460e+01 7.3369619839e+01
1518825600: 3.2061181861e+01 4.5497778460e+01 7.3369619839e+01
1518825900: 3.1643500844e+01 4.3921327692e+01 7.3575419924e+01
1518826200: 3.1643500844e+01 4.3921327692e+01 7.3575419924e+01
1518826500: 3.1545001269e+01 4.2242916051e+01 7.3706673830e+01
1518826800: 3.2448316884e+01 4.1336600000e+01 7.3625000000e+01
1518827100: nan nan nan
1518827400: nan nan nan
1518827700: nan nan nan
1518828000: nan nan nan
1518828300: 3.1771387266e+01 4.4934501366e+01 7.2915205254e+01
1518828600: 3.1771387266e+01 4.4934501366e+01 7.2915205254e+01
1518828900: 4.5777247261e+01 4.3855330256e+01 7.3384958549e+01
1518829200: 4.5777247261e+01 4.3855330256e+01 7.3384958549e+01
1518829500: nan nan nan
1518829800: nan nan nan
1518830100: nan nan nan
1518830400: nan nan nan
1518830700: nan nan nan
1518831000: nan nan nan
1518831300: nan nan nan
1518831600: nan nan nan
1518831900: nan nan nan
1518832200: nan nan nan
1518832500: nan nan nan
1518832800: nan nan nan
1518833100: nan nan nan
1518833400: nan nan nan
1518833700: nan nan nan
1518834000: 1.9775491246e+00 1.1632283243e+01 3.2310814358e+01
1518834300: 1.9775491246e+00 1.1632283243e+01 3.2310814358e+01
1518834600: 1.9775491246e+00 1.1632283243e+01 3.2310814358e+01
1518834900: -4.4978812715e+00 5.4877931381e+00 2.3107978895e+01
1518835200: nan nan nan
1518835500: nan nan nan
Here is my cron for root

Code: Select all

# m h  dom mon dow   command
@reboot              /usr/local/bin/pigpiod

*/5 * * * * /home/pi/readTemp.py

I did add to the ds18b20.py file to change the Celsius it reads to Fahrenheit, and when I commented that line out, it seemed to run a little better but still missed some entries. Do you have any suggestions?

danjperron
Posts: 3031
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: DHT11 readings onto a web page!

Sat Feb 17, 2018 1:23 pm

The "nan" is because the data is not numeric. Maybe something is wrong with your wiring or your Fahrenheit conversion.

How long is your total wire length?
What is your pull-up resistor?
Is your sensor at 3.3V or 5V. 5V help ( be sure that your pull-up is on the 3.3V)

akwing12
Posts: 3
Joined: Sat Feb 17, 2018 2:24 am

Re: DHT11 readings onto a web page!

Sat Feb 17, 2018 10:27 pm

This is all I did to change to Fahrenheit, not sure that the problem is there.

Code: Select all

    #ok Valid temperature
    line2 = text1.split("\n")[1]
    text2 = line2.split(" ")[9]
    TempValue = (float(text2[2:])/1000.0)
    TempValueF = (TempValue*9/5+32)
    if TempValue < (-55.0):
      return None
    if TempValue > 125.0:
      return None
    return TempValueF

My longest cable run is maybe 15 feet, the other two sensors are just the 3 foot cables they came with when i ordered them.
I used a 4.7k resistor.
My sensors are on 3.3. I guess I could try 5.5 and keep the resistor on the 3.3.

When I manually run readTemp.py it gives me results, however.

Code: Select all

[email protected]:~ $ python  readTemp.py
TDS1:30.65
TDS2:43.5866
TDS3:67.8866
So I am thinking everything is wired, but there is an issue somewhere in the code. I did not edit any other files or scripts, however. Just DS18B20.py. I have posted the contents below so you can tell me if there is something else wrong that you can see.

Code: Select all

#!/usr/bin/env python


"""
   Read DS18B20 sensor from Raspberry PI GPIO4 using 1-wire
"""

class DS18B20:

  def read(self,sensorId):
    if sensorId == None:
      return None
    retry=0
    while True:
      try:
        fileH = open( "/sys/bus/w1/devices/" + sensorId + "/w1_slave")
        text1 = fileH.read()
        fileH.close()
        line1 = text1.split("\n")[0]
        crc   = line1.split("crc=")[1]
        if crc.find("YES")>=0:
          break;
      except:
        #ok error, loop it
        pass

      retry = retry + 1
      if retry >= 5 :
        return None

    #ok Valid temperature
    line2 = text1.split("\n")[1]
    text2 = line2.split(" ")[9]
    TempValue = (float(text2[2:])/1000.0)
    TempValueF = (TempValue*9/5+32)
    if TempValue < (-55.0):
      return None
    if TempValue > 125.0:
      return None
    return TempValueF
    #stand alone  part to check if it is running
#this will output the configuration table with the current sensor
if __name__ == "__main__":

  import DS18B20Config

  #get the config object
  config = DS18B20Config.DS18B20Config()

  #create the DS18B20 object
  ds = DS18B20()

  #get the table

  sensorInfo= config.read()

  if sensorInfo == None:
    print("Unable to get sensor information in file 'DS18B20.conf'.")
    quit()

  numberOfSensor= len(sensorInfo)

  #Now let's print what we have



  for sensor in sensorInfo:
    value = ds.read(sensor[1])
    if value == None:
   print("{:20s}:???".format(sensor[2]))
    else:
      print("{:20s}:{}".format(sensor[2],value))

pascale
Posts: 14
Joined: Mon Feb 05, 2018 11:04 am

Re: DHT11 readings onto a web page!

Sun Apr 29, 2018 6:57 pm

Hi I have read through the last few pages of this post, but I cannot find the answer to my problem, I want to post the temperature and humidity to a web page with a simple php file.
I am using a pi zero w 4gb disc stretch lite php7 apache2 mariadb, I have one DHT11 but would like to use two, I have found this code https://www.instructables.com/id/DHT11-on-Raspberry-PI/ the code is

Code: Select all

<?php
$test="";
<?php
//test_dht11.php
// executes python file to read DHT11 temperature sensor
//and extracts the temperature and humidity values

$temperature=0;
$humidity=0;
$my_pos=0;
$exec_msg="sudo /var/www/AdafruitDHT.py 11 22 2>&1";
$test = shell_exec($exec_msg);

//extracts temperature
$my_pos = strpos($test, "Temp=",0);
$temperature = substr($test, $my_pos+5, 4);
echo "\n ".$temperature;

//extracts humidity
$my_pos = strpos($test, "Humidity=",$my_pos);
$humidity = substr($test, $my_pos+9, 4);
echo "\n ".$humidity;
?>
When I run "sudo php test_dht11.php" I get an error " expected >line 5, When I add a ">" then it works in the command line But in my browser says
"RUST YOU"
I am trying to incorporate this into another instructables tutorial https://github.com/jonsag/pi-heating
This uses 1-Wire Digital Thermometers ( DS18B20 ) which I am using, But for one part of my plane I need to know the humidity to trigger a relay to activate a two speed fan.
Other wise I need help to incorporate some code into this php file to trigger the AdafruitDHT.py file and display the information here

Code: Select all

<?php

$SENSOR_ID = isset($_GET['id']) ? $_GET['id'] : '0';

if ( $SENSOR_ID < 1 ) {

    print_r ( '' );

  }
    else
  {

    $w1dir = "/sys/bus/w1/devices/";
    
    $sensorfile = '/home/pi/pi-heating-remote/configs/sensors';

    $sensors = file($sensorfile, FILE_SKIP_EMPTY_LINES);

    $sensor = explode( " = ", $sensors[$SENSOR_ID-1] );

    $ref=$sensor[0];
    //$name=$sensor[1];
  
    do {
      $raw_data = file_get_contents($w1dir.$ref.'/w1_slave');
      $success = substr( explode( "\n", $raw_data )[0], -3, 3 );
    } while ( $success !="YES" ); 
 
    $value = (float)(explode( "=", explode( " ", explode( "\n", $raw_data )[1] )[9] )[1])/1000.0;

    echo number_format( $value, 1 );

  }

?>

<?php
I hope I am not asking the impossible!!!

Kind regards
Paul

Return to “Python”

Who is online

Users browsing this forum: No registered users and 14 guests