Permalink
Find file
dae6c1d Sep 29, 2016
157 lines (138 sloc) 4.46 KB
/*
Big thanks for MakerAsia for dns example, and stackoverflow.com/questions/26192758/how-can-i-send-a-simple-http-request-with-a-lwip-stack
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "lwip/tcp.h"
#include "lwip/ip4_addr.h"
#include "string.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "lwip/inet.h"
struct tcp_pcb *testpcb;
#define EXAMPLE_WIFI_SSID "SSID"
#define EXAMPLE_WIFI_PASS "PASS"
static EventGroupHandle_t wifi_event_group;
const int CONNECTED_BIT = BIT0;
static esp_err_t event_handler(void *ctx, system_event_t *event)
{
switch(event->event_id) {
case SYSTEM_EVENT_STA_START:
esp_wifi_connect();
break;
case SYSTEM_EVENT_STA_GOT_IP:
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
/* This is a workaround as ESP32 WiFi libs don't currently
auto-reassociate. */
esp_wifi_connect();
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
break;
default:
break;
}
return ESP_OK;
}
static void initialise_wifi(void)
{
tcpip_adapter_init();
wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
wifi_config_t wifi_config = {
.sta = {
.ssid = EXAMPLE_WIFI_SSID,
.password = EXAMPLE_WIFI_PASS,
},
};
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
ESP_ERROR_CHECK( esp_wifi_start() );
}
err_t tcpRecvCallback(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{
printf("Data recieved.\n");
if (p == NULL) {
printf("The remote host closed the connection.\n");
printf("Now I'm closing the connection.\n");
return ERR_ABRT;
} else {
printf("Number of pbufs %d\n", pbuf_clen(p));
printf("Contents of pbuf %s\n", (char *)p->payload);
}
return 0;
}
uint32_t tcp_send_packet(void)
{
char *string = "HEAD /update.php?api_key=<key>&field1=<data> HTTP/1.0\r\nHost: api.thingspeak.com\r\n\r\n ";
err_t error;
/* push to buffer */
error = tcp_write(testpcb, string, strlen(string), TCP_WRITE_FLAG_COPY);
if (error) {
printf("ERROR: Code: %d (tcp_send_packet :: tcp_write)\n", error);
return 1;
}
/* now send */
error = tcp_output(testpcb);
if (error) {
printf("ERROR: Code: %d (tcp_send_packet :: tcp_output)\n", error);
return 1;
}
return 0;
}
err_t connectCallback(void *arg, struct tcp_pcb *tpcb, err_t err)
{
printf("Connection Established.\n");
printf("Now sending a packet\n");
tcp_send_packet();
return 0;
}
void tcp_setup(void)
{
uint32_t data = 0xdeadbeef;
/* create an ip */
ip_addr_t ip;
IP_ADDR4(&ip, 184, 106, 153, 149); //IP of my PHP server
/* create the control block */
testpcb = tcp_new(); //testpcb is a global struct tcp_pcb
// as defined by lwIP
/* dummy data to pass to callbacks*/
tcp_arg(testpcb, &data);
/* register callbacks with the pcb */
tcp_err(testpcb, NULL);
tcp_recv(testpcb, tcpRecvCallback);
tcp_sent(testpcb, NULL);
/* now connect */
tcp_connect(testpcb, &ip, 80, connectCallback);
}
void mainTask(void *pvParameters){
tcp_setup();
while (1) {
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
void app_main()
{
nvs_flash_init();
system_init();
initialise_wifi();
xTaskCreatePinnedToCore(&mainTask, "mainTask", 2048, NULL, 5, NULL, 0);
}