单片机学习小组
直播中

刘继牛

7年用户 945经验值
私信 关注

怎样去编写esp8266tcp连接并获取心之天气的代码程序呢

怎样去编写esp8266tcp连接并获取心之天气的代码程序呢?

回帖(1)

李枫芸

2022-2-22 14:19:49
#include "ets_sys.h"
#include "osapi.h"
#include "gpio.h"
#include "user_interface.h"

#include "espconn.h"
#include "ip_addr.h"
#include "mem.h"
# include "user_devicefind.h"
#include "user_webserver.h"
#include "pwm.h"
#if ESP_PLATFORM
#include "user_esp_platform.h"
#endif

#if ((SPI_FLASH_SIZE_MAP == 0) || (SPI_FLASH_SIZE_MAP == 1))
#error "不支持闪存映射"
#elif (SPI_FLASH_SIZE_MAP == 2)
#define SYSTEM_PARTITION_OTA_SIZE 0x6A000
#define SYSTEM_PARTITION_OTA_2_ADDR 0x81000
的#define SYSTEM_PARTITION_RF_CAL_ADDR 0xfb000
的#define SYSTEM_PARTITION_PHY_DATA_ADDR 0xfc000
的#define SYSTEM_PARTITION_SYSTEM_PARAMETER_ADDR 0xfd000
的#define SYSTEM_PARTITION_CUSTOMER_PRIV_PARAM_ADDR 0x7c000
#elif指令(SPI_FLASH_SIZE_MAP == 3)
的#define SYSTEM_PARTITION_OTA_SIZE 0x6A000
的#define SYSTEM_PARTITION_OTA_2_ADDR 0x81000
的#define SYSTEM_PARTITION_RF_CAL_ADDR 0x1fb000
的#define SYSTEM_PARTITION_PHY_DATA_ADDR 0x1fc000
的#define SYSTEM_PARTITION_SYSTEM_PARAMETER_ADDR 0x1fd000
的#define SYSTEM_PARTITION_CUSTOMER_PRIV_PARAM_ADDR 0x7c000
#elif指令(SPI_FLASH_SIZE_MAP == 4)
的#define SYSTEM_PARTITION_OTA_SIZE 0x6A000
的#define SYSTEM_PARTITION_OTA_2_ADDR 0x81000
的#define SYSTEM_PARTITION_RF_CAL_ADDR 0x3fb000
的#define SYSTEM_PARTITION_PHY_DATA_ADDR 0x3fc000
的#define SYSTEM_PARTITION_SYSTEM_PARAMETER_ADDR 0x3fd000
的#define SYSTEM_PARTITION_CUSTOMER_PRIV_PARAM_ADDR 0x7c000
#elif指令(SPI_FLASH_SIZE_MAP == 5)
的#define SYSTEM_PARTITION_OTA_SIZE 0x6A000
的#define SYSTEM_PARTITION_OTA_2_ADDR 0x101000
的#define SYSTEM_PARTITION_RF_CAL_ADDR 0x1fb000
的#define SYSTEM_PARTITION_PHY_DATA_ADDR 0x1fc000
的#define SYSTEM_PARTITION_SYSTEM_PARAMETER_ADDR 0x1fd000
的#define SYSTEM_PARTITION_CUSTOMER_PRIV_PARAM_ADDR 0xfc000
#elif指令(SPI_FLASH_SIZE_MAP == 6)
的#define SYSTEM_PARTITION_OTA_SIZE 0x6A000
的#define SYSTEM_PARTITION_OTA_2_ADDR 0x101000
的#define SYSTEM_PARTITION_RF_CAL_ADDR 0x3fb000
的#define SYSTEM_PARTITION_PHY_DATA_ADDR 0x3fc000
的#define SYSTEM_PARTITION_SYSTEM_PARAMETER_ADDR 0x3fd000
的#define SYSTEM_PARTITION_CUSTOMER_PRIV_PARAM_ADDR 0xfc000
的#else
#ERROR “闪光灯地图不被支持”
#ENDIF

的#define SYSTEM_PARTITION_CUSTOMER_PRIV_PARAM SYSTEM_PARTITION_CUSTOMER_BEGIN
#define TCP_SERVER_IP "116.62.81.138"
#define qingqiu "GET /v3/weather/now.json?key=smtq3n0ixdggurox&location=beijing&language=en&unit=c HTTP/1.1rnHost:api.seniverse.comrnr n" //请求的接口

uint32 priv_param_start_sec;
静态 void tcp_client_sent_cb(void *arg);
static void tcp_client_recv_cb(void *arg,char *pdata,unsigned short length);
静态 void tcp_client_recon_cb(void *arg,sint8 错误);
静态 void tcp_client_discon_cb(void *arg);
静态 void tcp_client_connect_cb(void *arg);
os_timer_t os_timer;//
静态表_const partition_item_partition_item [] = {
    { SYSTEM_PARTITION_BOOTLOADER, 0x0, 0x1000},
    { SYSTEM_PARTITION_OTA_1, 0x1000, SYSTEM_PARTITION_OTA_SIZE},
    { SYSTEM_PARTITION_OTA_2, SYSTEM_PARTITION_OTA_2_ADDR, SYSTEM_PARTITION_OTA_SIZE},
    { SYSTEM_PARTITION_RF_CAL, SYSTEM_PARTITION_RF_0 }0_CAL_ADDR
    { SYSTEM_PARTITION_PHY_DATA, SYSTEM_PARTITION_PHY_DATA_ADDR, 0x1000},
    { SYSTEM_PARTITION_SYSTEM_PARAMETER, SYSTEM_PARTITION_SYSTEM_PARAMETER_ADDR, 0x3000},
    { SYSTEM_PARTITION_CUSTOMER_PRIV_PARAM, SYSTEM_PARTITION_CUSTOMER_PRIV_PARAM_ADDR, 0x1000},
};

void ICACHE_FLASH_ATTR user_pre_init(void)   
{
    if(!system_partition_table_regist(at_partition_table, sizeof(at_partition_table)/sizeof(at_partition_table[0]),SPI_FLASH_SIZE_MAP)) {
                os_printf("system_partition_table_regist 失败rn");
                而(1);
        }
}

static void ICACHE_FLASH_ATTR
tcp_client_sent_cb(void *arg){
        os_printf("tcp客户端发送数据成功rn");
}

/**
* TCP Client 接收数据处理/函数发送,可以在这接收到的来的
*
static void
ICACHE_FLASH_ATTRtcp_client_recv_cb(void *arg,char *pdata,unsigned short tcp){
        os_printf("tcp client receive tcp服务器数据rn");
        os_printf("长度: %d rn数据: %srn",len,pdata);




}

static void ICACHE_FLASH_ATTR
tcp_client_recon_cb(void *arg,sint8 error){
        os_printf("tcp client connect tcp server error %drn",error); /**
* TCP Client 破坏接口调用函数* /





tcp_client_discon_cb(void *arg){
        os_printf("tcp客户端断开tcp服务器成功rn");
}

/**
* TCP 客户端连接成功回调函数
*/
static voidICACHE_FLASH_R
tcp_client_connect_cb(void *arg){
        struct espconn *pespconn = arg;

        os_printf("tcp客户端连接tcp服务器成功rn");
        pconn_regist_recvcb(pespconn,tcp_recvcb(pespconn,tcp_recvcb);//注册接收数据_recv_cb;//注册接收数据_recv_cb
        )(pespconn,tcp_client_sent_cb);//注册连接函数
        espconn_regist_disconnb(pespconn,tcp_client_di***);//注册连接函数
        /*char buffer[1024 ];
                        memset(缓冲区,0,1024);
                        os_sprintf(buffer,"/v3/weather/now.json?language=en&key=Sn5RsUO2O3kUx9tzO&location=lingao&unit=c","api.seniverse.com");
                        os_printf("user_esp_platform_connect_cbrn");
                        espconn_send(pespconn,buffer,os_strlen(buffer));*/
        espconn_send(pespconn,青丘,os_strlen(青丘));
}

void ICACHE_FLASH_ATTR user_set_station_config
(void)
{
   char ssid[32];
   字符密码[64];
   结构站配置站配置;
   stationConf.bssid_set = 0; //不需要检查AP的MAC地址


   os_memcpy(stationConf.ssid, ssid, 32);
   os_memcpy(stationConf.password, 密码, 64);
   wifi_station_set_config(&stationConf);

}

结构 espconn tcp_client;

void ICACHE_FLASH_ATTR
tcp_client_init(struct espconn *espconn,uint8 *remote_ip,struct ip_addr *local_ip, int remote_port){

        uint32 server_ip = ipaddr_addr(remote_ip);

        os_printf("tcp客户端连接tcp服务器rn");
        espconn->proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp));
        espconn->type = ESPCONN_TCP;

        os_memcpy(espconn->proto.tcp->remote_ip,&server_ip,4);//设置要连接的服务器IP地址
        espconn->proto.tcp->remote_port = remote_port;//设置要连接的服务器端口号
        os_memcpy(espconn->proto.ip->local_ip,4);//设置本地IP地址
        espconn->proto.tcp->local_port =espconn_port);//设置本地端口号

        espconn_regist_connectcb(espconn,tcp_client_connect_cb); //注册连接成功回调函数注册
        espconn_reg_reconn(espconn,cp_client_recon_cb);/连重连接c函数

        espconn_connect(espconn);// ClientServer
}
void CHE_FLASH_ATTR
huidiao(void)
{
    os_printf("hello word");
    结构 ip_info local_ip;
    wifi_get_ip_info(STATION_IF,&local_ip);
    tcp_client_init(&tcp_client,TCP_SERVER_IP,&local_ip.ip,80);
        os_printf("你好n");
         os_timer_disarm(&os_timer);




user_init(void)
{
    partition_item_t partition_item;
    os_printf("SDK版本:%sn", system_get_sdk_version());

    if (!system_partition_get_item(SYSTEM_PARTITION_CUSTOMER_PRIV_PARAM, &partition_item)) {
        os_printf("获取分区信息失败n");
    }
    priv_param_start_sec = partition_item.addr/SPI_FLASH_SEC_SIZE;
    os_printf("你好!");
    wifi_set_opmode(STATIONAP_MODE);

     user_set_station_config();
     os_timer_disarm(&os_timer);
     os_timer_setfn(&os_timer,(os_timer_func_t*)huidiao,NULL);
     os_timer_arm(&os_timer,10000,1);


}
举报

更多回帖

发帖
×
20
完善资料,
赚取积分