int main (void) { int value, pin ; // Enable the on-goard GPIO wiringPiSetup () ; pinMode (LED, OUTPUT) ; // On-board LED // Add in the pcf8591 on the q2w board pcf8591Setup (Q2W_ABASE, 0x48) ; printf ("Raspberry Pi - Quick2Wire Voltmeter\n") ; for (;;) { for (pin = 0 ; pin < 4 ; ++pin) { value = analogRead (Q2W_ABASE + pin) ; printf (" %5.2f", (double)value * 3.3 / 255.0) ; } printf ("\r") ; fflush (stdout) ; delay (100) ; digitalWrite (LED, !digitalRead (LED)) ; // Flicker the LED } return 0 ; }
static screen_t * screen_init(const int _din, const int _sclk, const int _dc, const int _rst, const int _cs, const int contrast) { screen_t *scretp; if (wiringPiSetup() == -1) { syslog(LOG_LOCAL0|LOG_ERR, "wiringPi-Error"); return (NULL); } if (pcf8591Setup(200, 0x48) == -1) { syslog(LOG_LOCAL0|LOG_ERR, "wiringPi-Error"); return (NULL); } /* init and clear lcd */ LCDInit(_sclk, _din, _dc, _cs, _rst, contrast); LCDclear(); /* show logo */ LCDshowLogo(); if ((scretp = calloc(1, sizeof(screen_t))) == NULL) { return (NULL); } scretp->sc_contrast = contrast; snprintf(scretp->sc_line1_buf, sizeof(scretp->sc_line1_buf), "Pilab"); cur_time(scretp->sc_line2_buf, sizeof(scretp->sc_line2_buf)); return (scretp); }
MyPCF8591::MyPCF8591() { if(!inited){ cerr << "Init PCF8591.\n" << endl; pcf8591Setup(ADC_BASE_PIN, ADC_BASE_ADDRESS); inited = true; } }
main () { wiringPiSetup () ; pcf8591Setup (200, 0x48) ; for (;;) printf ("%4d %4d %4d %4d\n", analogRead (200), analogRead (201), analogRead (202), analogRead (203)) ; }
int sensors_init(sensors_t *sn) { sensor_t *sp; char *endptr; long i2caddress; int scnf = 0; wiringPiSetup(); mcp23017Setup (300, 0x24) ; sp = sn->sn_head; do { if (sp->s_st != SENS_INIT || (sp->s_type == SENS_I2C && sn->sn_adc_configured > 0)) { if (sn->sn_adc_configured > 0) { sp->s_st = SENS_OK; } sp = sp->s_next; continue; } switch(sp->s_type) { case SENS_I2C: switch(sp->s_i2ctype) { case I2C_PCF8591P: i2caddress = strtol(sp->s_address, &endptr, 16); pcf8591Setup(PIN_BASE, i2caddress); sp->s_st = SENS_OK; sn->sn_adc_configured = 1; break; case I2C_BMP85: bmp85_init(); sp->s_st = SENS_OK; /* MQTT_log */ break; default: return (-1); } scnf++; break; case SENS_W1: sp->s_st = SENS_OK; /* NOP */ scnf++; break; default: return (-1); } sp = sp->s_next; } while (sp != NULL && sp != sn->sn_head); return (scnf); }
main () { wiringPiSetup () ; pcf8591Setup (200, 0x48) ; for (;;) { // printf ("%4d %4d %4d %4d\n", analogRead (200), analogRead (201), analogRead (202), analogRead (203)) ; sleep(2); //printf("%4d\n",analogRead(203)); analogWrite(200,analogRead(203)); } }
int main(int argc, char *argv[]) { wiringPiSetup(); // Check for errors in setup if(pcf8591Setup(PCF, DEVICE_ID) == -1) { printf("Error setting up connection to PCF8591\n"); return -1; } else if(analogRead(PCF) == -1) { printf("Error reading from ADC\n"); return -1; } // Everything's ok else { // STARTUP MESSAGES /* printf("Setup OK\n\n"); printf("\nGetting a baseline voltage will take 10 seconds...\n"); printf("\nNOTE:\nMQ-2 smoke/gas sensor requires about ten minutes to warm up.\n"); printf("If the sensor has not warmed up, press <Ctrl-C> now.\n"); */ // Get sum of analogRead 100 times for an average for(int i = 0; i < 100; i++) { baselineVolt = baselineVolt + analogRead(PCF); //delay(100); } // Divide by 100 to get average and by 51 to get voltage baselineVolt = baselineVolt/5100; // Calculate RsAir and Ro RsAir = (5-baselineVolt)/baselineVolt; Ro = RsAir/9.8; // PRINTING FOR DEBUG/INFO printf("RsAir=%f\n", RsAir); /* printf("baselineVolt: %f\n", baselineVolt); printf("Ro: %f\n", Ro); printf("To call gas reading function, use RsAir as an argument: %f\n", RsAir); */ } return 0; }
/* * Function: ioBoardI2cTest * Description: be used to test I2C interface on the board */ void ioBoardI2cTest(int addr, int outv, int chmax) { int val; int ch; pcf8591Setup(I2C_BASE, addr); //address >>1 analogWrite(I2C_BASE, outv); //set the analog output(3.3v*128/256) printf("I2C: please measure the output voltage!and Expectation value: 1.65v\n"); for(ch = 0; ch < chmax; ch++) { val = analogRead(I2C_BASE + ch); printf("I2C: the channel %d input voltage: %d and expectation value range from 0 to 256\n", ch, val); } }
int main() { unsigned char analogVal; double Vr, Rt, temp; int tmp, status; if(wiringPiSetup() == -1){ printf("setup wiringPi failed !"); return 1; } // Setup pcf8591 on base pin 120, and address 0x48 pcf8591Setup(PCF, 0x48); pinMode(DOpin, INPUT); status = 0; while(1) // loop forever { printf("loop"); analogVal = analogRead(PCF + 0); Vr = 5 * (double)(analogVal) / 255; Rt = 10000 * (double)(Vr) / (5 - (double)(Vr)); temp = 1 / (((log(Rt/10000)) / 3950)+(1 / (273.15 + 25))); temp = temp - 273.15; printf("Current temperature : %lf\n", temp); // For a threshold, uncomment one of the code for // which module you use. DONOT UNCOMMENT BOTH! //--------------------------------------------- // 1. For Analog Temperature module(with DO) tmp = digitalRead(DOpin); // 2. For Thermister module(with sig pin) // if (temp > 33) tmp = 0; // else if (temp < 31) tmp = 1; //--------------------------------------------- if (tmp != status) { Print(tmp); status = tmp; } delay (200); } return 0; }
int main(void) { int pin = 0; //ain0 double pin_val; wiringPiSetupGpio(); // enable bcm pin numbering pcf8591Setup(PINBASE, 0x48); while(1) { pin_val = analogRead(PINBASE + pin) / 2.55; //analog range 0 - 255, convert to 0 - 100 pin_val = (pin_val == 0) ? pin_val = 1 : pin_val; //pwm doesnt allow 0 printf("pin_val: %5.2f\n", pin_val); delay(500); //ms } return(0); }
static int doExtensionPcf8591 (char *progName, int pinBase, char *params) { int i2c ; if ((params = extractInt (progName, params, &i2c)) == NULL) return FALSE ; if ((i2c < 0x03) || (i2c > 0x77)) { verbError ("%s: i2c address (0x%X) out of range", progName, i2c) ; return FALSE ; } pcf8591Setup (pinBase, i2c) ; return TRUE ; }
int main (int argc, char **argv) { int wifiCount=0; int peakSound=0; int meanSound=0; int humAttempt=0; char disp1[30]; char disp2[30]; fd = wiringPiI2CSetup(LCDAddr); // Setup pcf8591 on base pin 120, and address 0x48 pcf8591Setup (PCF, 0x48); init(); if (wiringPiSetup () == -1) exit (1) ; // Set up wifiCount if the parameter was passed if (argc>1) { wifiCount=atoi(argv[1]); } else { wifiCount=getWifiCount(); } peakSound=getPeakSound(1000); meanSound=getMeanSound(100); while((read_dht11_dat()!=1)&& (humAttempt !=5)) { delay(1000); humAttempt++; } clear(); sprintf(disp1,"Mob %d Snd %d (%d)", wifiCount, meanSound, peakSound); if((humAttempt==5)) { sprintf(disp2,"Hum ?? Tem ??"); sprintf(mqMsg,"./mqpub %d,%d,%d", wifiCount, meanSound, peakSound); } else { sprintf(disp2,"Hum %d.%d Tem %d.%d", dht11_dat[0], dht11_dat[1], dht11_dat[2], dht11_dat[3]); sprintf(mqMsg,"./mqpub %d,%d,%d,%d.%d,%d.%d", wifiCount, meanSound, peakSound, dht11_dat[0], dht11_dat[1], dht11_dat[2], dht11_dat[3]); } write(0, 0, disp1); write(0, 1, disp2); printf("Try to send >> %s <<\n", mqMsg); system(mqMsg); return 0 ; }
int main(int argNum, char* argv[]) { RsAir = atof(argv[1]); // If user passes anything other than RsAir, print erro message and return if(argNum != 2) { printf("\n*ERROR*\nUsage: sudo smokeGasRead <RsAir value>\n"); return -1; } else { // divisor == 255/5, since MQ2 sensor will provide Vout in range [0, Vin] const float divisor = 51.0; // Equivalent voltage, resistance of sensor in normal air, in 1000 ppm H2 int data = -1; wiringPiSetup(); // Check for errors in setup if(pcf8591Setup(PCF, DEVICE_ID) == -1) printf("Error setting up connection to PCF8591\n"); else if(analogRead(PCF) == -1) printf("Error reading from ADC\n"); // Everything's ok else { sensorVoltage = analogRead(PCF)/divisor; // Display equivalent voltage seen by ADC printf("Sensor:%fV ", sensorVoltage); printf("H2:%fppm ", H2_ppm()); printf("LPG:%fppm ", LPG_ppm()); printf("CH4:%fppm ", CH4_ppm()); printf("CO:%fppm ", CO_ppm()); printf("Alcohol:%fppm ", Alcohol_ppm()); printf("Smoke:%fppm ", Smoke_ppm()); printf("Propane:%fppm ", Propane_ppm()); } } return 0; }
int main(void){ int value; if(wiringPiSetup() == -1){ //when initialize wiring failed,print messageto screen printf("setup wiringPi failed !"); return 1; } pinMode(enablePin,OUTPUT);//set mode for the pin pinMode(motorPin1,OUTPUT); pinMode(motorPin2,OUTPUT); softPwmCreate(enablePin,0,100);//define PMW pin pcf8591Setup(pinbase,address);//initialize PCF8591 while(1){ value = analogRead(A0); //read A0 pin printf("ADC value : %d \n",value); motor(value); //start the motor delay(100); } return 0; }
int main (void) { unsigned int usecs; usecs = 100; wiringPiSetupGpio(); pinMode(ENB0, OUTPUT); //sw pwm pinMode(ENB1, OUTPUT); //pinMode(ENB1, PWM_OUTPUT); //if using hw pwm pinMode(JOYSTICK, INPUT); softPwmCreate(ENB1, 1, RANGE); //pin, initial value, range pcf8591Setup(PINBASE, 0x48); wiringPiISR(JOYSTICK, INT_EDGE_BOTH, start_pwm); while(1) { usleep(usecs); } return(0); }
int main(int argc, char *argv[]) { char request[SZ_INFO_BUFF]; char registry[SZ_CHAR_BUFF]; PServiceRegistry psr = NULL; PRegistryEntry pre = NULL; PServiceRequest pnsr = NULL; int vIndex = 0; gd_i_i2c_address = 0; memset(registry, 0, sizeof(registry)); memset(request, 0, sizeof(request)); strncpy(registry, "DisplayClient: Raspberry Pi where are you?", sizeof(registry) - 1); skn_program_name_and_description_set( "a2d_display_client", "Send Measured Temperature and Light(lux) to Display Service." ); /* Parse any command line options, * like request string override */ if (skn_handle_locator_command_line(argc, argv) == EXIT_FAILURE) { exit(EXIT_FAILURE); } if (gd_pch_message != NULL) { strncpy(request, gd_pch_message, sizeof(request)); free(gd_pch_message); // from strdup() gd_pch_message = request; } else if (argc == 2) { strcpy(request, argv[1]); } skn_logger(SD_DEBUG, "Request Message [%s]", request); skn_logger(SD_DEBUG, "Registry Message [%s]", registry); /* Initialize Signal handler */ signals_init(); // wiringPiSetup () ; wiringPiSetupSys(); // Add in the pcf8591 if (gd_i_i2c_address == 0) { gd_i_i2c_address = 0x49; } pcf8591Setup (A2D_BASE, gd_i_i2c_address) ; pinMode (LED, OUTPUT) ; // On-board LED analogWrite(LED, 0) ; // Turn off the LED /* Create local socket for sending requests */ gd_i_socket = skn_udp_host_create_broadcast_socket(0, 4.0); if (gd_i_socket == EXIT_FAILURE) { signals_cleanup(gi_exit_flag); exit(EXIT_FAILURE); } skn_logger(SD_NOTICE, "Application Active..."); /* Get the ServiceRegistry from Provider * - could return null if error */ psr = service_registry_get_via_udp_broadcast(gd_i_socket, registry); if (psr != NULL && service_registry_entry_count(psr) != 0) { char *service_name = "lcd_display_service"; if (gd_pch_service_name != NULL) { service_name = gd_pch_service_name; } /* find a single entry */ pre = service_registry_find_entry(psr, service_name); if (pre != NULL) { skn_logger(" ", "\nLCD DisplayService (%s) is located at IPv4: %s:%d\n", pre->name, pre->ip, pre->port); } /* * Switch to non-broadcast type socket */ close(gd_i_socket); gd_i_socket = -1; gd_i_socket = skn_udp_host_create_regular_socket(0, 8.0); if (gd_i_socket == EXIT_FAILURE) { if (psr != NULL) service_registry_destroy(psr); signals_cleanup(gi_exit_flag); exit(EXIT_FAILURE); } } // we have the location if (pre != NULL) { if (request[0] == 0) { sknGetModuleTemp(request); } pnsr = skn_service_request_create(pre, gd_i_socket, request); } if (pnsr != NULL) { do { analogWrite(LED, 255) ; // Flicker the LED /* * Do Work */ sknGetModuleTemp(pnsr->request); vIndex = skn_udp_service_request(pnsr); if ((vIndex == EXIT_FAILURE) && (gd_i_update == 0)) { // ignore if non-stop is set break; } sleep(3); /* * Do Work */ sknGetModuleBright(pnsr->request); vIndex = skn_udp_service_request(pnsr); if ((vIndex == EXIT_FAILURE) && (gd_i_update == 0)) { // ignore if non-stop is set break; } analogWrite(LED, 0) ; // Flicker the LED sleep(gd_i_update); } while(gd_i_update != 0 && gi_exit_flag == SKN_RUN_MODE_RUN); free(pnsr); // Done } else { skn_logger(SD_WARNING, "Unable to create Network Request."); } /* Cleanup and shutdown * - if shutdown was caused by signal handler * then a termination signal will be sent via signal() * otherwise, a normal exit occurs */ analogWrite(LED, 0) ; // LED off if (gd_i_socket) close(gd_i_socket); if (psr != NULL) service_registry_destroy(psr); signals_cleanup(gi_exit_flag); exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { prgState programState = init; int buttonPin = 4; int relayPin = 1; int initButton; float deltaResistance; float ohmmeterInitValue; float ohmmeterMinValue; float ohmmeterMaxValue; float ohmmeterCurrentValue; short int alertCenceled = 0; short int alarm = 0; hdlDataPacage rpiDataPacage; hdlDataPacage recievedHdlDataPacage; int deviceNumber = 0; rpiDataPacage.devType = twoCharToUint(RPI_DEV_TYPE[0], RPI_DEV_TYPE[1]); rpiDataPacage.orgSubnet = '1'; rpiDataPacage.orgDeviceId = '3'; rpiDataPacage.operCode = twoCharToUint(RPI_OPR_CODE[0], RPI_OPR_CODE[1]); rpiDataPacage.targSubnet = '255'; rpiDataPacage.targId = '255'; while (1){ switch (programState){ case init:{ wiringPiSetup(); pcf8591Setup(20, 0x48); pinMode(buttonPin, INPUT); pinMode(ralayPin, OUTPUT); initButton = digitalRead(buttonPin); if (initButton == 1){ ohmmeterInitValue = analogRead(20); ohmmeterMaxValue = ohmmeterInitValue + (deltaResistance/2); ohmmeterMinValue = ohmmeterInitValue - (deltaResistance/2); sendHdlPacage(rpiDataPacage); time_t start = time(); for(;;){ recievedHdlDataPacage = recieveHdlPacage(); if (recievedHdlDataPacage.orgDeviceId != '0'){ if (isIn(recievedHdlDataPacage.orgDeviceId, hdlDevsInfo, deviceNumber) == 1){ continue; }else{ hdlDevsInfo[deviceNumber].devId = recievedHdlDataPacage.orgDeviceId; hdlDevsInfo[deviceNumber].devSubnet = recievedHdlDataPacage.orgSubnet; hdlDevsInfo[deviceNumber].devType = recievedHdlDataPacage.devType; deviceNumber++; } } if(time() > start + 10000){ break; } } }; break; } case monitoringBus: { ohmmeterCurrentValue = analogRead(20); if (ohmmeterCurrentValue < ohmmeterMinValue || ohmmeterCurrentValue > ohmmeterMaxValue){ programState = shortCicuit; break; }; while (alarm == 0){ recievedHdlDataPacage = recieveHdlPacage(); if (recievedHdlDataPacage.orgDeviceId != '0'){ if (isIn(recievedHdlDataPacage.orgDeviceId, hdlDevsInfo, deviceNumber) == 0){ alarm = 1; programState = shortCicuit; } } } }; case shortCicuit: { if (alertCenceled == 1){ alertCenceled = 0; digitalWrite(relayPin, 0); programState = monitoringBus; break; }; if (alertCenceled == 0){ digitalWrite(relayPin, 1); programState = alertSending; break; }; }; case alertSending: { break; }; }; return 0; } }