BOOL modem_command(COM_HANDLE com_handle, const char* cmd) { char resp[128]; int i; for(i=0;i<=mdm_cmdretry;i++) { if(i) { lprintf(LOG_WARNING,"Retry #%u: sending modem command (%s) on %s", i, cmd, com_dev); lprintf(LOG_DEBUG,"Dropping DTR on %s", com_dev); if(!comLowerDTR(com_handle)) lprintf(LOG_ERR,"ERROR %u lowering DTR on %s", COM_ERROR_VALUE, com_dev); SLEEP(dtr_delay); lprintf(LOG_DEBUG,"Raising DTR on %s", com_dev); if(!comRaiseDTR(com_handle)) lprintf(LOG_ERR,"ERROR %u raising DTR on %s", COM_ERROR_VALUE, com_dev); } if(!modem_send(com_handle, cmd)) { lprintf(LOG_ERR,"ERROR %u sending modem command (%s) on %s" ,COM_ERROR_VALUE, cmd, com_dev); continue; } if(modem_response(com_handle, resp, sizeof(resp))) break; } if(i<=mdm_cmdretry) { lprintf(LOG_INFO,"Modem Response: %s", resp); return TRUE; } lprintf(LOG_ERR,"Modem command (%s) failure on %s (%u attempts)", cmd, com_dev, i); return FALSE; }
void modem_disable() { // try to power down the SIM800, then switch off power domain modem_send("AT+CPOWD=1"); modem_expect_urc(14, 7000); #if ((defined BOARD_CELL_PWR_EN_GPIO) && (defined BOARD_CELL_PWR_EN_PIN)) CSTDEBUG("GSM #### -- power off\r\n"); GPIO_WritePinOutput(BOARD_CELL_PWR_EN_GPIO, BOARD_CELL_PWR_EN_PIN, false); #endif }
Modem modem_init(int port, int baud, int retries, int min_signal) { Modem modem = (Modem)calloc(sizeof(Modem), 1); char file_descriptor[12]; char *response; modem->timeout = 5; modem->retries = retries; modem->min_signal = min_signal; modem->oldest = 1; sprintf(file_descriptor, "/dev/ttyS%d", port); modem->file = open(file_descriptor, O_RDWR | O_NOCTTY | O_NDELAY); response = modem_send(modem, "AT\r\n", 4); if (response) { modem_set_detailed_error(modem, 1); text_mode(modem); } else { free(modem); modem = NULL; fputs("Selectanotherporta", stderr); } return modem; }
BOOL wait_for_call(COM_HANDLE com_handle) { char str[128]; char* p; BOOL result=TRUE; DWORD events=0; time_t start=time(NULL); ZERO_VAR(cid_name); ZERO_VAR(cid_number); if(!comRaiseDTR(com_handle)) lprintf(LOG_ERR,"ERROR %u raising DTR", COM_ERROR_VALUE); if(com_alreadyconnected) return TRUE; if(!mdm_null) { if(mdm_init[0]) { lprintf(LOG_INFO,"Initializing modem:"); if(!modem_command(com_handle, mdm_init)) return FALSE; } if(!mdm_manswer && mdm_autoans[0]) { lprintf(LOG_INFO,"Setting modem to auto-answer:"); if(!modem_command(com_handle, mdm_autoans)) return FALSE; } if(mdm_cid[0]) { lprintf(LOG_INFO,"Enabling modem Caller-ID:"); if(!modem_command(com_handle, mdm_cid)) return FALSE; } } lprintf(LOG_INFO,"Waiting for incoming call (%s) ...", mdm_manswer ? "Ring Indication" : "Carrier Detect"); while(1) { if(terminated) return FALSE; if(comReadLine(com_handle, str, sizeof(str), /* timeout (ms): */250) > 0) { truncsp(str); if(str[0]==0) continue; lprintf(LOG_DEBUG,"Received from modem: '%s'", str); p=str; SKIP_WHITESPACE(p); if(*p) { lprintf(LOG_INFO, "Modem Message: %s", p); if(strncmp(p,"CONNECT ",8)==0) { long rate=atoi(p+8); if(rate) SAFEPRINTF2(termspeed,"%u,%u", rate, rate); } else if(strncmp(p,"NMBR",4)==0 || strncmp(p,"MESG",4)==0) { p+=4; FIND_CHAR(p,'='); SKIP_CHAR(p,'='); SKIP_WHITESPACE(p); if(cid_number[0]==0) /* Don't overwrite, if multiple messages received */ SAFECOPY(cid_number, p); } else if(strncmp(p,"NAME",4)==0) { p+=4; FIND_CHAR(p,'='); SKIP_CHAR(p,'='); SKIP_WHITESPACE(p); SAFECOPY(cid_name, p); } else if(strcmp(p,"NO CARRIER")==0) { ZERO_VAR(cid_name); ZERO_VAR(cid_number); } else if(mdm_ring[0] && strcmp(p,mdm_ring)==0 && mdm_manswer && mdm_answer[0]) { if(!modem_send(com_handle, mdm_answer)) { lprintf(LOG_ERR,"ERROR %u sending modem command (%s) on %s" ,COM_ERROR_VALUE, mdm_answer, com_dev); } } } continue; /* don't check DCD until we've received all the modem msgs */ } if(carrier_detect(com_handle)) break; if(mdm_reinit && (time(NULL)-start)/60 >= mdm_reinit) { lprintf(LOG_INFO,"Re-initialization timer elapsed: %u minutes", mdm_reinit); return TRUE; } } if(strcmp(cid_name,"P")==0) SAFECOPY(cid_name,"Private"); else if(strcmp(cid_name,"O")==0) SAFECOPY(cid_name,"Out-of-area"); if(strcmp(cid_number,"P")==0) SAFECOPY(cid_number,"Private"); else if(strcmp(cid_number,"O")==0) SAFECOPY(cid_number,"Out-of-area"); lprintf(LOG_INFO,"Carrier detected"); return TRUE; }
void Tarefa_GPRS(void) { /* task setup */ /* Tarefa do Logger para comunicacao com modem na UART2 a 9600 bps */ #define SEND_STRING0 "AT+CIPSENDI=0,\"GET node/set.json?nodeid=10&data=" #define SEND_STRING1 "}&apikey=90a004390f3530d0ba10199ac2b1ac3d HTTP/1.1\\r\\nHost: emon-gpsnetcms.rhcloud.com\\r\\n\\r\\n\\r\\n\"" #define SEND_STRING2 "AT+CIPSENDI=0,\"GET /input/post.json?json={pow:" //#define SEND_STRING3 ("}&" API_KEY " HTTP/1.1\r\nHost: " ESP_TCP_SERVER_NAME "\r\n\r\n\r\n") #define SEND_STRING3 "}&apikey=90a004390f3530d0ba10199ac2b1ac3d HTTP/1.1\\r\\nHost: emon-gpsnetcms.rhcloud.com\\r\\n\\r\\n\\r\\n\"" const char resp_200_OK[] = "HTTP/1.1 200 OK"; #define REPLY_LENGTH (sizeof(resp_200_OK) + 2) char valor[] = "01"; INT8U cnt = 1; volatile char c; static char resp_serv[REPLY_LENGTH]; static INT8U resp_cnt = 0; INT16U tentativas = 0; /* init ESP_UART */ uart_init(ESP_UART,ESP_BAUD,ESP_UART_BUFSIZE,ESP_UART_PINS,ESP_MUTEX,ESP_MUTEX_PRIO); /* init modem */ modem_send("AT\r\n"); modem_wait_reply(); modem_send("AT+CWMODE=3\r\n"); modem_wait_reply(); modem_send(("AT+CWJAP=\"" ESP_AP "\",\"" ESP_PWD "\"\r\n")); modem_wait_reply(); modem_send("AT+CIPCREATE=\"TCP\",10201,2048\r\n"); modem_wait_reply(); modem_send("AT+CIPCONNECT=0,\"54.160.189.224\",80\r\n"); modem_wait_reply(); /* task main loop */ for (;;) { DelayTask(5000); /* delay de 5s */ /* Comando de envio GET /input/post.json?json={campo:valor}&apikey=90a004390f3530d0ba10199ac2b1ac3d HTTP/1.1 Host:emon-gpsnetcms.rhcloud.com GET /node/set.json?nodeid=10&data=20,20,20,20&apikey=90a004390f3530d0ba10199ac2b1ac3d HTTP/1.1 Host:emon-gpsnetcms.rhcloud.com */ tentativas++; modem_send(SEND_STRING2); sprintf(valor,"%d", cnt); modem_send(valor); modem_send(SEND_STRING3); NEWLINE(); //modem_wait_reply(); INT8U keep_data = 0; DelayTask(1000); /* delay de 1s */ resp_cnt = 0; while((c=modem_receive()) != (CHAR8)-1) { putchar_usb(c); if (keep_data < 2){ if (c=='+') { keep_data++; } }else{ resp_server_ok[resp_cnt++]=c; if(resp_cnt == 9) { if(c=='1'){ resp_cnt = 0; break; } } } } while((c=modem_receive()) != (CHAR8)-1) { putchar_usb(c); } modem_send("AT+CIPRD=0\r\n"); DelayTask(10); /* delay de 10ms */ /* resposta: HTTP/1.1 200 OK .Date: Wed, 03 Dec 2014 18:43:17 GMT .Server: Apache/2.2.15 (Red Hat) .Content-Length: 2 .Content-Type: application/json .Vary: Accept-Encoding .Accept-Ranges: none . .ok */ while((c=modem_receive()) != (CHAR8)-1) { putchar_usb(c); } cnt++; /* TODO: timeout */ #if 0 while(1){ c=modem_receive(); if ((c != '\r') && (c != '\n') && ((resp_cnt < sizeof(resp_serv)-2))){ resp_serv[resp_cnt++]=c; }else{ resp_serv[resp_cnt++]='\0'; if(strcmp (resp_serv,resp_200_OK) == 0){ cnt = (++cnt)%100; } resp_cnt = 0; break; } } #endif } }
bool modem_enable() { char response[10]; size_t len; #if BOARD_CELL_PWR_DOMAIN CSTDEBUG("GSM #### -- power on\r\n"); GPIO_WritePinOutput(BOARD_CELL_PWR_EN_GPIO, BOARD_CELL_PWR_EN_PIN, true); // TODO check that power has come up correctly #endif // after enabling power, power on the SIM800 while (modem_read() != -1) /* clear buffer */; // we need to identify if the chip is already on by sending AT commands // send AT and just ignore the echo and OK to get into a stable state // sometimes there is initial noise on the serial line modem_send("AT"); len = modem_readline(response, 9, 500); CIODEBUG("GSM (%02d) -> '%s'\r\n", len, response); len = modem_readline(response, 9, 500); CIODEBUG("GSM (%02d) -> '%s'\r\n", len, response); // now identify if the chip is actually on, by issue AT and expecting something // if we can't read a response, either AT or OK, we need to run the power on sequence modem_send("AT"); len = modem_readline(response, 9, 1000); CIODEBUG("GSM (%02d) -> '%s'\r\n", len, response); if (!len) { CSTDEBUG("GSM #### !! trigger PWRKEY\r\n"); #if defined(BOARD_UBIRCH_1R02) // there is a bug in the circuit on the board which does not use an extra // transistor to switch the PWRKEY pin, so the signals are reversed // power on the SIM800H GPIO_WritePinOutput(BOARD_CELL_PIN_GPIO, BOARD_CELL_PWRKEY_PIN, true); delay(10); //10ms GPIO_WritePinOutput(BOARD_CELL_PIN_GPIO, BOARD_CELL_PWRKEY_PIN, false); delay(1100); // 1.1s GPIO_WritePinOutput(BOARD_CELL_PIN_GPIO, BOARD_CELL_PWRKEY_PIN, true); #else // power on the cell phone chip GPIO_WritePinOutput(BOARD_CELL_PIN_GPIO, BOARD_CELL_PWRKEY_PIN, false); delay(10); //10ms GPIO_WritePinOutput(BOARD_CELL_PIN_GPIO, BOARD_CELL_PWRKEY_PIN, true); delay(1100); // 1.1s GPIO_WritePinOutput(BOARD_CELL_PIN_GPIO, BOARD_CELL_PWRKEY_PIN, false); #endif } else { CSTDEBUG("GSM #### !! already on\r\n"); } bool is_on = false; // wait for the chip to boot and react to commands for (int i = 0; i < 5; i++) { modem_send("ATE0"); // if we still have echo on, this fails and falls through to the next OK if ((is_on = modem_expect_OK(1000))) break; if ((is_on = modem_expect_OK(1000))) break; } return is_on; }