1

First Post.

I'm using a NodeMCU esp8266 connected to a digital vibration sensor. My project is to detect when a pager vibrates to then trigger through the maker channel of ifttt an outcome.

I'm following this project as my base, hoping to adapt it ever so slightly, but the more I adapt the code the less it seems to work. https://blog.erindachtler.me/tutorial-a-wifi-enabled-washing-machine/

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

#define WIFI_SSID "wifissid"
#define WIFI_KEY "wifikey"

#define NOTIFY_URL 
"https://maker.ifttt.com/trigger/myproject/with/key/mykey"

int vibration = 16; //vibration sensor   
int LedPin =4; //built in led
int numberofshouts = 0;
int vibrationstate = 0;
int lastvibrationstate = 0;

void setup() {
pinMode(vibration, INPUT);
pinMode(LedPin, OUTPUT);
Serial.begin(9600);

// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(WIFI_SSID);

WiFi.begin(WIFI_SSID, WIFI_KEY);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");

// Start the server
Serial.println("Server started");

// Print the IP address
Serial.println(WiFi.localIP());

} 


void loop() {
// put your main code here, to run repeatedly:
int vibrationvalue = digitalRead(vibration);

if (vibrationvalue == HIGH)  {
  Serial.print("Vibration");
  sendDoneNotification();
}

else {
digitalWrite(LedPin, LOW);
}

}

void sendDoneNotification() {
WiFi.begin(WIFI_SSID, WIFI_KEY);

while((WiFi.status() != WL_CONNECTED)) {
delay(100);
}

HTTPClient http;
http.begin(NOTIFY_URL);
int httpCode = http.GET();
if(httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] GET... code: %d\n", httpCode);

// file found at server
if(httpCode == HTTP_CODE_OK) {
  String payload = http.getString();
  Serial.println(payload);
}
} else {
  Serial.printf("[HTTP] GET... failed, error: %s\n", 
http.errorToString(httpCode).c_str());
}
http.end();

 WiFi.disconnect();
 }

What happens is my serial just reports

[HTTP] GET... failed, error: connection refused

I don't quite get the post commands. I basically want it to read the vibration sensor and while it's reporting no movement do nothing, but after it detects vibration for a couple of seconds it then sends the post command to the maker url that will trigger my ifttt command, thus notifying me on my phone etc.

I have created and run a serial print program to test the sensor and adjust the sensitivity so I know the sensor is working. My NodeMCU doesn't have a D1 pin so i have it connected to D0, not sure if this is correct or not.

Any tips or advice if there is an easier way of doing this.

Many thanks and have a nice day.

Adrian.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.