Heltec-Aaron-Lee / WiFi_Kit_series

Arduino source codes and toolchain for WiFi_Kit_series made by HelTecAutomation.
GNU Lesser General Public License v2.1
783 stars 310 forks source link

Board ressetting when using pull-ups on LM75B #93

Open EHR11 opened 5 years ago

EHR11 commented 5 years ago

Hello,

I'm trying to build a LoRa temperature probe using a NXP LM75BD, connected via I2C to the board... thing is, when doing a pull-up connection (Specifically in the Overtemperature Shutdown pin), the board self resets constantly in a loop.

I'm not using the OLED Display, so I guess that conflict is out of the question. Not using pull-ups seems to work, so I guess the issue has to do with some sort of internal pull-ups.

Have you got any ideas as what may be happening? Below there's the code that I'm working with (based on ttn-abp example of LMiC library)

#include <LoRa.h>

/*******************************************************************************
 * Copyright (c) 2015 Thomas Telkamp and Matthijs Kooijman
 *
 * PermiserialGPSion is hereby granted, free of charge, to anyone
 * obtaining a copy of this document and accompanying files,
 * to do whatever they want with them without any restriction,
 * including, but not limited to, copying, modification and redistribution.
 * NO WARRANTY OF ANY KIND IS PROVIDED.
 *
 * This example sends a valid LoRaWAN packet with payload "Hello,
 * world!", using frequency and encryption settings matching those of
 * the The Things Network.
 *
 * This uses ABP (Activation-by-personalisation), where a DevAddr and
 * SeserialGPSion keys are preconfigured (unlike OTAA, where a DevEUI and
 * application key is configured, while the DevAddr and seserialGPSion keys are
 * aserialGPSigned/generated in the over-the-air-activation procedure).
 *
 * Note: LoRaWAN per sub-band duty-cycle limitation is enforced (1% in
 * g1, 0.1% in g2), but not the TTN fair usage policy (which is probably
 * violated by this sketch when left running for longer)!
 *
 * To use this sketch, first register your application and device with
 * the things network, to set or generate a DevAddr, NwkSKey and
 * AppSKey. Each device should have their own unique values for these
 * fields.
 *
 * Do not forget to define the radio type correctly in config.h.
 *
 *******************************************************************************/

#include <lmic.h>
#include <hal/hal.h>
#include <SPI.h>
#include <Wire.h>
#include <Temperature_LM75_Derived.h>
//#include <TinyGPS++.h> 
//#include <HardwareSerial.h>
NXP_LM75B temperature;
// LoRaWAN NwkSKey, network seserialGPSion key
// This is the default Semtech key, which is used by the prototype TTN
// network initially.
static const PROGMEM u1_t NWKSKEY[16] = { 0xB0, 0x04, 0x88, 0xCD, 0x44, 0x09, 0x95, 0x4F, 0x3A, 0xBF, 0x67, 0xE2, 0xA1, 0xAE, 0xFA, 0xDE };

// LoRaWAN AppSKey, application seserialGPSion key
// This is the default Semtech key, which is used by the prototype TTN
// network initially.
static const u1_t PROGMEM APPSKEY[16] = { 0x89, 0x11, 0x7F, 0x2A, 0xBC, 0xFB, 0x0A, 0x05, 0xE4, 0x98, 0x88, 0x15, 0x7E, 0x8B, 0x0A, 0x6E };

// LoRaWAN end-device addreserialGPS (DevAddr)
// See http://thethingsnetwork.org/wiki/AddreserialGPSSpace
static const u4_t DEVADDR = 0x260112A9; // <-- Change this addreserialGPS for every n

// These callbacks are only used in over-the-air activation, so they are
// left empty here (we cannot leave them out completely unleserialGPS
// DISABLE_JOIN is set in config.h, otherwise the linker will complain).
void os_getArtEui (u1_t* buf) { }
void os_getDevEui (u1_t* buf) { }
void os_getDevKey (u1_t* buf) { }

static unsigned char mydata[120];
static int largo=0;
int outputValue = 0;
int sensorValue = 0;

static osjob_t sendjob;
// Schedule TX every this many seconds (might become longer due to duty
// cycle limitations).
const unsigned TX_INTERVAL = 10;

// Pin mapping
const lmic_pinmap lmic_pins = {
    .nss = 18,
    .rxtx = LMIC_UNUSED_PIN,
    .rst = 14,
    .dio = {26, 33, 32},
};

// TinyGPSPlus gps;
// HardwareSerial serialGPS(1);

void onEvent (ev_t ev) {
    Serial.print(os_getTime());
    Serial.print(": ");
    switch(ev) {
        case EV_SCAN_TIMEOUT:
            Serial.println(F("EV_SCAN_TIMEOUT"));
            break;
        case EV_BEACON_FOUND:
            Serial.println(F("EV_BEACON_FOUND"));
            break;
        case EV_BEACON_MISSED:
            Serial.println(F("EV_BEACON_MIserialGPSED"));
            break;
        case EV_BEACON_TRACKED:
            Serial.println(F("EV_BEACON_TRACKED"));
            break;
        case EV_JOINING:
            Serial.println(F("EV_JOINING"));
            break;
        case EV_JOINED:
            Serial.println(F("EV_JOINED"));
            break;
        case EV_RFU1:
            Serial.println(F("EV_RFU1"));
            break;
        case EV_JOIN_FAILED:
            Serial.println(F("EV_JOIN_FAILED"));
            break;
        case EV_REJOIN_FAILED:
            Serial.println(F("EV_REJOIN_FAILED"));
            break;
        case EV_TXCOMPLETE:
            Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
            digitalWrite(13, HIGH);
            if (LMIC.txrxFlags & TXRX_ACK)
              Serial.println(F("Received ack"));
            if (LMIC.dataLen) {
              Serial.print(F("Received "));
              Serial.print(LMIC.dataLen);
              Serial.println(F(" bytes of payload"));
              for (int i = 0; i < LMIC.dataLen; i++) {
              if (LMIC.frame[LMIC.dataBeg + i] < 0x10) {
                  Serial.print(F("0"));
              }
            Serial.write(LMIC.frame[LMIC.dataBeg + i]);
            }
            Serial.println();
            }
            // Schedule next transmiserialGPSion
            os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
            break;
        case EV_LOST_TSYNC:
            Serial.println(F("EV_LOST_TSYNC"));
            break;
        case EV_RESET:
            Serial.println(F("EV_RESET"));
            break;
        case EV_RXCOMPLETE:
            // data received in ping slot
            Serial.println(F("EV_RXCOMPLETE"));
            break;
        case EV_LINK_DEAD:
            Serial.println(F("EV_LINK_DEAD"));
            break;
        case EV_LINK_ALIVE:
            Serial.println(F("EV_LINK_ALIVE"));
            break;
         default:
            Serial.println(F("Unknown event"));
            break;
    }
}

void do_send(osjob_t* j){
    // Check if there is not a current TX/RX job running
    if (LMIC.opmode & OP_TXRXPEND) {
        Serial.println(F("OP_TXRXPEND, not sending"));
    } else {
        // Prepare upstream data transmiserialGPSion at the next poserialGPSible time.
        digitalWrite(13, LOW);
        Serial.println(largo);
        LMIC_setTxData2(1, mydata, largo, 0);
        Serial.println(F("Packet queued"));
    }
    // Next TX is scheduled after TX_COMPLETE event.
}

//void getGPS(){
//  bool newData = false;
// //Datos_t datos;
//  unsigned short sentences, failed;
//
//  // For one second we parse GPS data and report some key values
//
//    while (serialGPS.available()) {
//        if(gps.encode(serialGPS.read())) {
//             //gps.f_get_position(&latitude, &longitude);
//             //gps.crack_datetime(&year,&month,&day,&hour,&minute,&second,&hundredths);
//             //StaticJsonBuffer<200> jsonBuffer;
//             char strlatitude[10];
//             char strlongitude[10];
//             char strvelocidad[10];
//             char strcurso[10];
//             memset(strlatitude,0,10);
//             memset(strlongitude,0,10);
//             memset(strvelocidad,0,10);
//             memset(strcurso,0,10);
//             dtostrf(gps.location.lat(),7,5,strlatitude);
//             dtostrf(gps.location.lng(),7,5,strlongitude); 
//             dtostrf(gps.speed.kmph(),5,2,strvelocidad);
//             dtostrf(gps.course.deg(),5,2,strcurso);
//             char miJson[120];
//             memset(miJson,0,120);
//             largo=(sprintf(miJson,"{\"lat\":%s,\"lon\":%s}",strlatitude,strlongitude));
//             memcpy(mydata,miJson,largo);
//
//            }
//        }
//        //Serial.println("Leyendo...");
////        Serial.write(mydata,largo);
////        Serial.println();
//    }

void setup() {
    //SPI.begin(/*sck*/ 5, /*miso*/ 19, /*mosi*/ 27, /*ss*/ 18);
    Wire.begin(); 
    Serial.begin(115200);

    Serial.println(F("Starting"));
    //serialGPS.begin(9600, SERIAL_8N1, 16, 17)
    pinMode(13,OUTPUT);
    pinMode(12,INPUT_PULLUP);
//    pinMode(21,INPUT_PULLUP);
    pinMode(35,INPUT);

    temperature.setAlertActiveHigh();
    // At what temperature should ALERT be enabled when the temperature is
    // increasing? Chosen so that touching the sensor can raise the temperature
    // enough to enable ALERT.
     temperature.setTemperatureHighC(-5.0);

    // At what temperature should ALERT be disabled when the temperature is
    // decreasing (to provided hysteresis and prevent rapid cycling of the
    // ALERT pin)?
    temperature.setTemperatureLowC(0.0);

    #ifdef VCC_ENABLE
    // For Pinoccio Scout boards
    pinMode(VCC_ENABLE, OUTPUT);
    digitalWrite(VCC_ENABLE, HIGH);
    delay(1000);
    #endif
    Serial.println ("PASA POR ACA");

    // LMIC init
    os_init();

    Serial.println ("PASA POR ACA");
    // Reset the MAC state. Session and pending data transfers will be discarded.
    LMIC_reset();
    //LMIC_setAdrMode(1);

    // Set static session parameters. Instead of dynamically establishing a session
    // by joining the network, precomputed session parameters are be provided.
    #ifdef PROGMEM
    // On AVR, these values are stored in flash and only copied to RAM
    // once. Copy them to a temporary buffer here, LMIC_setSession will
    // copy them into a buffer of its own again.
    uint8_t appskey[sizeof(APPSKEY)];
    uint8_t nwkskey[sizeof(NWKSKEY)];
    memcpy_P(appskey, APPSKEY, sizeof(APPSKEY));
    memcpy_P(nwkskey, NWKSKEY, sizeof(NWKSKEY));
    LMIC_setSession (0x1, DEVADDR, nwkskey, appskey);
    #else
    // If not running an AVR with PROGMEM, just use the arrays directly
    LMIC_setSession (0x1, DEVADDR, NWKSKEY, APPSKEY);
    #endif

    #if defined(CFG_eu868)
    // Set up the channels used by the Things Network, which corresponds
    // to the defaults of most gateways. Without this, only three base
    // channels from the LoRaWAN specification are used, which certainly
    // works, so it is good for debugging, but can overload those
    // frequencies, so be sure to configure the full frequency range of
    // your network here (unless your network autoconfigures them).
    // Setting up channels should happen after LMIC_setSession, as that
    // configures the minimal channel set.
    // NA-US channels 0-71 are configured automatically
    LMIC_setupChannel(0, 868100000, DR_RANGE_MAP(DR_SF10, DR_SF7),  BAND_CENTI);      // g-band
//    LMIC_setupChannel(1, 868300000, DR_RANGE_MAP(DR_SF10, DR_SF7), BAND_CENTI);      // g-band
//    LMIC_setupChannel(2, 868500000, DR_RANGE_MAP(DR_SF10, DR_SF7),  BAND_CENTI);      // g-band
//    LMIC_setupChannel(3, 868700000, DR_RANGE_MAP(DR_SF10, DR_SF7),  BAND_CENTI);      // g-band
//    LMIC_setupChannel(4, 868900000, DR_RANGE_MAP(DR_SF10, DR_SF7),  BAND_CENTI);      // g-band
//    LMIC_setupChannel(5, 869100000, DR_RANGE_MAP(DR_SF10, DR_SF7),  BAND_CENTI);      // g-band
//    LMIC_setupChannel(6, 869300000, DR_RANGE_MAP(DR_SF10, DR_SF7),  BAND_CENTI);      // g-band
//    LMIC_setupChannel(7, 869500000, DR_RANGE_MAP(DR_SF10, DR_SF7),  BAND_CENTI);      // g-band
//    LMIC_setupChannel(8, 868800000, DR_RANGE_MAP(DR_FSK,  DR_FSK),  BAND_MILLI);      // g2-band
    // TTN defines an additional channel at 869.525Mhz using SF9 for class B
    // devices' ping slots. LMIC does not have an easy way to define set this
    // frequency and support for class B is spotty and untested, so this
    // frequency is not configured here.
    #elif defined(CFG_us915)
    // NA-US channels 0-71 are configured automatically
    // but only one group of 8 should (a subband) should be active
    // TTN recommends the second sub band, 1 in a zero based count.
    // https://github.com/TheThingsNetwork/gateway-conf/blob/master/US-global_conf.json
    LMIC_selectSubBand(1);
    #endif
    LMIC.txpow=30;
    // Disable link check validation
    LMIC_setLinkCheckMode(0);

    // TTN uses SF9 for its RX2 window.

    // Set data rate and transmit power for uplink (note: txpow seems to be ignored by the library)
    LMIC_setDrTxpow(DR_SF8,14);

    // Start job
    memset(mydata,0,120);
    do_send(&sendjob);
}

void loop() {
    //getGPS();
    Serial.print(temperature.readTemperatureC());
    Serial.print(": ");
    Serial.println(digitalRead(12) ? "HIGH" : "LOW");
    //Serial.print(temperature.readTemperatureC());
    char miJson[120];
    memset(miJson,0,120);
    sensorValue=analogRead(35);
    outputValue=map(sensorValue,0,1023,100,0);
    //Serial.println(temperature.temp());
    //largo=(sprintf(miJson,"{\"dev\":\"%x\",\"temp\":%f,\"lvl\":%d}",DEVADDR,temperature.temp(),outputValue));
    memcpy(mydata,miJson,largo);
    //Serial.println(" C");
    os_runloop_once();
    delay(100);
}
Heltec-Aaron-Lee commented 5 years ago

@EHR11 You board rebooting all the time? I think you had chosen an error board on the Arduino board manager. Maybe you have the same situation as this guy? http://community.heltec.cn/t/byteduino-hardware-cosigner-wallet-crashing/102