int main() { int address; int number_of_slaves; sys_start(); i2c_module_init(); i2c_init(&i2c, &i2c_0_dev, I2C_BAUDRATE_100KBPS, -1); i2c_start(&i2c); std_printf(FSTR("Scanning the i2c bus for slaves...\r\n" "\r\n")); number_of_slaves = 0; for (address = 0; address < 128; address++) { if (i2c_scan(&i2c, address) == 1) { std_printf(FSTR("Found slave with address 0x%x.\r\n"), address); number_of_slaves++; } } std_printf(FSTR("\r\n" "Scan complete. Found %d slaves.\r\n"), number_of_slaves); return (0); }
/*! * \brief Execute \h_i2c slave device scan. * * scan * * \param pI2C Pointer to \h_i2c handle. * \param nArgc Number of input arguments. * \param sArgv Array of input argument strings. * * \return Returns OK on success, RC_ERROR on error. */ static int execScan(i2c_t *pI2C, int nArgc, const char *sArgv[]) { int n; if( nArgc > 1 ) { printf("Error: %s takes no arguments\n", sArgv[0]); return RC_ERROR; } LOGDIAG1CALL(_TPTR(pI2C), _TINT(nArgc), _TSTR(sArgv[0])); if( OptVerbose ) { printf("command: %s\n", sArgv[0]); printf("response:\n"); printf(" scanned devices: "); } n = i2c_scan(pI2C, scanCallback, NULL); printf("\n"); if( OptVerbose ) { printf(" number found: %d\n", n); } return OK; }
int main(void) { wdt_enable(WDTO_1S); #if DEBUG_LEVEL > 0 /* let debug routines init the uart if they want to */ odDebugInit(); #else #ifdef DEBUG /* quick'n dirty uart init */ UCSRB |= _BV(TXEN); UBRRL = F_CPU / (19200 * 16L) - 1; #endif #endif #ifdef DEBUG stdout = &mystdout; #endif DEBUGF("i2c-tiny-usb - (c) 2006 by Till Harbaum\n"); i2c_init(); #ifdef DEBUG i2c_scan(); #endif /* clear usb ports */ // USB_CFG_IOPORT &= (uchar)~((1<<USB_CFG_DMINUS_BIT)|(1<<USB_CFG_DPLUS_BIT)); /* make usb data lines outputs */ // USBDDR |= ((1<<USB_CFG_DMINUS_BIT)|(1<<USB_CFG_DPLUS_BIT)); /* USB Reset by device only required on Watchdog Reset */ // _delay_loop_2(40000); // 10ms /* make usb data lines inputs */ // USBDDR &= ~((1<<USB_CFG_DMINUS_BIT)|(1<<USB_CFG_DPLUS_BIT)); usbInit(); sei(); for(;;) { /* main event loop */ wdt_reset(); usbPoll(); } return 0; }
int shw_i2c_io_scan(uint8_t * dev_map) { int i; int detect; if (i2c_io_bus.err) return -1; detect = i2c_scan(&i2c_io_bus, dev_map); pr_debug("\ni2c_bus: %s: %d devices\n", i2c_io_bus.name, detect); for (i = 0; i < 128; i++) if (dev_map[i / 8] & (1 << (i % 8))) pr_debug("device at: 0x%02X\n", i); return detect; }
static int test_scan(struct harness_t *harness_p) { int number_of_slaves_found; int address; number_of_slaves_found = 0; for (address = 0; address < 128; address++) { if (i2c_scan(&i2c, address) == 1) { std_printf(OSTR("Found slave with address 0x%02x.\r\n"), address); number_of_slaves_found++; } } std_printf(OSTR("Number of slaves found: %d\r\n"), number_of_slaves_found); return (0); }
void loop() { if (safeMode) { // safeMode engaged, enter blocking loop wait for an OTA update int safeDelay=30000; // five minutes in 100ms counts while (safeDelay--) { ArduinoOTA.handle(); delay(100); } ESP.reset(); // restart, try again delay(5000); // give esp time to reboot } if(wifiMulti.run() != WL_CONNECTED) { // reboot if wifi connection drops ESP.reset(); delay(5000); } if (!mqtt.connected()) { mqttreconnect(); // check mqqt status } doTick(); if (hasRSSI) doRSSI(); if (hasTout) doTout(); if (hasVout) doVout(); if (hasIout) doIout(); if (getRGB) doRGBout(); if (hasSpeed) doSpeed(); if ( (doUpdate) || (updateCnt>= 60 / ((updateRate * 20) / 1000) ) ) runUpdate(); // check for config update as requested or every 60 loops if (wsConcount>0) wsData(); if (useMQTT) mqttData(); // regular update for non RGB controllers if (prtConfig) printConfig(); // config print was requested sprintf(str,"Sleeping in %u seconds.", (updateRate*20/1000)); if ((!skipSleep) && (sleepEn)) { if (useMQTT) mqtt.publish(mqttpub, str); } int cnt = 30; if (updateRate>30) cnt=updateRate; while(cnt--) { ArduinoOTA.handle(); if (useMQTT) mqtt.loop(); #ifndef _MINI httpd.handleClient(); #endif webSocket.loop(); if (getTime) updateNTP(); // update time if requested by command if (scanI2C) i2c_scan(); if (hasRGB) doRGB(); // rgb updates as fast as possible if (rgbTest) testRGB(); #ifndef _MINI if (doUpload) { // upload file to spiffs by command doUpload = false; fileSet = false; int stat = uploadFile(fileName, fileURL); sprintf(str, "Upload complete: %s %d bytes.",fileName,stat); if (useMQTT) mqtt.publish(mqttpub, str); } #endif if (setPolo) { setPolo = false; // respond to an mqtt 'ping' of sorts if (useMQTT) mqtt.publish(mqttpub, "Polo"); } if (doReset) { // reboot on command if (useMQTT) { mqtt.publish(mqttpub, "Rebooting!"); mqtt.loop(); } delay(50); ESP.reset(); delay(5000); // allow time for reboot } if (!hasRGB) delay(20); // don't delay for rgb controller } if ((!skipSleep) && (sleepEn)) { if ((sleepPeriod<60) || (sleepPeriod>43200)) sleepPeriod=900; // prevent sleeping for less than 1 minute or more than 12 hours sprintf(myChr,"Back in %d minutes", sleepPeriod/60); if (useMQTT) { mqtt.publish(mqttpub, myChr); mqtt.loop(); } ESP.deepSleep(1000000 * sleepPeriod, WAKE_RF_DEFAULT); // sleep for 15 min delay(5000); // give esp time to fall asleep } skipSleep = false; updateCnt++; }
void setup() { memset(voltsChr,0,sizeof(voltsChr)); memset(amps0Chr,0,sizeof(amps0Chr)); memset(amps1Chr,0,sizeof(amps1Chr)); memset(tmpChr,0,sizeof(tmpChr)); // if the program crashed, skip things that might make it crash String rebootMsg = ESP.getResetReason(); if (rebootMsg=="Exception") safeMode=true; else if (rebootMsg=="Hardware Watchdog") safeMode=true; else if (rebootMsg=="Unknown") safeMode=true; else if (rebootMsg=="Software Watchdog") safeMode=true; if (sw1>=0) { pinMode(sw1, OUTPUT); } if (sw2>=0) { pinMode(sw2, OUTPUT); } if (sw3>=0) { pinMode(sw3, OUTPUT); } if (sw4>=0) { pinMode(sw4, OUTPUT); } // "mount" the filesystem bool success = SPIFFS.begin(); if (!success) SPIFFS.format(); if (!safeMode) fsConfig(); // read node config from FS #ifdef _TRAILER wifiMulti.addAP("DXtrailer", "2317239216"); #else wifiMulti.addAP("Tell my WiFi I love her", "2317239216"); #endif int wifiConnect = 240; while ((wifiMulti.run() != WL_CONNECTED) && (wifiConnect-- > 0)) { // spend 2 minutes trying to connect to wifi // connecting to wifi delay(1000); } if (wifiMulti.run() != WL_CONNECTED ) { // still not connected? reboot! ESP.reset(); delay(5000); } if (hasHostname) { // valid config found on FS, set network name WiFi.hostname(String(nodename)); // set network hostname ArduinoOTA.setHostname(nodename); // OTA hostname defaults to esp8266-[ChipID] MDNS.begin(nodename); // set mDNS hostname } WiFi.macAddress(mac); // get esp mac address, store it in memory, build fw update url sprintf(macStr,"%x%x%x%x%x%x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); sprintf(theURL,"/iotfw?mac=%s", macStr); // request latest config from web api if (!safeMode) getConfig(); // check web api for new firmware if (!safeMode) httpUpdater(); // start UDP for ntp client udp.begin(localPort); updateNTP(); setSyncProvider(getNtptime); // use NTP to get current time setSyncInterval(600); // refresh clock every 10 min #ifndef _MINI // start the webserver httpd.onNotFound(handleNotFound); httpd.begin(); // Add service to MDNS-SD MDNS.addService("http", "tcp", 80); #endif // start websockets server webSocket.begin(); webSocket.onEvent(webSocketEvent); // setup other things setupOTA(); setupMQTT(); // setup i2c if configured, basic sanity checking on configuration if (hasI2C && iotSDA>=0 && iotSCL>=0 && iotSDA!=iotSCL) { sprintf(str,"I2C enabled, using SDA=%u SCL=%u", iotSDA, iotSCL); mqtt.publish(mqttpub, str); Wire.begin(iotSDA, iotSCL); // from api config file //Wire.begin(12, 14); // from api config file i2c_scan(); printConfig(); if (hasRGB) setupRGB(); if (hasIout) setupADS(); if (hasSpeed) setupSpeed(); } // OWDAT = 4; if (OWDAT>0) { // setup onewire if data line is using pin 1 or greater sprintf(str,"Onewire Data OWDAT=%u", OWDAT); mqtt.publish(mqttpub, str); oneWire.begin(OWDAT); if (hasTout) { ds18b20 = DallasTemperature(&oneWire); ds18b20.begin(); // start one wire temp probe } if (hasTpwr>0) { pinMode(hasTpwr, OUTPUT); // onewire power pin as output digitalWrite(hasTpwr, LOW); // ow off } } if (useMQTT) { String rebootReason = String("Last reboot cause was ") + rebootMsg; rebootReason.toCharArray(str, rebootReason.length()+1); mqtt.publish(mqttpub, str); } }