Ok o que quero fazer é o seguinte:
Tenho uma Ethernet Shield e uma Sim900 Shield, vou enviar via SMS pela internet. Meu código até o momento esta logo abaixo:
/*
* Ethernet SMS Server
* Version 1.0 April, 2013
* Copyright 2013 Renato Tavares
* For details, see https://bitbucket.org/renatotavares/ethernet-sms-server
*
* Modified by Renato Tavares <dr.renatotavares@gmail.com> to support ethernet shield, GSM shield and multitasking.
*
* Complete Web Server to manage sending SMS with a GSM API using Arduino.
*/
// Including the required libraries
#include <Ethernet.h>
#include <SPI.h>
#include <SoftwareSerial.h>
// Defining the constants present in the code
unsigned long UpdateNoIPCycle = 300000;
unsigned long UpdateNoIPLastMillis = 0;
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
byte ip[] = {
192, 168, 1, 177};
EthernetServer server(80);
SoftwareSerial mySerial(2, 3);
// Initial system setup
void setup(){
Serial.begin(9600);
mySerial.begin(19200);
Ethernet.begin(mac, ip);
}
// Main body system, the magic happens here
void loop(){
if(cycleCheck(&UpdateNoIPLastMillis, UpdateNoIPCycle)) {
Serial.println("Atualizando No-IP...");
UpdateNoIP();
}
}
// function for multitasking
// http://arduino.cc/forum/index.php/topic,5686.msg44073.html
boolean cycleCheck(unsigned long *UpdateNoIPLastMillis, unsigned long UpdateNoIPCycle) {
unsigned long currentMillis = millis();
if(currentMillis - *UpdateNoIPLastMillis >= UpdateNoIPCycle) {
*UpdateNoIPLastMillis = currentMillis;
return true;
}
else{
return false;
}
}
// Function to update my IP with the No-IP domain
void UpdateNoIP(){
EthernetClient client;
if(client.connect("dynupdate.no-ip.com", 80)){
Serial.println("Conectando com No-IP");
client.println("GET /nic/update?hostname=fds.no-ip.biz HTTP/1.0");
client.println("Host: dynupdate.no-ip.com");
client.println("Authorization: Basic ZHWOEJ6Q2VpSlA=\r\n");
client.println("User-Agent: Renato Arduino Client/0.0 dr.renatotavares@gmail.com");
client.println();
while(client.connected()){
// stay in this loop until the server closes the connection
while(client.available()){
// The server will not close the connection until it has sent the last packet
// and this buffer is empty
char read_char = client.read();
Serial.write(read_char);
}
}
// close your end after the server closes its end
client.stop();
Serial.println("Conexão com No-IP fechada");
}
else{
Serial.println("Falha na Conexão");
}
}
//function for sending SMS
// http://elecfreaks.com/wiki/index.php?title=EFCom_GPRS/GSM_Shield
void sendSMS(String msg, String phone){
mySerial.print("\r");
delay(1000);
mySerial.print("AT+CMGF=1\r");
delay(1000);
mySerial.print("AT+CMGS=\""+ phone + "\"\r");
delay(1000);
mySerial.print(msg + " (Enviado via: SMSWeb)\r");
delay(1000);
mySerial.write(26);
}
Como pode ter notado a função para fazer update no No-IP, função para simular o multitasking e também a função para enviar o SMS estão prontas. Só preciso fazer o arduino escutar as chamadas dos clientes e responder cada uma delas enviando o SMS.
Para ficar melhor vamos tratar que o PHP vai enviar uma string unica em seguinte formato:
Mensagem a ser enviada pelo usuário do site|+556293840000
Ou seja, o formato será mensagem seguida pelo caractere de separação | e em seguida o número do telefone.
PHP seu sei, logo podemos fazer o seguinte:
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0);
// connect to server
$result = socket_connect($socket, "192.168.1.177", 80);
// send string to server
$message = "minha mensagem escrita pelo PHP|+556293844000";
socket_write($socket, $message , strlen($message));
// get server response
$result = socket_read ($socket, 1024);
echo "Reply From Server :".$result;
// close socket
socket_close($socket);
Isso deve enviar para o Arduino a string no formato definido. Agora o que me falta, e que meu conhecimento é limitado, e pegar esse string no arduino, separar a parte da mensagem do telefone e chamar a função sendSMS. Minha dúvida toda se resume a isso =(