#include <Arduino.h>
/*1---wifi include files*/
#include <WiFi.h>
#include <WiFiClient.h>
const char* ssid = "##########";
const char* password = "************";
/*2---mqtt include files*/
#include "mqtt_client.h"
#include "freertos/event_groups.h"
/*3---socket include files*/
#include "tcpip_adapter.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include "lwip/err.h"
/*4---ftp include files*/
#include <ESP32_FTPClient.h>
#include "octocat.h"
/*5---freeRTOS include files*/
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "freertos/queue.h"
#include "freertos/timers.h"
void app_aerial_init(uint32_t baud) {
Serial.begin(baud);
}
void app_led_init(uint8_t mode) {
pinMode(12,mode);
pinMode(13,mode);
pinMode(15,mode);
}
void app_wifi_init(void) {
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
static uint8_t connectTimes;
connectTimes++;
if(connectTimes > 10) {
esp_restart();
}
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
#define BROKER_URL "mqtt://xxx.xxx.xx.xx:1883"
static const char *TAG = "pppos_example";
static EventGroupHandle_t event_group = NULL;
static const int CONNECT_BIT = BIT0;
static const int STOP_BIT = BIT1;
static const int GOT_DATA_BIT = BIT2;
esp_mqtt_client_handle_t mqttClient;
static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) {
esp_mqtt_client_handle_t client = event->client;
int msg_id;
switch (event->event_id) {
case MQTT_EVENT_CONNECTED:
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
msg_id = esp_mqtt_client_subscribe(client, "/topic/esp-pppos", 0);
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
break;
case MQTT_EVENT_DISCONNECTED:
ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");
break;
case MQTT_EVENT_SUBSCRIBED:
ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
msg_id = esp_mqtt_client_publish(client, "/topic/esp-pppos", "esp32-pppos", 0, 0, 0);
ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
break;
case MQTT_EVENT_UNSUBSCRIBED:
ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
break;
case MQTT_EVENT_PUBLISHED:
ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
break;
case MQTT_EVENT_DATA:
ESP_LOGI(TAG, "MQTT_EVENT_DATA");
if(strstr(event->data,"buddy")) {
printf("get data from broker\r\n");
}
// printf("TOPIC=%.*s\r\n", event->topic_len, event->topic);
// printf("DATA=%.*s\r\n", event->data_len, event->data);
xEventGroupSetBits(event_group, GOT_DATA_BIT);
break;
case MQTT_EVENT_ERROR:
ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
break;
default:
ESP_LOGI(TAG, "MQTT other event id: %d", event->event_id);
break;
}
return ESP_OK;
}
esp_mqtt_client_handle_t app_mqtt_init(void) {
esp_mqtt_client_handle_t mqtt_client;
esp_mqtt_client_config_t mqtt_config = {
.event_handle = mqtt_event_handler,
.uri = BROKER_URL
};
mqtt_client = esp_mqtt_client_init(&mqtt_config);
esp_mqtt_client_start(mqtt_client);
xEventGroupWaitBits(event_group, GOT_DATA_BIT, pdTRUE, pdTRUE, portMAX_DELAY);
return mqtt_client;
}
void app_mqtt_publish(esp_mqtt_client_handle_t client, const char *topic, const char *data, int len, int qos, int retain) {
esp_mqtt_client_publish(client, topic, data, len, qos, retain);
}
#define HOST_IP_ADDR "xxx.xxx.xx.xxx"
#define PORT 80
int sock_client;
int app_tcp_client_init(void) {
char host_ip[] = HOST_IP_ADDR;
int addr_family = 0;
int ip_protocol = 0;
struct sockaddr_in dest_addr;
dest_addr.sin_addr.s_addr = inet_addr(host_ip);
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(PORT);
addr_family = AF_INET;
ip_protocol = IPPROTO_IP;
int sock = socket(addr_family, SOCK_STREAM, ip_protocol);
if (sock < 0) {
ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
}
ESP_LOGI(TAG, "Socket created, connecting to %s:%d", host_ip, PORT);
int err = connect(sock, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr_in6));
if (err != 0) {
ESP_LOGE(TAG, "Socket unable to connect: errno %d", errno);
}
ESP_LOGI(TAG, "Successfully connected");
return sock;
}
int app_set_socket_timeout(uint32_t seconds){
struct timeval tv;
tv.tv_sec = seconds;
tv.tv_usec = 0;
int ans = 0;
ans = setsockopt(sock_client, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval));
if(ans < 0) {
return -1;
} else {
return ans;
}
}
ssize_t app_socket_send(int s,const void *dataptr,size_t size,int flags) {
ssize_t ans = -1;
ans = send(s, dataptr, size, flags);
return ans;
}
char ftp_server[] = "xxx.xxx.xxx.xx";
char ftp_user[] = "##########";
char ftp_pass[] = "*************";
ESP32_FTPClient ftp (ftp_server,1050,ftp_user,ftp_pass);
void app_ftp_init(void) {
const char * fileName = "FILE05.txt";
size_t fileSize = 0;
String list[128];
ftp.OpenConnection();
ftp.ChangeWorkDir("/ftpTest");
ftp.InitFile("Type A");
ftp.ContentList("", list);
Serial.println("list");
for( uint8_t i = 0; i < sizeof(list); i++)
{
uint8_t indexSize = 0;
uint8_t indexMod = 0;
if(list[i].length() > 0)
{
if( list[i].indexOf(fileName) > -1 )
{
list[i].toLowerCase();
Serial.println(list[i]);
indexSize = list[i].indexOf("size") + 5;
Serial.printf("indexSize = %d\r\n",indexSize);
indexMod = list[i].indexOf("file05") - 2;
Serial.printf("indexMod = %d\r\n",indexMod);
fileSize = list[i].substring(indexSize, indexMod).toInt();
Serial.println("\nFile size is: " + String(fileSize));
}
}
else {
break;
}
}
unsigned char * downloaded_file = (unsigned char *) malloc(fileSize);
ftp.InitFile("Type I");
ftp.DownloadFile(fileName, downloaded_file, fileSize, false);
Serial.println("ftp.DownloadFile sucessed!");
for(size_t i = 0; i < fileSize; i++) {
Serial.printf("%c",downloaded_file[i]);
}
Serial.println(" ");
ftp.CloseConnection();
Serial.println("CloseConnection");
}
SemaphoreHandle_t socket_sem;
void app_task_socket(void *parameter) {
while(1) {
//xSemaphoreTake(socket_sem,portMAX_DELAY);
if(pdTRUE == xSemaphoreTake(socket_sem,2000)) {
app_socket_send(sock_client,"hello,world!",12,0);
} else {
app_socket_send(sock_client,"got nothing from server!",24,0);
}
vTaskDelay(2000);
}
}
void app_task_mqtt(void *parameter) {
while(1) {
app_mqtt_publish(mqttClient, "DEV-EC600N-04", "hello,world!", 12, 0, 0);
vTaskDelay(2000);
}
}
void setup() {
// put your setup code here, to run once:
//1---串口调试
app_aerial_init(115200);
event_group = xEventGroupCreate();
//2---IO控制
app_led_init(OUTPUT);
//3---WIFI连接
app_wifi_init();
//4---MQTT初始化
mqttClient = app_mqtt_init();
//5---Socket初始化
sock_client = app_tcp_client_init();
app_set_socket_timeout(1);
//6---FTP测试
app_ftp_init();
socket_sem = xSemaphoreCreateBinary();
xTaskCreate(app_task_socket, "app_task_socket", 2048, NULL, 8, NULL);
xTaskCreate(app_task_mqtt, "app_task_mqtt", 2048, NULL, 8, NULL);
}
//7---fressRTOS任务、信号量应用
uint8_t ledStatus = 0;
char rx_buffer[128];
void loop() {
// put your main code here, to run repeatedly:
Serial.println("hello,world");
if(ledStatus == 0) {
ledStatus = 1;
} else {
ledStatus = 0;
}
digitalWrite(12,ledStatus);
digitalWrite(13,ledStatus);
digitalWrite(15,ledStatus);
int len = recv(sock_client, rx_buffer, sizeof(rx_buffer) - 1, 0);
if (len < 0){
printf("not got packet from server!\r\n");
} else {
Serial.println("get packet from server!");
xSemaphoreGive(socket_sem);
}
// app_mqtt_publish(mqttClient, "DEV-EC600N-04", "hello,world!", 12, 0, 0);
// app_socket_send(sock_client,"hello,world!",12,0);
delay(1000);
}