undefined reference to main issue (using arduino, RaspberryPi, GCC) - c

I'm making a program(C language) on RaspberryPi
My program get a data from Arduino by Serial communication
Source Code here :
#ifdef RaspberryPi
//include system librarys
#include <stdio.h> //for printf
#include <stdint.h> //uint8_t definitions
#include <stdlib.h> //for exit(int);
#include <string.h> //for errno
#include <errno.h> //error output
//wiring Pi
#include <wiringPi.h>
#include <wiringSerial.h>
char device[]= "/dev/ttyACM0";
// filedescriptor
int fd;
unsigned long baud = 9600;
unsigned long time=0;
//prototypes
void loop(void);
void setup(void);
void setup(){
printf("%s \n", "Raspberry Startup!");
fflush(stdout);
//get filedescriptor
if ((fd = serialOpen (device, baud)) < 0){
fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
exit(1); //error
}
//setup GPIO in wiringPi mode
if (wiringPiSetup () == -1){
fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
exit(1); //error
}
}
void loop(){
// Pong every 3 seconds
if(millis()-time>=3000){
serialPuts (fd, "Received\n");
time=millis();
}
// read signal
if(serialDataAvail (fd)){
char newChar = serialGetchar (fd);
printf("%c", newChar);
if((int)newChar >= 1){
system("raspistill -o image.jpg");
system("convert image.jpg -threshold 20% imagebw.jpg");
system("tesseract imagebw.jpg imageocr");
system("cat imageocr.txt");
}
fflush(stdout);
}
}
int main(void){
setup();
while(1) loop();
return 0;
}
#endif //#ifdef RaspberryPi
Before I put
system("convert image.jpg -threshold 20% imagebw.jpg");
system("tesseract imagebw.jpg imageocr");
system("cat imageocr.txt");
this code, it worked well
But, It does not work even if I delete it.
I found following error on my compiler(GCC)
/usr/lib/gcc/arm-linux-gneabihf/4.9/../../../arm-linux-gnueabihf/ort1.o:In function '_start':/build/glibc-mqlSLF/glibc-2.19/csu/../ports/sysdeps/arm/start.S:119 : undefined reference to 'main'
collect2: error: ld returned 1 exit sutatus
And I used following two command
sudo gcc test.c -o hello -lwiringPi -DRaspberryPi <br>
sudo gcc test.c -o hello -lwiringPi -DRaspberryPi -nostartfiles
Can anybody help me with this issue?

Related

wiringPiISR fires callback at every pin

i'm using RaspberryPI with Wiring libray in c++, and I want to use the wiringPiISR for fire an event when i click my button on PIN 5.
This is the code
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <wiringPi.h>
#include <unistd.h>
unsigned long last_interrupt_time = 0;
#define PIN 5
//gcc foo.c -o foo -lwiringPi
void myEdge (void)
{
unsigned long interrupt_time = millis();
if (interrupt_time - last_interrupt_time > 500)
{
delay(200);
if( digitalRead(PIN)== 1 )
fprintf(stdout,"gpio rising\n");
else
fprintf(stdout,"gpio falling\n");
fflush(stdout);
}
last_interrupt_time = interrupt_time;
}
int main (int argc,char **argv)
{
if (wiringPiSetupGpio() < 0)
{
fprintf (stderr, "Errore: Unable to GPIO: %s\n", strerror (errno)) ;
return 1 ;
}
pinMode(PIN, INPUT) ;
pullUpDnControl(PIN, PUD_DOWN) ;
if (wiringPiISR (PIN, INT_EDGE_BOTH , &myEdge) < 0)
{
fprintf (stderr, "Errore: Unable to setup ISR: %s\n", strerror (errno)) ;
return 1 ;
}
while (1)
delay (1000) ;
return 1;
}
I compile white
gcc foo.c -o foo -lwiringPi
This code works but fires when I click EVERY PIN... the pin 5, and also the 4 and the 6 and maybe others... WHY? Anyone can help me to understand?

Error in msgget: no such file or directory

I know a similar question like this has been asked, but I also believe my issue is slightly different. I'm learning about message queues for a class assignment and the professor suggested we use this tutorial: http://beej.us/guide/bgipc/html/multi/mq.html . It's incredibly helpful and has a sample sender and receiver code titled kirk and spock.
kirk.c:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
struct my_msgbuf {
long mtype;
char mtext[200];
};
int main(void)
{
struct my_msgbuf buf;
int msqid;
key_t key;
if ((key = ftok("kirk.c", 'B')) == -1) {
perror("ftok");
exit(1);
}
if ((msqid = msgget(key, 0644 | IPC_CREAT)) == -1) {
perror("msgget");
exit(1);
}
printf("Enter lines of text, ^D to quit:\n");
buf.mtype = 1; /* we don't really care in this case */
while(fgets(buf.mtext, sizeof buf.mtext, stdin) != NULL) {
int len = strlen(buf.mtext);
/* ditch newline at end, if it exists */
if (buf.mtext[len-1] == '\n') buf.mtext[len-1] = '\0';
if (msgsnd(msqid, &buf, len+1, 0) == -1) /* +1 for '\0' */
perror("msgsnd");
}
if (msgctl(msqid, IPC_RMID, NULL) == -1) {
perror("msgctl");
exit(1);
}
return 0;}
spock.c:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
struct my_msgbuf {
long mtype;
char mtext[200];
};
int main(void)
{
struct my_msgbuf buf;
int msqid;
key_t key;
if ((key = ftok("kirk.c", 'B')) == -1) { /* same key as kirk.c */
perror("ftok");
exit(1);
}
if ((msqid = msgget(key, 0644)) == -1) { /* connect to the queue */
perror("msgget");
exit(1);
}
printf("spock: ready to receive messages, captain.\n");
for(;;) { /* Spock never quits! */
if (msgrcv(msqid, &buf, sizeof buf.mtext, 0, 0) == -1) {
perror("msgrcv");
exit(1);
}
printf("spock: \"%s\"\n", buf.mtext);
}
return 0;
}
When I initially compiled this code I ran across the error: msgget: no such file or directory when executing spock.c. I understand this error is typically because of a permissions/file path issue. However, the working directory of both files is the same, as well as the permission is properly set for both. I later found the error is removed when I type ctrl+c (^c) into the terminal once I've completed my input. My issue is this project is based on using a file input of text and while it is simple to have this code take in a file, I need to place a getchar() in order to request for the user to type the ^C interrupt in order for the receiver to properly read the message queue. I was wondering if anyone has come across this issue, and if so any suggestions for fixing it so that I don't need the user to request the ^C interrupt? Apologies for not posting my altered code however, understandably, my professor does not want any possible copying/plagiarism from other students.
Here is more detail of what I am writing in to the terminal:
Successful Output with use of ^C:
user#user-ThinkPad-S1-Yoga:~/Documents/message_queue$ gcc -o kirk -g -Wall
kirk.c
user#user-ThinkPad-S1-Yoga:~/Documents/message_queue$ gcc -o spock -g -Wall
spock.c
user#user-ThinkPad-S1-Yoga:~/Documents/message_queue$ ./kirk
Enter lines of text, ^D to quit:
value1
value2
value3
^C
user#user-ThinkPad-S1-Yoga:~/Documents/message_queue$ ./spock
spock: ready to receive messages, captain.
spock: "value1"
spock: "value2"
spock: "value3"
^C
user#user-ThinkPad-S1-Yoga:~/Documents/message_queue$
Output with use of print instructions, meaning using ^D:
user#user-ThinkPad-S1-Yoga:~/Documents/message_queue$ gcc -o kirk -g -Wall
kirk.c
user#user-ThinkPad-S1-Yoga:~/Documents/message_queue$ gcc -o spock -g -Wall
spock.c
user#user-ThinkPad-S1-Yoga:~/Documents/message_queue$ ./kirk
Enter lines of text, ^D to quit:
value1
value2
value3
user#user-ThinkPad-S1-Yoga:~/Documents/message_queue$ ./spock
msgget: No such file or directory
user#user-ThinkPad-S1-Yoga:~/Documents/message_queue$
Now, even though a proper output occurs when ^C interrupt is typed by the user, I don't want to have to type that into the terminal and simply want the sender program to be executed, then followed by the receiver program execution with no need for a "quitting" action because a file will be loaded as the input for the sender program in place of the stdin.

Executing binary/elf-file directly from (shared)-memory in C

we are trying to copy a binary/elf file into a shared-memory region of our system and then execute it thereafter. We don't want to call our "client"-program directly, since we need to execute it from the memory itself for our purpose.
While we know that our approach (described below) won't really work, we are (obviously) trying to get it to work. How would it be possible to copy a binary/elf/etc. file directly into the (shared)-memory and execute it thereafter? Maybe we just compiled it in the wrong way? Or something else was done wrong?
We also don't want to convert it into hex/shell-code, we already did that. We are looking for an easier and more practical solution.
Is anyone able to help? Would be much appreciated!
Two programs:
"Host"-Program (copy & execute client-program in shared memory)
"Client"-Program (basically a hello-world echo)
"Client"-Program:
#include <stdio.h>
int main()
{
printf("Hello, World!\n");
return 0;
}
Compiled with gcc -o binfile clientprogram.c -static.
"Host"-Program:
#include <string.h>
#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
FILE *fp; //filepointer
size_t size; //filesize
unsigned char *buffer; //buffer
fp = fopen("binfile","rb");
fseek(fp, 0, SEEK_END);
size = ftell(fp);
fseek(fp, 0, SEEK_SET);
buffer = (unsigned char *) malloc(size);
if (fp == NULL){ //file empty?
printf("Error: There was an Error reading the file %s \n", "binfile");
exit(1);
}
else if (fread(buffer, sizeof *buffer, size, fp) != size){
printf("Error: There was an Error reading the file %s\n", "binfile");
exit(1);
}else{
int i;
// for(i=0; i<size;i++){
// printf("%02x", buffer[i]);
// }
}
void *mem = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
memcpy(mem, buffer, size);
mprotect(mem, size, PROT_READ|PROT_WRITE|PROT_EXEC);
void (*func)();
func = (void (*)()) buffer;
func();
munmap(mem, size);
fclose(fp);
free(buffer);
return 0;
}
Compiled with gcc hostprogram.c.
Build the client as a PIE, with -rdynamic. Then you'll be able to dlopen() it and dlsym() its main symbol (dlopen() will do the mmaping and mprotecting for you, as you'll be able to see if you strace the program), after which you'll be able to run its main from within the address space of the host.
Example:
#!/bin/sh
cat > client.c <<EOF
#include <stdio.h>
#include <unistd.h>
int main()
{
printf("Hello, World!: from %ld\n", (long)getpid());
return 0;
}
EOF
gcc -fpic -c client.c
gcc -pie -rdynamic -o client client.o
cat > host.c <<EOF
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
printf("Hello, I'm your host: %ld\n", (long)getpid()); ;
void *client_hndl;
typedef int main_t(int, char**);
main_t *client_main;
client_hndl = dlopen("./client", RTLD_LAZY);
if (!client_hndl){
fprintf(stderr, "%s\n", dlerror());
exit(1);
}
client_main = (main_t*)dlsym(client_hndl, "main");
if (!client_main){
fprintf(stderr, "%s\n", dlerror());
exit(2);
}
return client_main(1, (char*[]){"client", 0});
}
EOF
gcc host.c -ldl
./client
echo =============
./a.out
Example output:
Hello, World!: from 14520
=============
Hello, I'm your host: 14521
Hello, World!: from 14521
You are looking for a solution to this GLIBC feature request.
This feature request is 7 years old, and it's somewhat unlikely that anything will happen with it any time soon.
Your best bet is probably to do roughly what you are already doing (building a fully-static binary).
Your approach doesn't work because the executable you built requires to be loaded at the address it was linked at (visible in readelf -l binfile as the address of the first PT_LOAD segment. You would need to mmap your binfile there with MAP_FIXED, no other address will do.
You also need to read and decode the Elf{32,64}_Ehdr that is found at the beginning of the file to find entry point to jump to. You currently are jumping to the ELF header itself, but that header is not where the execution should start.

Segmentation Fault when moving binary outside directory

Hello guys this is my first post here.
My problem is stupid I think but I can't find any solution, hope you can help me!
So, me and a friend are coding a small system monitor (learn better/fun), the code has 2 sections: the daemon and the command line interface (for now), when I compile the CLI section all went great, the daemon is particular one, because when I compile and I execute it in the compile directory it works without error! Magically, when I move out of the compile directory it gives me a segmentation fault!
Compiler: GCC
Here is the repository: https://github.com/StefanoBelli/JASM
Makefile:
#!/usr/bin/make -f
SHELL=/bin/sh
#### CONFIGURATION ####
CC=gcc
DEBUG=-g
CFLAGS=-O2 -pipe -Wall -std=c11 $(DEBUG)
LIBS=
BINOUT=jasm
#### SOURCES & RULES ####
OBJS:=$(patsubst %.c,%.o,$(wildcard *.c))
install:$(OBJS)
$(CC) $(CFLAGS) $(LIBS) -o $(BINOUT) $(OBJS)
clean:
rm -fv *.o
.PHONY: install,clean
GDB Output:
(gdb) run
Starting program: /home/stefanozzz123/Devel/C.Cpp/JASM/bin/jasm
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7a7db04 in vfprintf () from /usr/lib/libc.so.6
(gdb)
Thank all of you guys! :)
EDIT: As you requested here is code:
jasm.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "queue.h"
#include "miscellaneous.h"
#include "ipc.h"
int main(int argc, char *argv[])
{
start_daemon();
start_server();
}
ipc.c
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <string.h>
#include <netinet/in.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include "ipc.h"
#include "miscellaneous.h"
#include "getter.h"
static void excecute_command(int fd, char *command)
{
/*
* if get* -> modulo get
* if start* -> modulo dei moduli
*/
// ************************** getter ***************************************
if(strncmp("get", command, 3)==0) { //ricevuto comando getter
int i;
//char buf[BUFSIZ];
strcpy(command, &command[3]);
for(i=0; i<NGETTER; i++) {
if(strcmp(getterName[i], command)==0) { //se esiste getter
log_string("getter found :)");
getterFunction[i](fd);
return;
}
}
log_error("getter NOT found :(");
write(fd, "null\0", 4);
return;
}
// ************************** starter **************************************
if(strncmp("start", command, 5)==0) { //ricevuto start modulo
log_error("starter NOT found :(");
write(fd, "null\0", 4);
return;
}
// ************************** miscellaneous ********************************
if(strcmp("halt", command)==0) { //spegne jasm
log_string("# halt and catch fire, done");
write(fd, "halt\0", 4);
exit(0);
}
/*if(strcmp("getVersion", command)==0) {
write(fd, (void *)VERSION, sizeof(VERSION));
log_string("server reply <version> with success");
return;
}*/
log_error("request not found");
write(fd, "null\0", 4);
}
void start_server()
{
int server_sockfd, client_sockfd;
int server_len;
socklen_t client_len;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
int result;
fd_set readfds, testfds;
server_sockfd=socket(AF_INET, SOCK_STREAM, 0);
server_address.sin_family=AF_INET;
server_address.sin_addr.s_addr=htonl(INADDR_ANY);
server_address.sin_port=htons(SERVER_PORT);
server_len=sizeof(server_address);
bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
listen(server_sockfd, 5);
FD_ZERO(&readfds);
FD_SET(server_sockfd, &readfds);
log_string("server started");
while(1) {
char buf[BUFSIZ];
char received[BUFSIZ];
int fd;
int nread;
testfds=readfds;
result=select(FD_SETSIZE, &testfds, (fd_set *)0, (fd_set *)0, (struct timeval *)0);
if(result<1) {
log_error("server fail");
exit(1);
}
for(fd=0; fd<FD_SETSIZE; fd++) {
if(FD_ISSET(fd, &testfds)) {
if(fd==server_sockfd) {
client_len=sizeof(client_address);
client_sockfd=accept(server_sockfd, (struct sockaddr *)&client_address, &client_len);
FD_SET(client_sockfd, &readfds);
sprintf(buf, "adding client on fd %d", client_sockfd);
log_string(buf);
} else {
ioctl(fd, FIONREAD, &nread);
if(nread==0) {
close(fd);
FD_CLR(fd, &readfds);
sprintf(buf, "removing client on fd %d", fd);
log_string(buf);
} else {
read(fd, &received, BUFSIZ);
sprintf(buf, "received from fd %d command <%s>", fd, received);
log_string(buf);
excecute_command(fd, received);
}
}
}
}
}
}
miscellanous.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "miscellaneous.h"
char * getTime()
{
time_t curtime;
struct tm *loctime;
static char *ret;
curtime=time(NULL);
loctime=localtime(&curtime);
ret=asctime(loctime);
ret[24]='\0';
return ret;
}
void log_string(const char *message)
{
FILE *fp;
fp=fopen(LOGPATH, "a+");
fprintf(fp, "[%s] %s\n", getTime(), message);
fclose(fp);
}
void log_error(const char *message)
{
FILE *fp;
fp=fopen(LOGPATH, "a+");
fprintf(fp, "[%s] ERROR: %s!\n", getTime(), message);
fclose(fp);
}
void start_daemon()
{
pid_t pid;
char buf[BUFSIZ];
log_string("boot");
pid=fork();
switch(pid) {
case -1:
log_error("fork fail");
exit(1);
break;
case 0:
log_string("fork success");
break;
default:
exit(0);
break;
}
if(setsid()<0) {
log_error("setsid fail");
exit(1);
} else {
log_string("setsid success");
}
//chiude i file descriptor di stdin, stdout, stderr
close(0);
close(1);
close(2);
sprintf(buf, "jasm started with pid %d and ppid %d", getpid(), getppid());
log_string(buf);
}
Essentially these are main srcs...
GDB Backtrace says nothing as the program run stops immediately
Since the question was tagged gdb, let's see how gdb can help. In my case, I've installed the debuginfo files for libc, so that I can examine the arguments to C library functions, but you don't really need that in this case because we can find the bug by looking at the user's source code.
(gdb) run
Starting program: ./jasm
Program received signal SIGSEGV, Segmentation fault.
_IO_vfprintf_internal (s=0x0, format=0x4019b1 "[%s] %s\n",
ap=ap#entry=0x7fffffffbd38) at vfprintf.c:1295
1295 vfprintf.c: No such file or directory.
(gdb) bt
#0 _IO_vfprintf_internal (s=0x0, format=0x4019b1 "[%s] %s\n",
ap=ap#entry=0x7fffffffbd38) at vfprintf.c:1295
#1 0x00007ffff7a693f7 in __fprintf (stream=<optimized out>,
format=<optimized out>) at fprintf.c:32
#2 0x000000000040149d in log_string (message=0x4019cb "boot")
at miscellaneous.c:46
#3 0x000000000040151f in start_daemon () at miscellaneous.c:64
#4 0x0000000000401400 in main (argc=1, argv=0x7fffffffdf78) at jasm.c:31
The declaration for vfprintf is:
int vfprintf(FILE *restrict stream, const char *restrict format, va_list ap);
Even though we don't have the source code for vfprintf installed, we can see that the first argument passed to it, s, is a NULL stream pointer, and that is likely the cause of the seg fault.
Let's look at something we have source code for: frame 2, log_string.
(gdb) frame 2
#2 0x000000000040149d in log_string (message=0x4019cb "boot")
at miscellaneous.c:46
46 fprintf(fp, "[%s] %s\n", getTime(), message);
(gdb) print fp
$2 = (FILE *) 0x0
There it is.
#define LOGPATH "../../../../data/log/jasm.log"
void log_string(const char *message)
{
FILE *fp;
fp=fopen(LOGPATH, "a+");
fprintf(fp, "[%s] %s\n", getTime(), message);
fclose(fp);
}
void log_error(const char *message)
{
FILE *fp;
fp=fopen(LOGPATH, "a+");
fprintf(fp, "[%s] ERROR: %s!\n", getTime(), message);
fclose(fp);
}
Check the return value from fopen. It may be NULL depending on what directory the program is run from. It may be better to use an absolute pathname, possibly settable in the Makefile for portability.
write(fd, "halt\0", 4);
All of these should have a count of 5, to include the trailing NUL. (And it isn't absolutely necessary to explicitly include \0 in the string literal, because C string literals implicitly have a \0 at the end.)

Writing to a file with stdio

I am using an example code from the wiringPi library to read data from Arduino to Raspberry Pi through serial, it is displaying the data correctly with printf("%c", newChar); but I can't write the same data to a text file.
This is the whole file:
/*
Pi_Serial_test.cpp - SerialProtocol library - demo
Copyright (c) 2014 NicoHood. All right reserved.
Program to test serial communication
Compile with:
sudo gcc -o Pi_Serial_Test.o Pi_Serial_Test.cpp -lwiringPi -DRaspberryPi -pedantic -Wall
sudo ./Pi_Serial_Test.o
*/
// just that the Arduino IDE doesnt compile these files.
#ifdef RaspberryPi
//include system librarys
#include <stdio.h> //for printf
#include <stdint.h> //uint8_t definitions
#include <stdlib.h> //for exit(int);
#include <string.h> //for errno
#include <errno.h> //error output
//wiring Pi
#include <wiringPi.h>
#include <wiringSerial.h>
char device[]= "/dev/ttyAMA0";
// filedescriptor
int fd;
unsigned long baud = 9600;
unsigned long timeTemp=0;
unsigned long timeHum=0;
//unsigned long timeLight=0;
//unsigned long timeMotion=0;
//prototypes
int main(void);
void loop(void);
void setup(void);
void setup(){
printf("%s \n", "Raspberry Startup!");
fflush(stdout);
//get filedescriptor
if ((fd = serialOpen (device, baud)) < 0){
fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
exit(1); //error
}
//setup GPIO in wiringPi mode
if (wiringPiSetup () == -1){
fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
exit(1); //error
}
}
void loop() {
// Temperature every 3 seconds
if(millis()-timeTemp>=3000){
serialPuts (fd, "05\n");
// you can also write data from 0-255
// 65 is in ASCII 'A'
//serialPutchar (fd, 5);
timeTemp=millis();
}
// read signal
if(serialDataAvail (fd)){
char newChar = serialGetchar (fd);
FILE * writeTemp = fopen("temp.txt", "w");
printf("%c", newChar);
fputc(newChar, writeTemp);
fflush(stdout);
fclose(writeTemp);
}
// Humidity every 4 seconds
if(millis()-timeHum>=4000){
serialPuts (fd, "06\n");
// you can also write data from 0-255
// 65 is in ASCII 'A'
//serialPutchar (fd, 5);
timeHum=millis();
}
// read signal
if(serialDataAvail (fd)){
char newChar = serialGetchar (fd);
//printf("received from ardiono \n");
printf("%c", newChar);
fflush(stdout);
}
}
// main function for normal c++ programs on Raspberry
int main(){
setup();
while(1) loop();
return 0;
}
#endif //#ifdef RaspberryPi
I've tried different commands, but I'm constantly getting errors for invalid conversions from const char* to char or to FILE.
I just need to write the data from printf("%c", newChar); in a file.
In the line fputc(&newChar, writeTemp);, you're taking a pointer to your character, converting it to int, then writing that to your file. You should just write your character; something like fputc(newChar, writeTemp);. Or fprintf(writeTemp, "%c", newchar); if you prefer printf.
fputc takes a int, not an address. The problem that you are having is because you are passing the address of newChar. change the code to
fputc(newValue, tempFile);
and you should be good to go.
Good luck :)
If you want a printf-like function, use fprintf.
To write your output to a file, you would write
fprintf(writeTemp, "%c", newChar);
Also, the line
char value = printf("%c", newChar);
does not make sense, since value is declared as a char but is assigned the return status from printf.
As another reply points out, the arguments to fputc() are also wrong. You can write
fputc(newChar, writeTemp);
Using append instead of write somehow fixed the problem:
FILE * writeTemp = fopen("temp.txt", "a");
I also had to remove the second request:
/*
// Humidity every 3 seconds
if(millis()-timeHum>=3000){
serialPuts (fd, "06\n");
timeHum=millis();
}
// read signal
if(serialDataAvail (fd)){
char humChar = serialGetchar (fd);
printf("%c", humChar);
fflush(stdout);
}
*/
because it was interfering with the data for some reason, even with different char variable.
Thank you for the answers.

Resources