I am working on a project where I need to set a SIM900 module attached to a Raspberry PI to work on server mode and receive requests from remote clients. Everything works well as far as setting up the server and receiving client connections. These are established but no data is going through. The server can't receive data nor send it to the clients. I am in Argentina and I am using a SIM card from the carrier Claro and an APN that provides a public IP. In order to talk to the GPRS module I am using PI4J and code as follows.
package pi4j;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import com.pi4j.io.serial.Serial;
import com.pi4j.io.serial.SerialDataEvent;
import com.pi4j.io.serial.SerialDataListener;
import com.pi4j.io.serial.SerialFactory;
public class SerialExample {
static String post;
static final Serial serial = SerialFactory.createInstance();
static List<InputHandler> handlers = new ArrayList<InputHandler>();
public static void main(String args[]) throws InterruptedException,
IOException {
serial.addListener(new SerialDataListener() {
String input = "";
public void dataReceived(final SerialDataEvent event) {
System.out.print(event.getData());
input += event.getData();
for (int i = 0; i < handlers.size(); i++) {
InputHandler handler = handlers.get(i);
synchronized (handler) {
String output = handler.handle(input);
if (!input.equals(output)) {
input = output;
handler.notify();
}
}
if (handler.pop) {
handlers.remove(handler);
i++;
}
}
}
});
serial.open(Serial.DEFAULT_COM_PORT, 115200);
new Thread() {
public void run() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
try {
while((line = br.readLine()) != null) {
line = line.replace("ctrl+z", "" + (char) 0x1a);
if(line.endsWith("" + (char) 0x1a))
serial.write(line);
else
serial.writeln(line);
}
} catch (IllegalStateException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
sendCommandWaitOK("AT");
sendCommandWaitOK("AT+CREG=1");
sendCommandWaitOK("AT+CSTT=\"internet.ctimovil.com.ar\",\"ctigprs\",\"ctigprs999\"");
sendCommandWaitOK("AT+CIICR");
sendCommandWaitOK("AT+CIPHEAD=1");
sendCommandWaitOK("AT+CIPSRIP=1");
sendCommandWaitOK("AT+CIPSHOWTP=1");
sendCommandWaitResponse("AT+CIFSR", new InputHandler() {
public String handle(String input) {
if (input.indexOf("AT+CIFSR" + "\r\n") == -1)
return input;
String[] parts = input.split("\r\n");
mainLoop: for (String part : parts) {
String[] b = part.split("\\.");
if (b.length != 4)
continue;
for (String c : b)
try {
int d = Integer.parseInt(c);
if (d < 0 || d > 255)
continue;
} catch (NumberFormatException e) {
continue;
}
input = input.replace("AT+CIFSR" + "\r\n", "").replace(
"\r\n" + part + "\r\n", "");
pop = true;
break mainLoop;
}
return input;
}
});
sendCommandWaitEndsWith("AT+CIPSERVER=1,1234", true,
"\r\nOK\r\n\r\nSERVER OK\r\n");
}
private static void sendCommandWaitResponse(String command,
InputHandler handler) {
try {
synchronized (handler) {
handlers.add(handler);
if(command.endsWith("" + (char) 0x1a))
serial.write(command);
else
serial.writeln(command);
handler.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private static void sendCommandWaitOK(String command) {
sendCommandWaitEndsWith(command, true, "\r\nOK\r\n");
}
private static void sendCommandWaitEndsWith(final String command,
final boolean echo, final String expected) {
sendCommandWaitResponse(command, new InputHandler() {
public String handle(String input) {
if (echo && input.indexOf(command + "\r\n") == -1)
return input;
if (input.indexOf(expected) != -1) {
if (echo) {
input = input.replace(command + "\r\n", "");
}
input = input.replace(expected, "");
pop = true;
}
return input;
}
});
}
static abstract class InputHandler {
abstract String handle(String input);
boolean pop;
}
}
The output is this:
Aug 02, 2014 1:51:22 AM com.pi4j.util.NativeLibraryLoader loadLibraryFromResource
WARNING: The temporary file already exists [/tmp/libpi4j.so]; attempting to delete it now.
AT
OK
AT+CREG=1
OK
AT+CSTT="internet.ctimovil.com.ar","ctigprs","ctigprs999"
OK
AT+CIICR
OK
AT+CIPHEAD=1
OK
AT+CIPSRIP=1
OK
AT+CIPSHOWTP=1
OK
AT+CIFSR
186.12.33.115
AT+CIPSERVER=1,1234
OK
SERVER OK
REMOTE IP: 190.195.1.211
AT+CIPSEND
AT+CIPSEND
> Hola
Hola
But then nothing happens and it never returns the expected "SEND OK". In some rare occasions the client data does go through and I can see it printed in the console but I couldn't find the rule to recreate these conditions.
I have tried connecting both through web browsers and a small Java console socket client, the result is always the same. This is the code for it:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.Socket;
public class Pepin {
public static void main(String[] args) throws MalformedURLException, IOException {
final Socket s = new Socket("186.12.33.115", 1234);
new Thread() {
public void run() {
while(!s.isClosed())
try {
if(s.getInputStream().available() > 0)
System.out.print((char) s.getInputStream().read());
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(true) {
byte[] bytes = br.readLine().getBytes();
s.getOutputStream().write(bytes);
s.getOutputStream().flush();
}
}
}
I type text and hit enter but nothing's goning in or out.
Is there anything you notice I am doing wrong? Help is apreicated.
Gabriel