I am currently in a arduino project to read some parameters from another microcontroller serially and the arduino is supposed to transmit these serially to dweet.io cloud. I use SIM900 modem for this. But my problem is that if i put some data to the variables, then the post to the cloud becomes unsuccessfull. Without any data in all parameters, the code runs fine.
Please find the code
char code[4] = {'0円'}, voltage[4] = {'0円'}, current[4] = {'0円'}, mcurrent[4] = {'0円'}, acurrent[4] = {'0円'}, power[4] = {'0円'}, pf[4] = {'0円'}, freq[4] = {'0円'}, temp[4] = {'0円'};
const char APN[18] = "bsnlnet";
byte dtype = 0;
#include <avr/pgmspace.h>
void read_comp() {
byte flag = 0, index = 0, l = 0;
char c;
char readings[8] = {'0円'};
dtype = 0;
do {
while (Serial.available() && l == 0) {
c = Serial.read();
delay(20);
if (c == '\n') {
l = 1;
break;
} else {
readings[index] = c;
index++;
}
}
readings[index] = '0円';
index = 0;
l = 0;
if (readings[0] == 'v' || readings[0] == 'V') { //check if received reading is voltage
dtype = 1;
flag = 1;
if (readings[1] == 'o') {
flag = 100;
}
} else if (readings[0] == 'c' || readings[0] == 'C') { //check if received reading is current
dtype = 2;
flag = 1;
if (readings[1] == 'u') {
flag = 100;
}
} else if (flag == 1 && readings[1] == 'o') {
dtype = 9;
flag = 100;
} else if (readings[0] == 'm' || readings[0] == 'M') {
dtype = 3;
flag = 1;
if (readings[1] == 'c' && flag == 1) {
flag = 100;
}
} else if (readings[0] == 'a' || readings[0] == 'A') {
dtype = 4;
flag = 1;
if (readings[1] == 'c') {
flag = 100;
}
} else if (readings[0] == 'p' || readings[0] == 'P') {
flag = 1;
if (readings[1] == 'o') {
flag = 100;
dtype = 5;
} else if (readings[1] == 'f' || readings[1] == 'F') {
flag = 100;
dtype = 6;
}
} else if (readings[0] == 'f' || readings[0] == 'F') {
dtype = 7;
flag = 1;
if (readings[1] == 'r') {
flag = 100;
}
} else if (readings[0] == 't' || readings[0] == 'T') {
dtype = 8;
flag = 1;
if (readings[1] == 'e') {
flag = 100;
}
}
if (flag == 100) {
switch (dtype) {
case 1:
voltage[0] = readings[3];
voltage[1] = readings[4];
voltage[2] = readings[5];
voltage[3] = readings[6];
voltage[4] = '0円';
flag = 0;
break;
case 2:
current[0] = readings[3];
current[1] = readings[4];
current[2] = readings[5];
current[3] = readings[6];
current[4] = '0円';
flag = 0;
break;
case 3:
mcurrent[0] = readings[3];
mcurrent[1] = readings[4];
mcurrent[2] = readings[5];
mcurrent[3] = readings[3];
mcurrent[4] = '0円';
flag = 0;
break;
case 4:
acurrent[0] = readings[3];
acurrent[1] = readings[4];
acurrent[2] = readings[5];
acurrent[3] = readings[6];
acurrent[4] = '0円';
flag = 0;
break;
case 5:
power[0] = readings[3];
power[1] = readings[4];
power[2] = readings[5];
power[3] = readings[6];
power[4] = '0円';
flag = 0;
break;
case 6:
pf[0] = readings[3];
pf[1] = readings[4];
pf[2] = readings[5];
pf[3] = readings[6];
pf[4] = '0円';
flag = 0;
break;
case 7:
freq[0] = readings[3];
freq[1] = readings[4];
freq[2] = readings[5];
freq[3] = readings[6];
freq[4] = '0円';
flag = 0;
break;
case 8:
temp[0] = readings[3];
temp[1] = readings[4];
temp[2] = readings[5];
temp[3] = readings[6];
temp[4] = '0円';
flag = 0;
break;
case 9:
code[0] = readings[3];
code[1] = readings[4];
code[2] = readings[5];
code[3] = readings[6];
code[4] = '0円';
dtype = 10;
flag = 0;
break;
}
}
} while (dtype != 10);
}
void modem_initialization(void) {
char rec_data, r;
byte network_status = 0 ;//network_status initialized as zero
byte status_check = 0 ;//status_check initialized as zero
byte Serial3_cnt = 0;//Serial3_cnt initialized as zero
byte ok_flag = 0; //ok_flag initialized as zero
byte count = 0; //count initialized as zero
clear_rx_buffer();
while (Serial3_cnt < 4) { // repeat entire loop until Serial3_cnt less than 5
switch (Serial3_cnt) {
case 0:
clear_rx_buffer();//clearing receiving buffer
Serial3.println(F("AT"));// Attention command to wake up Serial3 modem
delay(1000);
break;
case 1:
clear_rx_buffer();//clearing receiving buffer
Serial3.println(F("ATE0"));//Command for disable echo
delay(1000);
break;
/*case 2: clear_rx_buffer();//clearing receiving buffer
// Serial3.println("ATV0");// Command for numeric response after this '0'(zero) will be recieved instead of "OK"
delay(1000);
break;*/
case 2:
clear_rx_buffer();//clearing receiving buffer
Serial3.println(F("AT&W"));// Command TO SAVE SETTINGS
delay(1000);
break;
case 3:
Serial3_cnt = 4;// exit from the loop
break;
default :
break;
}
while (Serial3.available() > 0) {
rec_data = Serial3.read();
if (rec_data == 'O') { // 'o' is recieved
ok_flag = 1;
} else if (ok_flag == 1 && rec_data == 'K') { // 'K' is recieved
Serial3_cnt ++;
ok_flag = 0;
} else if (rec_data == '0') { // '0'(zero) is recieved (numeric response)
Serial3_cnt ++;
ok_flag = 0;
} else if (rec_data == '+') { // '+' is recieved message
clear_rx_buffer();
if (Serial3_cnt > 0) {
Serial3_cnt --;
}
}
}
}
Serial3_cnt = 0;
ok_flag = 0;
while (network_status == 0) { //wait for PIN READY
if (status_check == 0) {
delay(1000);
status_check = 1 ;
clear_rx_buffer();
Serial3.println(F("AT+CPIN?")); //checkin PIN return ready(+CPIN: READY) with a valid simcard otherwise error
}
while (Serial3.available() > 0) {
rec_data = Serial3.read();
if (rec_data == '+' && status_check == 1) { // '+' is recieved
status_check = 2 ;
} else if (rec_data == 'C' && status_check == 2) { // 'C' is recieved
status_check = 3 ;
} else if (rec_data == 'P' && status_check == 3) { // 'P' is recieved
status_check = 4 ;
} else if (rec_data == 'I' && status_check == 4) { // 'I' is recieved
status_check = 5 ;
} else if (rec_data == 'N' && status_check == 5) { // 'N' is recieved
status_check = 6 ;
} else if (rec_data == ':' && status_check == 6) { // ':' is recieved
status_check = 7 ;
} else if (rec_data == ' ' && status_check == 7) { // ' ' is recieved
status_check = 8 ;
} else if (rec_data == 'R' && status_check == 8) { // 'R' is recieved
status_check = 9 ;
} else if (rec_data == 'E' && status_check == 9) { // 'E' is recieved
status_check = 10 ;
} else if (rec_data == 'A' && status_check == 10) { // 'A' is recieved
status_check = 11 ;
} else if (rec_data == 'D' && status_check == 11) { // 'D' is recieved
status_check = 12 ;
} else if (rec_data == 'Y' && status_check == 12) { // 'Y' is recieved
status_check = 13 ;
} else if (rec_data == 0X0D && status_check == 13) { //Carriage return
status_check = 14 ;
} else if (rec_data == 0X0A && status_check == 14) { //Line Feed
status_check = 15 ;
} else if (rec_data == 'O' && status_check == 15) { // '0' is recieved
status_check = 16;
} else if (rec_data == 'K' && status_check == 16) {
clear_rx_buffer();
status_check = 0 ;
network_status = 1; //goto next step
} else if (rec_data != 'R' && status_check == 8) { //+CPIN: NOT READY
clear_rx_buffer();
status_check = 0 ;
network_status = 0; //repeat current step
} else if (rec_data == 'M' && status_check == 3) { // in case of any message
clear_rx_buffer();
status_check = 0 ;
network_status = 0; //repeat current step
}
}
}
while (network_status == 1) { //wait for SIM network registration
if (status_check == 0) {
delay(1000);
status_check = 1 ;
clear_rx_buffer();
Serial3.println(F("AT+CREG?")); //checking for SIM card registration ,if registerd "+CREG: 0,1" will receive
}
while (Serial3.available() > 0) {
rec_data = Serial3.read();
if (rec_data == '+' && status_check == 1) { // '+' is recieved
status_check = 2 ;
} else if (rec_data == 'C' && status_check == 2) { // 'C' is recieved
status_check = 3 ;
} else if (rec_data == 'R' && status_check == 3) { // 'R' is recieved
status_check = 4 ;
} else if (rec_data == 'E' && status_check == 4) { // 'E' is recieved
status_check = 5 ;
} else if (rec_data == 'G' && status_check == 5) { // 'G' is recieved
status_check = 6 ;
} else if (rec_data == ':' && status_check == 6) { // ':' is recieved
status_check = 7 ;
} else if (rec_data == ' ' && status_check == 7) { // ' ' is recieved
status_check = 8 ;
} else if (rec_data == '0' && status_check == 8) { // '0' is recieved
status_check = 9 ;
} else if (rec_data == ',' && status_check == 9) { // ',' is recieved
status_check = 10 ;
} else if (rec_data == '1' && status_check == 10) { // '1' is recieved
status_check = 11 ;
} else if (rec_data == 0X0D && status_check == 11) { //Carriage return
status_check = 12 ;
} else if (rec_data == 0X0A && status_check == 12) { //Line Feed
status_check = 12 ;
} else if (rec_data == 'O' && status_check == 12) { // '0' is recieved
status_check = 13;
} else if (rec_data == 'K' && status_check == 13) {
clear_rx_buffer();
status_check = 0 ;
network_status = 2; //goto next step
} else if (rec_data != '1' && status_check == 10) { // +CREG: 0,2 not registered
clear_rx_buffer();
status_check = 0 ;
network_status = 1; //repeat current step
} else if (rec_data == 'M' && status_check == 3) { // in case of any message
clear_rx_buffer();
status_check = 0 ;
network_status = 1; //repeat current step
}
}
}
Serial3_cnt = 0;
while (Serial3_cnt < 4) { // repeat entire loop until Serial3_cnt less than 4
switch (Serial3_cnt) {
case 0:
clear_rx_buffer();//clearing receiving buffer
Serial3.println(F("AT+CMGF=1"));// Attention command to wake up Serial3 modem
delay(1000);
break;
case 1:
clear_rx_buffer();//clearing receiving buffer
Serial3.println(F("AT+CNMI=2,1,0,0,0"));//Command to configure new message indication
delay(1000);
break;
case 2:
clear_rx_buffer();//clearing receiving buffer
Serial3.println(F("AT+CMGD=1,4"));// Command to delete all received messages
delay(1000);
break;
case 3:
Serial3_cnt = 4;// exit from the loop
break;
default :
break;
}
while (Serial3.available() > 0) {
rec_data = Serial3.read();
if (rec_data == 'O') { // '0'(zero) is recieved (numeric response)
rec_data = Serial3.read();
if (rec_data == 'K') {
Serial3_cnt ++;
}
} else if (rec_data == '+') { // '+' recieved ,before "AT+CIICR" command,may be any message
clear_rx_buffer();
if (Serial3_cnt > 0) {
Serial3_cnt --;
}
} else if (rec_data == 'E' && Serial3_cnt > 2) { // '4' recieved (error),in gprs initialisation commands
clear_rx_buffer();
Serial3_cnt = 4;
}
}
}
Serial3_cnt = 0;
}
void clear_rx_buffer(void) {
Serial3.flush();
}
void gprs_init() {
byte st = 1, i = 0, response = 0;
char r[10];
while (st <= 9) {
delay(1000);
clear_rx_buffer();
switch (st) {
case 1:
Serial3.println(F("AT+CGATT?"));
break;
case 2:
Serial3.println(F("AT+CIPSHUT"));
break;
case 3:
Serial3.println(F("AT+CIPMUX=0"));
break;
case 4:
response = cmd1_check(1);
break;
case 5:
Serial3.println(F("AT+CIICR"));
delay(1000);
break;
case 6:
Serial3.println(F("AT+CIFSR"));
break;
case 7:
response = cmd1_check(2);
delay(2200);
clear_rx_buffer();
break;
case 8:
Serial3.println(F("AT+CIPSEND"));
break;
case 9:
Serial3_send();
++st;
break;
}
if (st == 10) {
clear_rx_buffer();
}
delay(1000);
while (Serial3.available() && st < 9 && st != 7 && st != 4) {
r[i] = Serial3.read();
i++;
}
i = 0;
if (strstr(r, "ALREADY") != NULL) {
Serial3.println("AT+CIPSHUT");
delay(50);
clear_rx_buffer();
st = 2;
r[0] = '0円';
}
if (strstr(r, "OK") != NULL || strstr(r, "+CGATT") != NULL || (strstr(r, ".") != NULL && st == 6) || strstr(r, ">") != NULL) {
st++;
} else if (strstr(r, "ERR") != NULL) {
break;
} else if (response == 1 && (st == 7 || st == 4)) {
st++;
response = 0;
}
}
}
char cmd1_check(short int o) {
byte timout1 = 10500, m1, p1 = 0, resp1 = 0;
char response1[25] = {'0円'};
m1 = millis();
clear_rx_buffer();
if (o == 1) {
Serial3.print(F("AT+CSTT="));
Serial3.print('"');
Serial3.print(APN);
Serial3.print('"');
Serial3.print(',');
Serial3.print('"');
Serial3.print('"');
Serial3.print(',');
Serial3.print('"');
Serial3.println('"');
delay(3000);
} else if (o == 2) {
Serial3.print(F("AT+CIPSTART="));
Serial3.print('"');
Serial3.print(F("TCP"));
Serial3.print('"');
Serial3.print(',');
Serial3.print('"');
Serial3.print(F("dweet.io"));
Serial3.print('"');
Serial3.print(',');
Serial3.println(80);
delay(2000);
}
do {
p1 = 0;
while (Serial3.available()) {
response1[p1] = Serial3.read();
p1++;
delay(20);
}
if (strstr(response1, "OK") != NULL && o == 1) {
resp1 = 1;
} else if (strstr(response1, "ERR") != NULL) {
resp1 = 2;
} else if (strstr(response1, "CONNECT OK") != NULL && o == 2) {
resp1 = 1;
}
} while ((millis() - m1) <= timout1 && resp1 == 0);
return (resp1);
}
void Serial3_send() {
Serial3.print("POST /dweet/for/");
Serial3.print("mythingname");
Serial3.print("?vo=");
Serial3.print(voltage);
Serial3.print("&cu=");
Serial3.print(current);
Serial3.print("&Mc=");
Serial3.print(mcurrent);
Serial3.print("&Ac=");
Serial3.print(acurrent);
Serial3.print("&Po=");
Serial3.print(power);
Serial3.print("&PF=");
Serial3.print(pf);
Serial3.print("&fr=");
Serial3.print(freq);
Serial3.print("&Te=");
Serial3.print(temp);
Serial3.println(" HTTP/1.1");
Serial3.println("connection: keep-alive");
Serial3.println();
Serial3.println();
Serial3.write(26);
Serial3.println();
delay(10000);
Serial3.println("AT+CIPSHUT");
delay(50);
}
void setup() {
Serial.begin(9600);
Serial3.begin(9600);
modem_initialization();
}
void loop() {
dtype = 0;
read_comp();
gprs_init();
}
1 Answer 1
This looks like you are constructing the data to be sent as GET parameters (i.e as part of the querystring), whereas I believe dweet.io expects them as part of the POST body - as seen here.
There seem to be many people struggling to successfully send HTTP POSTs with a SIM900, but I think this answer outlines the correct sequence to make it work.