// AppleLM87::getReading IOReturn AppleLM8x::getReading(UInt32 subAddress, SInt32 *value) { IOReturn status; UInt8 data; if (systemIsRestarting == TRUE) return kIOReturnOffline; status = openI2C(kLM8xBus); if(status != kIOReturnSuccess) { DLOG("AppleLM8x::getReading(0x%x) failed to open I2C bus.\n", kLM8xAddr<<1); return status; } status = readI2C((UInt8)subAddress, (UInt8 *) &data, 1); if(status != kIOReturnSuccess) { DLOG("AppleLM8x::getReading(0x%x) readI2C failed.\n", kLM8xAddr<<1); closeI2C(); return status; } closeI2C(); *value = (SInt32)data; return kIOReturnSuccess; }
/* switchesOFF() - switches off the MakeSuremote switches specified * * It switches off all MakeSuremote switches */ void MakeSurePWR::switchesOFF(uint8_t option) { cbi(ADCSRA,ADEN); // switch Analog to Digital Converter OFF //pinMode(SERID_PW,OUTPUT); //digitalWrite(SERID_PW,LOW); // Disable MEM_PW and SS_PIN not to consume // battery from SD when MakeSuremote is asleep //Rupam //pinMode(SS_PIN, OUTPUT); //digitalWrite(SS_PIN, LOW); pinMode(MEM_PW,OUTPUT); digitalWrite(MEM_PW,LOW); if( option & SENS_OFF ) { pinMode(SENS_PW_3V3,OUTPUT); digitalWrite(SENS_PW_3V3,LOW); pinMode(SENS_PW_5V,OUTPUT); digitalWrite(SENS_PW_5V,LOW); } if( option & UART0_OFF ) { // close UART0 //Rupam // closeSerial(SOCKET0); // set XBee module off pinMode(XBEE_PW,OUTPUT); digitalWrite(XBEE_PW,LOW); } if( option & UART1_OFF ) { // close UART1 //Rupam // closeSerial(SOCKET1); pinMode(MUX_PW, OUTPUT); digitalWrite(MUX_PW, LOW); pinMode(GPS_PW, OUTPUT); digitalWrite(GPS_PW, LOW); } if( option & RTC_OFF ) { RTC.close(); pinMode(RTC_PW,OUTPUT); digitalWrite(RTC_PW,LOW); closeI2C(); } if( option & BAT_OFF ) { pinMode(BAT_MONITOR_PW,OUTPUT); digitalWrite(BAT_MONITOR_PW,LOW); } }
IOReturn AppleLM8x::restoreRegisters() { IOReturn status; DLOG("AppleLM8x::restoreRegisters(0x%x) entered.\n", kLM8xAddr<<1); status = openI2C(kLM8xBus); if(status != kIOReturnSuccess) { DLOG("AppleLM8x::restoreRegisters(0x%x) failed to open I2C bus.\n", kLM8xAddr<<1); return status; } status = writeI2C((UInt8)kChannelModeRegister, (UInt8 *)&savedRegisters.ChannelMode, 1); if(status != kIOReturnSuccess) { DLOG("AppleLM8x::restoreRegisters(0x%x) writeI2C failed.\n", kLM8xAddr<<1); closeI2C(); return status; } status = writeI2C((UInt8)kConfReg2, (UInt8 *)&savedRegisters.Configuration2, 1); if(status != kIOReturnSuccess) { DLOG("AppleLM8x::restoreRegisters(0x%x) writeI2C failed.\n", kLM8xAddr<<1); closeI2C(); return status; } // restore Configuration Register 1 last, since it starts polling status = writeI2C((UInt8)kConfReg1, (UInt8 *)&savedRegisters.Configuration1, 1); if(status != kIOReturnSuccess) { DLOG("AppleLM8x::restoreRegisters(0x%x) writeI2C failed.\n", kLM8xAddr<<1); closeI2C(); return status; } closeI2C(); return status; }
IOReturn AppleLM8x::saveRegisters() { IOReturn status; DLOG("AppleLM8x::saveRegisters(0x%x) entered.\n", kLM8xAddr<<1); status = openI2C(kLM8xBus); if(status != kIOReturnSuccess) { DLOG("AppleLM8x::saveRegisters(0x%x) failed to open I2C bus.\n", kLM8xAddr<<1); return status; } status = readI2C((UInt8)kChannelModeRegister, (UInt8 *)&savedRegisters.ChannelMode, 1); if(status != kIOReturnSuccess) { DLOG("AppleLM8x::saveRegisters(0x%x) readI2C failed.\n", kLM8xAddr<<1); closeI2C(); return status; } status = readI2C((UInt8)kConfReg1, (UInt8 *)&savedRegisters.Configuration1, 1); if(status != kIOReturnSuccess) { DLOG("AppleLM8x::saveRegisters(0x%x) readI2C failed.\n", kLM8xAddr<<1); closeI2C(); return status; } status = readI2C((UInt8)kConfReg2, (UInt8 *)&savedRegisters.Configuration2, 1); if(status != kIOReturnSuccess) { DLOG("AppleLM8x::saveRegisters(0x%x) readI2C failed.\n", kLM8xAddr<<1); closeI2C(); return status; } closeI2C(); return status; }
// inits the value of the digipot used in the battery detector void WaspPWR::setLowBatteryThreshold(float threshold) { uint8_t dig=0; uint8_t rpot=200; dig = uint8_t (rpot-(((threshold-1.15)*470/1.15)-806))*255/rpot; if( !Wire.I2C_ON ) Wire.begin(); delay(200); Wire.beginTransmission(0x2d); // Address Wire.send(0x11); // Write command Wire.send(dig); // Data Wire.endTransmission(); if( Wire.I2C_ON ) closeI2C(); }
// inits the value of the digipot used in the battery detector void MakeSurePWR::setLowBatteryThreshold(float threshold) { if ( threshold > 3.4 ) { threshold = 3.4; } if ( threshold < 3.1 ) { threshold = 3.1; } uint8_t dig=0; uint8_t rpot=200; dig = uint8_t (rpot-(((threshold-1.15)*470/1.15)-806))*255/rpot; if( !Wire.I2C_ON ) Wire.begin(); delay(200); Wire.beginTransmission(0x56); // Address Wire.write(0x11); // Write command Wire.write(dig); // Data Wire.endTransmission(); if( Wire.I2C_ON ) closeI2C(); }
IOReturn AppleLM8x::initHW(IOService *provider) { IOReturn status; UInt8 cfgReg = 0; UInt8 attempts = 0; DLOG("AppleLM8x::initHW(0x%x) entered.\n", kLM8xAddr<<1); status = openI2C(kLM8xBus); if(status != kIOReturnSuccess) { DLOG("AppleLM8x::initHW(0x%x) failed to open I2C bus.\n", kLM8xAddr<<1); return status; } // Attempt to start LM87 by setting bit 0 in Configuration Register 1. If we detect the RESET // bit, then make a second attempt. for ( ; attempts < 2; attempts++ ) { // Read Configuration Register 1 status = readI2C((UInt8)kConfReg1, (UInt8 *) &cfgReg, 1); if(status != kIOReturnSuccess) { DLOG("AppleLM8x::initHW(0x%x) readI2C failed.\n", kLM8xAddr<<1); break; } DLOG("AppleLM8x::readI2C(0x%x) retrieved data = 0x%x\n", kLM8xAddr<<1, cfgReg); // If we detect RESET, wait at least 45ms for it to clear. if ( cfgReg & kConfRegRESET ) { IOSleep(50); status = kIOReturnError; } if(status != kIOReturnSuccess) { IOLog("AppleLM8x::initHW(0x%x) readI2C detected RESET bit.\n", kLM8xAddr<<1); } // Start monitoring operations cfgReg = 0x1; // Failure of this write operation is fatal status = writeI2C((UInt8)kConfReg1, (UInt8 *)&cfgReg, 1); // Read Configuration Register 1 status = readI2C((UInt8)kConfReg1, (UInt8 *) &cfgReg, 1); if(status != kIOReturnSuccess) { DLOG("AppleLM8x::initHW(0x%x) readI2C failed.\n", kLM8xAddr<<1); break; } if ( cfgReg == kConfRegStart ) { break; } else { IOLog("AppleLM8x::readI2C(0x%x) retrieved configuration register 1 = 0x%x\n", kLM8xAddr<<1, cfgReg); } } closeI2C(); return status; }
/* switchesOFF() - switches off the Waspmote switches specified * * It switches off all Waspmote switches */ void WaspPWR::switchesOFF(uint8_t option) { // set this pin LOW to decrease consumption // if hibernate is not used pinMode(RTC_SLEEP, OUTPUT); digitalWrite(RTC_SLEEP, LOW); // unset I2C bus pinMode(I2C_SDA,OUTPUT); digitalWrite(I2C_SDA,LOW); pinMode(I2C_SCL,OUTPUT); digitalWrite(I2C_SCL,LOW); // switch Analog to Digital Converter OFF cbi(ADCSRA,ADEN); //pinMode(SERID_PW,OUTPUT); //digitalWrite(SERID_PW,LOW); // Disable MEM_PW and SS_PIN not to waste // battery from SD when Waspmote is asleep pinMode(SD_SS, OUTPUT); digitalWrite(SD_SS, LOW); pinMode(MEM_PW,OUTPUT); digitalWrite(MEM_PW,LOW); // Set down SOCKET0 SPI SS pin pinMode(SOCKET0_SS,OUTPUT); digitalWrite(SOCKET0_SS,LOW); if( option & SENS_OFF ) { // switch OFF sensor boards PWR.setSensorPower(SENS_3V3, SENS_OFF); PWR.setSensorPower(SENS_5V, SENS_OFF); Wire.isBoard = false; } // close UART0 closeSerial(SOCKET0); if( option & SOCKET0_OFF ) { // set SOCKET0 power supply off pinMode(XBEE_PW,OUTPUT); digitalWrite(XBEE_PW,LOW); // update Waspmote Register WaspRegister &= ~(REG_SOCKET0); } // close UART1 closeSerial(SOCKET1); // set Expansion board power supply off unless a // Smart Cities board remains powered on if( ( option & SENS_OFF ) && !(WaspRegister & REG_CITIES_V14) && !(WaspRegister & REG_CITIES_V15) ) { pinMode(DIGITAL6, OUTPUT); digitalWrite(DIGITAL6, LOW); } // switch off the RTC power supply RTC.close(); pinMode(RTC_PW,OUTPUT); digitalWrite(RTC_PW,LOW); closeI2C(); // switch off the battery monitor power supply pinMode(BAT_MONITOR_PW,OUTPUT); digitalWrite(BAT_MONITOR_PW,LOW); // check if a Smart Metering board has been switched and proceed to disable // the digital pins so as not to waste energy if( WaspRegister & REG_METERING ) { setSensorPower(SENS_3V3,SENS_ON); pinMode(DIGITAL3, OUTPUT); pinMode(DIGITAL4, OUTPUT); digitalWrite(DIGITAL3, HIGH); digitalWrite(DIGITAL4, HIGH); } // check if a Gases Sensor Board is used. In this case, switch off the // digital pins so as not to waste energy if( WaspRegister & REG_GASES ) { digitalWrite(DIGITAL4,LOW); } // check if an Agriculture Sensor Board is used. In this case, // switch off the digital pins so as not to waste energy if( WaspRegister & REG_AGRICULTURE ) { // switch off sensors power supply digitalWrite(DIGITAL7, LOW); digitalWrite(DIGITAL1, LOW); digitalWrite(DIGITAL5, LOW); digitalWrite(DIGITAL3, LOW); digitalWrite(DIGITAL6, LOW); digitalWrite(ANA0, LOW); } // set the interruption line down pinMode(MUX_RX, INPUT); digitalWrite(MUX_RX, LOW); }
void SmartMote::work(){ while(1){ /* se l'ora corrente è di scansione */ if(!(RTCC.hours() % 6) && (RTCC.minutes() >= 0x00 && RTCC.minutes() <= 0x05)){ AString data; AString message; AString answer; /* abilito il core timer (la prima volta è già abilitato) */ System::wakeCoreTimer(); /* accendo il led verde */ turnOnGreen(); /* accendo la seriale */ openUART(); /* accendo l'i2c */ openI2C(); /* prendo le misure */ data += mac(); data += luminosity(); data += ambientTempAndHum(); data += groundTemp(); data += groundHum(); data += battey(); /* compongo la stringa */ message += _PHP_CHUNK1; /* inserisco l'hostname */ message += getHost(); /* inserisco la seconda parte di richiesta http */ message += _PHP_CHUNK2; /* inserisco la lunghezza dei dati */ message += AString(static_cast<sint32>(data.size())); /* inserisco la terza parte di richiesta http */ message += _PHP_CHUNK3; /* inserisco i dati */ message += data; /* se fallisce l'inizializzazione dell'esp */ if(!m_net.initialize()){ /* notifico l'errore */ error(); } /* se fallisce l'avvio del dhcp */ if(!m_net.setDhcp(true)){ /* notifico l'errore */ error(); } /* se fallisce la connessione alla rete */ if(!m_net.joinAP(getSSID(), getKey())){ /* notifico l'errore */ error(); } /* se fallisce la connessione al server */ if(!m_net.connectToHost(getHost(), 80)){ /* notifico l'errore */ error(); } /* invio i dati */ m_net.send(message); /* aspetto l'ok */ m_net.waitForData(answer); /* notifico l'ok o lerrore dell'invio */ wasSuccess(answer); /* lascio l'ap */ m_net.leaveAP(); /* libero la memoria occupata dalle stringhe */ message.clear(); data.clear(); answer.clear(); } /* calcolo del tempo di sleep per il wifi */ m_net.sleep(getSleepTime()); /* punto la prossima sveglia */ setNextAlarm(); /* spengo il led verde */ turnOffGreen(); /* spengo l'uart */ closeUART(); /* chiudo l'i2c */ closeI2C(); /* spengo il core timer */ System::stopCoreTimer(); /* vado a dormire */ System::sleep(); } }