Skip to content

Commit

Permalink
update makefile and mqttpublish.c pThread, add logs
Browse files Browse the repository at this point in the history
update the make file adding targets for all and clean.
remove the delay() and replace with pThread_join() for the
checkSendMsg() function thread.
added additional progress logs and made the loop in the
function checkSendMsg() to iterate max of 20 times.
  • Loading branch information
RichardChambers committed Jan 17, 2018
1 parent 3fe907a commit 6cecbf4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
5 changes: 5 additions & 0 deletions mqtt/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
# libfastjson is the Fast JSON library for JSON text handling
# use sudo apt-get install libfastjson-dev

all: mqtttest mqttpublish

clean:
rm -f mqtttest mqttpublish

mqtttest: mqtttest.c
cc -o mqtttest mqtttest.c -lmosquitto -lfastjson -lsqlite3

Expand Down
15 changes: 12 additions & 3 deletions mqtt/mqttpublish.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ int myAnalogRead(int spiChannel,int channelConfig,int analogChannel)
return ( (buffer[1] & 3 ) << 8 ) + buffer[2]; // get last 10 bits
}

// simple function to test using the pThread library
void *testThread (void *pObj)
{
for (int i = 0; i < 20; i++) {printf ("testThread loop %d\n", i);delay (1000); }
return pObj;
}

// the main thread function which samples the analog input
// and reports the value using MQTT to the designated broker.
Expand All @@ -105,7 +111,6 @@ void *checkSendMsg (void *pObj)

struct mosquitto *mosq = pObj;
char *pDevice = "DEV-101-23";
int iCount = 0;
int iRet = 0;
char MsgBuffer[512] = {0};

Expand All @@ -122,6 +127,9 @@ void *checkSendMsg (void *pObj)
pwmSetClock (400);
pwmSetRange (1024);

printf ("beginning pin read and publish loop.\n");

int iCount = 0;
do {
char DateString[24] = "Dec-12-2017 10:06:32";

Expand All @@ -137,7 +145,7 @@ void *checkSendMsg (void *pObj)
}
iCount++;
sleep(1);
} while (1);
} while (iCount < 20);

close (myFd) ;

Expand All @@ -148,6 +156,7 @@ void *checkSendMsg (void *pObj)
pinMode(LEDPIN, INPUT);
digitalWrite (LEDPIN, LOW);

printf ("thread exit.\n");
return pObj;
}

Expand Down Expand Up @@ -192,7 +201,7 @@ int main (int argc, char *argv[])

iRet = pthread_create (&levelsThread, NULL, checkSendMsg, mosq);

sleep (10);
pthread_join (levelsThread, NULL);

// cleanup before exit
sleep (1);
Expand Down

0 comments on commit 6cecbf4

Please sign in to comment.