Makerfabs / Lora-Soil-Moisture-Sensor

Lora Soil Moisture Sensor
50 stars 23 forks source link

the unit will not program #4

Open seamaster101 opened 2 years ago

seamaster101 commented 2 years ago

I can compile your sample, but when i try to upload the upload fails. I'm using this programmer: https://www.amazon.ca/DSD-TECH-Adapter-FT232RL-Compatible/dp/B07BBPX8B8 the programmer is set to 3.3V I tried connecting the DTR to the reset line on the programmer and to the DTR line on the programmer (it has pad an the board that I pulled out for that) in both cases the programming fails, but when the unit is powered I can see communication on the serial port:


Soil start. success! AHT10 acknowledged. success! ADC:888 ADC:894 ADC:894 BAT:1006 3.24VBAT:1006 3.24VBAT:1006 3.24VID010209 REPLY : SOIL INEDX:0 H:39.58 T:25.84 ADC:894 BAT:1006 [Set]Sleep Mode Set [Watch dog]0


etc. etc..... I have the board setup as Arduino UNO with processor ATmega328P here is the error message when i try to upload code:


Arduino: 1.8.19 (Windows Store 1.8.57.0) (Windows 10), Board: "Arduino Nano, ATmega328P"

Sketch uses 23508 bytes (76%) of program storage space. Maximum is 30720 bytes.

Global variables use 1028 bytes (50%) of dynamic memory, leaving 1020 bytes for local variables. Maximum is 2048 bytes.

avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x53

avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x6f

avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0x69

avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0x6c

avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x20

avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0x73

avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0x74

avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0x61

avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0x72

avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x74

An error occurred while uploading the sketch


please tell me what i'm doing wrong?

littlejogi commented 2 years ago

I select "Arduino Pro or Pro Mini" and ATMega328P (3,3V 8MHZ) and use https://www.amazon.de/gp/product/B01C2P9GD2/ref=ppx_yo_dt_b_asin_title_o03_s01?ie=UTF8&psc=1 Everything works fine. Sometimes i have some problems with the contact of the pins on the sensor. If i use a serial connector without DTR i have the same problem

roopesh commented 1 year ago

How did you compile this? I have a V3. I set the board settings to "Arduino Pro or Pro Mini" and "ATMega328P (3.3V 8MHZ)" (thank you! They shouldn't make it so hard to figure out) but I still get

/Users/*****/Documents/Arduino/Lora-Soil-Moisture-Sensor-V3/Lora-Soil-Moisture-Sensor-V3.ino: In function 'void Lora_init()':
Lora-Soil-Moisture-Sensor-V3:86:82: error: 'SX127X_SYNC_WORD' was not declared in this scope
     int state = radio.begin(FREQUENCY, BANDWIDTH, SPREADING_FACTOR, CODING_RATE, SX127X_SYNC_WORD, OUTPUT_POWER, PREAMBLE_LEN, GAIN);
                                                                                  ^~~~~~~~~~~~~~~~
/Users/*****/Documents/Arduino/Lora-Soil-Moisture-Sensor-V3/Lora-Soil-Moisture-Sensor-V3.ino:86:82: note: suggested alternative: 'RADIOLIB_SX127X_SYNC_WORD'
     int state = radio.begin(FREQUENCY, BANDWIDTH, SPREADING_FACTOR, CODING_RATE, SX127X_SYNC_WORD, OUTPUT_POWER, PREAMBLE_LEN, GAIN);
                                                                                  ^~~~~~~~~~~~~~~~
                                                                                  RADIOLIB_SX127X_SYNC_WORD
Lora-Soil-Moisture-Sensor-V3:87:18: error: 'ERR_NONE' was not declared in this scope
     if (state == ERR_NONE)
                  ^~~~~~~~
/Users/*****/Documents/Arduino/Lora-Soil-Moisture-Sensor-V3/Lora-Soil-Moisture-Sensor-V3.ino:87:18: note: suggested alternative: 'SKIP_NONE'
     if (state == ERR_NONE)
                  ^~~~~~~~
                  SKIP_NONE
exit status 1
'SX127X_SYNC_WORD' was not declared in this scope
roopesh commented 1 year ago

For anyone looking for a solution, I was able to fix it with the following code. Please be sure to read the comments and update the correct values.


#include <Wire.h>
#include <RadioLib.h>
#include <avr/wdt.h>
#include <avr/sleep.h>
#include "I2C_AHT10.h"

#define ERR_NONE 0

//#define NODENAME "LORA_POWER_1"

//IMPORTANT Uncomment out this line with the right ID on the case or on the side of the sensor
//String node_id = String("ID") + "010933";

//Set sleep time, when value is 1 almost sleep 20s,when value is 450, almost 1 hour.
#define SLEEP_CYCLE 450

//Lora set
//Set Lora frequency
//Commented out any frequency in this example to make you choose the relevant one for your sensor.
//IMPORTANT Uncomment out this line with the right Frequency.
//As an aside, I was able to get all of the sensor to broadcast at 915MHz (even the ones marked 434 and 868).
//#define FREQUENCY 434.0
//#define FREQUENCY 868.0
//#define FREQUENCY 915.0

//Nothing below this line should need to be touched.
#define BANDWIDTH 125.0
#define SPREADING_FACTOR 9
#define CODING_RATE 7
#define OUTPUT_POWER 10
#define PREAMBLE_LEN 8
#define GAIN 0

//328p
#define DIO0 2
#define DIO1 6

#define LORA_RST 4
#define LORA_CS 10

#define SPI_MOSI 11
#define SPI_MISO 12
#define SPI_SCK 13

//pin set
#define VOLTAGE_PIN A3
#define PWM_OUT_PIN 9
#define SENSOR_POWER_PIN 5
#define ADC_PIN A2

#define DEBUG_OUT_ENABLE 1

SX1276 radio = new Module(LORA_CS, DIO0, LORA_RST, DIO1);
AHT10 humiditySensor;

bool readSensorStatus = false;
int sensorValue = 0; // variable to store the value coming from the sensor
int batValue = 0;    // the voltage of battery
int count = 0;
int ADC_O_1;           // ADC Output First 8 bits
int ADC_O_2;           // ADC Output Next 2 bits
int16_t packetnum = 0; // packet counter, we increment per xmission
float temperature = 0.0;
float humidity = 0.0;

bool AHT_init()
{
    bool ret = false;
    Wire.begin();
    if (humiditySensor.begin() == false)
    {
#if DEBUG_OUT_ENABLE
        Serial.println("AHT10 not detected. Please check wiring. Freezing.");
#endif
    }

    if (humiditySensor.available() == true)
    {
        temperature = humiditySensor.getTemperature();
        humidity = humiditySensor.getHumidity();
        ret = true;
    }
    if (isnan(humidity) || isnan(temperature))
    {
#if DEBUG_OUT_ENABLE
        Serial.println(F("Failed to read from AHT sensor!"));
#endif
    }
    return ret;
}
void Lora_init()
{
    int state = radio.begin(FREQUENCY, BANDWIDTH, SPREADING_FACTOR, CODING_RATE, RADIOLIB_SX127X_SYNC_WORD, OUTPUT_POWER, PREAMBLE_LEN, GAIN);
    if (state == ERR_NONE)
    {
#if DEBUG_OUT_ENABLE
        Serial.println(F("success!"));
#endif
    }
    else
    {
#if DEBUG_OUT_ENABLE
        Serial.print(F("failed, code "));
        Serial.println(state);
#endif
        // while (true)
        //     ;
    }
}
void setup()
{
#if DEBUG_OUT_ENABLE
    Serial.begin(115200);
    Serial.println("Soil start.");
#endif
    delay(100);

    // set up Timer 1
    pinMode(PWM_OUT_PIN, OUTPUT);

    TCCR1A = bit(COM1A0);            // toggle OC1A on Compare Match
    TCCR1B = bit(WGM12) | bit(CS10); // CTC, scale to clock
    OCR1A = 1;

    pinMode(LORA_RST, OUTPUT);
    digitalWrite(LORA_RST, HIGH);
    delay(100);

    pinMode(SENSOR_POWER_PIN, OUTPUT);
    digitalWrite(SENSOR_POWER_PIN, HIGH); //Sensor power on
    delay(100);

    Lora_init();

    Wire.begin();
    if (humiditySensor.begin() == false)
    {

#if DEBUG_OUT_ENABLE
        Serial.println("AHT10 not detected. Please check wiring. Freezing.");
#endif
    }
#if DEBUG_OUT_ENABLE
    else
        Serial.println("AHT10 acknowledged.");
#endif

    do_some_work();
//setup over
#if DEBUG_OUT_ENABLE
    Serial.println("[Set]Sleep Mode Set");
#endif
    low_power_set();
}

void loop()
{
    wdt_disable();

    if (count > SLEEP_CYCLE) //(7+1) x 8S  450
    {
#if DEBUG_OUT_ENABLE
        //code start
        Serial.println("Code start>>");
#endif

        do_some_work();
        all_pins_low();

#if DEBUG_OUT_ENABLE
        //code end
        Serial.println("Code end<<");
#endif
        //count init
        count = 0;
    }

    low_power_set();
}

ISR(WDT_vect)
{
#if DEBUG_OUT_ENABLE
    Serial.print("[Watch dog]");
    Serial.println(count);
#endif
    delay(100);
    count++;
    //wdt_reset();
    wdt_disable(); // disable watchdog
}

//Set low power mode and into sleep
void low_power_set()
{
    all_pins_low();
    delay(10);
    // disable ADC
    ADCSRA = 0;

    sleep_enable();
    watchdog_init();
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);
    delay(10);
    noInterrupts();
    sleep_enable();

    // turn off brown-out enable in software
    MCUCR = bit(BODS) | bit(BODSE);
    MCUCR = bit(BODS);
    interrupts();

    sleep_cpu();
    sleep_disable();
}

//Enable watch dog
void watchdog_init()
{
    // clear various "reset" flags
    MCUSR = 0;
    // allow changes, disable reset
    WDTCSR = bit(WDCE) | bit(WDE);
    WDTCSR = bit(WDIE) | bit(WDP3) | bit(WDP0); // set WDIE, and 8 seconds delay
    wdt_reset();                                // pat the dog
}

void do_some_work()
{

    digitalWrite(SENSOR_POWER_PIN, HIGH); // Sensor/RF95 power on
    digitalWrite(LORA_RST, HIGH);
    delay(5);
    pinMode(PWM_OUT_PIN, OUTPUT);    //digitalWrite(PWM_OUT_PIN, LOW);
    TCCR1A = bit(COM1A0);            // toggle OC1A on Compare Match
    TCCR1B = bit(WGM12) | bit(CS10); // CTC, scale to clock
    OCR1A = 1;                       // compare A register value (5000 * clock speed / 1024).When OCR1A == 1, PWM is 2MHz

    Lora_init();
    delay(50);

    //ADC2  AVCC as reference voltage
    ADMUX = _BV(REFS0) | _BV(MUX1);

    //ADC2  internal 1.1V as ADC reference voltage
    //ADMUX = _BV(REFS1) |_BV(REFS0) | _BV(MUX1);

    // 8  分频
    ADCSRA = _BV(ADEN) | _BV(ADPS1) | _BV(ADPS0);
    delay(50);
    for (int i = 0; i < 3; i++)
    {
        //start ADC conversion
        ADCSRA |= (1 << ADSC);

        delay(10);

        if ((ADCSRA & 0x40) == 0)
        {
            ADC_O_1 = ADCL;
            ADC_O_2 = ADCH;

            sensorValue = (ADC_O_2 << 8) + ADC_O_1;
            ADCSRA |= 0x40;
#if DEBUG_OUT_ENABLE
            Serial.print("ADC:");
            Serial.println(sensorValue);
#endif

            if (readSensorStatus == false)
                readSensorStatus = AHT_init();
        }
        ADCSRA |= (1 << ADIF); //reset as required
        delay(50);
    }

    //ADC3  internal 1.1V as ADC reference voltage
    ADMUX = _BV(REFS1) | _BV(REFS0) | _BV(MUX1) | _BV(MUX0);

    delay(50);
    for (int i = 0; i < 3; i++)
    {
        //start ADC conversion
        ADCSRA |= (1 << ADSC);

        delay(10);

        if ((ADCSRA & 0x40) == 0)
        {
            ADC_O_1 = ADCL;
            ADC_O_2 = ADCH;

            batValue = (ADC_O_2 << 8) + ADC_O_1;
            ADCSRA |= 0x40;
#if DEBUG_OUT_ENABLE
            Serial.print("BAT:");
            Serial.println(batValue);
            float bat = (float)batValue * 3.3;
            bat = bat / 1024.0;
            Serial.print(bat);
            Serial.print("V");
#endif
        }
        ADCSRA |= (1 << ADIF); //reset as required
        delay(50);
    }
    send_lora();
    delay(1000);
    radio.sleep();

    packetnum++;
    readSensorStatus = false;
    digitalWrite(SENSOR_POWER_PIN, LOW); // Sensor/RF95 power off
    delay(100);
}

void all_pins_low()
{
    pinMode(PWM_OUT_PIN, INPUT);
    pinMode(A4, INPUT_PULLUP);
    pinMode(A5, INPUT_PULLUP);

    delay(50);
}

void send_lora()
{
    String message = "INEDX:" + (String)packetnum + " H:" + (String)humidity + " T:" + (String)temperature + " ADC:" + (String)sensorValue + " BAT:" + (String)batValue;
    String back_str = node_id + " REPLY : SOIL " + message;

#if DEBUG_OUT_ENABLE
    Serial.println(back_str);
#endif

    radio.transmit(back_str);
}````
fredmccr commented 5 months ago

Thank you roopesh for your code. Unfortunately, I get the following error message while trying to compile it:

"text section exceeds available space in board Sketch uses 31062 bytes (101%) of program storage space. Maximum is 30720 bytes"

I first tried to remove all the comments lines, hoping it will lighten the weight of the code .. without success. I finally managed to get rid of this by commenting a few "serial.println..." lines that are relevant for debugging purposes only. Compilation and programming are ok now. I hope this could help those with the same issue.

roopesh commented 5 months ago

I first tried to remove all the comments lines, hoping it will lighten the weight of the code .. without success. I finally managed to get rid of this by commenting a few "serial.println..." lines that are relevant for debugging purposes only. Compilation and programming are ok now. I hope this could help those with the same issue.

hahaha I didn't have that problem, but yeah remove all my extra dumb logging. I am an Arduino newbie so I needed lots of extra help.