728x90

NanoPi M1 Plus - DHT11 control with pi4j

출처 : Java Native Interface (JNI) - Java Programming Tutorial
Use arrays (JNI) - Real's Java How-to - Rgagnon
android - How to return an array from JNI to Java? - Stack Overflow

NanoPi M1 Plus에서 pi4j로만으로 DHT11센서의 값을 읽을 수 없어 pi4j의 native 코드를 추가해서 온도 값을 가지고 오도록 수정하였습니다. 첨부한 jar을 적용해서 사용하세요.

pi4j-core.jar

핀배열

 DHT11  BCM  GPIO  Name  Pin
 VCC      3.3v  1
 GND  0v  9
 Sensor  3  22  CTS2  15

dht11.h

int* read_dht11_dat(int pin);

dht11.c

/*
 *  dht11.c:
 *      Simple test program to test the wiringPi functions
 *      DHT11 test
 */
  
#include <wiringPi.h>
  
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <dht11.h>

#define MAXTIMINGS      85
#define DHTPIN    3
int dht11_dat[5] = { 0, 0, 0, 0, 0 };
  
int* read_dht11_dat(int pin) {
    uint8_t laststate       = HIGH;
    uint8_t counter  = 0;
    uint8_t j          = 0, i;
    float   f; /* fahrenheit */
  
    dht11_dat[0] = dht11_dat[1] = dht11_dat[2] = dht11_dat[3] = dht11_dat[4] = 0;
  
    /* pull pin down for 18 milliseconds */
    pinMode( pin, OUTPUT );
    digitalWrite( pin, LOW );
    delay( 18 );
    /* then pull it up for 40 microseconds */
    digitalWrite( pin, HIGH );
    delayMicroseconds( 40 );
    /* prepare to read the pin */
    pinMode( pin, INPUT );
  
    /* detect change and read data */
    for ( i = 0; i < MAXTIMINGS; i++ ) {
        counter = 0;
        while ( digitalRead( pin ) == laststate ) {
            counter++;
            delayMicroseconds( 1 );
            if ( counter == 255 ) {
                break;
            }
        }
        laststate = digitalRead( pin );
  
        if ( counter == 255 )
            break;

// printf("i = %d, j = %d\n", i, j);
  
        /* ignore first 3 transitions */
        if ( (i >= 4) && (i % 2 == 0) ) {
            /* shove each bit into the storage bytes */
            dht11_dat[j / 8] <<= 1;
            if ( counter > 16 )
                dht11_dat[j / 8] |= 1;
            j++;
        }
    }
  
    /*
     * check we read 40 bits (8bit x 5 ) + verify checksum in the last byte
     * print it out if data is good
     */
    if ( (j >= 40) && (dht11_dat[4] == ( (dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3]) & 0xFF) ) ) {
        f = dht11_dat[2] * 9. / 5. + 32;
        //printf( "Humidity = %d.%d %% Temperature = %d.%d *C (%.1f *F)\n",
        //    dht11_dat[0], dht11_dat[1], dht11_dat[2], dht11_dat[3], f );
    } else {
        //printf( "Data not good, skip\n" );
    }
    
    return dht11_dat;    
}

com_pi4j_wiringpi_Gpio.h 내용 추가

/*
 * Class:     com_pi4j_wiringpi_Gpio
 * Method:    readDHT11Data
 * Signature: (II)V
 */
JNIEXPORT jintArray JNICALL Java_com_pi4j_wiringpi_Gpio_readDHT11Data
  (JNIEnv *, jclass, jint);

com_pi4j_wiringpi_Gpio.c 내용 추가

#include <dht11.h>

/*
 * Class:     com_pi4j_wiringpi_Gpio
 * Method:    readDHT11Data
 * Signature: (II)V
 */
JNIEXPORT jintArray JNICALL Java_com_pi4j_wiringpi_Gpio_readDHT11Data
  (JNIEnv *env, jclass obj, jint pin)
{
    // return (jintArray) read_dht11_dat(pin);
    int size = 5;
    jintArray result;
    result = (*env)->NewIntArray(env, size);
    if (result == NULL) {
    	return NULL; /* out of memory error thrown */
    }
    
    int *a1 = read_dht11_dat(pin);
    
    int i;
    // fill a temp structure to use to populate the java int array
    jint fill[size];
    for (i = 0; i < size; i++) {
    	 fill[i] = a1[i]; // put whatever logic you want to populate the values here.
    }
    // move from the temp structure to the java structure
    (*env)->SetIntArrayRegion(env, result, 0, size, fill);
    return result;
}

Gpio 클래스 함수 추가

package com.pi4j.wiringpi;

public class Gpio {
    
    public static native int[] readDHT11Data(int pin);
}

DHT11_native.java

import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.NanoPiM1PlusGpioProvider;
import com.pi4j.wiringpi.Gpio;
 
public class DHT11_native {
    private static final int MAXTIMINGS = 85;
    private int[] dht11_dat = { 0, 0, 0, 0, 0 };
    
    private final static int DHTPIN = 3; // 3;
 
    public float getTemperature() {
    	float f = 0;
    	dht11_dat = Gpio.readDHT11Data(DHTPIN);
    	
       if ( (dht11_dat[4] == ( (dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3]) & 0xFF) ) ) {
      	 f = (float) (dht11_dat[2] * 9. / 5. + 32);
         System.out.println(String.format( "Humidity = %d.%d %% Temperature = %d.%d *C (%.1f *F)" , dht11_dat[0], dht11_dat[1], dht11_dat[2], dht11_dat[3], f) );
     } else {
    	 System.out.println( "Data not good, skip" );
     }
       
        return f;
 
    }
 
    private boolean checkParity() {
      return (dht11_dat[4] == ((dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3]) & 0xFF));
    }
 
    public static void main (String ars[]) throws Exception {
    	
    	GpioFactory.setDefaultProvider(new NanoPiM1PlusGpioProvider());
 
    	DHT11_native dht = new DHT11_native();
 
        for (int i=0; i<10; i++) {
           Thread.sleep(1000);
           dht.getTemperature();
        }
 
        System.out.println("Done!!");
 
    }
}

실행

# java -cp .:pi4j-core.jar DHT11_native
Data not good, skip
Humidity = 31.0 % Temperature = 23.0 *C (73.4 *F)
Humidity = 30.0 % Temperature = 23.0 *C (73.4 *F)
Humidity = 30.0 % Temperature = 23.0 *C (73.4 *F)

실행결과

728x90
728x90

NanoPi M1 Plus - DHT11 온도 습도 측정

출처 : DHT11 Humidity & Temperature Sensor Module | UUGear

C 언어

/*
 *  dht11.c:
 *      Simple test program to test the wiringPi functions
 *      DHT11 test
 */
 
#include <wiringPi.h>
 
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define MAXTIMINGS      85
#define DHTPIN	  7
int dht11_dat[5] = { 0, 0, 0, 0, 0 };
 
void read_dht11_dat() {
	uint8_t laststate       = HIGH;
	uint8_t counter	 = 0;
	uint8_t j	       = 0, i;
	float   f; /* fahrenheit */
 
	dht11_dat[0] = dht11_dat[1] = dht11_dat[2] = dht11_dat[3] = dht11_dat[4] = 0;
 
	/* pull pin down for 18 milliseconds */
	pinMode( DHTPIN, OUTPUT );
	digitalWrite( DHTPIN, LOW );
	delay( 18 );
	/* then pull it up for 40 microseconds */
	digitalWrite( DHTPIN, HIGH );
	delayMicroseconds( 40 );
	/* prepare to read the pin */
	pinMode( DHTPIN, INPUT );
 
	/* detect change and read data */
	for ( i = 0; i < MAXTIMINGS; i++ ) {
		counter = 0;
		while ( digitalRead( DHTPIN ) == laststate ) {
			counter++;
			delayMicroseconds( 1 );
			if ( counter == 255 ) {
				break;
			}
		}
		laststate = digitalRead( DHTPIN );
 
		if ( counter == 255 )
			break;
 
		/* ignore first 3 transitions */
		if ( (i >= 4) && (i % 2 == 0) ) {
			/* shove each bit into the storage bytes */
			dht11_dat[j / 8] <<= 1;
			if ( counter > 16 )
				dht11_dat[j / 8] |= 1;
			j++;
		}
	}
 
	/*
	 * check we read 40 bits (8bit x 5 ) + verify checksum in the last byte
	 * print it out if data is good
	 */
	if ( (j >= 40) && (dht11_dat[4] == ( (dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3]) & 0xFF) ) ) {
		f = dht11_dat[2] * 9. / 5. + 32;
		printf( "Humidity = %d.%d %% Temperature = %d.%d *C (%.1f *F)\n",
			dht11_dat[0], dht11_dat[1], dht11_dat[2], dht11_dat[3], f );
	} else {
		printf( "Data not good, skip\n" );
	}
}
 
int main( void ) {
	printf( "Raspberry Pi wiringPi DHT11 Temperature test program\n" );
 
	if ( wiringPiSetup() == -1 )
		exit( 1 );
 
	while ( 1 ) {
		read_dht11_dat();
		delay( 1000 ); /* wait 1sec to refresh */
	}
 
	return(0);
}

컴파일

# gcc -Wall -o dht11 dht11.c -lwiringPi -lpthread

실행

디버깅 모드 설정

# export WIRINGPI_DEBUG=TRUE

디버깅 모드 해지

# unset WIRINGPI_DEBUG

실행

# ./dht11     
Raspberry Pi wiringPi DHT11 Temperature test program
Data not good, skip
Humidity = 22.0 % Temperature = 23.0 *C (73.4 *F)
Humidity = 22.0 % Temperature = 23.0 *C (73.4 *F)
Data not good, skip
Data not good, skip
Humidity = 22.0 % Temperature = 23.0 *C (73.4 *F)
728x90
728x90

출처

x윈도우 설치

$ sudo apt-get install lubuntu-desktop
$ sudo shutdown -r now

eMMC에 OS 복사하기

출처 : How to burn Linux image to eMMC
bpi-copy command

eMMC 용량 확인

$ sudo fdisk -l
Disk /dev/mmcblk0: 14.9 GiB, 15931539456 bytes, 31116288 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x36acac02
 
Device         Boot  Start      End  Sectors  Size Id Type
/dev/mmcblk0p1       49152   131071    81920   40M 83 Linux
/dev/mmcblk0p2      131072 31116287 30985216 14.8G 83 Linux
 
 
Disk /dev/mmcblk1: 7.3 GiB, 7818182656 bytes, 15269888 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x3794dafa
 
Device         Boot  Start      End  Sectors  Size Id Type
/dev/mmcblk1p1       49152   131071    81920   40M 83 Linux
/dev/mmcblk1p2      131072 15269887 15138816  7.2G 83 Linux
 
 
Disk /dev/mmcblk1boot1: 4 MiB, 4194304 bytes, 8192 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
 
 
Disk /dev/mmcblk1boot0: 4 MiB, 4194304 bytes, 8192 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

이미지 복원

이미지 복원이 완료되면 SD 메모리를 제거하고 재부팅

$ sudo dd if=nanopi-m1-plus_friendlycore-xenial_4.14.52_20180628.img of=/dev/mmcblk1 bs=10MB
389+1 records in
389+1 records out
3899999232 bytes (3.9 GB, 3.6 GiB) copied, 511.303 s, 7.6 MB/s

OpenJDK 1.8 설치

# apt-get install openjdk-8-jdk

JAVA_HOME 환경변수 추가

# vi /etc/profile

내용 추가 export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-armhf

export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-armhf

IP 확인 (eflasher 에서)

# ip addr

add-apt-repository 명령이 없는 경우

# apt-get install python-software-properties
# apt-get install software-properties-common

jre repository 주소 추가

# add-apt-repository ppa:webupd8team/java
# apt-get update

JDK 1.7 설치

# apt-get install openjdk-7-jre  openjdk-7-jdk

JDK 1.8 설치

출처 : Installing OpenJDK 8 and Tomcat 8 on Debian Jessie

# vi /etc/apt/sources.list

내용추가

# Backport Testing on stable
# JDK 8
deb http://ftp.de.debian.org/debian jessie-backports main

jre 설치

# apt install -t jessie-backports  openjdk-8-jre-headless ca-certificates-java
728x90
728x90

NanoPi M1 Plus mjpg-streamer

출처 : NanoPi NEO Air

패키지 업데이트

패키지 업데이트를 하면 /root/mjpg-streamer 가 생성됨

# apt-get update

mjpg-streamer 컴파일 및 설치

출처 : http://www.lavrsen.dk/svn/motion/tags/3.2.9/picture.c

# cd /root/mjpg-streamer
# make
# sudo make install
make: svnversion: Command not found
install --mode=755 mjpg_streamer /usr/local/bin
install --mode=644 input_uvc.so output_file.so output_udp.so output_http.so input_file.so /usr/local/lib/
install --mode=755 -d /usr/local/www
install --mode=644 -D www/* /usr/local/www

LD_LIBRARY_PATH 경로 추가

# echo "export LD_LIBRARY_PATH=\$LD_LIBRARY_PATH:/usr/local/lib:/usr/local/lib/mjpg-streamer" | sudo tee -a /etc/profile

mjpg-streamer 실행

# ./mjpg_streamer -i "./input_uvc.so -y 1 -r 640x480 -f 30 -q 90 -n" -o "./output_http.so -w ./www"
MJPG Streamer Version: svn rev: 
 i: Using V4L2 device.: /dev/video0
 i: Desired Resolution: 640 x 480
 i: Frames Per Second.: 30
 i: Format............: YUV
 i: JPEG Quality......: 90
 o: www-folder-path...: ./www/
 o: HTTP TCP port.....: 8080
 o: username:password.: disabled
 o: commands..........: enabled

실행 결과

728x90

+ Recent posts