The use of the ESP8266 in the world of IoT

User avatar
By matheus.camp
#75997 Hello,

My graduation project is a Smart Vending Machine, I chose ESP8266 NodeMCU because it's a cheap board and it has WiFi connection integrated, so it would fit my needs.

Basically, my project has a MySQL Database connected to a Java Application, it will be in a local Network, so I thought of using Java Socket in order to open a socket and communicate then with my ESP8266 board via WiFi.
ESP8266 currently has an OLED Display and a RFID Card connected, the current code is running but I've been facing some issues after I implemented the Java-SQL validation.
Steps: (at least what it should be doing)
1 - Run Java Socket Server listening in a port.
2 - User approaches a RFID card, code is read by the program and stored in a variable RFID which is already converted to HEX, ready to send to the Socket Server.
3 - ESP8266 connects to the Server, send this message and wait for response (I've been facing problems with it because I just don't know what I can do to make the code literally stop and wait for a response from the server).
4 - Java Application reads the message received from the ESP8266 and validate the RFID code in the MySQL database, returns the response to the ESP8266.
5 - After response is received by ESP8266, an if, else will control the next steps according to the response received, there will be only 2 responses possible, Valid or Invalid.

It just ignores the message sent by the Socket Server, finishes loop and start running again.

My code below so you can try to look and see what could be possibly wrong:

Code: Select all#define SS_PIN 4  //D2
#define RST_PIN 5 //D1
#define NAME "ssid"
#define PASS "password"


#include <SPI.h>
#include <MFRC522.h>
#include <ESP8266WiFi.h>
#include <Wire.h>
#include "SSD1306Wire.h"


//NodeMCU Pins for OLED Display
// SDA => D3
// SCK => D4

SSD1306Wire  display(0x3c, D3, D4);

const char* host = "192.168.0.101";



MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
int statuss = 0;
int out = 0;
void setup()
{
  Serial.begin(115200);
  SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
  display.init(); //Initiate Display
  display.flipScreenVertically();
  Serial.println();
  WiFi.persistent(false);
  WiFi.mode(WIFI_STA);
  WiFi.begin(NAME, PASS);
  Serial.print("Connecting");
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.print("Connected, IP address: ");
  Serial.println(WiFi.localIP());
  delay(200);

}
void loop()
{
  delay(200);
  display.clear();
  display.setTextAlignment(TEXT_ALIGN_CENTER);
  display.setFont(ArialMT_Plain_16);
  display.drawString(63, 10, "Approach your card");
  display.display();
 
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent())
  {
    return;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial())
  {
    return;
  }
          unsigned int hex_num;
          hex_num =  mfrc522.uid.uidByte[0] << 24;
          hex_num += mfrc522.uid.uidByte[1] << 16;
          hex_num += mfrc522.uid.uidByte[2] <<  8;
          hex_num += mfrc522.uid.uidByte[3];

         unsigned int RFID = hex_num;

 

  Serial.print("connecting to ");
  Serial.println(host);
 
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 12345;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  } 


 
  // This will send the request to the server
  client.print(RFID, HEX);   
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      return;
    }
  } 
 
   //Read all the lines of the reply from server and print them to Serial
   
   Serial.println("Reading");
   while (client.available() > 0){
    String line = client.readString(); //Response from the Java Socket Server.
    Serial.println(line);   
    Serial.println("");   
    Serial.println("");   
  }       
 
  Serial.println("closing connection");
 
  //Show UID on serial monitor
  Serial.println();
  Serial.print(" UID tag :");
  String content= "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++)
  {
     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
     Serial.print(mfrc522.uid.uidByte[i], HEX);
     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
     content.concat(String(mfrc522.uid.uidByte[i], HEX));
     
  }
  content.toUpperCase();
  Serial.println();
  if (line == "Valid") //line = Response from the Java Socket Server.
  {
    display.setFont(ArialMT_Plain_16);
    display.clear();
    display.drawString(63, 10, "Welcome");   
    display.display();
    display.clear();
    Serial.println(" Bem vindo Matheus ");   
    delay(3000);   
    display.drawString(63, 10, "Choose");
    display.drawString(63, 30, "Your product");
    display.display();
    delay(3000);
    Serial.println("Choose your product");
    Serial.println();
    statuss = 1;
  }
 
  else   { //if the response is invalid
    Serial.println(" Access Denied ");
    display.setFont(ArialMT_Plain_16);
    display.clear();
    display.drawString(63, 30, "Acess Denied");
    display.display();
    delay(3000);
  }
}
User avatar
By mikeperreman
#76108 Im attempting a similar task, I have an esp8266 dev board, and this problem has stumped me for at least a few weekends. Here's what I did to fix it:
Code: Select all#include "ESP8266WiFi.h"
...
WiFiClient client;
....
int ret = client.connect(SERVER_IP, SERVER_PORT);

Now, lets look at this. According to the arduino documentation for EthernetCient.connect (the esp8266 library is designed to be identical in use) ret should be 1 of 5 values:
    1 =Success
    -1 =Timeout
    -2 =Sever Invalid
    -3 =Truncated
    -4 =Invalid Response

Well, this is fine and all, but I got a return of 0. because of course it cant be easy.

After lots of digging I found a post (and for the life of me, I cant find it again), but in summary, the dude fixed it by doing WiFi.mode(WIFI_STA), this turns the esp8266 into a station, and making sure that SERVER_PORT (eg 8080) was allowed through the firewall.

I went ahead and applied these changes, and low and behold, the damned thing works! Go ahead and give these a shot and let me know how it goes. Good luck!