ESP 8266 WiFiManager.h> //https://github.com/tzapu/WiFiManager wifiManager.setConnectTimeout(180); - esp8266

in my cases wifiManager.setConnectTimeout(180); does not wait for 180 sec, But only 60 sec. my router takes about 2 minutes to setup after power outage my esp does not connect to the router until it hard reset.
code is here for your reviews.
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
void setup() {
Serial.begin(115200);
WiFiManager wifiManager;
//******this
wifiManager.setConnectTimeout(180);
wifiManager.setTimeout(180);
if(!wifiManager.autoConnect("AutoConnectAP")) {
Serial.println("failed to connect and hit timeout");
delay(3000);
ESP.reset();
delay(5000);
}
Serial.println("connected...yeey :)");
}
void loop() {
// put your main code here, to run repeatedly:
}

Related

How to drive stepper motor using Arduino Accelstepper library with ros message?

At the moment I am working on a code that should send a message to a arduino uno through rosserial to drive the stepper motor to move a speciafied number of steps. I was using a 28BYJ-48 with ULN2003 driver. Everything is hooked up correctly. My goal is to send a message using raspberry pi 2b to arduino uno through rosserial and that then the stepper moves to a certain position. However, I am not succeeding in this. I am using AccelStepper and at the moment I have the following code:
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include <WProgram.h>
#endif
// Include the AccelStepper Library
#include <AccelStepper.h>
#include <ros.h>
#include <std_msgs/UInt16.h>
#include <std_msgs/Empty.h>
// Define step constant
#define HALFSTEP 8
// Creates an instance
// Pins entered in sequence IN1-IN3-IN2-IN4 for proper step sequence
AccelStepper stepper(HALFSTEP, 4, 6, 5, 7);
uint8_t run_flag = 0;
void startStepper(const std_msgs::Empty& start_msg) {
run_flag = 1;
digitalWrite(13, HIGH);
}
void runStepper(const std_msgs::UInt16& pos_msg) {
run_flag = 2;
stepper.moveTo(pos_msg.data);
}
ros::NodeHandle nh;
ros::Subscriber<std_msgs::Empty> stepperStart("stepper/start", &startStepper);
ros::Subscriber<std_msgs::UInt16> stepperRun("stepper/run", &runStepper);
void setup() {
pinMode(13, OUTPUT);
// set the maximum speed, acceleration factor,
// initial speed and the target position
stepper.setMaxSpeed(1000.0);
stepper.setAcceleration(50.0);
stepper.setSpeed(200);
stepper.moveTo(2038);
nh.initNode();
nh.subscribe(motorStart);
nh.subscribe(motorRun);
}
void loop() {
if (run_flag == 1) {
stepper.run();
if (stepper.distanceToGo() == 0) {
run_flag = 0;
digitalWrite(13, LOW);
stepper.setCurrentPosition(0);
}
}
nh.spinOnce();
}
Run "roscore" in the terminal;
Open a new terminal and run the command "rosrun rosserial_python serial_node.py /dev/ttyACM0"
Open a new terminal and run the command "rostopic pub stepper/start std_msgs/Empty --once"
The code could run correctly for the first time after the arduino uno was power on. After that the arduino did not respond to the message from raspberry pi.
Any help you can offer me is appreciated! Thanks for your time.

Lose MQTT Connection after 15-30 minutes

I am currently having a problem with my ESP8266 establishing a stable connection to my mosquitto MQTT broker.
I have moved house and am therefore using a different network.
In the previous network, my ESP ran stably and I had no problems at all. The MQTTserver run on a Raspberry PI 4.
In the new network, as already mentioned, it breaks off every 15-30 minutes (no fixed length of time).
The code is of course adapted to the new network and the IP of the broker.
The code I use is probably the generally known code for connecting to the MQTT server.
I only added some I/O Ports.
Since the code worked without problems in the old network, I first thought that the WLAN connection was failing.
So in the loop I inserted the reestablishment with the WLAN.
But this is running stably:
Attempting MQTT connection...failed, rc=-2 try again in 5 seconds
Attempting MQTT connection...failed, rc=-2 try again in 5 seconds
Connecting to Vodafone-947E
.....
WiFi connected
IP address:
192.168.0.52
Attempting MQTT connection...failed, rc=-2 try again in 5 seconds
Attempting MQTT connection...failed, rc=-2 try again in 5 seconds
..............
Can it be that my new router simply interrupts the connection after some time? Otherwise, I don't understand what the problem could be.
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
//----------------------------------------------------------------------------------------------------------------------
#define wifi_ssid "xxxxxxx"
#define wifi_password "xxxxxxxxx"
#define mqtt_server "192.168.0.xxx"
//----------------------------------------------------------------------------------------------------------------------
WiFiClient espClient;
PubSubClient client(espClient);
bool status;
const int OutputPin2 = 12;
const int ResetPin = 4;
int ResetCounter =0;
//----------------------------------------------------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
pinMode(OutputPin2, OUTPUT);
pinMode(ResetPin, OUTPUT);
digitalWrite(OutputPin2, HIGH);
digitalWrite(ResetPin, HIGH);
}
//----------------------------------------------------------------------------------------------------------------------
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
WiFi.begin(wifi_ssid, wifi_password);
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId= "ESP8266-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
delay(100);
if (client.connect(clientId.c_str())) {
Serial.println("connected");
// Once connected, publish an announcement...
// ... and resubscribe
client.subscribe("esp3/LED");
} else {
ResetCounter++;
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
Serial.println(ResetCounter);
if (ResetCounter >=5)
{
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
//digitalWrite(ResetPin, LOW);
ResetCounter =0;
}
// Wait 5 seconds before retrying
delay(5000);
}
}
}
//----------------------------------------------------------------------------------------------------------------------
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
}
//----------------------------------------------------------------------------------------------------------------------
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(wifi_ssid);
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
//----------------------------------------------------------------------------------------------------------------------
void callback(char* topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();
client.subscribe ("esp3/LED");
if (strcmp(topic,"esp3/LED")) {
Serial.print("Changing output to ");
if(messageTemp == "on LED"){
Serial.println("on LED");
digitalWrite(OutputPin2, LOW); //Invertiertes Signal
delay(200);
}
else if(messageTemp == "off LED"){
Serial.println("off LED");
digitalWrite(OutputPin2, HIGH);
delay(200);
}
}
}
```
Fortunately, I was able to solve the problem. The reason was that the WLAN signal on the ESP8266 was simply too low. As a result, the ESP was not able to stay on the network permanently. I actually ruled this out because the router is only one room away. The problem is that many routers in the surrounding flats also transmit on the same frequency band and thus influence the signal. I bought a WLAN amplifier (of course this does not improve the situation with the signal overlapping). Now I have no more problems with disconnects from the ESP and this has been the case for 2 days.

ESP8266 unable to read data from Google Firebase

I'm trying to make an IOT app. My android side is working good so far, however my ESP8266 is unable to read data from Firebase Database. There is no error while connecting to the internet using onboard ESP8266 though. Can anyone point to a possible error? Thank You
#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>
#define WIFI_SSID "WiFi"
#define WIFI_PASSWORD "xxxxxxxx"
#define FIREBASE_HOST "https://iot.firebaseio.com"
#define FIREBASE_AUTH "xxxxxxxx"
int LED1 = D5;
void setup()
{
Serial.begin(115200);
pinMode(LED1, OUTPUT);
Serial.println('\n');
wifiConnect();
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
delay(10);
}
void loop()
{
if(WiFi.status() != WL_CONNECTED)
{
wifiConnect();
}
delay(10);
Serial.print("Data from firebase:" + Firebase.getString("LED1") + "\n");
digitalWrite(LED1, Firebase.getString("LED1").toInt());
//digitalWrite(LED1, HIGH);
delay(10);
}
void wifiConnect()
{
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); // Connect to the network
Serial.print("Connecting to ");
Serial.print(WIFI_SSID); Serial.println(" ...");
int teller = 0;
while (WiFi.status() != WL_CONNECTED)
{ // Wait for the Wi-Fi to connect
delay(1000);
Serial.print(++teller); Serial.print(' ');
}
Serial.println('\n');
Serial.println("Connection established!");
Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
}
For anyone encountering this issue. I got it working by removing the forward slash '/' at the end and the https:// from the start of Host Address.
At the line:
#define FIREBASE_HOST "https://iot.firebaseio.com"
you should fix to:
#define FIREBASE_HOST "iot.firebaseio.com"

TX/RX Communication with NodeMCU

I am trying to set up a transmitter-receiver-style relationship between two NodeMCUs (ESP-12e) using WiFi. I have one NodeMCU (the "transmitter") configured as a server and the other ("receiver") as a client. The transmitter reads in an analog value and sends it to the receiver, which outputs it in digital format via 8 LEDs. The transmitter says that the network is active, but the client cannot connect, and when I look in my laptop WiFi manager the network isn't there either. Can someone tell me what I'm doing wrong? Is it something in my code or is it how the board is configured?
Transmitter/Server Code:
#include <ESP8266WiFi.h>
#define PORT 8555
#define LED0 13
#define LED1 14
#define ADC 0
const char* NETWORK = "NodeMCU";
const char* PASSKEY = "asdf1234";
WiFiServer host(PORT);
WiFiClient node;
IPAddress IP(192,168,4,1);
IPAddress gateway(192,168,4,1);
IPAddress subnet(255,255,255,0);
void setup() {
pinMode(LED0, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(ADC, INPUT);
digitalWrite(LED0, LOW);
digitalWrite(LED1, LOW);
WiFi.disconnect();
WiFi.mode(WIFI_AP);
WiFi.softAP(NETWORK, PASSKEY);
WiFi.softAPConfig(IP, gateway, subnet);
delay(100);
WiFi.begin(NETWORK, PASSKEY);
delay(100);
host.begin();
delay(100);
digitalWrite(LED1, HIGH);
}
void loop() {
if (node.connected()) digitalWrite(LED0, HIGH);
else digitalWrite(LED0, LOW);
host.write(analogRead(ADC) / 4);
}
Receiver/Client Code:
#include <ESP8266WiFi.h>
#define PORT 8555
#define DAC0 16
#define DAC1 5
#define DAC2 4
#define DAC3 2
#define DAC4 14
#define DAC5 12
#define DAC6 13
#define DAC7 15
const char* NETWORK = "NodeMCU";
const char* PASSKEY = "asdf1234";
IPAddress hostIP(192,168,4,1);
WiFiClient node;
void setup() {
pinMode(DAC0, OUTPUT);
pinMode(DAC1, OUTPUT);
pinMode(DAC2, OUTPUT);
pinMode(DAC3, OUTPUT);
pinMode(DAC4, OUTPUT);
pinMode(DAC5, OUTPUT);
pinMode(DAC6, OUTPUT);
pinMode(DAC7, OUTPUT);
digitalWrite(DAC7, HIGH);
WiFi.disconnect();
WiFi.mode(WIFI_STA);
WiFi.begin(NETWORK, PASSKEY);
while (WiFi.status() != WL_CONNECTED) { // <-- gets stuck here
digitalWrite(DAC7, LOW);
delay(200);
digitalWrite(DAC7, HIGH);
delay(200);
}
if(node.connect(hostIP, PORT)) digitalWrite(DAC7, LOW);
}
void loop() {
if (node.available()) adcWrite(node.read());
}
void adcWrite(byte level) {
bool vals[8];
for (int i = 0; i < 8; i++) {
vals[i] = level % 2;
level /= 2;
}
digitalWrite(DAC0, vals[0]);
digitalWrite(DAC1, vals[1]);
digitalWrite(DAC2, vals[2]);
digitalWrite(DAC3, vals[3]);
digitalWrite(DAC4, vals[4]);
digitalWrite(DAC5, vals[5]);
digitalWrite(DAC6, vals[6]);
digitalWrite(DAC7, vals[7]);
}
Ports LED0 and LED1 on the TX and DAC0-7 on the RX are connected to LEDs, the ADC on the TX is connected to a 10k potentiometer. When I run the code, LED0 is off, LED1 is on, and DAC7 blinks indefinitely.

Node MCU failed to connect with firebase but does not return any error code

I need to send my data to firebase via NODE MCU. I've created an application that is used to turn on and off led in node mcu. My node mcu connects with the wifi network but does not send data to firebase. The if(firebase.failed()) executes, but does not return an error code. In the serial monitor it just prints setting/number failed:. How can I fix this?
#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>
#include <ArduinoJson.h>
#define FIREBASE_HOST "http://temphu*****.firebaseio.com/"
#define FIREBASE_AUTH "VblTNS************OmWTW6n"
#define WIFI_SSID "A****"
#define WIFI_PASSWORD "9*****"
#define LED 2
void setup() {
pinMode(LED,OUTPUT);
digitalWrite(LED,0);
Serial.begin(9600);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("connecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.print("connected: ");
Serial.println(WiFi.localIP());
Firebase.begin(FIREBASE_HOST,FIREBASE_AUTH);
Firebase.setInt("LEDStatus",0);
}
void loop() {
if(Firebase.getInt("LEDStatus")) {
digitalWrite(LED,HIGH);
}
else {
digitalWrite(LED,LOW);
}
if (Firebase.failed()) { // Check for errors
Serial.print("setting /number failed:");
Serial.println(Firebase.error());
return;
}
delay(1000);
}
In the source code it clearly says that (https://raw.githubusercontent.com/FirebaseExtended/firebase-arduino/master/src/FirebaseError.h) two error codes are used in addition to regular HTTP error codes. Therefore Firebase rejection might not raise an error even though it fails.
So check the firebase rules and change read/write to true.

Resources