void ESCController::arming() { if (!armed) { allSetTo(1000); gpioSleep(PI_TIME_RELATIVE, 5, 0); armed = true; } }
void *tickerThread(void *arg) { struct _gpio_param* pParam; if( (pParam = (struct _gpio_param*) arg) != NULL ) { gpioSetAlertFunc( pParam->bcm_gpio, pParam->callback ); // set event handler "ticker" while(1) { gpioSleep(PI_TIME_RELATIVE, 0, 10000); } } return( NULL ); }
void *W1ThermThread( void *arg ) { // fbhvorlauf - fbhrucklauf × 1,667 × 450 char curlBuffer[512]; static CURL *curl; static CURLcode res; FILE *fdSlaveList; int slaveFd; char w1_SlaveFileName[PATH_MAX]; char w1_ListLineBuffer[LINE_BUF_LENGTH]; char w1_DataBuffer[DATA_BUF_LENGTH]; int temperature; int fbhvorlauf, fbhrucklauf; float cummValue; struct pollfd fds[1]; int rc; int timeout_msecs = -1; unsigned int dummy[19]; char valid[10]; int values; int sleepSecs, sleepMicros; char logBuffer[LOG_BUFFER_SIZE+1]; do { #ifdef ALL_OUTPUT fprintf(stderr, "getting slave list from %s\n", W1_SLAVE_LIST ); #endif // ALL_OUTPUT if( (fdSlaveList = fopen(W1_SLAVE_LIST, "r")) != NULL ) { fbhvorlauf = -1; fbhrucklauf = -1; while( fgets(w1_ListLineBuffer, sizeof(w1_ListLineBuffer)-1, fdSlaveList) ) { if(w1_ListLineBuffer[strlen(w1_ListLineBuffer)-1] == '\n') { w1_ListLineBuffer[strlen(w1_ListLineBuffer)-1] = '\0'; } sprintf(w1_SlaveFileName, W1_SLAVE_PATH, w1_ListLineBuffer); #ifdef ALL_OUTPUT fprintf(stderr, "getting values from slave file %s\n", w1_SlaveFileName ); #endif // ALL_OUTPUT if( (slaveFd = open(w1_SlaveFileName, O_RDONLY)) > 0 ) { #ifdef ALL_OUTPUT fprintf(stderr, "open successful\n"); #endif // ALL_OUTPUT memset(w1_DataBuffer, '\0', DATA_BUF_LENGTH); fds[0].fd = slaveFd; fds[0].events = POLLIN; // fds[0].events = POLLRDNORM; timeout_msecs = 1000; rc = poll(fds, 1, timeout_msecs); if(rc == -1) { fprintf(stderr, "poll: an error occurred: %d\n", errno); } else { if( rc == 0 ) { fprintf(stderr, "poll: timeout!\n"); } else { if( fds[0].revents & POLLIN ) { rc = read(slaveFd, w1_DataBuffer, sizeof(w1_DataBuffer)-1); if( rc > 0 ) { #ifdef ALL_OUTPUT fprintf(stderr, "buffer[%s]\n", w1_DataBuffer); #endif // ALL_OUTPUT memset(valid, '\0', sizeof(valid)); values = sscanf(w1_DataBuffer, DATA_SCAN_FMT, dummy, dummy+1, dummy+2, dummy+3, dummy+4, dummy+5, dummy+6, dummy+7, dummy+8, dummy+9, valid, dummy+10, dummy+11, dummy+12, dummy+13, dummy+14, dummy+15, dummy+16, dummy+17, dummy+18, &temperature ); #ifdef ALL_OUTPUT fprintf(stderr, "scanned values: %d - temp is %d, crc was %s\n", values, temperature, valid); #endif // ALL_OUTPUT if( values == NUM_VALUES_TO_SCAN && strcmp(valid, W1_TOKEN_YES) == 0 ) { // error-indicator if( temperature != 85000 ) { // fbhvorlauf - fbhrucklauf × 1,667 × 450 if( strcmp(w1_ListLineBuffer, confOptions.thermIdVorlauf) == 0 ) { fbhvorlauf = temperature; } if( strcmp(w1_ListLineBuffer, confOptions.thermIdRuecklauf) == 0 ) { fbhrucklauf = temperature; } if( (curl = curl_easy_init()) ) { sprintf(curlBuffer, DS18B20_EMON_URL, confOptions.emonApiKey, serToFeederId(w1_ListLineBuffer), (float) (temperature / 1000.0 ) ); #ifdef ALL_OUTPUT fprintf(stderr, "-> %s <-\n", curlBuffer ); #endif // ALL_OUTPUT curl_easy_setopt(curl, CURLOPT_URL, curlBuffer); /* example.com is redirected, so we tell libcurl to follow redirection */ curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_dummy_write); /* 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); } } } } } else { fprintf(stderr, "flags are: %x\n", fds[0].revents); } } } close(slaveFd); } if(confOptions.tempReaderDelay > 0) { // seconds and microseconds if(confOptions.tempReaderDelay <= READER_DELAY_MAX) { sleepSecs = confOptions.tempReaderDelay / 1000; sleepMicros = confOptions.tempReaderDelay - (sleepSecs*1000); gpioSleep(PI_TIME_RELATIVE, sleepSecs, sleepMicros); // wait some ms } } } fclose(fdSlaveList); } // // here we go to accumulate values // for a seperate feed // if( fbhvorlauf != -1 && fbhrucklauf != -1 ) { // fbhvorlauf - fbhrucklauf × 1,667 × 450 cummValue = (((fbhvorlauf - fbhrucklauf) / 1000.0) * confOptions.termCoefficent) * confOptions.literAnHour; if( (curl = curl_easy_init()) ) { sprintf(curlBuffer, DS18B20_EMON_URL, confOptions.emonApiKey, FEEDER_ID_CUMM, cummValue ); #ifdef ALL_OUTPUT fprintf(stderr, "-> %s <-\n", curlBuffer ); #endif // ALL_OUTPUT curl_easy_setopt(curl, CURLOPT_URL, curlBuffer); /* example.com is redirected, so we tell libcurl to follow redirection */ curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_dummy_write); /* 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); } } // if( fbhvorlauf != -1 && fbhrucklauf != -1 ) sleep(confOptions.tempReaderIntval); } while( 1 ); }
int main(int argc, char* argv[] ) { int i; pthread_t *pS0Thread[NUM_GPIOS]; pthread_t *pW1Thread; cfg_t *cfg; char logBuffer[LOG_BUFFER_SIZE+1]; cfg_opt_t opts[] = { CFG_SIMPLE_FLOAT("thermal_coefficient", &confOptions.termCoefficent), CFG_SIMPLE_INT("flow_per_hour", &confOptions.literAnHour), CFG_SIMPLE_INT("log_level", &confOptions.logLevel), CFG_SIMPLE_INT("temp_reader_delay", &confOptions.tempReaderDelay), CFG_SIMPLE_INT("temp_reader_interval", &confOptions.tempReaderIntval), CFG_SIMPLE_BOOL("push_bullet_notification", &confOptions.bulletNotificationEnabled), CFG_SIMPLE_BOOL("email_notification", &confOptions.emailNotificationEnabled), CFG_SIMPLE_STR("emoncms_api_key", &confOptions.emonApiKey), CFG_SIMPLE_BOOL("client_watchdog", &confOptions.watchdogEnabled), CFG_SIMPLE_INT("gpio_4_s0_in_use", &confOptions.numGPIO4S0InUse), CFG_SIMPLE_STR("therm_id_vorlauf", &confOptions.thermIdVorlauf), CFG_SIMPLE_STR("therm_id_ruecklauf", &confOptions.thermIdRuecklauf), CFG_SIMPLE_INT("oil_consumption_per_hour", &confOptions.oilConsumptionPerHour), CFG_END() }; confOptions.run_mode = DEFAULT_RUN_MODE; confOptions.termCoefficent = CONF_DEFAULT_THERMCOEFF; confOptions.literAnHour = CONF_DEFAULT_LITERANHOUR; confOptions.logLevel = CONF_DEFAULT_LOGLEVEL; confOptions.tempReaderDelay = CONF_DEFAULT_READERDELAY; confOptions.tempReaderIntval = CONF_DEFAULT_READERINTVAL; confOptions.bulletNotificationEnabled = CONF_DEFAULT_BULLETNOTICICATION; confOptions.emailNotificationEnabled = CONF_DEFAULT_EMAILNOTICICATION; confOptions.emonApiKey = CONF_DEFAULT_APIKEY; confOptions.watchdogEnabled = CONF_DEFAULT_WATCHDOGENABLED; confOptions.numGPIO4S0InUse = CONF_DEFAULT_NUMS0PINS; confOptions.thermIdVorlauf = strdup(CONF_DEFAULT_THERMIDVORLAUF); confOptions.thermIdRuecklauf = strdup(CONF_DEFAULT_THERMIDRUECKLAUF); if( (cfg = cfg_init(opts, 0)) != NULL ) { cfg_parse(cfg, CONF_FILE_PATH); #ifdef DEBUG_ALL printf("thermal_coefficient ....: %f\n", confOptions.termCoefficent); printf("flow_per_hour ..........: %ld\n", confOptions.literAnHour); printf("log_level ..............: %ld\n", confOptions.logLevel); printf("temp_reader_delay ......: %ld\n", confOptions.tempReaderDelay); printf("temp_reader_interval ...: %ld\n", confOptions.tempReaderIntval); printf("push_bullet_notification: %s\n", confOptions.bulletNotificationEnabled?"true":"false"); printf("email_notification .....: %s\n", confOptions.emailNotificationEnabled?"true":"false"); printf("emoncms_api_key ........: %s\n", confOptions.emonApiKey?confOptions.emonApiKey:"NULL"); printf("client_watchdog ........: %s\n", confOptions.watchdogEnabled?"true":"false"); printf("gpio_4_s0_in_use .......: %ld\n", confOptions.numGPIO4S0InUse); printf("therm_id_vorlauf .......: %s\n", confOptions.thermIdVorlauf?confOptions.thermIdVorlauf:"NULL"); printf("therm_id_ruecklauf .....: %s\n", confOptions.thermIdRuecklauf?confOptions.thermIdRuecklauf:"NULL"); printf("oil_consumption_per_hour: %ld\n", confOptions.oilConsumptionPerHour); #endif // DEBUG_ALL } else { exit(3); } mainEndFlag = 0; if( confOptions.emonApiKey != NULL && confOptions.thermIdVorlauf != NULL && confOptions.thermIdRuecklauf != NULL && confOptions.tempReaderDelay <= READER_DELAY_MAX && confOptions.tempReaderDelay >= 0 ) { if (gpioInitialise() < 0) { // pigpio initialisation failed. fprintf(stderr, "cannot initialize pigpio!\n"); return(1); } else { signal( SIGINT, sig_handler ); signal( SIGQUIT, sig_handler ); signal( SIGKILL, sig_handler ); signal( SIGTERM, sig_handler ); signal( SIGALRM, alrm_handler ); // pigpio initialisation okay ... for( i = 0; i < NUM_GPIOS; i++) { #ifdef DEBUG_ALL printf("Index %d\n", i); #endif // DEBUG_ALL gpioSetMode( gpioInUse[i].bcm_gpio, PI_INPUT ); // set GPIO as an input gpioSetPullUpDown( gpioInUse[i].bcm_gpio, PI_PUD_DOWN); // activate internal pull-up // create thread for this gpio pS0Thread[i] = gpioStartThread(tickerThread, (void*) &gpioInUse[i]); // gpioSetAlertFunc( gpioInUse[i].bcm_gpio, ticker[i] ); // set event handler "ticker" } if( (pW1Thread = gpioStartThread(W1ThermThread, NULL)) == NULL ) { fprintf(stderr, "W1Thread failed to start ...\n"); for( i = 0; i < NUM_GPIOS; i++) { if( pS0Thread[i] != NULL ) { gpioStopThread( pS0Thread[i] ); } } gpioTerminate(); } else { while( !mainEndFlag ) { gpioSleep(PI_TIME_RELATIVE, 0, 10000); // hold 1/100 sec high level } for( i = 0; i < NUM_GPIOS; i++) { if( pS0Thread[i] != NULL ) { gpioStopThread( pS0Thread[i] ); } } gpioStopThread( pW1Thread ); gpioTerminate(); cfg_free(cfg); if(confOptions.emonApiKey) { free(confOptions.emonApiKey); } if(confOptions.thermIdVorlauf) { free(confOptions.thermIdVorlauf); } if(confOptions.thermIdRuecklauf) { free(confOptions.thermIdRuecklauf); } } } } else { confFail(); } return(0); }
int Sleep(int sec, int usec) { int result = gpioSleep(PI_TIME_RELATIVE,sec,usec); return result; }
void sleep(int t) { gpioSleep(PI_TIME_RELATIVE, t, 0); }