Newbie NodeMCU to webhooks question

Hi all, firstly I really hope that this is posted in the right category. I've recently become acquainted with Arduino, and still have very limited knowledge, but I'll try my best to explain my project and the issue I'm facing. I'm assuming that the answer will be very obvious to most :grinning_face_with_smiling_eyes:

I'm become obsessed with smart home gadgets lately, where I'm using Smartthings to control many devices in my home (I already have an ESP-01S powered iRobot, which works great!). I have an iSmartgate garage door opener, when together with my phone as a presence sensor via Smartthings, works great for automatically opening the garage door when I come home. The only thing is that it opens the garage door every time I arrive home, when in reality, I only want it to open the garage door automatically when driving home, and not walking home...

So I got inspired by this project (linked below), with an idea to use an ultrasonic sensor to detect if the car is in the garage. This would then send a request to webhooks/ifttt to turn on or off a virtual switch in Smartthings, where this switch is part of an automation I have set up.

I have copied the exact component and Arduino code as a starting point, just modifying the code to suit my project. For example, I've changed the webpage to display either 'yes' or 'no' for whether the car is present or not. So it should all be quite simple.

When testing at the computer, I can flash the code to the NodeMCU, and the system works straight away, I can see the requests being sent to webhooks every 2 minutes, my virtual switch updates, so everything looks great. I've let it run for a couple of hours whilst still connected to the computers usb connection, and it seems to work fine.

Now here's where the issues arise (and probably my newbie stupidness). My end goal is to power this via a wall socket on the ceiling of the garage. Once I use a 5v phone charger connected to the NodeMCU's micro USB port, the system will only run once (send one request to webhooks), and that's it. Therefore doesn't send new requests to webhooks, and therefore not update my virtual switch. If I then go back and plug it in at the computer, it works as it should, sending regular requests to webhooks etc.

So what an I missing here? Any help would be greatly appreciated. I'm get the code uploaded as I'm sure it could be streamlined by the pros :grinning:

Here is the code if it helps to answer my issue :slight_smile:


#include <ESP8266WiFi.h>
const int trigPin = D5;
const int echoPin = D6;
long duration;
int distance; 
float level;
const char* ssid = "xxxxx"; 
const char* password = "xxxxxxxxxxx";
void send_event(const char *event);
const char *host = "maker.ifttt.com";
const char *privateKey = "xxxxxxxxxxxxxxx";
WiFiServer server(80);
void setup() {
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 Serial.begin(9600);
 Serial.print("Connecting to Wifi Network");
 Serial.println(ssid);
 WiFi.begin(ssid, password);
 while (WiFi.status() != WL_CONNECTED) {
 delay(500);
 Serial.print(".");
 }
 Serial.println("");
 Serial.println("Successfully connected to WiFi.");
 Serial.println("IP address is : ");
 Serial.println(WiFi.localIP());
 server.begin();
 Serial.println("Server started");
}
void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.0340 / 2;
  Serial.println("Distance");
  Serial.println(distance);
  delay(1000);
  WiFiClient client = server.available();
  if (client) 
{ 
Serial.println("Web Client connected ");
String request = client.readStringUntil('\r'); 
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Refresh: 10");  // update the page after 10 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<style>html { font-family: Cairo; display: block; margin: 0px auto; text-align: center;color: #333333; background-color: ##f3ffee;}");
client.println("body{margin-top: 50px;}");
client.println("h1 {margin: 50px auto 30px; font-size: 50px;  text-align: center;}");
client.println(".side_adjust{display: inline-block;vertical-align: middle;position: relative;}");
client.println(".text1{font-weight: 180; padding-left: 5px; font-size: 50px; width: 170px; text-align: left; color: #3498db;}");
client.println(".data1{font-weight: 180; padding-left: 1px; font-size: 50px;color: #3498db;}");
client.println(".data{padding: 1px;}");
client.println("</style>");
client.println("</head>");
client.println("<body>");
client.println("<div id=\"webpage\">");   
client.println("<h1>Car in Garage</h1>");
client.println("<div class=\"data\">");
client.println("<div class=\"side_adjust text1\">Car home?</div>");
client.println("<div class=\"side_adjust data1\">");
if (distance<100)
  {
client.print("Yes");
}
  else{
client.print("No");
 }
client.println("</div>");
client.println("</div>");
client.println("</body>");
client.println("</html>");
//client.println("<h1>Level Indicator</h1>");
   if ( distance <= 100) {
        send_event("Car_in_garage");
        }
   else 
        send_event("Car_not_in_garage");
} 
}
void send_event(const char *event)
{
  Serial.print("Connecting to "); 
  Serial.println(host);
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("Connection failed");
    return;
  }
  // We now create a URI for the request
  String url = "/trigger/";
  url += event;
  url += "/with/key/";
  url += privateKey;
  Serial.print("Requesting URL: ");
  Serial.println(url);
  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");
  while(client.connected())
  {
    if(client.available())
    {
      String line = client.readStringUntil('\r');
      Serial.print(line);
    } else {
      // No data yet, wait a bit
      delay(60000);
    };
  }


}