$10 Paypal to someone who can add a couple of lines for me

I have this working so when I upload the .ino to my UNO it sends a SMS to my cell phone. I want it to send a SMS each time a button it pressed like on Digital pin 2. I'm in my 60's and cant figure out how to do it and I need it soon. Thanks

/*
Send SMS from Arduino over the Internet using ENC28J60 and Thingspeak
Change one line to use with ethernet shield
Add sensor readings in the loop and a time interval
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>

//change the following line to #include <Ethernet.h> to use the eithent shield
#include <Ethernet.h> 

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

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

//if DHCP fails, use a static IP
IPAddress ip(192,168,0,177);

// Initialize the Ethernet client library
EthernetClient client;

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

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

void setup()
{
Serial.begin(9600);

//set up Ethernet:
setupEthernet();

//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("Hello World!"));

}

void loop()
{

}


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



void setupEthernet()
{
Serial.println("Setting up Ethernet...");
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
  Serial.println(F("Failed to configure Ethernet using DHCP"));
  // no point in carrying on, so do nothing forevermore:
  // try to congifure using IP address instead of DHCP:
  Ethernet.begin(mac, ip);
}
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
// give the Ethernet shield a second to initialize:
delay(1000);
}

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

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

ThingSpeakTwilio.ino (3.21 KB)

You can PM me if you like. Thanks

At start of code:
unsigned long lastPressed=0;
byte lastState;

in setup:
pinMode(D2,INPUT_PULLUP)

void loop() //in loop, we check the pin, and if it's low (pressed) when it wasn't before, and it hasn't been pressed in the past 100 milliseconds (due to the switch contacts bouncing)
{
if (digitalRead(D2)==LOW) {
if (lastState==HIGH) {
//we know the button was just pressed
if (lastPressed +100 < millis()) { //debounce
//sendSMS() - I don't know what message you want to send to whom, but this is where to to it
lastPressed=millis();
}
}
}
}

And you'd wire the button with one side on D2, other side to ground.

That was just written off the top of my head - no guarantees.

n0xd:
I'm in my 60's and cant figure out how to do it

What relevance has your age to the problem?

I, and many other contributors here, are also of a similar age. There are even people here who remember DEC PDP11 computers :slight_smile:

...R

There are even people here who remember DEC PDP11 computers :slight_smile:

I can't abide that modern stuff, with their fancy operating systems - PDP8-/E

Thanks Robin2 ....Well I should of said I'm in my 60's and had a stroke recently and my brain isn't working good and I have never coded before. But your input had been noted.

All I am trying to do is send a sms to my phone if a button is pushed.

Load and run this. It needs a jumper in pin 2 that you ground safely on the USB connector box or a button between pin 2 and GND. You also need to set Serial Monitor to 115200 or change the code to 9600.

This code only prints a message to serial monitor but where it does that you can substitute sendSMS.

Keep your $10 for in case you need help debugging after wedging this into your code. I started with your setup() and loop(), not that I had to make 2 lines in setup() into comments that you will need to uncomment. The button code is from one of my old posted examples, it may live again! LOL!

I know a bit of how you feel. Got a head full of clots and trying to refill holes, half of what I learned since 1990 was lost. Fortunate for me I was coding long before 1990! Some of this I can still do in my sleep and yes, some recovery of abilities and self is possible, Ive been at it since 2001.

// button variables
const byte buttonPin = 2;
byte buttonRead;  // wired for pullup -- if down then LOW, if up then HIGH
byte lastButtonRead = HIGH; // so debounce knows previous read
byte checkDebounce = 0; // only checks decounce after every button pin change
byte lastButtonState = 0; // last stable button state
byte buttonState = 0;  // stable button state
// 0 = button is up after debounce
// 1 = button is down after debounce
// button debounce timing variables
const word debounceMs = 20; // debounced when reads for debounceMs ms are the same
word debounceMillisLowBytes = 0; // don't need 32 bits
word msNow = 0; // don't worry, unsigned rollover makes it work


void setup()
{
  Serial.begin(115200);

  pinMode( buttonPin, INPUT_PULLUP ); // my button connects to ground, not 5V
  // however that means that when the button is pressed the pin is LOW.

  //set up Ethernet:
  // setupEthernet();  This commented out to compile button code only!

  //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("Hello World!"));   This commented out to compile button code only!

}

void loop()
{
  // BUTTON CODE BLOCK, it handles debouncing.
  // the task is to set the variable buttonState when a Stable Button State is reached.
  // other sensor code could change the same variable if desired

  // read the pin which may be changing fast and often as the jumper contacts the housing
  buttonRead = digitalRead( buttonPin ); // momentary state

  // msNow gets the low bytes of millis() to time the very short debounce time
  msNow = (word)( millis() & 0xFFFF ); // set once, used in 2 places

  if ( buttonRead != lastButtonRead )
  {
    debounceMillisLowBytes = msNow;
    checkDebounce = 1;
  }
  else if ( checkDebounce )
  {
    if ( msNow - debounceMillisLowBytes >= debounceMs ) // stable button state achieved
    {
      buttonState = !buttonRead; // mission accomplished, button is stable
      // note that buttonState is opposite buttonRead
      checkDebounce = 0; // stop debounce checking until pin change
    }
  }
  lastButtonRead = buttonRead;
  //
  // End of the BUTTON CODE BLOCK

  if ( lastButtonState != buttonState )
  {
    lastButtonState = buttonState;

    if ( buttonState )
    {
      Serial.println("Sending SMS");
    }
  }
}

Thanks GoForSmoke. I am working on it now.

You can arrange different SMS messages for different buttons, get a keypad and send numbers if you want.

How much Arduino C have you absorbed? I gather not much yet and man I know it goes slow.

Do you know arrays and loops as well as if-else? Do you know switch-case?
All good basic tools that make coding easier.

How are you with bookmarks? You may want to make a sub-folder for just Arduino bookmarks.
When you deal with the IDE, keep a window open with a tab for each help page you need.

You can look from the IDE window over to instant online help. If you see a command in the code that you don't know, then you can look it up and even size the text to readable, I put my IDE text up to 16 pt, browser pages to 120%, way easier on the eyes.

Have browser tabs with the Arduino Reference, Libraries, and other site pages and wherever else you need to see the code, there's many how-to blogs out there you might refer to, so have it there on a tab.

Linkies:
The first place to look for basic C.

This is where to look up library functions. You'll find out

These are all in your IDE under File->Examples. The libraries have example code too.
You can learn a lot just reading the code and looking it up.
http://www.arduino.cc/en/Tutorial/HomePage

Every board linked on this page has useful details described including links to pin maps.

This is the Arduino Playground Libraries. Wait a while for this.
http://playground.arduino.cc/Main/GeneralCodeLibrary

This is the libraries page of the C/C+ Arduino uses. You can use these, this is the reference.
http://www.nongnu.org/avr-libc/user-manual/modules.html

You'll get more in time. In my signature space below are 3 keys to writing real time code on Arduino.