0

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");
}

enter image description here

Anyone has the solutions please help me

  • is the LCD SPI? you use 4 pins D5/SCK, D6/MISO, D7/MOSI, D8/SS like it would be 10 different pins. how did you wire things? – Juraj Dec 26 '18 at 13:05
  • LCD wire code is LCD(MISO, MOSI,D5, D4, D3, D2) which consists of 6pins for the LCD and then the IN and OUT pins are IR SENSOR PINS AND TRIG AND ECHO for the ultrasonic sensor pins but all of these sensors can’t work. – Annoymous20 Dec 26 '18 at 13:23
  • MISO is D6, MOSI id D7. why do you use SPI pin defines if it is not SPI? – Juraj Dec 26 '18 at 13:26
  • MISO is the Wemos board pins... if I use the arduino board pins it wouldn’t work – Annoymous20 Dec 26 '18 at 13:48
  • Wemos has D0 to D8. MISO and MOSI are aliases to D6 and D7 – Juraj Dec 26 '18 at 14:10

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.