diff --git a/main.c b/main.c new file mode 100644 index 0000000..afe180d --- /dev/null +++ b/main.c @@ -0,0 +1,72 @@ +// gcc -o test1 test1.c -lwiringPi -lcurl + +#include +#include +#include +#include +#include + +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) ; + } +} \ No newline at end of file