Как отправить информацию на сервер от клиента senser. я попробовал с этим шнуром - PullRequest
0 голосов
/ 01 ноября 2019

я и мой друг пытаемся отправить температуру от клиента к серверу. Клиент, кажется, может прочитать температуру, но сервер, кажется, не может прочитать ее.

Сначала я зарегистрировался в SOF! Потому что это! надеюсь, что есть PPL может помочь мне ^^

пожалуйста, игнорируйте, что есть корейский

-клиент

enter code here

```#include <stdio.h>
#include <wiringPi.h>
#include <pcf8591.h>
#include <math.h> 
#include <stdlib.h> 
#include <string.h> 
#include <unistd.h> 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <netinet/in.h> 
#include <netdb.h> 
#define BUFSIZE 1024 
#define PCF 120 
#define DOpin 0 
#define DHT11PIN 25 
#define MAX_TIME 100 


int dht11_val[5] ={0,0,0,0,0};
int dht11_temp[5] ={0,0,0,0,0};
float farenheit_temp;
float farenheit;


void dht11_read_val(){
    uint8_t Iststate=HIGH;
    uint8_t counter=0;
    uint8_t j=0,i;

    for(i=0;i<5;i++)
        dht11_val[i] =0;


    pinMode(DHT11PIN,OUTPUT);
    digitalWrite(DHT11PIN,0);
    delay(18);
    digitalWrite(DHT11PIN,1);
    delayMicroseconds(40);
    pinMode(DHT11PIN,INPUT);

    for(i=0;i<MAX_TIME;i++){
        counter=0;
        while(digitalRead(DHT11PIN)==Iststate){
            counter++;
            delayMicroseconds(1);
            if(counter==255)
                break;
        }
        Iststate=digitalRead(DHT11PIN);
        if(counter==255)
            break;

        if((i>=4)&&(i%2==0)){
            dht11_val[j/8]<<=1;
            if(counter>16)
                dht11_val[j/8]|=1;
            j++;
        }
    }
    if((j>=40)&&(dht11_val[4]==((dht11_val[0]+dht11_val[1]+dht11_val[2]+dht11_val[3])&0xFF))){
        farenheit = dht11_val[2]*9./5.+32;
        printf("Humidity =%d.%d % % Temperature = %d.%d *C (%.lf *F)\n",dht11_val[0],dht11_val[1],dht11_val[2],dht11_val[3],farenheit);

        for(i=0;i<5;i++)
            dht11_temp[i] = dht11_val[i];
        farenheit_temp = farenheit;

        }else {
            printf("Humidity = %d.%d %% Temperature = %d.%d *C (%.lf *F)\n"
        ,dht11_temp[0],dht11_temp[1],dht11_temp[2],dht11_temp[3],farenheit_temp);

    }
}
void Print(int x) {
    switch(x) {
        case 0: 
            printf("\n************\n" ); 
            printf( "* Too Hot! *\n" ); 
            printf( "************\n\n"); 
            break;
        case 1: 
            printf("\n***********\n" ); 
            printf( "* Better~ *\n" ); 
            printf( "***********\n\n"); 
            break; 
        default:
            printf("\n**********************\n" ); 
            printf( "* Print value error. *\n" ); 
            printf( "**********************\n\n"); 
            break;
    } 
} /*  *error - wrapper for perror   */ 
void error(char *msg) { 
    perror(msg); 
    exit(0); 
} 
int main(int argc, char *argv[]) {
    unsigned char analogVal; 
    double Vr, Rt, temp; 
    int tmp, status; 
    int sockfd, portno, n; // 
    struct sockaddr_in serveraddr; // 
    struct hostent *server; 
    char *hostname; // 
    char buf [BUFSIZE]; // 
    if (argc != 3) {  
        fprintf(stderr, "usage: %s <hostname> <port>/n", argv[0]);
        exit(0); 
    } 
    hostname = argv[1]; 
    portno = atoi(argv[2]); /* socket: create the socket */ 
    sockfd = socket(AF_INET, SOCK_STREAM, 0); //  
    if (sockfd < 0) // 
        error("ERROR opening socker"); // /* gethostbyname: get the server's DNS entry */ // 
    server = gethostbyname(hostname); 
    if (server == NULL) { // 
        fprintf(stderr, "ERROR, no such host as %s\n", hostname); // 
        exit(0); // 
    }  
    bzero((char *) &serveraddr, sizeof(serveraddr)); 
    serveraddr.sin_family = AF_INET; 
    bcopy((char *)server->h_addr, (char *)&serveraddr.sin_addr.s_addr, server->h_length); 
    serveraddr.sin_port = htons(portno); /* connect: create a connection with the server */ 
    if (connect(sockfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr) ) < 0) {
        printf("ERROR connecting"); 
    }
    if(wiringPiSetup() == -1){      
        printf("setup wiringPi failed !"); 
        return 1; 
    } // Setup pcf8591 on base pin 120, and address 0x48 
    pcf8591Setup(PCF, 0x48); 
    //pinMode(D0pin, INPUT); 
    pinMode(DHT11PIN, OUTPUT); // 
    status = 0; // 

    while(1){ // loop forever 
    printf("loop"); 
    dht11_read_val();

    sleep(1);
    //printf("Current temperature : %lf\n", temp); 
    printf("Humidity =%d.%d % % Temperature = %d.%d *C (%.lf *F)\n",dht11_val[0],dht11_val[1],dht11_val[2],dht11_val[3],farenheit);
    sprintf(buf,"Humidity =%d.%d % % Temperature = %d.%d *C (%.lf *F)\n",dht11_val[0],dht11_val[1],dht11_val[2],dht11_val[3],farenheit);
    //sprintf(buf, "Current temperature : %lf\n", temp); // 
    n = write(sockfd, buf, strlen(buf));
    if (n < 0) { 
        error("ERROR writing to socket"); // 
    }
    // For a threshold, uncomment one of the code for 
    // which module you use. DONOT UNCOMMENT BOTH! 
    //--------------------------------------------- 
    // 1. For Analog Temperature module(with DO) 
    tmp = digitalRead(DHT11PIN); 
    printf("Digital Value : %d\n", tmp); 
        // 2. For Thermister module(with sig pin)  
        //if (temp > 33) tmp = 0; 
        // else if (temp < 31) tmp = 1; 
    //--------------------------------------------- 
    if (tmp != status) {
        Print(tmp); 
        status = tmp; 
    } 
        delay (5000); 

    }       
    while(1){

    } 
    return 0; 
}  ```

-сервер

enter code here

``` /*     communication between RPi to RPi through network         server program */ 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <unistd.h> 
#include <arpa/inet.h> 
#include <sys/types.h> 
#include <sys/socket.h> 
#define MAXLINE 10000 // 데이터 크기
 #define PORT 5000 // 임의로 설정한 통신포트 
 int main(int argc, char* argv[]){ 
    int serv_sock; 
    int conn_sock; 
    struct sockaddr_in serv_addr; // 
    struct sockaddr_in conn_addr; // 소켓 구조체 생성  
    int addrlen, datalen; char buf[MAXLINE +1]; // 메시지 버퍼 생성
    int nbytes; 
    int nport; 
    pid_t pid; 
    if (argc == 1){ // 서버 포트 확인  
        nport = PORT; //default port : 65021 
    } 
    else if (argc == 2) { 
        nport = atoi(argv[1]); 
    } 
    else { printf("Usage: %s <port>\n", argv[0]); // 서버 포트가 정상적으로 입력되지 않으면 용례 표시  
        exit(0); 
    } 
    serv_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); 
    if (serv_sock == -1){ // 소켓이 생성되지 않으면 에러 표시 
        perror("socket() error!\n"); 
        exit(0); 
    } 
    memset(&serv_addr, 0, sizeof(serv_addr)); 
    serv_addr.sin_family = AF_INET; //default       
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); //my addr  TCP IP, Port 설정  
    serv_addr.sin_port = htons(nport); //my port to serv 
    if (bind(serv_sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1) { // 서버 IP와 Por 연결 
        perror("bind() error\n"); exit(0); 
    } 
    if(listen(serv_sock, 1) == -1) { // 클라이언트 접속 대기  
        perror("listen() error\n"); exit(0); 
    } 
    addrlen = sizeof(conn_sock); // connection socket 
    conn_sock = accept(serv_sock, (struct sockaddr *)&conn_addr, &addrlen); //wait for call 클라이언트 접속 허용 
    if(conn_sock == -1){
        perror("accept() error\n"); 
        exit(0); 
    }
    if((pid = fork()) == -1){ // 잘못된 소켓인 경우 접속 종료  
        close(conn_sock); 
        perror("fork() error\n"); 
    } 
    else if(pid ==0) { //child process 
        while(1){ 
            fgets(buf,sizeof(buf), stdin); // 사용자의 입력을 받아서 
            nbytes = strlen(buf); write(conn_sock, buf, MAXLINE); //입력된 문자를 소켓을 통해 전송 
            if(strncmp(buf, "exit", 4) ==0) { //입력 문자가 "exit" 로 시작하면 -> exit 콜 =>종료 
                puts("exit program"); //output message 
                exit(0); //exit when "exit" 
            } 
        } 
    }
    else if (pid > 0){ //parent process 
        while(1){ 
            if((nbytes = read(conn_sock, buf, MAXLINE)) < 0){ // 클라이언트에서 온 문자를 읽어 
                perror("read() error\n"); 
                exit(0); 
            } 
            printf("%s\n", buf); // 표준 출력으로 출력 

            if(strncmp(buf, "exit",4) == 0) 
                exit(0); 
        } 
    } 
    close(conn_sock); 
    close(serv_sock); 
    return 0; 
 } ```

я не знаю, что добавить morei не знаю, что добавить morei не знаю, что добавить morei не знаю, что добавить morei не знаю, что добавить morei не знаю, что добавить morei не знаю, что добавить morei не знаю, что добавить morei не знаю, что добавитьmorei не знает, что добавить morei не знает, что добавить morei не знает, что добавить morei не знает, что добавить morei не знает, что добавить morei не знает, что добавить morei не знает, что добавить morei не знает, что добавить morei не знает, что добавить morei не делаетзнаю, что добавить morei, не знаю, что добавить more

я не знаю, что добавить morei, не знаю, что добавить morei, не знаю, что добавить morei, не знаю, что добавить morei, не знаю, что добавить morei не знаю, что добавить morei не знаючто добавить morei не знаю что добавить morei не знаю что добавить morei не знаю что добавить morei не знаю что добавить morei не знаю что добавить morei не знаю чточтобы добавить morei, не знаю, что добавить morei, не знаю, что добавить morei, не знаю, что добавить morei, не знаю, что добавить больше

...