Show Posts
Pages: 1 ... 57 58 [59] 60 61 62
871  Using Arduino / Programming Questions / Re: Multiple ethernet clients on: April 07, 2011, 08:19:05 pm
Sure, it's sort of working somewhat, most of the time.  We all know that we are never, ever done with any project, we just get it working well enough and start using it.  There will always be improvements over time.

Some stuff is missing of course, like the declaration of the client and so forth, but that can be lifted directly off the examples.  But basically I do exactly as described above, pass in an index into an array of objects.  You could do the same thing by simply passing the object.  The respReq parameter is for interactions that don't need a response.

The little routine down at the bottom is to make darn sure it works or I reboot the board and reset the ethernet card.  I have had a lot of trouble with the network card hanging up.  This kind of thing fixes that.

Hey, it ain't pretty, but it works pretty well and can only get better.

Code:
boolean Thermoconnected = false;

boolean tryGetThermostat(int idx,char* command, boolean respReq){
  char* ptr;
  char* data;
  int cnt, len;
  int waitcnt = 0;
  long timer = 0;
 
  while(1){
    if (!Thermoconnected){
      strcpy_P(Dbuf2,PSTR("%s Thermostat Connecting..."));
      sprintf(Dbuf,Dbuf2, (idx==0) ? "North" : "South");
      Serial.print(Dbuf);
      if (Thermostats[idx].connect()) {
        Thermoconnected = true;
        lastthermoCheck = millis();
        waitcnt++;
        strcpy_P(Dbuf,PSTR("OK"));
        Serial.println(Dbuf);
        strcpy_P(Dbuf2,PSTR("GET /%s HTTP/1.0"));
        sprintf(Dbuf,Dbuf2,command);
        Thermostats[idx].println(Dbuf);
        timer = millis(); // to time the response, just in case
      }
      else{
        strcpy_P(Dbuf,PSTR("failed"));
        waitcnt++;
        timer = millis();  // if the connection failed I don't need to time it.
        Serial.println(Dbuf);
        Thermostats[idx].flush();
        Thermostats[idx].stop();
        while (Thermostats[idx].status() != 0){
          delay(5);
        }
      }
    }
    if (Thermostats[idx].available() > 0){
      ptr = Dbuf;
      while (Thermostats[idx].available()) {
        *ptr++ = Thermostats[idx].read();
      }
      Thermostats[idx].flush(); //suck out any extra chars 
      Thermostats[idx].stop();
      while (Thermostats[idx].status() != 0){
        delay(5);
      }
      Thermoconnected = false;
     
      *ptr='\0';
      if(!respReq){ //response not required
        return(true);
      }
//       Serial.println(Dbuf+44); //debug only
      // finally got some data
      ThermoData[idx].currentTemp = atof(strtok_r(Dbuf+44, ",", &ptr));   // first item is current temp
      data = strtok_r(0, ",", &ptr);                  // first find the token
      len = strcspn(data, ",");                       // then the length
      memset(ThermoData[idx].state,0,sizeof(ThermoData[idx].state)); //make sure it's empty
      strncpy(ThermoData[idx].state, data, len);          // copy out what it's doing currently
      ThermoData[idx].tempSetting = atof(strtok_r(ptr, ",", &ptr));   // temp setting
      data = strtok_r(0, ",", &ptr); 
      len = strcspn(data, ",");
      memset(ThermoData[idx].mode,0,sizeof(ThermoData[idx].mode));
      strncpy(ThermoData[idx].mode, data, len);           // Mode setting (cool, heat, off)
      data = strtok_r(0, ",", &ptr); 
      len = strcspn(data, ",");
      memset(ThermoData[idx].fan,0,sizeof(ThermoData[idx].fan));
      strncpy(ThermoData[idx].fan, data, len);            // Fan setting (Auto, On, Recirc - no off)
      data = strtok_r(0, ",", &ptr); 
      len = strcspn(data, ",\n\r");
      memset(ThermoData[idx].peak,0,sizeof(ThermoData[idx].peak));
      strncpy(ThermoData[idx].peak, data, len);           // In peak period ?
      strcpy_P(Dbuf2,PSTR("Temp is %d, Heat pump is %s, \nTemp Setting is %d, Mode %s, Fan %s, %s Period"));
      sprintf(Dbuf,Dbuf2, ThermoData[idx].currentTemp,
        ThermoData[idx].state,
        ThermoData[idx].tempSetting,
        ThermoData[idx].mode,
        ThermoData[idx].fan,
        ThermoData[idx].peak);
      Serial.println(Dbuf);
      return(true);
    }
    if (millis() - timer >5000){
      strcpy_P(Dbuf,"Timeout waiting");
      Serial.println(Dbuf);
      Thermostats[idx].flush();
      Thermostats[idx].stop();
      while (Thermostats[idx].status() != 0){
        delay(5);
      }
      Thermoconnected = false;
    }
   
    if (!Thermoconnected){
      strcpy_P(Dbuf2,PSTR("Thermo try # %d"));
      sprintf(Dbuf,Dbuf2,waitcnt);
      Serial.println(Dbuf);
      delay (1000);
    }
    if (waitcnt > 9){
      Thermostats[idx].flush();
      Thermostats[idx].stop();
      while (Thermostats[idx].status() != 0){
        delay(5);
      }
      return(false);
    }
  }
}
void getThermostat(int idx, char* command, boolean respReq ){
  if(tryGetThermostat(idx, command, respReq) == false){
    strcpy_P(Dbuf,PSTR("Thermo comm failure, rebooting"));
    Serial.println(Dbuf);
    resetFunc();  //call reset if you can't get the comm to work
  }
  return;
}

872  Topics / Home Automation and Networked Objects / Re: Arduino temperature&presence sensor with web page and SNMP on: April 07, 2011, 09:39:19 am
Yes, there is a MemoryFree library, google for the latest location.  I honestly can't remember where I got it a few months ago.  It's simple and works well.
873  Topics / Home Automation and Networked Objects / Re: Arduino temperature&presence sensor with web page and SNMP on: April 06, 2011, 12:28:44 pm
To check and see if you are running out of memory distribute some "Serial.println(freeMemory());" in the code.  I've found I need a couple of hundred bytes free or the device dies erratically.  If you are running out of memory, use PSTR to handle strings and get some back.  I noticed you already include MemoryFree.h.

strcpy_P(buffer,PSTR("string text goes in here"));
Serial.println(buffer);

Hope this helps
874  Using Arduino / Programming Questions / Re: Multiple ethernet clients on: April 06, 2011, 11:42:26 am
THANK YOU.

I couldn't get it to work exactly as you described; compiled properly and executed but something somewhere didn't work for me.  I had to declare the clients first them put them in an array.

Client therm1 (addr,80);
Client therm2 (addr2,80);
Client therms[]={therm1,therm2};

Then used the index to pick which one in the code like therms[idx].connect and it worked.  I'll claim that I intentionally did it that way for 'clarity' and ease of later maintenance .
875  Topics / Home Automation and Networked Objects / Re: Online Thermostat on: April 06, 2011, 09:47:44 am
I built two of them and had them working for a few months now.  It's not easy, but there is a lot of information out there to help you.  I have my source posted online; you're welcome to grab and modify as you need.
876  Using Arduino / Programming Questions / Re: Multiple ethernet clients on: April 05, 2011, 08:19:55 pm
If I was working direct with the board I probably would do just that.  However, I' confused by the "client thermo(thermoAddr,80)" and then the thermo.connect(), thermo.available(), etc.  It seems that I would have to have a

client thermo1(thermoaddr1,80);
client thermo2(thermoaddr2,80);

and then the code to match each of the above.  I have this working beautifully for one of the devices, but I really hate duplicating code (and bugs) to do another one.  There's obviously something I'm overlooking.
877  Using Arduino / Programming Questions / Multiple ethernet clients on: April 05, 2011, 05:52:43 pm
I have two ethernet enabled arduino thermostats.  They are running essentially the same code and respond to the same http get commands.  I want to set up a third arduino to talk to them and show their status.  This I can do pretty easily, but I have to duplicate the code for each one because I can't figure out how to call a common routine and pass it who I want to talk to.  It's easy enough to:

getstuff()  //where getstuff uses the thermostat1.available and so forth

So I could create a getstuff2(), but that would mean duplicating the code from getstuff.....wouldn't it? 

I'm old to c, but new to c++ and some advice would be nice.
878  Using Arduino / Networking, Protocols, and Devices / Re: DIY Wiznet module on: April 01, 2011, 11:41:15 pm
This is totally possible and will make a great project.  One difficulty I see is finding a good perfboard to mount both the ethernet components and arduino components.  However, a dedicated arduino + ethernet could see a lot of use.
879  Community / Exhibition / Gallery / Re: Make Your Own Arduino on: March 26, 2011, 11:32:20 pm
Thanks for the board reference.  I've been looking for something to mount a 328 and an XBee on for sensors and controls; that board looks perfect.  Using an entire arduino for something like this gets too expensive.  Nice project writeup.  I'll be interested in what you come up with for sources.
880  Community / Exhibition / Gallery / Re: Make Your Own Arduino on: March 26, 2011, 03:18:59 pm
Where did you get that perfboard?  I looked all over the place and didn't come across one like it.
881  Using Arduino / Networking, Protocols, and Devices / Re: Xbee+arduino problem: one hour restarting on: March 25, 2011, 11:14:27 pm
0x41 is "packet was acknowledged, from an end device"; they don't always show all the bits in a response.

The last code listing didn't show anything that could cause the code to run off uncontrolled.  You're not using any buffers that could be overrun and you're simply throwing away the bytes that are not sensor data.  However, your packet listings didn't come from the code you posted (there aren't any prints in the posted code) so you must have gotten them from some other device during the period you were testing.

I recommend you put in Serial.print(incoming_byte) for every character you are receiving on the arduino that is restarting and try it again.  There's something that is happening that you can't see.  Perhaps you could display and discard all characters until you get a 0x7e and start from there to decode the packet?

The sleep period of an end device won't cause problems on the coordinator.  They might affect the end device, but restarting the coordinator's arduino shouldn't be possible.  You don't have the XBee's reset pin tied to the arduino do you?  Possibly without a pull up resistor?  (I made that mistake once).

882  Using Arduino / Programming Questions / Re: The scaled_value function -- where is it, actually? on: March 23, 2011, 11:10:04 am
A long, long time ago compilers couldn't resolve a function that was declared after it was called and would throw an error.  Programmers whined that they wanted to put the meat of a module up at the top so they could find it easily when they were modifying it.  So language developers put in two pass scanning to resolve some of this.  Then the programmers complained that they wanted the lowest level routines at the end of the module because they were seldom changed so a new system of scanning the source came into being.  Now we can put the lowest level routines at the very bottom (end) of the module and the highest level routines at the top.

So now, we put the highest level routines in the middle and wrap stuff around it in a stream of consciousness fashion that makes it hard, even for the author, to find.

Seems like the further we go......
883  Using Arduino / Programming Questions / Re: The scaled_value function -- where is it, actually? on: March 22, 2011, 11:26:16 pm
Help me if I'm being a little dense here, but isn't that the scaled_value function about 8-9 lines down from where it's called?
884  Using Arduino / Networking, Protocols, and Devices / Re: While we're discussing xbees on: March 21, 2011, 06:09:09 pm
I posted the results of my trials and tribulations with XBees on my site.  There's also some sample code to help newcomers to these devices get over the hurdle that caused me to pull some of my little remaining hair out over. 

So, if you are having trouble getting them to talk after you do something to them, take a look.

http://draythomp.blogspot.com/

It looks simple, but it took me DAYS to find enough information and actually work through it.
885  Community / Products and Services / Re: "Electronic Bricks" Sensors, Outputs, more... on: March 21, 2011, 01:19:22 pm
Hmmm, doesn't a good set of documentation on a particular device sound like a good extra-credit project for a student starting out in this field?  And you're an instructor.....(wink, wink, nudge, nudge).
Pages: 1 ... 57 58 [59] 60 61 62