Add main.c

This commit is contained in:
smallsolar 2024-04-25 13:46:12 +00:00
parent 32d8d23e77
commit 3adcbff6ba
1 changed files with 72 additions and 0 deletions

72
main.c Normal file
View File

@ -0,0 +1,72 @@
// gcc -o test1 test1.c -lwiringPi -lcurl
#include <wiringPi.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
int main(void)
{
wiringPiSetup() ;
pinMode (6, INPUT) ;
int result;
int count;
long start_time = time(NULL);
printf("Start Time: %u\n", start_time);
CURL *curl;
CURLcode res;
for(;;)
{
long diff_result = time(NULL) % 60;
if (diff_result == 0) {
printf("Tick %d CPM\n", count);
char total_string[100];
sprintf(total_string, "cpm,host=nanopi value=%d", count);
printf(total_string);
printf("\n");
curl = curl_easy_init();
if(curl) {
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Accept: application/octet-stream");
headers = curl_slist_append(headers, "Content-Type: application/octet-stream");
headers = curl_slist_append(headers, "charset: utf-8");
curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.1.1:8086/write?db=maindb");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, total_string);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcrp/0.1");
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(total_string));
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
count = 0;
delay(1000);
}
result = digitalRead(6) ;
if (result == 0) {
printf("%u\n", (unsigned)time(NULL));
count++;
delay(1);
}
delay (0.2) ;
}
}