HTTP Post using AT command in J2ME | Telit Cinterion IoT Developer Community
January 8, 2016 - 11:08am, 3805 views
Hello,
I am trying to us AT command in J2ME program to perform HTTP post. However, when I try to issue command AT^SISW=1,5 (i.e I would like to write 5 bytes) after receiving URC: ^SISW: 1,1 the AT command instant is stuck and does not return any response (it should return ^SISW=1,5,0) so that I can enter post data. Using Terminal, this write command performs OK. Could you help me to solve this. This is my snapshot of Java code:
String lastATCmd = "AT^SISS=1,\"address\",\"http://httpbin.org\"";
System.out.println(lastATCmd);
m_Cmd.send(lastATCmd+"\r",new RspListener()); //Response: OK
Thread.sleep(5000);
System.out.println("AT^SISS=1,\"cmd\",\"post\"\r");
m_Cmd.send("AT^SISS=1,\"cmd\",\"post\"\r",new RspListener()); //Response: OK
Thread.sleep(2000);
lastATCmd = "AT^SISS=1,\"hcContLen\",5"; // Response: OK
System.out.println(lastATCmd);
m_Cmd.send(lastATCmd+"\r",new RspListener()); // Response: OK
Thread.sleep(2000);
System.out.println("AT^SISO=1\r");
m_Cmd.send("AT^SISO=1\r",new RspListener()); //Response OK with correct URC ^SISW:1,1
Thread.sleep(5000);
System.out.println("AT^SISW=1,5\r");
m_Cmd.send("AT^SISW=1,5\r", new RspListener()); // stuck here
Thread.sleep(20000);
System.out.println("12345");
m_Cmd.send("12345", new RspListener()); //error: another at command is in being processed
Thread.sleep(5000);
Hello,
I can see here is that you call the sent method in a non-blocking way with redirecting the response to the listener which you don't check. So you don't know if and when the response has come.
The most important thing is that to send the data you need to open the data output stream for this ATCommand instance. The send method you are using is only for AT commands sending. When you try to send anything that does not start with "AT" and finish with "\r" the method call will stuck.
Regards,
Bartłomiej
Hi Bartlomiej,
Thanks for your response. Yes, I have an at command response listener class to print out the response string. However when I send AT^SISW=1,5 to enter post data, it never print any response. Then I try to use blocking method to send and wait for response, but it is stuck at this command and never return.
Then, I try to open OutputStream fron the ATcommand instance and send post data after receive URC ^SISW: 1,1 (successful open http post connection) but java throws IOException that "ATCommand class instance is not in transparent data mode". I also try to catch response in URC listener and response listener to open data stream but it never happen.
Please see my java sourcode at: https://www.dropbox.com/s/bxm2v6fo5g5d28i/AtCmdDemo.java?dl=0
I prefer to use AT command instead of HttpConnection as its non blocking function so that the program can work on other task. With HttpConnection, there is no non-blocking functions to open, read, write data...
Thank you for your help,
Regards,
Thanh
I also put sleep time between command to ensure that the respone is print out before seding next command
Hello,
I've tried your code with some other server and unfortunately I've got the same result - the send() method hangs on AT^SISW command.
At the moment I don't know the reason for such a behaviour.
But I was able to send the post data within the "hcContent" parameter. There's a limit of 254 characters. You only need to set the "hcContLen" = 0 in order to send the data stored with "hcContent" parameter instead of using SISW.
The general advice would be that under Java the Java socket connections are the more natural and intended way instead of IP services over AT commands.
Best regards,
Bartłomiej
Hello,
you could use Java code without blocking the program. Inside your main Thread you would create another Thread which only job would be to POST the data and then terminate. So the main thread would carray on doing what should be doing while the seccond thread make HTTP POST.
Best regards,
Jure