433mhz doorbell sms help, works for a bit then stops.

I’m having an issue with arduino with an ethernet shield and a 433mhz receiver. It is being used to monitor for a wireless doorbell then send me a sms when it rings. This code works when I reset it for a bit then stops but I can still ping the arduino. I would appreciate any help getting it more stable.

/*
  Send SMS from Arduino over the Internet using esp8266 and Thingspeak
  Using Arduino UIP library from https://github.com/ntruchsess/arduino_uip

  Code based on Sparkfun's data logging service data.sparkfun.com
  URL encode function from http://hardwarefun.com/tutorials/url-encoding-in-arduino

*/


#include <SPI.h>
#include <Ethernet.h>
#include <RCSwitch.h>

//radio receiver
RCSwitch mySwitch = RCSwitch();

// Enter a MAC address for your controller below.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

//thingspeak server
char server[] = "api.thingspeak.com";

// Initialize the Ethernet client library
EthernetClient client;

//API key for the Thingspeak ThingHTTP already configured
const String apiKey = "APIKEY";

//the number the message should be sent to
const String sendNumber = "PHONENUMBER";

//IP settings
const IPAddress ipadd(192, 168, 1, 150);
const IPAddress ipgat(192, 168, 1, 1);
const IPAddress ipsub(255, 255, 255, 0);

void setup()
{
  Serial.begin(9600);
  delay(100);
  Serial.println("started");
  mySwitch.enableReceive(0);
  Ethernet.begin(mac, ipadd);
}

void loop()
{
  if (mySwitch.available()) {

    int value = mySwitch.getReceivedValue();

    if (value == 0) {
      Serial.print("Unknown encoding");
    }

    else if ((value == -15480) || (value == -15616)) {
      //send the sms
      Serial.println("Sending SMS");

      //this function will send the sms
      //the first argument is the number to send to, formatted like this +12345678901
      //the second argument is the body of the text message, which must be within URLEncode()
      sendSMS(sendNumber, URLEncode("Someone is at the front door!"));
      delay(20000);
    }
    else if ((value == -7832) || (value == -7936)) {
      //send the sms
      Serial.println("Sending SMS");

      //this function will send the sms
      //the first argument is the number to send to, formatted like this +12345678901
      //the second argument is the body of the text message, which must be within URLEncode()
      sendSMS(sendNumber, URLEncode("Someone is at the other door!"));
      delay(20000);
    }
    else {
      Serial.print("Received: ");
      Serial.print( mySwitch.getReceivedValue() );
      Serial.print("Value : ");
      Serial.print(value);
      Serial.print(" / ");
      Serial.print( mySwitch.getReceivedBitlength() );
      Serial.print("bit ");
      Serial.print("Protocol: ");
      Serial.println( mySwitch.getReceivedProtocol() );
    }
    mySwitch.resetAvailable();
  }
}


void sendSMS(String number, String message)
{
  // Make a TCP connection to remote host
  if (client.connect(server, 80))
  {

    //should look like this...
    //api.thingspeak.com/apps/thinghttp/send_request?api_key={api key}&number={send to number}&message={text body}

    client.print("GET /apps/thinghttp/send_request?api_key=");
    client.print(apiKey);
    client.print("&number=");
    client.print(number);
    client.print("&message=");
    client.print(message);
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println(server);
    client.println("Connection: close");
    client.println();
  }
  else
  {
    Serial.println(F("Connection failed"));
  }

  // Check for a response from the server, and route it
  // out the serial port.
  while (client.connected())
  {
    if ( client.available() )
    {
      char c = client.read();
      Serial.print(c);
    }
  }
  Serial.println();
  client.stop();
}


String URLEncode(const char* msg)
{
  const char *hex = "0123456789abcdef";
  String encodedMsg = "";

  while (*msg != '\0') {
    if ( ('a' <= *msg && *msg <= 'z')
         || ('A' <= *msg && *msg <= 'Z')
         || ('0' <= *msg && *msg <= '9') ) {
      encodedMsg += *msg;
    }
    else {
      encodedMsg += '%';
      encodedMsg += hex[*msg >> 4];
      encodedMsg += hex[*msg & 15];
    }
    msg++;
  }
  return encodedMsg;
}

This code works when I reset it for a bit then stops

Given you use the String class all over the place, I would suggest you start by getting rid of that and only use c-strings and then check again if you have instability issues

Strings (with a capital S) are not your friends. (Read this and use the standard libs such as stdlib.h and string.h)

also in your String URLEncode([color=red]const[/color] char* msg) function, you are doing msg++ so you lied to the compiler by stating msg was a constant...

Thanks for the advice. I'll make those changes and see how it goes.

Also, are you using the crapola green superregenerative receivers? I've seen them do weird things, like suddenly the range falls to inches for no apparent reason (was never able to figure out what happened when it did this, and they'd spontaneously start working again later). An RXB12 ($1.xx on ebay shipped) is a much better receiver.

yeah I think I'm using the ones your talking about: http://a.co/htN41aE . I'll pick up one of the other ones.