Printing 0

12/14/2016

Page 166

The printf and humidy calculation uses byte1 to byte5 before they have been defined. The order just needs to be changed:

Change

printf("Checksum %d %d \n\r", byte5,  
        (byte1 + byte2 + byte3 + byte4) & 0xFF);
float humidity = (float) (byte1 << 8 | byte2) / 10.0;
int byte1 = getByte(1, buf);
int byte2 = getByte(2, buf);
int byte3 = getByte(3, buf);
int byte4 = getByte(4, buf);
int byte5 = getByte(5, buf);
printf("Humidity= %f \n\r", humidity); 

 

To 


int byte1 = getByte(1, buf);
int byte2 = getByte(2, buf);
int byte3 = getByte(3, buf);
int byte4 = getByte(4, buf);
int byte5 = getByte(5, buf);
printf("Checksum %d %d \n\r", byte5,  
        (byte1 + byte2 + byte3 + byte4) & 0xFF);
float humidity = (float) (byte1 << 8 | byte2) / 10.0;
printf("Humidity= %f \n\r", humidity);

 

 

Page 257

Missing include means you get warnings about undeclared functions. Add the final include as listed:

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>

#include <unistd.h>

Page 263

Missing include means you get warnings about undeclared functions. Add the final include as listed:

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>

 You also need to change the inital part of the program to read:

 hints.ai_family =  AF_INET;
 hints.ai_socktype = SOCK_STREAM;
 hints.ai_flags = AI_PASSIVE;
 getaddrinfo(NULL, "80", &hints, &server);
int sockfd = socket(server->ai_family, server->ai_socktype| SOCK_NONBLOCK, server->ai_protocol);

The change is to take SOCK_NONB from the call to getaddrinfo and move it to socket.