Go Down

Topic: Code not running with power supply - has to be uploaded everytime (Read 398 times) previous topic - next topic

kentclub

Hello,

I have an Mkrfox that turns a relay on/off depending on an ultrasonic sensor's value.

The problem is, the code only works if I click "upload" on the arduino IDE, meaning it doesn't work if I just power it with a battery or via USB.


I should also note just in case that each time I upload the code, I get the following error (couldn't find a fix for it, don't know if it's related to my problem) :



Atmel SMART device 0x10010005 found
Device       : ATSAMD21G18A
Chip ID      : 10010005
Version      : v2.0 [Arduino:XYZ] Mar 10 2017 12:20:17
Address      : 8192
Pages        : 3968
Page Size    : 64 bytes
Total Size   : 248KB
Planes       : 1
Lock Regions : 16
Locked       : none
Security     : false
Boot Flash   : true
BOD          : true
BOR          : true
Arduino      : FAST_CHIP_ERASE
Arduino      : FAST_MULTI_PAGE_WRITE
Arduino      : CAN_CHECKSUM_MEMORY_BUFFER
Erase flash
done in 0.583 seconds

Write 42320 bytes to flash (662 pages)

[==                            ] 9% (64/662 pages)
[=====                         ] 19% (128/662 pages)
[========                      ] 29% (192/662 pages)
[===========                   ] 38% (256/662 pages)
[==============                ] 48% (320/662 pages)
[=================             ] 58% (384/662 pages)
[====================          ] 67% (448/662 pages)
[=======================       ] 77% (512/662 pages)
[==========================    ] 87% (576/662 pages)
[============================= ] 96% (640/662 pages)
[==============================] 100% (662/662 pages)

SAM-BA operation failed
done in 0.260 seconds

Verify 42320 bytes of flash with checksum.
Verify successful
done in 0.058 seconds
CPU reset.
Une erreur est survenue lors du transfert du croquis
Erreur durant la définition des paramètres du port série: 9 600 N 8 1



And here is the Arduino Code :


Code: [Select]
#include <Event.h>
#include <Timer.h>
#include <ArduinoLowPower.h>
#include <SigFox.h>
#define O5 3 // Relay
#define UINT16_t_MAX  65536
#define INT16_t_MAX   UINT16_t_MAX/
#define TRIGGER_PIN 5
#define ECHO_PIN 4

int counter = 0; // Counter to avoid sending downlink request and uplink at the same time

char messageSigfox[8] = {'0', '0', '0', '0', '0', '0', '0', '0'};

//const byte TRIGGER_PIN = 5;
//const byte ECHO_PIN = 4;
const unsigned long MEASURE_TIMEOUT = 25000UL;
const float SOUND_SPEED = 340.0;

char message = ' ';
Timer t;

byte a = 0;
byte b = 0;
int waterLevel = 0;
const float WATER_LEVEL = 20; // Modifier la valeur afin de changer la limite d'eau

uint8_t surverse = 0;
uint8_t vanne = 0;
float temperature = 0;
byte distanceEau = 0;

int override = 0;

typedef struct __attribute__ ((packed)) sfxMessage {
  int16_t niv;
  int16_t temp;
  uint8_t surverse;
  uint8_t vanne;

} SigfoxMessageStruct;

void setup() {
  Serial.begin(9600);
  pinMode(TRIGGER_PIN, OUTPUT);
  digitalWrite(TRIGGER_PIN, LOW);
  pinMode(ECHO_PIN, INPUT);
  pinMode(O5, OUTPUT);
  digitalWrite(O5, LOW); // turn the relayoff
 
  t.every(6000,regulateWaterFlow);
  t.every(600000, sendEPStatus);
  t.every(21600000, askNewMessage);
 
  while (!Serial) {};
 
  if (!SigFox.begin()) {
    Serial.println("Shield error or not present!");
    return;
  }
  SigFox.debug();
}

void loop() {
 
  t.update();
  //askNewMessage();
  /* DEBUG */
}

void sendEPStatus() {

  if (counter < 35) {
    Serial.println("Method sendEPStatus() called");

    b = measureWaterLevel();
    distanceEau = (byte) b;

    Serial.print(F("Variable distanceEau = "));
    Serial.println(b);
   
    SFX_SendMessage(distanceEau, surverse, vanne);
    Serial.println("method SFX_SendMessage() ended");
    counter = counter + 1;
  }
  else {
    Serial.println("Sending only BIDIR Callback");
    counter = 0;
  }
}


//Measure the distance between ultrasound captor and water, than calculate water level
float measureWaterLevel() {
  float level = -1;
  while (level == -1){
    level = checkStabilityLevel();
  }
  Serial.println("level");
  int distance_cm = level*0.34/2;
  Serial.println(distance_cm);
  waterLevel = 215 - distance_cm;
  if (waterLevel < 0){
    waterLevel=0;
  }
  Serial.println(waterLevel);
  return waterLevel;
}

float checkStabilityLevel(){
  Serial.println("Initiating checkStabilityLevel()");

  float measure2 = measureLevel();
  Serial.println(measure2);
  delay(1000);
  float measure3 = measureLevel();
  Serial.println(measure3);
  delay(1000);
  float measure4 = measureLevel();
  Serial.println(measure4);

  long mean = (measure2+measure3+measure4)/3;
  long stdev = sqrt((sq(measure2-mean)+sq(measure3-mean)+sq(measure4-mean))/2);
  Serial.print("Stdev = ");
  Serial.println(stdev);

  if (stdev > 30){
    return -1;
  }
  else{
    return measure4;
  }
}

float measureLevel(){
 
  digitalWrite(TRIGGER_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGGER_PIN, LOW);

  //pinMode(ECHO_PIN, INPUT);
  float measure = pulseIn(ECHO_PIN, HIGH);
  return measure;
}

//Check water level, open valve if necessary and print out water level in cm.
void regulateWaterFlow() {
  a = measureWaterLevel();
  if (a > WATER_LEVEL) {
    digitalWrite(O5, HIGH); // turn the relay on
    Serial.println("Closing Valve");
  }
  else {
    if (override == 0) {
      digitalWrite(O5, LOW); // turn the relay off
      Serial.println("Opening Valve");
    }
  }
  Serial.print(F("Niveau d'eau: "));
  Serial.print(a);
  Serial.println(F("mm"));
}



void setOverrideZero() {
  override = 0;
  Serial.println("Override canceled");
}


void askNewMessage()
{
  distanceEau = (byte) measureWaterLevel();
  getNewMessage(distanceEau, surverse, vanne);
}


void SFX_SendMessage(byte distanceEauActuelle, byte etat_surverse, byte etat_vanne) {
 
  SigfoxMessageStruct message;
 
  // Start the module
  SigFox.begin();
  Serial.println("Sigfox begin");
  // Wait at least 30ms after first configuration (100ms before)
  delay(100);

  // Get parameters
  temperature = SigFox.internalTemperature();
  message.temp = temperature;
  message.niv = distanceEauActuelle;
  message.surverse = etat_surverse;
  message.vanne = etat_vanne;

  SigFox.status();
  delay(1);

  SigFox.beginPacket();
  SigFox.write(message);

  int ret = SigFox.endPacket(true);
  if (ret > 0) {
    Serial.println("No transmission");
  } else {
    Serial.println("Transmission ok");
  }

  SigFox.end();
  Serial.println("Sigfox end of transmission");
}


void getNewMessage(byte distanceEauActuelle, byte etat_surverse, byte etat_vanne)
{
  Serial.println("Fetching new message with Sigfox!");

  SigfoxMessageStruct message;
  Serial.println("Sigfox initialising message");

  // Start the module
  SigFox.begin();

  delay(100);
 
  SigFox.status();
  delay(1);

  // Get parameters
  temperature = SigFox.internalTemperature();
 
  message.niv = distanceEauActuelle;
  message.temp = temperature;
  message.surverse = etat_surverse;
  message.vanne = etat_vanne;


  SigFox.beginPacket();
  SigFox.write(message);

  int ret = SigFox.endPacket(true); //send buffer to SIGFOX network
 
  if (ret > 0) {
    Serial.println("No transmission");
  } else {
    Serial.println("Transmission ok");
  }


  if (SigFox.parsePacket()) {
    Serial.println("Response from server:");
    int compteur = 0;
   
    while (SigFox.available()) {
      char incomingChar = char(SigFox.read());
      if (compteur < 8 && incomingChar != ' ') {
        Serial.println(incomingChar);
        messageSigfox[compteur] = incomingChar;
      } else {
        SigFox.read();
      }
      compteur++;
    }
    if (messageSigfox[0] == '1') {
      digitalWrite(O5, HIGH);
      override = 1;
     String o (messageSigfox[1]);
      int j = o.toInt();
      j = j * 600000;
      int k = t.after(j, setOverrideZero);
    }
  } else {
    Serial.println("Could not get any response from the server");

  }
  Serial.println();

  SigFox.end();
}


sandeepmistry

Hi @kentclub,

Make sure you remove the following line, otherwise the sketch will wait for the IDE to open the serial port:

Code: [Select]

while (!Serial) {};

Go Up