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: 30935
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

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

devtapra
Posts: 4
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

pi@raspberrypi ~ $ 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: 2699
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: 2699
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: 2699
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: 2699
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: 2699
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: 2699
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: 2699
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.

Return to “Python”

Who is online

Users browsing this forum: No registered users and 8 guests