Hi. I connected my ESP01s to send data to the Thingsboard cloud server to record the water level in the tank which triggers a relay that turns on a solenoid valve. It can send data to the server but it gets timeout after a few hours or sometimes even minutes. The ESP is powered by Arduino 3.3 V
This is the error message i got from serial monitor
[WiFiEsp] >>> TIMEOUT >>> [WiFiEsp] Data packet send error (2) [WiFiEsp] Failed to write to socket 3 [WiFiEsp] Disconnecting 3
#include "ThingsBoard.h"
#include <WiFiEspClient.h>
#include <WiFiEsp.h>
#include "SoftwareSerial.h"
#define WIFI_AP "Farm"
#define WIFI_PASSWORD "Farm"
#define TOKEN "U755rIAUVo57uzsQGjhnk"
#define THINGSBOARD_SERVER "46.137.226.125"
// Baud rate for serial debug
#define SERIAL_DEBUG_BAUD 9600
// Baud rate for communicating with ESP chip
#define SERIAL_ESP8266_BAUD 9600
// Serial driver for ESP
SoftwareSerial soft(2, 3); // RX, TX
// Initialize the Ethernet client object
WiFiEspClient espClient;
// Initialize ThingsBoard instance
ThingsBoard tb(espClient);
// the Wifi radio's status
int status = WL_IDLE_STATUS;
int trigpin = 4;
int echopin = 5;
int relay = 6;
long duration, cm, inches;
/* #include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 7
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float Celsius = 0;
float Fahrenheit = 0; */
void setup() {
Serial.begin(9600);
pinMode(trigpin,OUTPUT);
pinMode(echopin,INPUT);
pinMode(relay, OUTPUT);
digitalWrite(relay,LOW);
// sensors.begin(); // water sensor begin
// initialize serial for debugging
Serial.begin(SERIAL_DEBUG_BAUD);
// initialize serial for ESP module
soft.begin(SERIAL_ESP8266_BAUD);
// initialize ESP module
WiFi.init(&soft);
// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue
while (true);
}
Serial.println("Connected to AP");
}
void loop() {
delay(60000);
ultrasonic();
// temp();
int x = cm;
Serial.println(x);
/* Serial.print(Celsius);
Serial.print(" C ");
Serial.print(Fahrenheit);
Serial.println(" F"); */
if (x >=50 && x<=70){
digitalWrite(relay, HIGH); //turn on valve
}
else if (x<50){
digitalWrite(relay, LOW); // turn off valve
}
if (status != WL_CONNECTED) {
Serial.println("Connecting to AP ...");
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(WIFI_AP);
// Connect to WPA/WPA2 network
status = WiFi.begin(WIFI_AP, WIFI_PASSWORD);
return;
}
if (!tb.connected()) {
// Connect to the ThingsBoard
Serial.print("Connecting to: ");
Serial.print(THINGSBOARD_SERVER);
Serial.print(" with token ");
Serial.println(TOKEN);
if (!tb.connect(THINGSBOARD_SERVER, TOKEN)) {
Serial.println("Failed to connect");
return;
}
}
Serial.println("Sending data...");
tb.sendTelemetryInt("WaterLevel", x );
// tb.sendTelemetryFloat("Temperature", Celsius );
tb.loop();
}
I run a periodic task that checks for WiFi a connection. If the WiFI is not connected, then connect.
void MQTTkeepalive( void *pvParameters )
{
sema_MQTT_KeepAlive = xSemaphoreCreateBinary();
xSemaphoreGive( sema_MQTT_KeepAlive ); // found keep alive can mess with a publish, stop keep alive during publish
MQTTclient.setKeepAlive( 90 ); // setting keep alive to 90 seconds makes for a very reliable connection, must be set before the 1st connection is made.
TickType_t xLastWakeTime = xTaskGetTickCount();
const TickType_t xFrequency = 250; //delay for ms
for (;;)
{
//check for a is-connected and if the WiFi 'thinks' its connected, found checking on both is more realible than just a single check
if ( (wifiClient.connected()) && (WiFi.status() == WL_CONNECTED) )
{
xSemaphoreTake( sema_MQTT_KeepAlive, portMAX_DELAY ); // whiles MQTTlient.loop() is running no other mqtt operations should be in process
MQTTclient.loop();
xSemaphoreGive( sema_MQTT_KeepAlive );
}
else {
log_i( "MQTT keep alive found MQTT status % s WiFi status % s", String(wifiClient.connected()), String(WiFi.status()) );
if ( !(wifiClient.connected()) || !(WiFi.status() == WL_CONNECTED) )
{
connectToWiFi();
}
connectToMQTT();
}
xLastWakeTime = xTaskGetTickCount();
vTaskDelayUntil( &xLastWakeTime, xFrequency );
}
vTaskDelete ( NULL );
}
You could use a WiFi callback function to reconnect upon lost connection.
And, for the sad news. It's not reasonable to power the ESP with a Uno 3.3V. The ESP will draw a large amount of current at WiFi transmit time that can overwhelm the Uno.
And, for the sad news. It's not reasonable to power the ESP with a Uno 3.3V. The ESP will draw a large amount of current at WiFi transmit time that can overwhelm the Uno.
Would a 12 to 3.3V stepdown converter from a external buck converter work for the ESP instead of powering from Arduino?
Railroader:
Where is the 12 to 5 stepdown done? By an external buck converter?
Yes. I have a LM2596 buck converter that steps down from a 12V PSU.
momomola1:
Would a 12 to 3.3V stepdown converter from a external buck converter work for the ESP instead of powering from Arduino?
I have found that ESP32's do not operate well when supplying them with 3.3V from a switching regulator.
When I power the ESP32 development modules with 5V from a switching regulator, I use a 47uF cap hung off the 3.3V pin.
If I want to lower the power consumed by the ESP32, I supply 5V, switched, to a MCP1700 to get 3.3V, which feeds into the 3.3V pin of the ESP32. Of course proper input / output filtering is done with the MCP1700.