I cannot send data from Arduino to a server using ESP8266 !!!!!

Hi everyone,

I am trying to create a credit card payment system using RFID tags and other devices, and then post the money left in someone’s account to a server on the internet using wifi and the ESP8266 module.

Below you can see my code, and unfortunately, it doesn’t work. It says that <ESP8266HTTPClient.h> is not a file in the directory and when I try to change the board to ESP8266 module, it says: Error compiling for board Generic ESP8266 Module.

What do I do??

All other resources I have found online are not helpful since they only use the Arduino as a means of connecting the ESP8266 module to the computer.

Thank you in advance :slight_smile:

#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <SPI.h>
#include <MFRC522.h>
#include <Keypad.h>

#include "SoftwareSerial.h"

String ssid ="xxx"; //wifi network

String password="xxx"; //wifi password

SoftwareSerial esp(6, 7);// RX, TX pins

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



const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
    {'1','2','3','A'},
    {'4','5','6','B'},
    {'7','8','9','C'},
    {'*','0','#','D'}
};
float nums[5] ={};
byte rowPins[ROWS] = {8, 7, 6, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {A3, A2, A0, A1}; //connect to the column pinouts of the keypad
int decimal =0;
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2); 
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
byte ledPin = 13; 
#define RST_PIN         9          // Configurable, see typical pin layout above
#define SS_PIN          10         // Configurable, see typical pin layout above
float price = 0;
char MoneyLeft = 10;
MFRC522 mfrc522(SS_PIN, RST_PIN); 
String value = "A9 A4 DE 8B";// Create MFRC522 instance
float cost = 0;
int c = 0;
boolean add =false;
int rs = 7;//lcd screen
int en=8;
int d4 = 3;
int d5= 4;
int d6= 5;
int d7 = 6;//lcd screen

int buzzPin = 2;
int buzztime = 500;
int t= 0;
float addition = 0;
const int buzzer =2;


String lastscan;


String serialnumbers[] = {"A9 A4 DE 8B", "0A FA 82 16"};
String names[] = {"Alex", "Marta"};
float account[] = {10, 20};
bool schoolLunch[] = {true, false};






void setup() {
    // Initialize serial communications with the PC
  while (!Serial);    // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  SPI.begin();   
 lcd.init();
  lcd.backlight();// Init SPI bus
  mfrc522.PCD_Init();   // Init MFRC522
  mfrc522.PCD_DumpVersionToSerial();  // Show details of PCD - MFRC522 Card Reader details
  Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
 pinMode(buzzer, OUTPUT);
 Serial.begin(115200);
 WiFi.begin("UPC5C45E89", "cbymCvvm52jw");
 while (WiFi.status() != WL_CONNECTED){
  delay(500);
    Serial.println("Waiting for connection");
 }
 
 
}

void loop() {
  // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
lcd.setCursor(0,0);


  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) 
  {
    return;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  {
    return;
  }
  //Show UID on serial monitor
  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));
  }
  Serial.println();

if (WiFi.status() == WL_CONNECTED){
  HTTPClient http;
  http.begin("https://xxx.000webhostapp.com/post-esp-data.php");
  http.addHeader("Content-Type", "text/plain");

  content.toUpperCase();
  for (c = 0; c <= 1; c++){
       if (content.substring(1) == serialnumbers[c]) //change here the UID of the card/cards that you want to give access
  {

           
        tone(buzzer, 1600);
        delay(100);// ...for 1 sec
        noTone(buzzer);
        lcd.clear();
        if (lastscan != content.substring(1)){
          lcd.print(names[c]);
          lcd.print("'s account:");
          lcd.setCursor(0,1);
          lcd.print(account[c]);
          delay(1500);
          lcd.clear();
          lcd.print("Scan again to");
          lcd.setCursor(0,1);
          lcd.print("buy something...");
          
          }
        else{  
          lcd.clear();
          delay(50);
     Serial.print("How much does this cost");
       lcd.print("Item Cost: ");
       lcd.setCursor(0,1);
int c=0;
    int key=0;
   float price = 0;
   int decimal = 0;
   int z=1;
   boolean add=false;
   for (z=1;z<=4;z++){
    nums[z] = 0;
    entered[z] = false;
   }
  for (int x=1; x<=4; x++){
    int key = keypad.getKey();
    
    while(key == NO_KEY) {
      key = keypad.getKey();
    }
    if (key >= 48 and key<=57){
      key = key-48;
      nums[x] = key;
      entered[x] = true;
    }
    else{
      if (key==42){
        decimal =x;
      }
      if (key==65){
        add=true;
        break;
      }
      if (key==35){
        break;
      }
     }
      
 if (x-decimal==1 and decimal > 0){
  nums[x] = nums[x]/10  ;
 }
 if (x-decimal==2 and decimal > 0){
  nums[x] = nums[x]/100  ;
 }
 if (decimal == 0 and x ==2){
  nums[1] = nums[1]*10;
 }
if (entered[x]){
  Serial.print(key);
  
  
  
}
  }
  
   
 int x=1;
   for (x=1;x<=4;x++){
    price += nums[x];
   }
  Serial.println();  
if (add==false){
  lcd.setCursor(0,1);
  lcd.print(price);
  Serial.println(price);
}
        if (add==true) {
          lcd.clear();
          lcd.print ("How much do");
          lcd.setCursor(0,1);
          lcd.print("you want to add?");
          
          for (int x=1; x<=4; x++){
    int key = keypad.getKey();
    while(key == NO_KEY) {
      key = keypad.getKey();
    }
    if (key >= 48 and key<=57){
      key = key-48;
      nums[x] = key;
      entered[x] = true;
    }
    else{
      if (key==42){
        decimal =x;
      }
      if (key==65){
        add=true;
        break;
      }
      if (key==35){
        break;
      }
     }
      
 if (x-decimal==1 and decimal > 0){
  nums[x] = nums[x]/10  ;
 }
 if (x-decimal==2 and decimal > 0){
  nums[x] = nums[x]/100  ;
 }
 if (decimal == 0 and x ==2){
  nums[1] = nums[1]*10;
 }
if (entered[x]){
  Serial.print(key);
  lcd.print(key);
  
}
else{
  if (decimal==x){
    lcd.print(".");
  }
}

  }
  if (decimal==3){
    nums[1] = nums[1]*10;
  }

 int x =1;
   for (x=1;x<=4;x++){
    addition += nums[x];
   }
          lcd.clear();
          lcd.print("Money added:");
          lcd.setCursor(0,1);
          lcd.print(addition);
          account[c] += addition;
          delay(1500);
          lcd.clear();
          lcd.print("Money in account:");
          lcd.setCursor(0,1);
          Serial.println(account[c]);
          lcd.print(account[c]);
          delay(1500);
          lcd.clear();
          lcd.setCursor(4,0);
    lcd.print("Success!");
   
  
          addition = 0;
        }
      else{
        
        delay(1500);
        lcd.clear();
      float money = account[c];
      if (money - price >= 0){
      float (money -= price);
      account[c] = (float)money;
    Serial.println();
    Serial.println(money);
    lcd.print("Money left: ");
    lcd.setCursor(0,1);
    lcd.print(money);
    delay(1500);
    lcd.clear();
    lcd.setCursor(4,0);
    lcd.print("Success!");
    int httpCode = http.POST(price);
    Serial.println(httpCode);
    http.end();
   
   }
    else{
      lcd.print("Not enough");
      lcd.setCursor(0,1);
      lcd.print("money in account");
    }
        }
        }
    
    
  }
  }
  thirdscan = lastscan;
  lastscan = content.substring(1);
  content.substring(1) ="";
}

}

It says that <ESP8266HTTPClient.h> is not a file in the directory and when I try to change the board to ESP8266 module, it says: Error compiling for board Generic ESP8266 Module.

What was you ‘board’ selection before ? and what is the complete error message posted for the generic 8266 ?
And what kind of board do you have, i think you are mixing 2 things up. The ESP8266HTTPClient.h is part of the ESP core and can only be compiled for an ESP board.
A generic ESP8266 doesn’t have SPI.

Thank you for your answer :slight_smile:

My board selection was 'Arduino Uno'.

This is the enormous error message I get when selecting the generic 8266 as my board:

In file included from sketch/testpost.ino.cpp:1:0:
/Users/alexoort/Library/Arduino15/packages/esp8266/hardware/esp8266/2.6.3/cores/esp8266/Arduino.h:43:14: error: expected identifier before numeric constant
 #define HIGH 0x1
              ^
/Users/alexoort/Documents/Arduino/libraries/Keypad-3.1.1/src/Keypad.h:56:16: note: in expansion of macro 'HIGH'
 #define CLOSED HIGH
                ^
/Users/alexoort/Library/Arduino15/packages/esp8266/hardware/esp8266/2.6.3/libraries/ESP8266WiFi/src/include/wl_definitions.h:73:3: note: in expansion of macro 'CLOSED'
   CLOSED      = 0,
   ^
/Users/alexoort/Library/Arduino15/packages/esp8266/hardware/esp8266/2.6.3/cores/esp8266/Arduino.h:43:14: error: expected '}' before numeric constant
 #define HIGH 0x1
              ^
/Users/alexoort/Documents/Arduino/libraries/Keypad-3.1.1/src/Keypad.h:56:16: note: in expansion of macro 'HIGH'
 #define CLOSED HIGH
                ^
/Users/alexoort/Library/Arduino15/packages/esp8266/hardware/esp8266/2.6.3/libraries/ESP8266WiFi/src/include/wl_definitions.h:73:3: note: in expansion of macro 'CLOSED'
   CLOSED      = 0,
   ^
/Users/alexoort/Library/Arduino15/packages/esp8266/hardware/esp8266/2.6.3/cores/esp8266/Arduino.h:43:14: error: expected unqualified-id before numeric constant
 #define HIGH 0x1
              ^
/Users/alexoort/Documents/Arduino/libraries/Keypad-3.1.1/src/Keypad.h:56:16: note: in expansion of macro 'HIGH'
 #define CLOSED HIGH
                ^
/Users/alexoort/Library/Arduino15/packages/esp8266/hardware/esp8266/2.6.3/libraries/ESP8266WiFi/src/include/wl_definitions.h:73:3: note: in expansion of macro 'CLOSED'
   CLOSED      = 0,
   ^
In file included from /Users/alexoort/Documents/Arduino/testpost/testpost.ino:17:0:
/Users/alexoort/Library/Arduino15/packages/esp8266/hardware/esp8266/2.6.3/libraries/ESP8266WiFi/src/ESP8266WiFi.h:29:1: error: expected declaration before '}' token
 }
 ^
Multiple libraries were found for "LiquidCrystal_I2C.h"
 Used: /Users/alexoort/Documents/Arduino/libraries/NewliquidCrystal
 Not used: /Users/alexoort/Documents/Arduino/libraries/LiquidCrystal_I2C-master
exit status 1
Error compiling for board Generic ESP8266 Module

I have an Arduino Uno and then the ESP8266:

What should I change?? I assume that I should select the Arduino Uno as the board, but I am not sure.
I am also quite unsure about what libraries to use???

"What should I change?? I assume that I should select the Arduino Uno as the board, but I am not sure.
I am also quite unsure about what libraries to use???"

To cut to the chase, if you want to use the ESP8266 internet server functions, the easiest thing to do is get an ESP development board, and use the arduino as a slave board if needed. Mating an ESP to an arduino is a somewhat involved process. You might use the board search function for details.

You can upload sketches to both boards and get them to do the parts that you want them to do and communicate if you want. The esp came with a firmware on it, but i don't recommend holding on to it. Create the webserver on the ESP, and let the Uno do what you want it to (and what the ESP can't.)

How do I make the Arduino and the esp8266 communicate???

Sorry for being such a noob and thank you for your help :slight_smile:

The obvious way would be to use Serial. Have a look at Serial Input basics