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 :smile:

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 = "";
const char *privateKey = "xxxxxxxxxxxxxxx";
WiFiServer server(80);
void setup() {
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 Serial.print("Connecting to Wifi Network");
 WiFi.begin(ssid, password);
 while (WiFi.status() != WL_CONNECTED) {
 Serial.println("Successfully connected to WiFi.");
 Serial.println("IP address is : ");
 Serial.println("Server started");
void loop() {
  digitalWrite(trigPin, LOW);
  digitalWrite(trigPin, HIGH);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.0340 / 2;
  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("<!DOCTYPE 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("<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.println("<h1>Level Indicator</h1>");
   if ( distance <= 100) {
void send_event(const char *event)
  Serial.print("Connecting to "); 
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("Connection failed");
  // We now create a URI for the request
  String url = "/trigger/";
  url += event;
  url += "/with/key/";
  url += privateKey;
  Serial.print("Requesting 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");
      String line = client.readStringUntil('\r');
    } else {
      // No data yet, wait a bit


This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.