help with atoi code

Status
Not open for further replies.

Nick1802

Well-known member
hey all,

im having a lil issue with this code im using.

Code:
int posmin = readString.indexOf("min=");
  if (posmin > 0) {
    readString.substring(posmin + 4).toCharArray(bufmin, 3);
    tmin = atoi(bufmin);
  }

everything is working but tmin = atoi(bufmin); part.
i have done serial print for bufmin and is correct but tmin always stays 0.
this bit of code is used 15 times all but this one is working.
(also having server lockup and watchdog not working issues but i haven't tried to work out the issue there yet)

the code is in 9 tabs and quite a few htm pages. (attached)

main tab and tab with issue are below.

home_automation_sd
Code:
/*pins used
  serial1  pins 0,1
  read1    pin  2
  read2    pin  3
  network  pins 4,8,9,10,11,12,13
  rely con pins 5,6,7,15
  input sw pins 17,18,19
  dmx      pin  20
  garage   pins 21,22,23

  avalable pins 14,16,a14
*/
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
#include <DmxSimple.h>
#include <Bounce.h>
#include <EEPROM.h>
#include <TimeLib.h>
#define PRREG(x)

byte mac[] = { 0x04, 0xE9, 0xE5, 0x01, 0x05, 0x2D };
byte ip[] = { 192, 168, 0, 17 };
byte gateway[] = { 192, 168, 0, 1 };
byte subnet[] = { 255, 255, 255, 192 };
EthernetServer server(80);  // create a server at port 80
File webFile;               // the web page file on the SD card

const byte read1 = 2;
const byte read2 = 3;
const byte clockPin = 7;
const byte latchPin = 5;
const byte dataPin = 6;
const byte en = 15;
const byte reciverC = 17;
const byte reciverB = 18;
const byte reciverA = 19;
const byte dmx = 20;
const byte gd = 21;
const byte gs = 22;
const byte gu = 23;

byte i = 0;
byte save = 0;
byte light01 = 0;
byte light02 = 0;
byte light03 = 0;
byte light04 = 0;
byte light05 = 0;
byte light06 = 0;
byte light07 = 0;
byte light08 = 0;
byte light09 = 0;
byte light10 = 0;
byte light11 = 0;
byte light12 = 0;
byte light13 = 0;
byte light14 = 0;
byte light15 = 0;
byte light16 = 0;
byte light17 = 0;
byte light18 = 0;
byte power01 = 0;
byte power02 = 0;
byte power03 = 0;
byte power04 = 0;
byte power05 = 0;
byte power06 = 0;
byte power07 = 0;
byte power08 = 0;
byte power09 = 0;
byte power10 = 0;
byte power11 = 0;
byte power12 = 0;
byte power13 = 0;
byte power14 = 0;
byte power15 = 0;
byte power16 = 0;
byte relayByte1;
byte relayByte2;
byte newlight01;
byte newlight02;
byte newlight03;
byte newlight04;
byte newlight05;
byte newlight06;
byte newlight07;
byte newlight08;
byte newlight09;
byte newlight10;
byte newlight11;
byte newlight12;
byte newlight13;
byte newlight14;
byte newlight15;
byte newlight16;
byte newlight17;
byte newlight18;
byte am1;
byte ah1;
byte ae1;
byte am2;
byte ah2;
byte ae2;
byte am3;
byte ah3;
byte ae3;
byte am4;
byte ah4;
byte ae4;
byte am5;
byte ah5;
byte ae5;
byte netlight01 = 0;
byte netlight02 = 0;
byte netlight03 = 0;
byte netlight04 = 0;
byte netlight05 = 0;
byte netlight06 = 0;
byte netlight07 = 0;
byte netlight08 = 0;
byte netlight09 = 0;
byte netlight10 = 0;
byte netlight11 = 0;
byte netlight12 = 0;
byte netlight13 = 0;
byte netlight14 = 0;
byte netlight15 = 0;
byte netlight16 = 0;
byte netlight17 = 0;
byte netlight18 = 0;
byte blind01 = 0;
byte blind02 = 0;
byte blind03 = 0;
byte blind04 = 0;
byte blind05 = 0;
byte blind06 = 0;
byte blind07 = 0;
byte blind08 = 0;
byte blind09 = 0;
byte blind10 = 0;
byte blind11 = 0;
unsigned long currenttime01;
unsigned long looptime01;
unsigned long currenttime02;
unsigned long looptime02;
unsigned long currenttime03;
unsigned long looptime03;
unsigned long currenttime04;
unsigned long looptime04;
unsigned long currenttime05;
unsigned long looptime05;
unsigned long currenttime06;
unsigned long looptime06;
unsigned long currenttime07;
unsigned long looptime07;
unsigned long currenttime08;
unsigned long looptime08;
unsigned long currenttime09;
unsigned long looptime09;
unsigned long currenttime10;
unsigned long looptime10;
unsigned long currenttime11;
unsigned long looptime11;
unsigned long currenttime12;
unsigned long looptime12;
unsigned long currenttime13;
unsigned long looptime13;
unsigned long currenttime14;
unsigned long looptime14;
unsigned long currenttime15;
unsigned long looptime15;
unsigned long currenttime16;
unsigned long looptime16;
unsigned long currenttime17;
unsigned long looptime17;
unsigned long currenttime18;
unsigned long looptime18;
unsigned long alarmloop;
unsigned long alarmtime;
int loopeeprom;
int eepromtime;
unsigned long loopeeprom1;
unsigned long eepromtime1;
int garage = 0;
unsigned long loopgarage;
unsigned long garagetime;
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
char bufhour[2];
char bufmin[2];
char bufyear[2];
char bufmonth[2];
char bufday[2];
char bufah1[2];
char bufam1[2];
char bufah2[2];
char bufam2[2];
char bufah3[2];
char bufam3[2];
char bufah4[2];
char bufam4[2];
char bufah5[2];
char bufam5[2];
int thour;
int tmin;
int tyear;
int tmonth;
int tday;

Bounce button01 = Bounce(read1, 5);
Bounce button02 = Bounce(read1, 5);
Bounce button03 = Bounce(read1, 5);
Bounce button04 = Bounce(read1, 5);
Bounce button05 = Bounce(read1, 5);
Bounce button06 = Bounce(read1, 5);
Bounce button07 = Bounce(read1, 5);
Bounce button08 = Bounce(read1, 5);
Bounce button09 = Bounce(read2, 5);
Bounce button10 = Bounce(read2, 5);
Bounce button11 = Bounce(read2, 5);
Bounce button12 = Bounce(read2, 5);
Bounce button13 = Bounce(read2, 5);
Bounce button14 = Bounce(read2, 5);
Bounce button15 = Bounce(read2, 5);
Bounce button16 = Bounce(read2, 5);

String readString;

void setup() {
  pinMode(en, OUTPUT);      //relay disable
  digitalWrite(en, HIGH);
  pinMode(9, OUTPUT);       //begin reset WIZ820io
  digitalWrite(9, LOW);
  pinMode(10, OUTPUT);      //disable Ethernet chip
  digitalWrite(10, HIGH);
  pinMode(4, OUTPUT);       //sd disable
  digitalWrite(4, HIGH);
  digitalWrite(9, HIGH);    //end reset pulse

  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.begin(115200);
  Serial1.begin(2400);
  Serial1.transmitterEnable(14);
  DmxSimple.usePin(20);
  DmxSimple.maxChannel(20);
  pinMode(gd, OUTPUT);
  pinMode(gs, OUTPUT);
  pinMode(gu, OUTPUT);
  pinMode(read1, INPUT);
  pinMode(read2, INPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  digitalWrite(gd, LOW);
  digitalWrite(gs, LOW);
  digitalWrite(gu, LOW);
  digitalWrite(clockPin, LOW);
  digitalWrite(latchPin, LOW);
  digitalWrite(dataPin, LOW);
  digitalWrite(en, LOW);
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());

  relayByte1 = EEPROM.read(0);
  relayByte2 = EEPROM.read(1);
  newlight01 = EEPROM.read(2);
  newlight02 = EEPROM.read(3);
  newlight03 = EEPROM.read(4);
  newlight04 = EEPROM.read(5);
  newlight05 = EEPROM.read(6);
  newlight06 = EEPROM.read(7);
  newlight07 = EEPROM.read(8);
  newlight08 = EEPROM.read(9);
  newlight09 = EEPROM.read(10);
  newlight10 = EEPROM.read(11);
  newlight11 = EEPROM.read(12);
  newlight12 = EEPROM.read(13);
  newlight13 = EEPROM.read(14);
  newlight14 = EEPROM.read(15);
  newlight15 = EEPROM.read(16);
  newlight16 = EEPROM.read(17);
  newlight17 = EEPROM.read(18);
  newlight18 = EEPROM.read(19);
  am1 = EEPROM.read(20);
  ah1 = EEPROM.read(21);
  ae1 = EEPROM.read(22);
  am2 = EEPROM.read(23);
  ah2 = EEPROM.read(24);
  ae2 = EEPROM.read(25);
  am3 = EEPROM.read(26);
  ah3 = EEPROM.read(27);
  ae3 = EEPROM.read(28);
  am4 = EEPROM.read(29);
  ah4 = EEPROM.read(30);
  ae4 = EEPROM.read(31);
  am5 = EEPROM.read(32);
  ah5 = EEPROM.read(33);
  ae5 = EEPROM.read(34);
  shiftOut(dataPin, clockPin, relayByte2);
  shiftOut(dataPin, clockPin, relayByte1);
  dim();

  // initialize SD card
  if (!SD.begin(4)) {
    return;    // init failed
  }
  /*  KickDog();
    PRREG(WDOG_STCTRLH);
    PRREG(WDOG_PRESC);
    PRREG(WDOG_TOVALL);
    PRREG(WDOG_TOVALH);
    PRREG(WDOG_RSTCNT);
    for (int i = 1; i <= 5; i++) {
      KickDog();
      delay(1000);
    }*/
  setTime(13, 0, 0, 2, 9, 2017); //hour,min,sec,day,month,year
}

void loop() {
  EthernetClient client = server.available();  // try to get client

  if (client) {  // got client?
    while (client.connected()) {
      if (client.available()) {   // client data available to read
        char c = client.read();
        if (readString.length() < 100) {
          readString += c;
          Serial.print(c);
        }

        // last line of client request is blank and ends with \n
        // respond to client only after last line received
        if (c == '\n') {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          // remainder of header follows below, depending on if
          // web page or XML page is requested
          // Ajax request - send XML file
          if (readString.indexOf("ajax_lights") > 0) {
            // send rest of HTTP header
            client.println("Content-Type: text/xml");
            client.println("Connection: keep-alive");
            client.println();
            // send XML file containing input states
            XML_response(client);
          }

          else if (readString.indexOf("ajax_relays") > 0) {
            // send rest of HTTP header
            client.println("Content-Type: text/xml");
            client.println("Connection: keep-alive");
            client.println();
            // send XML file containing input states
            XML_relays(client);
          }

          else if (readString.indexOf("ajax_timeserver") > 0) {
            // send rest of HTTP header
            client.println("Content-Type: text/xml");
            client.println("Connection: keep-alive");
            client.println();
            // send XML file containing input states
            XML_timeserver(client);
          }

          else if (readString.indexOf("ajax_alarm") > 0) {
            // send rest of HTTP header
            client.println("Content-Type: text/xml");
            client.println("Connection: keep-alive");
            client.println();
            // send XML file containing input states
            XML_alarm(client);
          }

          else {  // web page request
            // send rest of HTTP header
            client.println("Content-Type: text/html");
            client.println("Connection: keep-alive");
            client.println();
            // send web page                                                    **************add pages here***************
            if (readString.indexOf("/index.htm") > 0) {
              webFile = SD.open("index.htm");        // open web page file
            }
            else if (readString.indexOf("/master.htm") > 0) {
              webFile = SD.open("master.htm");        // open web page file
            }
            else if (readString.indexOf("/outside.htm") > 0) {
              webFile = SD.open("outside.htm");        // open web page file
            }
            else if (readString.indexOf("/hallway.htm") > 0) {
              webFile = SD.open("hallway.htm");        // open web page file
            }
            else if (readString.indexOf("/workroom.htm") > 0) {
              webFile = SD.open("workroom.htm");        // open web page file
            }
            else if (readString.indexOf("/media.htm") > 0) {
              webFile = SD.open("media.htm");        // open web page file
            }
            else if (readString.indexOf("/gym.htm") > 0) {
              webFile = SD.open("gym.htm");        // open web page file
            }
            else if (readString.indexOf("/bathroom.htm") > 0) {
              webFile = SD.open("bathroom.htm");        // open web page file
            }
            else if (readString.indexOf("/bed2.htm") > 0) {
              webFile = SD.open("bed2.htm");        // open web page file
            }
            else if (readString.indexOf("/living.htm") > 0) {
              webFile = SD.open("living.htm");        // open web page file
            }
            else if (readString.indexOf("/blinds.htm") > 0) {
              webFile = SD.open("blinds.htm");        // open web page file
            }
            else if (readString.indexOf("/relays.htm") > 0) {
              webFile = SD.open("relays.htm");        // open web page file
            }
            else if (readString.indexOf("/time.htm") > 0) {
              webFile = SD.open("time.htm");        // open web page file
            }
            else if (readString.indexOf("/alarm1.htm") > 0) {
              webFile = SD.open("alarm1.htm");        // open web page file
            }
            else if (readString.indexOf("/alarm2.htm") > 0) {
              webFile = SD.open("alarm2.htm");        // open web page file
            }
            else if (readString.indexOf("/alarm3.htm") > 0) {
              webFile = SD.open("alarm3.htm");        // open web page file
            }
            else if (readString.indexOf("/alarm4.htm") > 0) {
              webFile = SD.open("alarm4.htm");        // open web page file
            }
            else if (readString.indexOf("/alarm5.htm") > 0) {
              webFile = SD.open("alarm5.htm");        // open web page file
            }

            // send web page to client                                        *******************************************
            if (webFile) {
              while (webFile.available()) {
                client.write(webFile.read());
              }
              webFile.close();
            }
          }
          // reset buffer index and all buffer elements to 0
          break;
        } //end if (c == '\n')
      } // end if (client.available())
    } // end while (client.connected())
    delay(1);      // give the web browser time to receive the data
    client.stop(); // close the connection
  } // end if (client)

  DmxSimple.write(1, light01);
  DmxSimple.write(2, light02);
  DmxSimple.write(3, light03);
  DmxSimple.write(4, light04);
  DmxSimple.write(5, light05);
  DmxSimple.write(6, light06);
  DmxSimple.write(7, light07);
  DmxSimple.write(8, light08);
  DmxSimple.write(9, light09);
  DmxSimple.write(10, light10);
  DmxSimple.write(11, light11);
  DmxSimple.write(12, light12);
  DmxSimple.write(13, light13);
  DmxSimple.write(14, light14);
  DmxSimple.write(15, light15);
  DmxSimple.write(16, light16);
  DmxSimple.write(17, light17);
  DmxSimple.write(18, light18);
  dim();
  eeprom();
  //  KickDog();

  eepromtime1 = millis();
  if (eepromtime1 - loopeeprom1 > 86400000) {
    if (readString.indexOf("?save=1") > 0) {
      save = 1;
      loopeeprom1 = eepromtime1;
    }
  }
  if (readString.indexOf("?light") > 0) {
    light();
  }

  else if (readString.indexOf("alarm") > 0) {
    alarm();
  }

  else if (readString.indexOf("?relay") > 0) {
    relay();
  }
  else if ((readString.indexOf("?blind") > 0 ) || (readString.indexOf("?pair") > 0) || (readString.indexOf("?unpair") > 0)) {
    blinds();
  }
  else if (readString.indexOf("?time") > 0 ) {
    systime();
  }

  readString = "0";

  if ((ae1 == 1) && (hour() == ah1) && (minute() == am1) && (second() <= 8)) {
    alarm1();
  }
  if ((ae2 == 1) && (hour() == ah2) && (minute() == am2) && (second() <= 8)) {
    alarm2();
  }
  if ((ae3 == 1) && (hour() == ah3) && (minute() == am3) && (second() <= 8)) {
    alarm3();
  }
  if ((ae4 == 1) && (hour() == ah4) && (minute() == am4) && (second() <= 8)) {
    alarm4();
  }
  if ((ae5 == 1) && (hour() == ah5) && (minute() == am5) && (second() <= 8)) {
    alarm5();
  }
}
alarms
Code:
void systime() {
  int poshour = readString.indexOf("hour=");
  if (poshour > 0) {
    readString.substring(poshour + 5).toCharArray(bufhour, 3);
    thour = atoi(bufhour);
  }

  int posmin = readString.indexOf("min=");
  if (posmin > 0) {
    readString.substring(posmin + 4).toCharArray(bufmin, 3);
    tmin = atoi(bufmin);
  }

  int posday = readString.indexOf("day=");
  if (posday > 0) {
    readString.substring(posday + 4).toCharArray(bufday, 3);
    tday = atoi(bufday);
  }

  int posmonth = readString.indexOf("month=");
  if (posmonth > 0) {
    readString.substring(posmonth + 6).toCharArray(bufmonth, 3);
    tmonth = atoi(bufmonth);
  }

  int posyear = readString.indexOf("year=");
  if (posyear > 0) {
    readString.substring(posyear + 5).toCharArray(bufyear, 5);
    tyear = atoi(bufyear);
    setTime(thour, tmin, 0, tday, tmonth, tyear); //hour,min,sec,day,month,year
  }
}

void alarm() {
  if (readString.indexOf("?alarm1") > 0) {
    int posah1 = readString.indexOf("hour=");
    if (posah1 > 0) {
      readString.substring(posah1 + 5).toCharArray(bufah1, 3);
      ah1 = atoi(bufah1);
    }
    int posam1 = readString.indexOf("min=");
    if (posam1 > 0) {
      readString.substring(posam1 + 4).toCharArray(bufam1, 3);
      am1 = atoi(bufam1);
    }
    if (readString.indexOf("?alarm1=0") > 0) {
      ae1 = 0;
    }
    if (readString.indexOf("?alarm1=1") > 0) {
      ae1 = 1;
    }
  }
  if (readString.indexOf("?alarm2") > 0) {
    int posah2 = readString.indexOf("hour=");
    if (posah2 > 0) {
      readString.substring(posah2 + 5).toCharArray(bufah2, 3);
      ah2 = atoi(bufah2);
    }
    int posam2 = readString.indexOf("min=");
    if (posam2 > 0) {
      readString.substring(posam2 + 4).toCharArray(bufam2, 3);
      am2 = atoi(bufam2);
    }
    if (readString.indexOf("?alarm2=0") > 0) {
      ae2 = 0;
    }
    if (readString.indexOf("?alarm2=1") > 0) {
      ae2 = 1;
    }
  }
  if (readString.indexOf("?alarm3") > 0) {
    int posah3 = readString.indexOf("hour=");
    if (posah3 > 0) {
      readString.substring(posah3 + 5).toCharArray(bufah3, 3);
      ah3 = atoi(bufah3);
    }
    int posam3 = readString.indexOf("min=");
    if (posam3 > 0) {
      readString.substring(posam3 + 4).toCharArray(bufam3, 3);
      am3 = atoi(bufam3);
    }
    if (readString.indexOf("?alarm3=0") > 0) {
      ae3 = 0;
    }
    if (readString.indexOf("?alarm3=1") > 0) {
      ae3 = 1;
    }
  }
  if (readString.indexOf("?alarm4") > 0) {
    int posah4 = readString.indexOf("hour=");
    if (posah4 > 0) {
      readString.substring(posah4 + 5).toCharArray(bufah4, 3);
      ah4 = atoi(bufah4);
    }
    int posam4 = readString.indexOf("min=");
    if (posam4 > 0) {
      readString.substring(posam4 + 4).toCharArray(bufam4, 3);
      am4 = atoi(bufam4);
    }
    if (readString.indexOf("?alarm4=0") > 0) {
      ae4 = 0;
    }
    if (readString.indexOf("?alarm4=1") > 0) {
      ae4 = 1;
    }
  }
  if (readString.indexOf("?alarm5") > 0) {
    int posah5 = readString.indexOf("hour=");
    if (posah5 > 0) {
      readString.substring(posah5 + 5).toCharArray(bufah5, 3);
      ah5 = atoi(bufah5);
    }
    int posam5 = readString.indexOf("min=");
    if (posam5 > 0) {
      readString.substring(posam5 + 4).toCharArray(bufam5, 3);
      am5 = atoi(bufam5);
    }
    if (readString.indexOf("?alarm5=0") > 0) {
      ae5 = 0;
    }
    if (readString.indexOf("?alarm5=1") > 0) {
      ae5 = 1;
    }
  }
}

void alarm1() {    //house open
  media_blind_up();
  gym_blind_up();
  living_blind_left_up();
  living_blind_mid_up();
  living_blind_right_up();
  kitchen_blind_up();
  work_blind_up();
  newlight08 = 0;
}

void alarm2() {    //house close
  master_blind_left_down();
  master_blind_right_down();
  master_blind_over_down();
  media_blind_down();
  gym_blind_down();
  bed2_blind_down();
  living_blind_left_down();
  living_blind_mid_down();
  living_blind_right_down();
  kitchen_blind_down();
  work_blind_down();
}

void alarm3() {    //night light on
  newlight08 = 20;
}

void alarm4() {    //bed blinds open
  master_blind_left_up();
  master_blind_right_up();
  master_blind_over_up();
}

void alarm5() {  // bed2 blinds open
  bed2_blind_up();
}

thanks for helping
:)
 

Attachments

  • home_automation_sd.zip
    26 KB · Views: 126
should you not declare bufmin and others with 3 charancters to hold also the trailing 0 that may be needed for string operations ?
 
I usually use c_str vs toCharArray to get a string to an int. The basic form is as follows:

Code:
atoi(cmd.substring(1, cmd.length()).c_str())

where cmd is declared a string.
 
You have buffer overflows everywhere. All your toCharArray() stuff is wrong and uses buffers that are too small.

There is no reason to use atoi. Use String::toInt() (it uses atol() under the hood):

Code:
int tmin = readString.substring(posmin + 4).toInt();

If you want to limit the length of the number to 2 digits:
Code:
int tmin = readString.substring(posmin + 4, posmin + 6).toInt();

In general, stay away from C-style string functions whenever you can. Writing bug-free code that uses them is cumbersome and difficult.
 
Status
Not open for further replies.
Back
Top