Wemos D1 not running code on startup

Hello, I am new here and I needed some help. I have a machine working at 24VDC, I have a power supply 24VDC to 5VDC connected to the WEMOS Vin pin to activate it. The arduino is sending it's own 3.3V to itself through a relay that activates with 24VDC impulses from a optical fiber cable.

The Wemos then sends POST messages everytime it detects the impulse. This works fine, but everytime the machine is turned off like on the weekend, when it powers back up, the Wemos doesn't run the code and I have to go restart it.

At first I didn't have the reconnect function, and when I pushed the reboot button the code would go through the setup fine and would say "connected" but when it verified again in the loop, it would return code 6 (aka disconnected) and I had to inject the code again to the wemos to work again because the reboot wouldn't solve it.

Now, with the reconnect function I only have to reboot, but still, I would like it to work without needing to reboot everytime

Can anyone help?

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
//#include <HTTPClient.h>

// Replace with your network credentials
const char* ssid = "dummy";
const char* password = "pass";

String contador ="430";
int previous_val=0;
int soma=0;
byte mac[6]; // the MAC address of your Wifi shield

void reconnect() {
Serial.print("Reconnecting");
IPAddress ip(xxxx.xxxx.xxxx.xxxx); // IP do ESP8266
IPAddress gateway(yyyy.yyyy.yyyy.yyyy); //IP do gateway
IPAddress subnet(255,255,255,0); //mascara da rede
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected!");
}

void setup () {
Serial.begin(115200);

WiFi.begin(ssid, password);

pinMode(D5, INPUT);

//network configuration//

IPAddress ip(xxxx.xxxx.xxxx.xxxx); // IP ESP8266
IPAddress gateway(yyyy.yyyy.yyyy.yyyy); //IP do gateway
IPAddress subnet(255,255,255,0); //mascara da rede
WiFi.config(ip, gateway, subnet); //configuraĆ§Ć£o

while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print("Connecting..");
}
Serial.println("Connected");

WiFi.macAddress(mac);
Serial.print("MAC: ");
Serial.print(mac[5],HEX);
Serial.print(":");
Serial.print(mac[4],HEX);
Serial.print(":");
Serial.print(mac[3],HEX);
Serial.print(":");
Serial.print(mac[2],HEX);
Serial.print(":");
Serial.print(mac[1],HEX);
Serial.print(":");
Serial.println(mac[0],HEX);

}

void loop() {

int sensorVal = digitalRead(D5);
//print out the value of the pushbutton
//Serial.println(sensorVal);
delay(50);
if(sensorVal!=previous_val){
Serial.println(sensorVal);
if(sensorVal){
HTTPClient http;

if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
http.begin("http://2222.2222.2222.2222:8080/api/machineinjopen/"+contador); //API de Destino para o pedido HTTP
http.addHeader("Content-Type", "text/plain"); //Specify content-type header
int httpResponseCode = http.POST(contador); //Send the actual POST request and store response code

if(httpResponseCode>0){
String response = http.getString(); //Get the response to the request should be 201
Serial.println(httpResponseCode); //Print return code
Serial.println(response); //Print request answer
}
else{
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
}
http.end(); //Free resources
}
else{
Serial.print("Error in WiFi connection ERROR CODE: ");
Serial.println(WiFi.status());
Serial.println(".....");
reconnect();
}
delay(100);
}
}
previous_val =sensorVal; //last value to compare
}

Hello and welcome to the forum.

Please read 'how to use this forum - please read' (there's 2 big clues in the title), then go back and edit your question according to the instructions, especially item #7.

Please include a schematic rather than rely on a text description of your circuit.

Thank you.

If you are detecting that the unit is disconnected and the unit needs a reboot to reconnect you might get away with using a ESP.restart();.

Something like:

If me detect no connection then me do a ESP.restart();

dragon123xy:
when I pushed the reboot button the code would go through the setup fine and would say "connected" but when it verified again in the loop, it would return code 6 (aka disconnected)

The "connected" message refers to the WiFi connection. The code 6 refers to the connection to the http client connection to the server. Two different connections.

The http.begin() method will also return a status code (0 means OK). Maybe change your code to check that before moving on to the code which sends the POST request. Maybe it is failing at that point.

The code 6 refers to the wifi connection, I print de WIFI.STATUS() and it gives me 6. I also print the response code and that gives me 201 when everything is ok and -1 when it fails.

dragon123xy:
The code 6 refers to the wifi connection, I print de WIFI.STATUS() and it gives me 6. I also print the response code and that gives me 201 when everything is ok and -1 when it fails.

Sorry, it wasn't clear from your first post which line the error message was from.

You still haven't fixed that post, by the way.