I attached all sensor into the Wemos-D1 for uploading the sensor data to the server. but when I upload the data to the server, it connects to the server but cannot save the data into the database and after that send the second data to the server, it shows connections failed... besides, the sensor such as ultrasonic sensor and IR sensor cannot function.
here is the source code for connecting the sensor to the wemos. Not sure the connecting those sensors are correct or not to the wemos.
#include <ESP8266WiFi.h>
#include <ESP.h>
#include <LiquidCrystal.h>
//LiquidCrystal LCD(12, 11, 5, 4, 3, 2);
LiquidCrystal LCD(D0,D1, D5, D4, D3, D2);
const char* ssid = "wifi username";
const char* password = "wifi password";
const char* host = "192.168.1.10";
#define in D7 //7
#define out D8 //8
#define trigPin D2 //10
#define echoPin D3 //13
#define relay D2 //2
#define led D6 //6 //red color
#define led2 D0 //yellow color
#define led3 D1 //9 //green color
int count = 0;
int distanceCm;
long duration;
void IN()
{
count++;
LCD.clear();
LCD.print("Number of Item:");
LCD.setCursor(0, 1);
LCD.print(count);
delay(10);
LCD.setCursor(3, 1);
LCD.print(distanceCm);
delay(10);
LCD.setCursor(6, 1);
LCD.print(" cm");
if (distanceCm <= 3) {
delayMicroseconds(10);
LCD.setCursor(10, 1);
LCD.print("Full");
digitalWrite(led, HIGH);
} else {
digitalWrite(led, LOW);
}
if (distanceCm > 3 && distanceCm <= 5) {
delayMicroseconds(10);
LCD.setCursor(10, 1);
LCD.print("Enable");
digitalWrite(led2, HIGH);
} else {
digitalWrite(led2, LOW);
}
if (distanceCm > 5) {
delayMicroseconds(10);
LCD.setCursor(10, 1);
LCD.print("Empty");
// digitalWrite(led3, HIGH);
} else {
// digitalWrite(led3, LOW);
}
delay(500);
}
void OUT()
{
count--;
LCD.clear();
LCD.print("Number of Item:");
LCD.setCursor(0, 1);
LCD.print(count);
delay(10);
LCD.setCursor(3, 1);
LCD.print(distanceCm);
LCD.setCursor(6, 1);
LCD.print(" cm");
if (distanceCm <= 3) {
delayMicroseconds(10);
LCD.setCursor(10, 1);
LCD.print("Full");
digitalWrite(led, HIGH);
} else {
digitalWrite(led, LOW);
}
if (distanceCm > 3 && distanceCm <= 5) {
delayMicroseconds(10);
LCD.setCursor(10, 1);
LCD.print("Enable");
digitalWrite(led2, HIGH);
} else {
digitalWrite(led2, LOW);
}
if (distanceCm > 5) {
delayMicroseconds(10);
LCD.setCursor(10, 1);
LCD.print("Empty");
digitalWrite(led3, HIGH);
} else {
digitalWrite(led3, LOW);
}
delay(500);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
delay(10);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
LCD.begin(16, 2);
LCD.print("Item Counter");
delay(2000);
pinMode(in, INPUT);
pinMode(out, INPUT);
pinMode(relay, OUTPUT);
LCD.clear();
LCD.print("Item In Box:");
LCD.setCursor(0, 1);
LCD.print(count);
// ESP.restart();
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
// ESP.wdtFeed();
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
int value = 0;
void loop() {
delay(0);
delay(5000);
++value;
digitalWrite(trigPin, LOW);
delayMicroseconds(10);
digitalWrite(trigPin, HIGH);
delayMicroseconds(50);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distanceCm = duration * 0.034 / 2;
//******************************************************//
//IR Sensor Counting Object
if (digitalRead(in) == HIGH && digitalRead(out) == LOW) {
IN();
}
if (digitalRead(out) == HIGH && digitalRead(in) == LOW) {
OUT();
}
if (count <= 0)
{
LCD.clear();
digitalWrite(relay, LOW);
LCD.clear();
LCD.print("No Item in box");
LCD.setCursor(0, 1);
LCD.print("box is empty");
delay(200);
}
else {
digitalWrite(relay, HIGH);
}
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 8095;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
if (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
Serial.println("Waiting for connection");
delay(0);
}
}
String IRCount, myDistance;
IRCount = String(count);
myDistance = String(distanceCm);
// We now create a URI for the request
String url = "/Registration/write_data.php";
url += "?item=";
url += IRCount;
url += "&distance=";
url += myDistance;
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");
int timeout = millis() + 5000;
while (client.available() == 0) {
if (timeout - millis() < 0) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
// Read all the lines of the reply from server and print them to Serial
while (client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
}
Anyone has the solutions please help me