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;
}