无法使用SIM800L在Thingspeak上发送连续数据

时间:2019-10-04 13:40:21

标签: tcp microcontroller pic

我正在尝试通过与SIM800L和PIC16F1936的TCP / IP连接在Thingspeak上发送数据。但是它并没有向峰值数据发送数据,同时ADC值随着模拟输入的变化而变化。我找不到我想要的东西。

#include "mcc_generated_files/mcc.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>

#define _XTAL_FREQ 16000000

#define RS RB0
#define RW RB1
#define EN RB2

char buff[80], a;
char data[80];
int flag, b, i = 0;
int adc_val;
bool conn = false;
bool server = false;
float f;
int e;
int val = 0;
char value[5];
//char b,c,d;

void server_conn();
void tcp_send();
void adc_display();
int adc_read(char);
void adc_init();
void send_value();

void lcd_cmd(unsigned char cmd)
{
    PORTB = (0xF0 & cmd);
    RS = 0;
    RW = 0;
    EN = 1;
    __delay_ms(5);
    EN = 0;

    PORTB = (cmd<<4 & 0xf0);
    RS = 0;
    RW = 0;
    EN = 1;
    __delay_ms(5);
    EN = 0;
}

void lcd_data(unsigned char data)
{
    PORTB = (0xF0 & data);
    RS = 1;
    RW = 0;
    EN = 1;
    __delay_ms(5);
    EN = 0;

    PORTB = (data<<4 & 0xf0);
    RS = 1;
    RW = 0;
    EN = 1;
    __delay_ms(5);
    EN = 0;
}

void lcd_string(char *str)
{
    while(*str != '\0')
    {
        lcd_data(*str++);
    }
}

void lcd_init()
{
    lcd_cmd(0x02);///return to home
    lcd_cmd(0x28);///4bitmode
    lcd_cmd(0x0C);///cursor off
    lcd_cmd(0x01);///display clear
    lcd_cmd(0x06);///increment cursor

}

void uart_init()
{
    SPBRG = 25;     ////9600 baud rate at 16MHz FOSC
    SYNC = 0;       
    BRGH = 0;
    BRG16 = 0;

    CREN = 1;
    TXEN = 1;
    SPEN = 1;

    GIE = 1;
    PEIE = 1;
    RCIE = 1;
    RCIF = 0;

    TRISC6 = 0;     ////TX pin
    TRISC7 = 1;     ////RX pin
}


void gsm_send_char(unsigned char data)
{
    while(!TXIF);
    TXREG = data;
}

char gsm_receive_char()
{
    while(!RCIF);
    return RCREG;
}


void gsm_send_string(char *p)
{
    while(*p != '\0')
    {
        gsm_send_char(*p);
        p++;
    }
}


__interrupt() void isr(void)
{
    if(RCIF == 1)
    {
        RCIF = 0;
        a =gsm_receive_char();
        buff[i] = a;
        i++;
    }
}


void main(void)
{
    SYSTEM_Initialize();

    ANSELA = 0x01;          ////Select RA0 as analog
    TRISA = 0xFF;           ////input PORTA for ADC
    TRISB = 0x00;
    ANSELB = 0x00;

    lcd_init();
    uart_init();

    lcd_cmd(0x01);
    server_conn();

    __delay_ms(2000);
    adc_init();

    while (1)
    {
        lcd_cmd(0xC0);
        lcd_string("SENDING DATA");
        adc_display();
        tcp_send();
        __delay_ms(1000);

    }
}

void server_conn()
{
    if(server == false)
    {
        i=0;
        gsm_send_string("AT\r");
        __delay_ms(500);
        buff[sizeof(buff)-1] = '\0';
        if(strstr(buff, "OK")!= NULL)
        {
            lcd_cmd(0xC0);
            lcd_string("OK");
            memset(buff, 0, sizeof(buff));
        }
        else{lcd_cmd(0xC0);
            lcd_string("AT ERROR");
        }

        i=0;
        gsm_send_string("AT+CIPSHUT\r");
        __delay_ms(1000);
        buff[sizeof(buff)-1] = '\0';
        if(strstr(buff, "OK")!= NULL)
        {
            lcd_cmd(0xC0);
            lcd_string("CIPSHUT OK");
            memset(buff, 0, sizeof(buff));
        }
        else{lcd_cmd(0xC0);
            lcd_string("CIPSHUT ERROR");
        }

        i=0;
        gsm_send_string("AT+CSTT=\"airtelgprs.com\"\r\n");
        __delay_ms(1000);
        buff[sizeof(buff)-1] = '\0';
        if(strstr(buff, "OK")!= NULL)
        {
            lcd_cmd(0xC0);
            lcd_string("CSTT OK");
            memset(buff, 0, sizeof(buff));
        }
        else{lcd_cmd(0xC0);
            lcd_string("CSTT ERROR");
        }

        i=0;
        gsm_send_string("AT+CIICR\r");
        __delay_ms(5000);
        buff[sizeof(buff)-1] = '\0';
        if(strstr(buff, "OK")!=NULL)
        {
            lcd_cmd(0xC0);
            lcd_string("CIICR OK");
            memset(buff, 0, sizeof(buff));
        }
        else{lcd_cmd(0xC0);
            lcd_string("CIICR ERROR");
        }

        i=0;
        gsm_send_string("AT+CIFSR\r");
        __delay_ms(5000);
        buff[sizeof(buff)-1] = '\0';
        if(strstr(buff, ".")!=NULL)
        {
            lcd_cmd(0xC0);
            lcd_string("CIFSR OK");
            memset(buff, 0, sizeof(buff));
        }
        else{lcd_cmd(0xC0);
            lcd_string("CIFSR ERROR\r");
        }

        i=0;
        gsm_send_string("AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",\"80\"\r");
        __delay_ms(5000);
        buff[sizeof(buff)-1] = '\0';
        if(strstr(buff, "CONNECT OK")!=NULL)
        {
            lcd_cmd(0xC0);
            lcd_string("CIPSTART OK");
            __delay_ms(500);
            conn = true;
            memset(buff, 0, sizeof(buff));
        }
        else{lcd_cmd(0xC0);
        lcd_string("CIPSTART ERROR");
        conn = false;
        }
}
}


void tcp_send()
{
    if(conn == true)
    {
    i=0;
    gsm_send_string("AT+CIPSEND\r");
    __delay_ms(2000);
    buff[sizeof(buff)-1] = '\0';
    if(strstr(buff, ">")!= NULL)
    {
        //gsm_send_string("GET https://api.thingspeak.com/update?api_key=KKNCJ61S64YMHJ9D&field1=");
        //gsm_send_string(value);
        strcat(data, "GET https://api.thingspeak.com/update?api_key=KKNCJ61S64YMHJ9D&field1=");
        strcat(data, value);
        gsm_send_string(data);
        gsm_send_string("\r\n");

        __delay_ms(4000);

            if(strstr(buff, "SEND OK")!=NULL)
            {
                gsm_send_char(0x1A);        ////CTRL Z
                lcd_cmd(0xC0);
                lcd_string("SEND OK");
                memset(buff, 0, sizeof(buff));
            }
    }

}
    else{}
}


void adc_init()
{
    //ADCON0 = 0x01;          /////ADON = 0 ADC channel 0
    ADCON1 = 0xE0;          ////Right justified, FOSC/64 4us at 16MHz acc to datasheet
}

int adc_read(char ch)
{   
    //ADCON0 = 0x00;          
    ADCON0 = (ch<<2);
    ADON = 1;
    __delay_ms(2);
    GO_nDONE = 1;
    while(GO_nDONE);
    //ADON = 0;
    return((ADRESH<<8)+ADRESL);
}

void adc_display()
{
    adc_val = adc_read(0);
    f = (float)(adc_val*5)/1024;
    e = f*10;

    value[0] = e/10 + '0';     ////2 of 25
    value[1] = '.';
    value[2] = e%10 + '0';     ////5 of 25
    value[sizeof(value)-1] = '\0';

    lcd_cmd(0x80);
    lcd_string("VAL : ");
    lcd_string(value);

}

它在while循环中卡在“ SENDING DATA”上,但Analog val工作正常。

我对该程序做了一些更改。现在它正在发送数据,但是只有一次。 我正在尝试发送连续数据,但到目前为止我还没有运气。 下面是代码。

#include "mcc_generated_files/mcc.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>

#define _XTAL_FREQ 16000000

#define RS RB0
#define RW RB1
#define EN RB2

char buff[80], a;
char data[80];
int flag, b, i = 0;
int adc_val;
bool conn = false;
bool server = false;
float f;
int e;
int val = 0;
char value[5];
//char b,c,d;

void server_conn();
void tcp_send();
void adc_display();
int adc_read(char);
void adc_init();
void send_value();

void lcd_cmd(unsigned char cmd)
{
    PORTB = (0xF0 & cmd);
    RS = 0;
    RW = 0;
    EN = 1;
    __delay_ms(5);
    EN = 0;

    PORTB = (cmd<<4 & 0xf0);
    RS = 0;
    RW = 0;
    EN = 1;
    __delay_ms(5);
    EN = 0;
}

void lcd_data(unsigned char data)
{
    PORTB = (0xF0 & data);
    RS = 1;
    RW = 0;
    EN = 1;
    __delay_ms(5);
    EN = 0;

    PORTB = (data<<4 & 0xf0);
    RS = 1;
    RW = 0;
    EN = 1;
    __delay_ms(5);
    EN = 0;
}

void lcd_string(char *str)
{
    while(*str != '\0')
    {
        lcd_data(*str++);
    }
}

void lcd_init()
{
    lcd_cmd(0x02);///return to home
    lcd_cmd(0x28);///4bitmode
    lcd_cmd(0x0C);///cursor off
    lcd_cmd(0x01);///display clear
    lcd_cmd(0x06);///increment cursor

}

void uart_init()
{
    SPBRG = 25;     ////9600 baud rate at 16MHz FOSC
    SYNC = 0;       
    BRGH = 0;
    BRG16 = 0;

    CREN = 1;
    TXEN = 1;
    SPEN = 1;

    GIE = 1;
    PEIE = 1;
    RCIE = 1;
    RCIF = 0;

    TRISC6 = 0;     ////TX pin
    TRISC7 = 1;     ////RX pin
}


void gsm_send_char(unsigned char data)
{
    while(!TXIF);
    TXREG = data;
}

char gsm_receive_char()
{
    while(!RCIF);
    return RCREG;
}


void gsm_send_string(char *p)
{
    while(*p != '\0')
    {
        gsm_send_char(*p);
        p++;
    }
}


__interrupt() void isr(void)
{
    if(RCIF == 1)
    {
        RCIF = 0;
        a =gsm_receive_char();
        buff[i] = a;
        i++;
    }
}


void main(void)
{
    SYSTEM_Initialize();

    ANSELA = 0x01;          ////Select RA0 as analog
    TRISA = 0xFF;           ////input PORTA for ADC
    TRISB = 0x00;
    ANSELB = 0x00;

    lcd_init();
    uart_init();

    lcd_cmd(0x01);
    server_conn();

    __delay_ms(2000);
    adc_init();

    while (1)
    {
        lcd_cmd(0xC0);
        lcd_string("SENDING DATA");
        adc_display();
        tcp_send();
        __delay_ms(1000);

    }
}

void server_conn()
{
    if(server == false)
    {
        i=0;
        gsm_send_string("AT\r");
        __delay_ms(500);
        buff[sizeof(buff)-1] = '\0';
        if(strstr(buff, "OK")!= NULL)
        {
            lcd_cmd(0xC0);
            lcd_string("OK");
            memset(buff, 0, sizeof(buff));
        }
        else{lcd_cmd(0xC0);
            lcd_string("AT ERROR");
        }

        i=0;
        gsm_send_string("AT+CIPSHUT\r");
        __delay_ms(1000);
        //buff[sizeof(buff)-1] = '\0';
        if(strstr(buff, "OK")!= NULL)
        {
            lcd_cmd(0xC0);
            lcd_string("CIPSHUT OK");
            memset(buff, 0, sizeof(buff));
        }
        else{lcd_cmd(0xC0);
            lcd_string("CIPSHUT ERROR");
        }

        i=0;
        gsm_send_string("AT+CSTT=\"airtelgprs.com\"\r\n");
        __delay_ms(1000);
       // buff[sizeof(buff)-1] = '\0';
        if(strstr(buff, "OK")!= NULL)
        {
            lcd_cmd(0xC0);
            lcd_string("CSTT OK");
            memset(buff, 0, sizeof(buff));
        }
        else{lcd_cmd(0xC0);
            lcd_string("CSTT ERROR");
        }

        i=0;
        gsm_send_string("AT+CIICR\r");
        __delay_ms(5000);
        //buff[sizeof(buff)-1] = '\0';
        if(strstr(buff, "OK")!=NULL)
        {
            lcd_cmd(0xC0);
            lcd_string("CIICR OK");
            memset(buff, 0, sizeof(buff));
        }
        else{lcd_cmd(0xC0);
            lcd_string("CIICR ERROR");
        }

        i=0;
        gsm_send_string("AT+CIFSR\r");
        __delay_ms(5000);
        //buff[sizeof(buff)-1] = '\0';
        if(strstr(buff, ".")!=NULL)
        {
            lcd_cmd(0xC0);
            lcd_string("CIFSR OK");
            memset(buff, 0, sizeof(buff));
        }
        else{lcd_cmd(0xC0);
            lcd_string("CIFSR ERROR\r");
        }

        i=0;
        gsm_send_string("AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",\"80\"\r");
        __delay_ms(5000);
        //buff[sizeof(buff)-1] = '\0';
        if(strstr(buff, "CONNECT OK")!=NULL)
        {
            lcd_cmd(0xC0);
            lcd_string("CIPSTART OK");
            __delay_ms(500);
            conn = true;
            memset(buff, 0, sizeof(buff));
        }
        else{lcd_cmd(0xC0);
        lcd_string("CIPSTART ERROR");
        conn = false;
        }
}
}

void tcp_send()
{
    if(conn == true)
    {
    i=0;
    gsm_send_string("AT+CIPSEND\r");
    __delay_ms(2000);
    //buff[sizeof(buff)-1] = '\0';
    if(strstr(buff, ">")!= NULL)
    {
        //gsm_send_string("GET https://api.thingspeak.com/update?api_key=KKNCJ61S64YMHJ9D&field1=");
        //gsm_send_string(value);
        strcat(data, "GET https://api.thingspeak.com/update?api_key=KKNCJ61S64YMHJ9D&field1=");
        strcat(data, value);
        gsm_send_string(data);
        gsm_send_string("\r\n");
        __delay_ms(4000);
        gsm_send_char(0x1A);        ////CTRL Z
    }
        i=0;
       // buff[sizeof(buff)-1] = '\0';
        if(strstr(buff, "SEND OK")!=NULL)
         {
            lcd_cmd(0xC0);
            lcd_string("SEND OK");
            memset(buff, 0, sizeof(buff));
         }
    }   
}


void adc_init()
{
    //ADCON0 = 0x01;          /////ADON = 0 ADC channel 0
    ADCON1 = 0xE0;          ////Right justified, FOSC/64 4us at 16MHz acc to datasheet
}

int adc_read(char ch)
{   
    //ADCON0 = 0x00;          
    ADCON0 = (ch<<2);
    ADON = 1;
    __delay_ms(2);
    GO_nDONE = 1;
    while(GO_nDONE);
    //ADON = 0;
    return((ADRESH<<8)+ADRESL);
}

void adc_display()
{
    adc_val = adc_read(0);
    f = (float)(adc_val*5)/1024;
    e = f*10;

    value[0] = e/10 + '0';     ////2 of 25
    value[1] = '.';
    value[2] = e%10 + '0';     ////5 of 25
    value[sizeof(value)-1] = '\0';

    lcd_cmd(0x80);
    lcd_string("VAL : ");
    lcd_string(value);

}

发送数据后,它卡在了while循环中的SENDING DATA上,但ADC值仍然更改。

0 个答案:

没有答案