Skip to content

Commit 26b75f9

Browse files
committed
fix(modem): Fixup tcp-client example with esp-at
1 parent 5ad7367 commit 26b75f9

File tree

4 files changed

+36
-17
lines changed

4 files changed

+36
-17
lines changed

components/esp_modem/examples/modem_tcp_client/main/command/sock_dce.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
#include <sys/socket.h>
99
#include "esp_vfs.h"
1010
#include "esp_vfs_eventfd.h"
11-
1211
#include "sock_dce.hpp"
12+
#include "cxx_include/esp_modem_command_library_utils.hpp"
1313

1414
namespace sock_dce {
1515

@@ -305,6 +305,21 @@ std::unique_ptr<DCE> create(const esp_modem::dce_config *config, std::shared_ptr
305305
}
306306

307307

308+
esp_modem::command_result DCE::sync()
309+
{
310+
return esp_modem::dce_commands::generic_command_common(dte.get(), "AT\r\n");
311+
}
312+
313+
esp_modem::command_result DCE::set_echo(bool on)
314+
{
315+
return esp_modem::dce_commands::generic_command_common(dte.get(), "ATE0\r\n");
316+
}
317+
318+
esp_modem::command_result DCE::set_pdp_context(esp_modem::PdpContext &pdp)
319+
{
320+
return esp_modem::command_result::OK;
321+
}
322+
308323
/**
309324
* @brief Opens network in AT command mode
310325
* @return OK, FAIL or TIMEOUT

components/esp_modem/examples/modem_tcp_client/main/command/sock_dce.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ class DCE : public ::esp_modem::GenericModule {
101101
using esp_modem::GenericModule::GenericModule;
102102
public:
103103

104+
esp_modem::command_result sync() override;
105+
esp_modem::command_result set_echo(bool on) override;
106+
esp_modem::command_result set_pdp_context(esp_modem::PdpContext &pdp) override;
104107
/**
105108
* @brief Opens network in AT command mode
106109
* @return OK, FAIL or TIMEOUT

components/esp_modem/examples/modem_tcp_client/main/modem_client.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Unlicense OR CC0-1.0
55
*/
@@ -23,8 +23,8 @@
2323
#include "tcp_transport_mbedtls.h"
2424
#include "tcp_transport_at.h"
2525

26-
#define BROKER_URL "mqtt.eclipseprojects.io"
27-
#define BROKER_PORT 8883
26+
#define BROKER_URL "test.mosquitto.org"
27+
#define BROKER_PORT 1883
2828

2929

3030
static const char *TAG = "modem_client";
@@ -114,7 +114,7 @@ extern "C" void app_main(void)
114114
mqtt_config.broker.address.port = BROKER_PORT;
115115
mqtt_config.session.message_retransmit_timeout = 10000;
116116
#ifndef CONFIG_EXAMPLE_CUSTOM_TCP_TRANSPORT
117-
mqtt_config.broker.address.uri = "mqtts://127.0.0.1";
117+
mqtt_config.broker.address.uri = "mqtt://127.0.0.1";
118118
dce->start_listening(BROKER_PORT);
119119
#else
120120
mqtt_config.broker.address.uri = "mqtt://" BROKER_URL;
@@ -127,6 +127,7 @@ extern "C" void app_main(void)
127127
esp_mqtt_client_register_event(mqtt_client, static_cast<esp_mqtt_event_id_t>(ESP_EVENT_ANY_ID), mqtt_event_handler, NULL);
128128
esp_mqtt_client_start(mqtt_client);
129129
#ifndef CONFIG_EXAMPLE_CUSTOM_TCP_TRANSPORT
130+
dce->set_rx_mode(1);
130131
if (!dce->connect(BROKER_URL, BROKER_PORT)) {
131132
ESP_LOGE(TAG, "Failed to start DCE");
132133
return;

components/esp_modem/examples/modem_tcp_client/main/sock_commands_espat.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ command_result net_open(CommandableIf *t)
2121
ESP_LOGV(TAG, "%s", __func__);
2222

2323
// Set WiFi mode to station
24-
auto ret = dce_commands::generic_command(t, "AT+CWMODE=1\r", "OK", "ERROR", 5000);
24+
auto ret = dce_commands::generic_command(t, "AT+CWMODE=1\r\n", "OK", "ERROR", 5000);
2525
if (ret != command_result::OK) {
2626
ESP_LOGE(TAG, "Failed to set WiFi mode");
2727
return ret;
2828
}
2929

3030
// Connect to WiFi network
31-
std::string wifi_cmd = "AT+CWJAP=\"" CONFIG_EXAMPLE_WIFI_SSID "\",\"" CONFIG_EXAMPLE_WIFI_PASSWORD "\"\r";
31+
std::string wifi_cmd = "AT+CWJAP=\"" CONFIG_EXAMPLE_WIFI_SSID "\",\"" CONFIG_EXAMPLE_WIFI_PASSWORD "\"\r\n";
3232
ret = dce_commands::generic_command(t, wifi_cmd, "OK", "ERROR", 15000);
3333
if (ret != command_result::OK) {
3434
ESP_LOGE(TAG, "Failed to connect to WiFi");
@@ -43,7 +43,7 @@ command_result net_close(CommandableIf *t)
4343
{
4444
ESP_LOGV(TAG, "%s", __func__);
4545
// Disconnect from WiFi
46-
auto ret = dce_commands::generic_command(t, "AT+CWQAP\r", "OK", "ERROR", 5000);
46+
auto ret = dce_commands::generic_command(t, "AT+CWQAP\r\n", "OK", "ERROR", 5000);
4747
if (ret != command_result::OK) {
4848
ESP_LOGW(TAG, "Failed to disconnect WiFi (may already be disconnected)");
4949
}
@@ -55,13 +55,13 @@ command_result tcp_open(CommandableIf *t, const std::string &host, int port, int
5555
ESP_LOGV(TAG, "%s", __func__);
5656

5757
// Set single connection mode (just in case)
58-
auto ret = dce_commands::generic_command(t, "AT+CIPMUX=0\r", "OK", "ERROR", 1000);
58+
auto ret = dce_commands::generic_command(t, "AT+CIPMUX=0\r\n", "OK", "ERROR", 1000);
5959
if (ret != command_result::OK) {
6060
ESP_LOGW(TAG, "Failed to set single connection mode");
6161
}
6262

6363
// Establish TCP connection
64-
std::string tcp_cmd = "AT+CIPSTART=\"TCP\",\"" + host + "\"," + std::to_string(port) + "\r";
64+
std::string tcp_cmd = "AT+CIPSTART=\"TCP\",\"" + host + "\"," + std::to_string(port) + "\r\n";
6565
ret = dce_commands::generic_command(t, tcp_cmd, "CONNECT", "ERROR", timeout);
6666
if (ret != command_result::OK) {
6767
ESP_LOGE(TAG, "Failed to establish TCP connection to %s:%d", host.c_str(), port);
@@ -75,7 +75,7 @@ command_result tcp_open(CommandableIf *t, const std::string &host, int port, int
7575
command_result tcp_close(CommandableIf *t)
7676
{
7777
ESP_LOGV(TAG, "%s", __func__);
78-
return dce_commands::generic_command(t, "AT+CIPCLOSE\r", "CLOSED", "ERROR", 5000);
78+
return dce_commands::generic_command(t, "AT+CIPCLOSE\r\n", "CLOSED", "ERROR", 5000);
7979
}
8080

8181
command_result tcp_send(CommandableIf *t, uint8_t *data, size_t len)
@@ -98,7 +98,7 @@ command_result get_ip(CommandableIf *t, std::string &ip)
9898
{
9999
ESP_LOGV(TAG, "%s", __func__);
100100
std::string out;
101-
auto ret = dce_commands::generic_get_string(t, "AT+CIFSR\r", out, 5000);
101+
auto ret = dce_commands::at_raw(t, "AT+CIFSR\r\n", out, "OK", "ERROR", 5000);
102102
if (ret != command_result::OK) {
103103
return ret;
104104
}
@@ -122,10 +122,10 @@ command_result get_ip(CommandableIf *t, std::string &ip)
122122

123123
command_result set_rx_mode(CommandableIf *t, int mode)
124124
{
125-
ESP_LOGV(TAG, "%s", __func__);
125+
ESP_LOGE(TAG, "%s", __func__);
126126
// Set passive receive mode (1) for better control
127127
// Active mode (0) would send +IPD automatically
128-
std::string cmd = "AT+CIPRECVTYPE=" + std::to_string(mode) + "\r";
128+
std::string cmd = "AT+CIPRECVTYPE=" + std::to_string(mode) + "\r\n";
129129
return dce_commands::generic_command(t, cmd, "OK", "ERROR", 1000);
130130
}
131131

@@ -137,17 +137,17 @@ void Responder::start_sending(size_t len)
137137
{
138138
data_to_send = len;
139139
send_stat = 0;
140-
send_cmd("AT+CIPSEND=" + std::to_string(len) + "\r");
140+
send_cmd("AT+CIPSEND=" + std::to_string(len) + "\r\n");
141141
}
142142

143143
void Responder::start_receiving(size_t len)
144144
{
145-
send_cmd("AT+CIPRECVDATA=" + std::to_string(len) + "\r");
145+
send_cmd("AT+CIPRECVDATA=" + std::to_string(len) + "\r\n");
146146
}
147147

148148
bool Responder::start_connecting(std::string host, int port)
149149
{
150-
std::string cmd = "AT+CIPSTART=\"TCP\",\"" + host + "\"," + std::to_string(port) + "\r";
150+
std::string cmd = "AT+CIPSTART=\"TCP\",\"" + host + "\"," + std::to_string(port) + "\r\n";
151151
send_cmd(cmd);
152152
return true;
153153
}

0 commit comments

Comments
 (0)