Konuyu Oyla:
  • Toplam: 0 Oy - Ortalama: 0
Share Thread:
Raspberry Pi Zero W ile WiringPi - multi threading - PWM - TCP Server
#1
Merhaba Arkadaşlar;

Sizlere WiringPi ile 2x16 LCD ekranda multi threading kullanarak Saat, Tarih, CPU sıcaklık, DS18B20 sıcaklık, ip adresi ve TCP den gelen dataları gösteren ve Soft PWM olan basit bir uygulama paylaşıyorum. Videodan daha ayrıntılı inceleyebilirsiniz. 



Kod:
/*
* lcduyg2.c
*
* Copyright 2017  <pi@zerow>
*
* UMUT KAZANC
*
*
*/





#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <signal.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <unistd.h>
#include <arpa/inet.h>

#include <wiringPi.h>
#include <lcd.h>
#include <softPwm.h>



time_t             timer;
clock_t         start, end;
char             buffer_date[26];
char             buffer_time[26];
struct             tm* tm_info;
char             lcdline0str[20];
char             lcdline1str[20];
uint16_t         cputemp=0;
char             *ptr1;  
uint16_t         dstemp=0;
static int         lcdHandle;
uint8_t            ledflash=0;
double             cpu_time_used;

uint16_t        CPUTemp=0;
uint16_t        DSTemp=0;

uint8_t            LCDrefcnt=0;
uint8_t            LCDvalcnt=0; //0 ise sıcaklıklar 1 ise saat 2 ise ip .....

char             netname[20];
char             netip[20];

uint8_t            pwmval=0;
uint8_t            lcdsrlen=1;

int             s;
struct             ifreq ifr;

int             n;
int             sockfd, newsockfd, portno, clilen;
char             buffer[256];
struct             sockaddr_in serv_addr, cli_addr;


//LCD pin baglantilari
#define     LCD_RS    3
#define     LCD_E    0
#define     LCD_D4    6
#define     LCD_D5    1
#define     LCD_D6    5
#define     LCD_D7    4

#define     LED25    25
#define     LED24    24
#define     LED23    23
#define     LED22    22
#define     LED21    21



void timer_handler (int signum);
uint16_t  GetCPUTemp();
uint16_t  GetDSTemp();
void LedToggle(void);
void MultiThreading_init(void);
void LCD_gpio_init(void);
void LCD_yaz(const char *line0,const char *line1);
void TCPServer_init(void);



int main(int argc, char **argv)
{

    
    //init
    wiringPiSetup();
    LCD_gpio_init();
    lcdCursor(lcdHandle,0);
    lcdClear(lcdHandle);
    
    printf("RaspberryPi ZERO LCD ve Multi Threading Uygulamasi\r\n");
    LCD_yaz("RaspberryPi ZERO","LCD Uyg.");
    delay (3000);
    MultiThreading_init();
    TCPServer_init();
    
    
    //main loop
    while(1)
    {
        //temps
        CPUTemp = GetCPUTemp();
        DSTemp = GetDSTemp();
        
        //ip adres
        char iface[] = "wlan0"; //Change this to the network of your choice (eth0, wlan0, etc.)
        s = socket(AF_INET, SOCK_DGRAM, 0);
        ifr.ifr_addr.sa_family = AF_INET;
        strncpy(ifr.ifr_name , iface , IFNAMSIZ - 1);
        ioctl(s, SIOCGIFADDR, &ifr);
        close(s);
        sprintf(netname,"IP: %s",iface);
        sprintf(netip,"%s", inet_ntoa(( (struct sockaddr_in *)&ifr.ifr_addr )->sin_addr));
        
        delay (10);
    }
    
    close(sockfd);
    return 0;
}

//////////multi-threading////////////

PI_THREAD (LCDRefresh)
{

    while(1)
    {
        if(lcdsrlen==1)
        {
            LCDrefcnt++;
            if(LCDrefcnt==30)
            {
                LCDrefcnt=0;
                LCDvalcnt++;
                if(LCDvalcnt>2)
                {
                    LCDvalcnt=0;
                }
                lcdPosition (lcdHandle, 0, 0);
                lcdPuts (lcdHandle, "                ") ;
                lcdPosition (lcdHandle, 0, 1);
                lcdPuts (lcdHandle, "                ");
            }
            
            switch(LCDvalcnt)
            {
                case 0: //CPU ve DS Temp
                    sprintf(lcdline0str,"CPU Temp:%5.3fC", (float) CPUTemp / 1000);
                    sprintf(lcdline1str,"DS  Temp:%5.3fC", (float) DSTemp / 1000);
                    break;
                case 1: //time
                    time(&timer);
                    tm_info = localtime(&timer);
                    strftime(lcdline0str, 26, "Tarih:%d:%m:%Y", tm_info);
                    strftime(lcdline1str, 26, "Saat :%H:%M:%S  ", tm_info);
                    break;
                case 2: //ip
                    sprintf(lcdline0str,"%s", netname);
                    sprintf(lcdline1str,"%s", netip);
                    break;
                    
            }
            
            LCD_yaz(lcdline0str,lcdline1str);
            if(LCDrefcnt==0)
            {
                printf("%s\r\n",lcdline0str);
                printf("%s\r\n",lcdline1str);
                printf("LCD ye gonderildi\r\n\r\n");
            }

        }
        delay (100);
    }
}

PI_THREAD (LEDpwm)
{

    while(1)
    {
        //arttır
        for(pwmval=0;pwmval<100;pwmval++)
        {
            softPwmWrite (LED24, pwmval);
            delay (10);
        }
        for(pwmval=0;pwmval<100;pwmval++)
        {
            softPwmWrite (LED23, pwmval);
            delay (10);
        }
        for(pwmval=0;pwmval<100;pwmval++)
        {
            softPwmWrite (LED22, pwmval);
            delay (10);
        }
        for(pwmval=0;pwmval<100;pwmval++)
        {
            softPwmWrite (LED21, pwmval);
            delay (10);
        }
        //azalt
        for(pwmval=100;pwmval>0;pwmval--)
        {
            softPwmWrite (LED24, pwmval);
            delay (10);
        }
        softPwmWrite (LED24, 0);
        for(pwmval=100;pwmval>0;pwmval--)
        {
            softPwmWrite (LED23, pwmval);
            delay (10);
        }
        softPwmWrite (LED23, 0);
        for(pwmval=100;pwmval>0;pwmval--)
        {
            softPwmWrite (LED22, pwmval);
            delay (10);
        }
        softPwmWrite (LED22, 0);
        for(pwmval=100;pwmval>0;pwmval--)
        {
            softPwmWrite (LED21, pwmval);
            delay (10);
        }
        softPwmWrite (LED21, 0);

        
        
        
        
        delay (250);
    }
}

PI_THREAD (LEDflash)
{

    while(1)
    {
        LedToggle();
        delay (500);
    }
}

PI_THREAD (TCPserver)
{

    while(1)
    {
        if((newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen))>=0)
        {
            n = read( newsockfd,buffer,sizeof(buffer));
            buffer[n] = '\0';
            printf("TCP Gelen Data:(%d) %s\r\n\r\n",n,buffer);
            lcdsrlen=0;
            delay (10);
            lcdClear(lcdHandle);
            if (strlen(buffer) < 17)
            {
                LCD_yaz("TCP Gelen Data:",buffer);
                printf("LCD ye gonderildi...\r\n\r\n");
                n = write(newsockfd,"OK",2);
            }
            else
            {
                LCD_yaz("TCP Gelen Data:","HATA !!!");
                printf("LCD ye gonderilemedi. data buyuk...\r\n\r\n");
                n = write(newsockfd,"Hata (data buyuk)",17);
            }
            delay (3000);
            lcdsrlen=1;
            close(newsockfd);
        }
        
        delay (10);
    }
}
/////////////////////////////////////


//---------------------fonk--------------------

void LCD_yaz(const char *line0,const char *line1)
{
    //lcdClear(lcdHandle);
    lcdPosition (lcdHandle, 0, 0);
    lcdPuts (lcdHandle, line0) ;
    lcdPosition (lcdHandle, 0, 1);
    lcdPuts (lcdHandle, line1);
}

void LCD_gpio_init(void)
{
   lcdHandle = lcdInit (2,16,4,LCD_RS,LCD_E,LCD_D4,LCD_D5,LCD_D6,LCD_D7,0,0,0,0);
   printf("LCD OK.\r\n");
   pinMode (LED25, OUTPUT);
   softPwmCreate (LED24, 0, 100);
   softPwmCreate (LED23, 0, 100);
   softPwmCreate (LED22, 0, 100);
   softPwmCreate (LED21, 0, 100);
   
   softPwmWrite (LED24, 0);
   softPwmWrite (LED23, 0);
   softPwmWrite (LED22, 0);
   softPwmWrite (LED21, 0);
   printf("Software PWM OK.\r\n");
   
}

void MultiThreading_init(void)
{
    piThreadCreate (LEDflash);
    piThreadCreate (LCDRefresh);
    piThreadCreate (LEDpwm);
    piThreadCreate (TCPserver);
    printf("Multi Threading OK.\r\n");
}

void TCPServer_init(void)
{
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    
    if (sockfd < 0) {
     perror("ERROR opening socket");
     exit(1);
  }
    

    bzero((char *) &serv_addr, sizeof(serv_addr));
    portno = 5001;
    
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(portno);
   
    if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
     perror("ERROR on binding");
     exit(1);
    }
    

    if(listen(sockfd,5)==0)
    {
        printf("TCP Server OK (%s:%d)\n",inet_ntoa(cli_addr.sin_addr),portno);
    }
    else
    {
        printf("TCP Server HATA...\n");
    }
    
    
    clilen = sizeof(cli_addr);
}


uint16_t  GetCPUTemp()
{
    FILE         *fp;
    char         filebuffer[100];
    
    fp=fopen("/sys/class/thermal/thermal_zone0/temp","r");
    fread(filebuffer, sizeof(filebuffer), 1, fp);
    fclose(fp);
    return atoi(filebuffer);
    
}

uint16_t  GetDSTemp()
{
    FILE         *fp;
    char         filebuffer[100];

    fp=fopen("/sys/devices/w1_bus_master1/28-041502394cff/w1_slave","r");
    fread(filebuffer, sizeof(filebuffer), 1, fp);
    fclose(fp);
    ptr1 = strstr(filebuffer, "t=");
    ptr1 = ptr1 + 2;
    return atoi(ptr1);    
    
}
void LedToggle(void)
{
    //led flash
    if(ledflash==0)
    {
        ledflash=1;
        digitalWrite (LED25, HIGH);
    }
    else
    {
        ledflash=0;
        digitalWrite (LED25, LOW);
    }
}
Ara
Cevapla


Konu ile Alakalı Benzer Konular
Konular Yazar Yorumlar Okunma Son Yorum
  Lazarus (Free Pascal) ile wiringPi Wrapper emarti 2 5,991 02-21-2019, 07:04 PM
Son Yorum: halil kundakci
  WiringPi ile Servo Motor kullanimi yetisbey 0 2,076 05-22-2017, 10:18 PM
Son Yorum: yetisbey
  [Makale] Raspberry Pi ZERO W ve WiringPi ile LCD de CPU Sıcaklık Gösterimi umutk 0 1,837 03-05-2017, 07:45 PM
Son Yorum: umutk
  Geany + WiringPi umutk 0 1,792 02-05-2017, 02:29 PM
Son Yorum: umutk
  QT + WiringPi + 2x16 LCD Uygulaması umutk 0 1,885 01-28-2017, 05:36 PM
Son Yorum: umutk
  WiringPi Kurulumu Celal 5 6,392 03-02-2015, 01:01 AM
Son Yorum: Arda

Hızlı Menü:


Konuyu Okuyanlar: 1 Ziyaretçi