Remove broken ESP32 modem driver
This driver was only compatible with mbed and didn't work reliably. Zephyr has an in-built ESP modem driver already.
This commit is contained in:
@@ -1,215 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2018 Martin Jäger / Libre Solar
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "ESP32.h"
|
||||
#include "mbed.h"
|
||||
|
||||
ESP32::ESP32(UARTSerial &s) :
|
||||
_serial(s),
|
||||
_at(&_serial, "\r\n", 200, 4000)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
ESP32::~ESP32()
|
||||
{ }
|
||||
|
||||
// Soft reset of ESP32
|
||||
int ESP32::reset(void)
|
||||
{
|
||||
_at.set_timeout(3000);
|
||||
_at.send("AT+RST");
|
||||
|
||||
char buf[100];
|
||||
_at.read(buf, sizeof(buf));
|
||||
printf(buf);
|
||||
|
||||
return _at.recv("ready");
|
||||
}
|
||||
|
||||
void ESP32::print_firmware()
|
||||
{
|
||||
_at.set_timeout(1000);
|
||||
char buf[100];
|
||||
_at.send("AT+GMR");
|
||||
_at.read(buf, sizeof(buf));
|
||||
printf(buf);
|
||||
}
|
||||
|
||||
int ESP32::set_wifi_mode(ESP32_wifi_mode_t mode)
|
||||
{
|
||||
_at.set_timeout(1000);
|
||||
_at.flush();
|
||||
_at.send("AT+CWMODE=%d", mode);
|
||||
return _at.recv("OK");
|
||||
}
|
||||
|
||||
void ESP32::list_APs(char *buf, int size)
|
||||
{
|
||||
_at.set_timeout(5000); // use long timeout
|
||||
_at.send("AT+CWLAP");
|
||||
_at.read(buf, size);
|
||||
}
|
||||
|
||||
int ESP32::join_AP(const char *ssid, const char *pwd)
|
||||
{
|
||||
_at.set_timeout(2000);
|
||||
_at.send("AT+CWJAP=\"%s\",\"%s\"", ssid, pwd);
|
||||
char buf[100];
|
||||
_at.read(buf, sizeof(buf));
|
||||
printf(buf);
|
||||
|
||||
return _at.recv("OK");
|
||||
}
|
||||
|
||||
int ESP32::quit_AP(void)
|
||||
{
|
||||
_at.send("AT+CWQAP");
|
||||
return _at.recv("OK");
|
||||
}
|
||||
|
||||
int ESP32::get_IP(char *ip, int size)
|
||||
{
|
||||
int ip_arr[4] = {0};
|
||||
|
||||
_at.set_timeout(500);
|
||||
_at.flush();
|
||||
_at.send("AT+CIFSR");
|
||||
// valid answer:
|
||||
// +CIFSR:STAIP,"192.168.178.50"
|
||||
// +CIFSR:STAMAC,"30:ae:a4:c3:70:88"
|
||||
//
|
||||
// OK
|
||||
|
||||
// hack to obtain IP address, as _at.recv seems to contain a bug (it returns 192.168.0.4 instead of 192.168.0.43)
|
||||
char buf[100];
|
||||
char *ip_start = buf;
|
||||
_at.read(buf, sizeof(buf));
|
||||
//printf(buf);
|
||||
for (unsigned int i = 0; i < sizeof(buf); i++) {
|
||||
if (strncmp(&buf[i], "busy now", 8) == 0) {
|
||||
printf("IP busy now!!\n");
|
||||
snprintf(ip, size, "ERROR");
|
||||
_at.flush();
|
||||
return 0;
|
||||
}
|
||||
else if (buf[i] < '0' || buf[i] > '9') { // find first numeric character
|
||||
ip_start++;
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
int res = sscanf(ip_start, "%d.%d.%d.%d", &ip_arr[0], &ip_arr[1], &ip_arr[2], &ip_arr[3]);
|
||||
|
||||
// TODO: figure out bug in _at.recv and correct it
|
||||
//int res = _at.recv("%d.%d.%d.%d\r", &ip_arr[0], &ip_arr[1], &ip_arr[2], &ip_arr[3]);
|
||||
|
||||
if (res > 0)
|
||||
snprintf(ip, size, "%d.%d.%d.%d", ip_arr[0], ip_arr[1], ip_arr[2], ip_arr[3]);
|
||||
else
|
||||
snprintf(ip, size, "ERROR");
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
int ESP32::ping(const char *ip)
|
||||
{
|
||||
_at.set_timeout(2000);
|
||||
_at.send("AT+PING=\"%s\"", ip);
|
||||
return _at.recv("OK");
|
||||
}
|
||||
|
||||
int ESP32::set_single()
|
||||
{
|
||||
_at.send("AT+CIPMUX=0");
|
||||
return _at.recv("OK");
|
||||
}
|
||||
|
||||
int ESP32::set_multiple()
|
||||
{
|
||||
_at.send("AT+CIPMUX=1");
|
||||
return _at.recv("OK");
|
||||
}
|
||||
|
||||
int ESP32::get_conn_status()
|
||||
{
|
||||
int status = -1;
|
||||
_at.set_timeout(2000);
|
||||
_at.flush();
|
||||
_at.send("AT+CIPSTATUS");
|
||||
//char buf[100];
|
||||
//_at.read(buf, sizeof(buf));
|
||||
//printf(buf);
|
||||
_at.recv("STATUS:%d", &status);
|
||||
return status;
|
||||
}
|
||||
|
||||
int ESP32::set_ip_mode(ESP32_ip_mode_t mode)
|
||||
{
|
||||
_at.set_timeout(500);
|
||||
_at.send("AT+CIPMODE=%d", mode);
|
||||
return _at.recv("OK");
|
||||
}
|
||||
|
||||
int ESP32::start_TCP_conn(const char *ip, const char *port, bool ssl)
|
||||
{
|
||||
_at.set_timeout(2000);
|
||||
if (ssl) {
|
||||
printf("Setting SSL conf... ");
|
||||
_at.send("AT+CIPSSLCCONF=0");
|
||||
int res = _at.recv("OK");
|
||||
printf((res > 0) ? "OK\n" : "ERROR\n");
|
||||
_at.send("AT+CIPSTART=\"SSL\",\"%s\",%s", ip, port);
|
||||
}
|
||||
else {
|
||||
_at.send("AT+CIPSTART=\"TCP\",\"%s\",%s", ip, port);
|
||||
}
|
||||
return _at.recv("OK");
|
||||
}
|
||||
|
||||
int ESP32::close_TCP_conn()
|
||||
{
|
||||
_at.set_timeout(500);
|
||||
_at.send("AT+CIPCLOSE");
|
||||
return _at.recv("OK");
|
||||
}
|
||||
|
||||
int ESP32::send_URL(char *url, char *host)
|
||||
{
|
||||
char request[500];
|
||||
snprintf(request, sizeof(request), "GET %s HTTP/1.0\r\nHost: %s\r\nConnection: close\r\n\r\n", url, host);
|
||||
|
||||
_at.send("AT+CIPSEND=%d", strlen(request));
|
||||
_at.recv("> ");
|
||||
|
||||
_at.send(request);
|
||||
return _at.recv("SEND OK");
|
||||
}
|
||||
|
||||
int ESP32::send_TCP_data(uint8_t *data, int length)
|
||||
{
|
||||
_at.send("AT+CIPSEND=%d", length);
|
||||
_at.recv("> ");
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
_at.putc(data[i]);
|
||||
}
|
||||
return _at.recv("SEND OK");
|
||||
}
|
||||
|
||||
int ESP32::start_TCP_server(int port)
|
||||
{
|
||||
_at.set_timeout(500);
|
||||
_at.send("AT+CIPSERVER=1,%d", port);
|
||||
return _at.recv("OK");
|
||||
}
|
||||
|
||||
int ESP32::close_TCP_server()
|
||||
{
|
||||
_at.set_timeout(500);
|
||||
_at.send("AT+CIPSERVER=0");
|
||||
return _at.recv("OK");
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2018 Martin Jäger / Libre Solar
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef __ESP32_H__
|
||||
#define __ESP32_H__
|
||||
|
||||
#include "mbed.h"
|
||||
|
||||
enum ESP32_status_t {
|
||||
ESP32_STATUS_AP_CONNECTED = 2,
|
||||
ESP32_STATUS_TCP_ACTIVE,
|
||||
ESP32_STATUS_TCP_DIS,
|
||||
ESP32_STATUS_AP_DISCONNECTED
|
||||
};
|
||||
|
||||
enum ESP32_wifi_mode_t {
|
||||
ESP32_WIFI_MODE_NULL = 0,
|
||||
ESP32_WIFI_MODE_STATION,
|
||||
ESP32_WIFI_MODE_SOFTAP,
|
||||
ESP32_WIFI_MODE_BOTH
|
||||
};
|
||||
|
||||
enum ESP32_ip_mode_t {
|
||||
ESP32_IP_MODE_NORMAL = 0,
|
||||
ESP32_IP_MODE_PASSTHROUGH
|
||||
};
|
||||
|
||||
class ESP32
|
||||
{
|
||||
public:
|
||||
ESP32(UARTSerial &s);
|
||||
~ESP32();
|
||||
|
||||
int reset();
|
||||
void print_firmware();
|
||||
|
||||
int set_wifi_mode(ESP32_wifi_mode_t mode);
|
||||
int get_conn_status();
|
||||
void list_APs(char *buf, int size);
|
||||
int join_AP(const char *id, const char *pwd);
|
||||
int quit_AP();
|
||||
|
||||
int get_IP(char* ip, int size);
|
||||
int ping(const char* ip);
|
||||
|
||||
int set_single();
|
||||
int set_multiple();
|
||||
|
||||
int set_ip_mode(ESP32_ip_mode_t mode);
|
||||
|
||||
int start_TCP_conn(const char *ip, const char *port, bool ssl=false);
|
||||
int close_TCP_conn();
|
||||
int send_URL(char *url, char *host);
|
||||
int send_TCP_data(uint8_t *data, int length);
|
||||
|
||||
int start_TCP_server(int port);
|
||||
int close_TCP_server();
|
||||
|
||||
private:
|
||||
UARTSerial &_serial;
|
||||
ATCmdParser _at;
|
||||
};
|
||||
|
||||
#endif /* __ESP32_H__ */
|
||||
@@ -48,8 +48,6 @@ build_unflags = -std=gnu++98 -std=gnu++14
|
||||
extra_scripts =
|
||||
generate_version_file.py
|
||||
|
||||
lib_ignore = ESP32
|
||||
|
||||
check_tool = cppcheck, clangtidy
|
||||
check_flags =
|
||||
cppcheck: --enable=warning,style,performance,portability,information,missingInclude -j 7
|
||||
|
||||
@@ -1,276 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2018 Martin Jäger / Libre Solar
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef UNIT_TEST
|
||||
|
||||
#ifdef WIFI_ENABLED
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
#include "load.h"
|
||||
#include "thingset.h"
|
||||
#include "board.h"
|
||||
#include "ESP32.h"
|
||||
|
||||
UARTSerial uext_serial(PIN_UEXT_TX, PIN_UEXT_RX, 115200);
|
||||
|
||||
ESP32 wifi(uext_serial);
|
||||
//DigitalOut wifi_enable(PIN_UEXT_SCL);
|
||||
DigitalOut wifi_enable(PIN_UEXT_SSEL);
|
||||
|
||||
enum WifiState {
|
||||
STATE_WIFI_RESET, // state before reset
|
||||
STATE_WIFI_INIT, // initial state
|
||||
STATE_WIFI_CONN, // successfully connected to WiFi AP
|
||||
STATE_LAN_CONN, // local IP address obtained
|
||||
STATE_INTERNET_CONN, // internet connection active
|
||||
STATE_WIFI_IDLE, // switch off WiFi to save energy
|
||||
STATE_WIFI_ERROR
|
||||
};
|
||||
|
||||
extern LoadOutput load;
|
||||
|
||||
extern ThingSet ts;
|
||||
extern const int pub_channel_emoncms;
|
||||
|
||||
int wifi_check_ap_connected()
|
||||
{
|
||||
printf("WiFi: Getting wifi module status... ");
|
||||
int status = wifi.get_conn_status();
|
||||
printf("%d\n", status);
|
||||
|
||||
if (status == ESP32_STATUS_AP_CONNECTED
|
||||
|| status == ESP32_STATUS_TCP_ACTIVE
|
||||
|| status == ESP32_STATUS_TCP_DIS)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int wifi_connect_ap()
|
||||
{
|
||||
if (wifi_check_ap_connected()) {
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
printf("WiFi: Joining network with SSID and PASS... ");
|
||||
int res = wifi.join_AP(WIFI_SSID, WIFI_PASS);
|
||||
printf("%d\n", res);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
int wifi_check_lan_connected()
|
||||
{
|
||||
char buffer[30];
|
||||
printf("WiFi: Getting IP address... ");
|
||||
int res = wifi.get_IP(buffer, sizeof(buffer));
|
||||
printf("%s\n", buffer);
|
||||
return res;
|
||||
}
|
||||
|
||||
int wifi_check_internet_connected()
|
||||
{
|
||||
printf("WiFi: Ping %s... ", EMONCMS_HOST);
|
||||
int res = wifi.ping(EMONCMS_HOST);
|
||||
printf((res > 0) ? "OK\n" : "ERROR\n");
|
||||
return res;
|
||||
}
|
||||
|
||||
void wifi_setup_internet_conn()
|
||||
{
|
||||
int res = 0;
|
||||
|
||||
printf("WiFi: Setting normal transmission mode... ");
|
||||
res = wifi.set_ip_mode(ESP32_IP_MODE_NORMAL);
|
||||
printf((res > 0) ? "OK\n" : "ERROR\n");
|
||||
|
||||
printf("WiFi: Setting single connection mode... ");
|
||||
res = wifi.set_single();
|
||||
printf((res > 0) ? "OK\n" : "ERROR\n");
|
||||
}
|
||||
|
||||
int wifi_send_emoncms_data()
|
||||
{
|
||||
char url[800];
|
||||
int res = 0;
|
||||
|
||||
printf("WiFi: Starting TCP connection to %s:%s ... ", EMONCMS_HOST, "80");
|
||||
res = wifi.start_TCP_conn(EMONCMS_HOST, "80", false);
|
||||
printf((res > 0) ? "OK\n" : "ERROR\n");
|
||||
|
||||
if (res <= 0) {
|
||||
wifi.close_TCP_conn();
|
||||
return 0;
|
||||
}
|
||||
|
||||
// create link
|
||||
sprintf(url, "/emoncms/input/post?node=%s&apikey=%s&json=", EMONCMS_NODE, EMONCMS_APIKEY);
|
||||
|
||||
// Write the ThingSet publication message to URL end - 2 bytes and afterwards
|
||||
// overwrite two publication message start bytes "# " with correct URL
|
||||
// content again to avoid strcpy
|
||||
int ts_pos = strlen(url) - 2;
|
||||
int ts_len = ts.pub_msg_json(url + ts_pos, sizeof(url) - ts_pos - 1, pub_channel_emoncms);
|
||||
url[ts_pos] = 'n';
|
||||
url[ts_pos+1] = '=';
|
||||
url[ts_pos+ts_len] = '\0'; // ThingSet does not null-terminate
|
||||
//printf("%s\n", url);
|
||||
|
||||
wait(0.1);
|
||||
printf("WiFi: Sending data... ");
|
||||
res = wifi.send_URL(url, (char *)EMONCMS_HOST);
|
||||
printf((res > 0) ? "OK\n" : "ERROR\n");
|
||||
|
||||
//wifi.close_TCP_conn();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int wifi_reset(void)
|
||||
{
|
||||
int res = 0;
|
||||
|
||||
wifi_enable = 0;
|
||||
wait(0.5);
|
||||
wifi_enable = 1;
|
||||
|
||||
printf("WiFi: Resetting wifi module... ");
|
||||
res = wifi.reset();
|
||||
printf((res > 0) ? "OK\n" : "ERROR\n");
|
||||
|
||||
//printf("WiFi: ESP8266 module firmware... \n");
|
||||
//wifi.print_firmware();
|
||||
|
||||
printf("WiFi: Setting wifi station mode... ");
|
||||
res = wifi.set_wifi_mode(ESP32_WIFI_MODE_STATION);
|
||||
printf((res > 0) ? "OK\n" : "ERROR\n");
|
||||
|
||||
//printf("WiFi: Listing APs... \n");
|
||||
//char buf[1000];
|
||||
//wifi.list_APs(buf, sizeof(buf));
|
||||
//printf(buf);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
// implement specific extension inherited from ExtInterface
|
||||
class ExtWifi: public ExtInterface
|
||||
{
|
||||
public:
|
||||
ExtWifi() {};
|
||||
void enable();
|
||||
void process_1s();
|
||||
};
|
||||
|
||||
static ExtWifi ext_wifi; // local instance, will self register itself
|
||||
|
||||
void ExtWifi::enable()
|
||||
{
|
||||
#ifdef PIN_UEXT_DIS
|
||||
DigitalOut uext_dis(PIN_UEXT_DIS);
|
||||
uext_dis = 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
void ExtWifi::process_1s()
|
||||
{
|
||||
static WifiState state = STATE_WIFI_RESET;
|
||||
static int error_counter = 0;
|
||||
|
||||
if (!load.usb_pgood) {
|
||||
wifi_enable = 0;
|
||||
state = STATE_WIFI_IDLE;
|
||||
}
|
||||
|
||||
if (uptime() % 10 == 0) {
|
||||
|
||||
printf("WiFi state: %d, error counter: %d\n", state, error_counter);
|
||||
|
||||
switch (state) {
|
||||
case STATE_WIFI_RESET:
|
||||
if (wifi_reset() > 0) {
|
||||
state = STATE_WIFI_INIT;
|
||||
}
|
||||
else {
|
||||
state = STATE_WIFI_ERROR;
|
||||
printf("WiFi: ESP module not recognized. Going to error state.\n");
|
||||
}
|
||||
break;
|
||||
case STATE_WIFI_INIT:
|
||||
if (wifi_connect_ap()) {
|
||||
error_counter = 0;
|
||||
state = STATE_WIFI_CONN;
|
||||
break;
|
||||
}
|
||||
else if (error_counter > 10) { // after 10 attempts go back to previous state
|
||||
error_counter = 0;
|
||||
state = STATE_WIFI_RESET;
|
||||
}
|
||||
error_counter++;
|
||||
break;
|
||||
case STATE_WIFI_CONN:
|
||||
if (wifi_check_lan_connected()) { // check if already got IP address
|
||||
wifi_setup_internet_conn();
|
||||
error_counter = 0;
|
||||
state = STATE_LAN_CONN;
|
||||
break;
|
||||
}
|
||||
else if (error_counter > 10) { // after 10 attempts go back to previous state
|
||||
error_counter = 0;
|
||||
state = STATE_WIFI_INIT;
|
||||
}
|
||||
error_counter++;
|
||||
break;
|
||||
case STATE_LAN_CONN:
|
||||
if (wifi_check_internet_connected()) {
|
||||
error_counter = 0;
|
||||
state = STATE_INTERNET_CONN;
|
||||
break;
|
||||
}
|
||||
else if (error_counter > 10) { // after 10 attempts go back to previous state
|
||||
error_counter = 0;
|
||||
state = STATE_WIFI_CONN;
|
||||
}
|
||||
error_counter++;
|
||||
break;
|
||||
case STATE_INTERNET_CONN:
|
||||
if (wifi_check_internet_connected()) { // still connected?
|
||||
error_counter = 0;
|
||||
#ifdef EMONCMS_ENABLED
|
||||
wifi_send_emoncms_data();
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
else if (error_counter > 10) { // after 10 attempts go back to previous state
|
||||
error_counter = 0;
|
||||
state = STATE_WIFI_CONN;
|
||||
}
|
||||
error_counter++;
|
||||
break;
|
||||
case STATE_WIFI_ERROR:
|
||||
if (error_counter > 240) { // after approx 1h try again
|
||||
error_counter = 0;
|
||||
state = STATE_WIFI_RESET;
|
||||
}
|
||||
error_counter++;
|
||||
break;
|
||||
case STATE_WIFI_IDLE:
|
||||
if (load.usb_pgood) {
|
||||
state = STATE_WIFI_RESET;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* WIFI_ENABLED */
|
||||
|
||||
#endif /* UNIT_TEST */
|
||||
Reference in New Issue
Block a user