Main Menu

Newbie Help

Started by ShadowGrass, March 05, 2014, 11:55:53 AM

ShadowGrass

Reading some of the projects and information on this site, I may be in a little over my head.  The wireless portion is what is giving me problems,  as I really don't know where to start with hit.  I have my two Moteino's setup, programmed, etc.  They are communicating and my program is running properly.  What I need to be able to do is send multiple variable values to my gateway.  They are:

1. tempSensor (int)
2. floatStatus (1 or 0)
3. pumpStatus (1 or 0)
4. lightStatus (1 or 0)
5. lightAlarm (1 or 0)
6. pumpAlarm (1 or 0)

Basically my string of data should look like this 45.0,1,0,1,1,1 or whatever the actually values are.  Right now, I have not changed the standard send/recv program so it is sending the 123 ABC.... and that looks to be sending and receiving fine.  I need to know how to package the data I want to send on the sender, send it, and receive it on the gateway and be able to apply that data to variables on the gateway. 

From what I can gather, the char payload[] = "123 ABCD..."; is what I want to change. I want the payload to be the values of my variables.  I believe it is something to this effect to send:

sprintf(buf,"%d,%d,%d,%d,%d,%d :\n",tempSensor,floatStatus,pumpStatus,lightStatus,lightAlarm,pumpAlarm);

Probably a strtok and atoi on the receving end, but any input on this would be a big help.

Thanks!

Felix

Yeah I think you're on the right path. You can use sprintf and sscanf to generate and extract your variables.

Here's an example of how to extract a variable:
uint16_t tmp=0;
sscanf((const char*)radio.DATA, "BLAH:%u:MOREBLAH", &tmp);


See this reference on printf/scanf formatting of variables: http://www.cplusplus.com/reference/cstdio/printf/

ShadowGrass

Ok, well you lost me with this:

uint16_t tmp=0;
sscanf((const char*)radio.DATA, "BLAH:%u:MOREBLAH", &tmp);

So on my sender I would put this?
sprintf(buf,"%d,%d,%d :\n",photoSensor,floatSwitch,tempSensor);  //Build string
radio.sendWithRetry(GATEWAYID, buf, sendSize); //send it

On the receiver I should do what?  How do I get it to accept what was sent?  I don't understand what BLAH and MOREBLAH is supposed to represent, are those my variables?

Felix

BLAH was just whatever else you might have in the message body, or nothing else.
sprintf generates text to a buffer, so use on the sender to make up your string from the variables
sscanf extracts variables from a buffer, so use on the receiver to extract your sent variables

ShadowGrass

I didn't have much luck on the sscanf, but I did get it working.  Initially I had some holes on my recv data, but I fixed that too.

On my sender the relevant code is:
sprintf(buf,"%d,%d,%d,%d,%d :\n",pumpAlarm,lightAlarm,photoSensor,floatSwitch,tempSensor);  //Build string
    //radio.send(GATEWAYID, buf, sendSize);
    radio.send(GATEWAYID, buf, 30);
    Blink(LED,3);
   


On the receiver the relevant code is:
char *pumpAlarm, *lightAlarm, *photoSensor, *floatSwitch, *tempSensor;
int pumpAlarm1, lightAlarm1, photoSensor1, floatSwitch1, tempSensor1;

void loop() {
if (radio.receiveDone())
  {
       pumpAlarm = strtok((char*)radio.DATA,",");
       lightAlarm = strtok(NULL,",");
        photoSensor = strtok(NULL,",");
         floatSwitch = strtok(NULL,",");
          tempSensor = strtok(NULL,",");
          
      pumpAlarm1 = atoi(pumpAlarm);
       lightAlarm1 = atoi(lightAlarm);
        photoSensor1 = atoi(photoSensor);
         floatSwitch1 = atoi(floatSwitch);
          tempSensor1 = atoi(tempSensor);
}
      }


I could tell there was a pattern with the holes in the data I was receiving where I would get all the data for approximately 20 recvs and then it would drop out for 10 or so then come back and the pattern would repeat.  So I changed this line in the sender code:
radio.send(GATEWAYID, buf, sendSize);

to
radio.send(GATEWAYID, buf, 30);

Looks like it is sending and receiving perfectly now.  This is somewhat of a complicated project, it is for a well house monitoring system.  It's hard for those in the south who have their water shutoff outside (that is crazy to me), but I'm in Michigan where is was -3F last night.  There is a 4' deep hole in the ground that is 4' x 4'.  It has a little house over the top of it.  Inside is my well head, pump, expansion tank, and pipes. I need to keep a bulb on for heat in the winter so it doesn't freeze.  Also it has a tendency to flood inside during spring and heavy rain.  In the well house I will have one Moteino which controls a 12v bilge pump and the heat light with relays based on the input of a float (checks water level) and the outside temperature.  It also has a photosensor that monitors the heat lamp (all who have a well house have had them burn out at the most inopportune time).  So if the heat lamp is supposed to be on (digitalread light pin HIGH) but the reading on the photosensor is <500 (is around 750 with the bulb on) I know that the bulb is burned out.  All that data is sent into the house to the recv Moteino.  This modeino is connected to an Arduino Pro Mini that runs a LCD and an Ethernet shield which posts the data to Exosite so I can review it remotely and get SMS if there is an alarm situation.  If there is an alarm situtaion either the bulb is burned out or the pump is running too long, it triggers an alarm on the LCD and a buzzer in the house.  Fun stuff.  Almost done with it, the wireless variables is what was killing me.  The inside Moteino I tried to connect to using a LCD with a I2C interface, but there seems to be some conflict with the RFM69.h and the LCD.h and or the LiquidCrystal_I2C.h.  So I send it serial to the Arduino pro and that handles the LCD and Ethernet.  I'll post more with pics on the project section when I am finished.