A function-definition is not allowed here before '{' token

Hello there, I'm new to Arduino IDE. I tried to coding to ESP8266 and this error always prompted. Pls help, I have no idea how to fix this.

Here's the code.

#include <ESP8266WiFi.h>
#include <ThingSpeak.h>
#include <SoftwareSerial.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

String apiKey = "FQUT56BIHJV0A4QU";

const char* ssid = "เปโรโระ";
const char* password = "12345678";

const char* server = "api.thingspeak.com/update?api_key=FQUT56BIHJV0A4QU&field1=0";

struct MyVariable
{
  byte nitrogen;
  byte phosphorus;
  byte potassium;
};

MyVariable variable;

WiFiClient client;

#define SCREEN_WIDTH 128    // OLED display width, in pixels
#define SCREEN_HEIGHT 64    // OLED display height, in pixels
#define OLED_RESET -1       // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
 
#define RE 2
#define DE 0
 
//const byte code[]= {0x01, 0x03, 0x00, 0x1e, 0x00, 0x03, 0x65, 0xCD};
const byte nitrogen[] = {0x01,0x03, 0x00, 0x1e, 0x00, 0x01, 0xe4, 0x0c};
const byte phosphorus[] = {0x01,0x03, 0x00, 0x1f, 0x00, 0x01, 0xb5, 0xcc};
const byte potassium[] = {0x01,0x03, 0x00, 0x20, 0x00, 0x01, 0x85, 0xc0};

byte values[11];
SoftwareSerial mod(14,12);

void setup() {
  Serial.begin(9600);
  mod.begin(4800);
  pinMode(RE, OUTPUT);
  pinMode(DE, OUTPUT);
  
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi Connected");

  Serial.print("Wi-Fi connected."); 
  Serial.print("IP Address : ");
  Serial.println(WiFi.localIP());  // ทำการ Print IP ที่ได้รับมา
  
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //initialize with the I2C addr 0x3C (128x64)
  delay(500);
  display.clearDisplay();
  display.setCursor(30, 5);
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.println(" NPK Sensor");
  display.setCursor(30, 20);
  display.setTextSize(1);
  display.print("Initializing");
  display.setCursor(35,40);
  display.setTextSize(1);
  display.print("IP Address \n");
  display.setCursor(20,50);
  display.setTextSize(1);
  display.println(WiFi.localIP());
  display.display();
  delay(3000);
}
void loop () {
  byte nitrogen();
  delay(250);
  byte phosphorus();
  delay(250);
  byte potassium();
  delay(250);
  
  Serial.println(WiFi.localIP());
  Serial.printf("Connection Status: %d\n", WiFi.status());// แสดงสถานะการเชื่อมต่อ
  delay(2000);
  
  
  Serial.print("Nitrogen: ");
  Serial.print(variable.nitrogen);
  Serial.println(" mg/kg");
  Serial.print("Phosphorous: ");
  Serial.print(variable.phosphorus);
  Serial.println(" mg/kg");
  Serial.print("Potassium: ");
  Serial.print(variable.potassium);
  Serial.println(" mg/kg");
  delay(2000);
 
  display.clearDisplay();

  display.setTextSize(1);
  display.setCursor(0, 5);
  display.print("IP Address : \n");
  display.setTextSize(1);
  display.setCursor(0, 15);
  display.println(WiFi.localIP());
  
  display.setTextSize(1);
  display.setCursor(0, 25);
  display.print("N: ");
  display.print(variable.nitrogen);
  display.setTextSize(1);
  display.print(" mg/kg");
 
  display.setTextSize(1);
  display.setCursor(0, 35);
  display.print("P: ");
  display.print(variable.phosphorus);
  display.setTextSize(1);
  display.print(" mg/kg");
 
  display.setTextSize(1);
  display.setCursor(0, 45);
  display.print("K: ");
  display.print(variable.potassium);
  display.setTextSize(1);
  display.print(" mg/kg");
 
  display.display();

  byte nitrogen(){
  digitalWrite(DE,HIGH);
  digitalWrite(RE,HIGH);
  delay(10);
  if(mod.write(nitro,sizeof(nitro))==8){
    digitalWrite(DE,LOW);
    digitalWrite(RE,LOW);
    for(byte i=0;i<7;i++){
    //Serial.print(mod.read(),HEX);
    values[i] = mod.read();
    //Serial.print(values[i],HEX);
    }
    //Serial.println();
  }
  return values[4];
  }
 
  byte phosphorous(){
  digitalWrite(DE,HIGH);
  digitalWrite(RE,HIGH);
  delay(10);
  if(mod.write(phos,sizeof(phos))==8){
    digitalWrite(DE,LOW);
    digitalWrite(RE,LOW);
    for(byte i=0;i<7;i++){
    //Serial.print(mod.read(),HEX);
    values[i] = mod.read();
    //Serial.print(values[i],HEX);
    }
    Serial.println();
  }
  return values[4];
  }
 
  byte potassium(){
  digitalWrite(DE,HIGH);
  digitalWrite(RE,HIGH);
  delay(10);
  if(mod.write(pota,sizeof(pota))==8){
    digitalWrite(DE,LOW);
    digitalWrite(RE,LOW);
    for(byte i=0;i<7;i++){
    //Serial.print(mod.read(),HEX);
    values[i] = mod.read();
    //Serial.print(values[i],HEX);
    }
    Serial.println();
  }
  return values[4];
  }

if (client.connect(server, 80))
{
  
  if (client.connect(server, 80)) {
    String postStr = apiKey;
    postStr += "&field1=";
    postStr += String(variable.nitrogen);
    postStr += "&field2=";
    postStr += String(variable.phosphorus);
    postStr += "&field3=";
    postStr += String(variable.potassium);
    postStr += "r\n\r\n\r\n";

    client.print ("POST/update HTTP/1.1\n");
    client.print ("Host: api.thingspeak.com/update?api_key=FQUT56BIHJV0A4QU&field1=0\n");
    client.print ("Connection: close\n");
    client.print ("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
    client.print ("Content-Type: application/x-www-form-urlencoded\n");
    client.print ("Content-Length: ");
    client.print (postStr.length());
    client.print ("\n\n");
    client.print (postStr);
    delay(1000);
    Serial.println ("Data Sent to Server");

  }

  client.stop();
}
}

Errors :
C:\Users\p6052\OneDrive\Documents\Arduino\DataCom_Test\DataCom_Test.ino: In function 'void loop()':
C:\Users\p6052\OneDrive\Documents\Arduino\DataCom_Test\DataCom_Test.ino:135:18: error: a function-definition is not allowed here before '{' token
135 | byte nitrogen(){
| ^
C:\Users\p6052\OneDrive\Documents\Arduino\DataCom_Test\DataCom_Test.ino:152:21: error: a function-definition is not allowed here before '{' token
152 | byte phosphorous(){
| ^
C:\Users\p6052\OneDrive\Documents\Arduino\DataCom_Test\DataCom_Test.ino:169:19: error: a function-definition is not allowed here before '{' token
169 | byte potassium(){
| ^
Multiple libraries were found for "Adafruit_SSD1306.h"
Used: C:\Users\p6052\OneDrive\Documents\Arduino\libraries\Adafruit_SSD1306
Not used: C:\Users\p6052\OneDrive\Documents\Arduino\libraries\Adafruit_SSD1306_Wemos_Mini_OLED
exit status 1

Compilation error: a function-definition is not allowed here before '{' token

You are missing the } closing the loop() function

If you indent the code such issues become visible

Does it supposed to be function call? Compiler thinks its a function prototype.

  byte potassium(){
  digitalWrite(DE,HIGH);
  digitalWrite(RE,HIGH);
  delay(10);
  if(mod.write(pota,sizeof(pota))==8){
    digitalWrite(DE,LOW);
    digitalWrite(RE,LOW);
    for(byte i=0;i<7;i++){
    //Serial.print(mod.read(),HEX);
    values[i] = mod.read();
    //Serial.print(values[i],HEX);
    }
    Serial.println();
  }
  return values[4];
  }

if (client.connect(server, 80))  <----------------?????????????????????????
{

What is this? Function defined in the middle of another function?

Is it "function definition" or "function prototype"?

1 Like

Prototype, I just corrected it.

1 Like

Still doesn't work😥

why don't you post your updated source code and the compiler's message (using code tags please)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.