[Error]a function-definition is not allowed here before ‘{’ token

Here is my code

void setup() {
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <ArduinoJson.h>
const char* s = "Yulius sulistyo";
const char* p = "12345678";
String sn = "http://random.skom.id/iot/api/iot?id_perangkat=1";
String so = "http://random.skom.id/iot/api/iot/notif?id_perangkat=1";
double lt = 0;
double td = 3000;
const int pin =4;
const int pinsensor=8;
int vibrationSensorState = 0;
void setup() {
  pinMode(pin,OUTPUT);
  pinMode(pinsensor,INPUT);
  digitalWrite(pin,LOW);
  pinMode(LED_BUILTIN, OUTPUT); 
  Serial.begin(115200); 
  WiFi.begin(s, p);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) {
    digitalWrite(LED_BUILTIN, HIGH);
    delay(500);
    digitalWrite(LED_BUILTIN, LOW);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
  Serial.println("Timer set to 5 seconds , it will take 5 seconds before publishing the first reading.");
}
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <ArduinoJson.h>
//const char* s = "Symbian Os";
//const char* p = "78f5522118fb";
const char* s = "Yulius sulistyo";
const char* p = "12345678";
String sn = "http://random.skom.id/iot/api/iot?id_perangkat=1";
String so = "http://random.skom.id/iot/api/iot/notif?id_perangkat=1";
double lt = 0;
double td = 3000;
const int pin =4;
const int pinsensor=8;
int vibrationSensorState = 0;
void setup() {
  pinMode(pin,OUTPUT);
  pinMode(pinsensor,INPUT);
  digitalWrite(pin,LOW);
  pinMode(LED_BUILTIN, OUTPUT); 
  Serial.begin(115200); 
  WiFi.begin(s, p);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) {
    digitalWrite(LED_BUILTIN, HIGH);
    delay(500);
    digitalWrite(LED_BUILTIN, LOW);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
  Serial.println("Timer set to 5 seconds , it will take 5 seconds before publishing the first reading.");
}
void loop() {
  vibrationSensorState = digitalRead(pinsensor);
//  Serial.println(digitalRead(pinsensor));
//  if(vibrationSensorState==HIGH){
//    Serial.println("Ada getaran cuy");
//  }else{
//    Serial.println("tidak ada getaran");
//  }
//  delay(1000);
  // Send an HTTP POST request depending on td
  if ((millis() - lt) > td) {
    //Check WiFi connection status
    if(WiFi.status()== WL_CONNECTED){
      digitalWrite(LED_BUILTIN, HIGH);
      WiFiClient client;
      HTTPClient http;
      String serverPath = sn;
      // Your Domain name with URL path or IP address with path
      http.begin(client, serverPath.c_str());
      // Send HTTP GET request
      int httpResponseCode = http.GET();
      if (httpResponseCode==200) {
        Serial.print("HTTP Response code: ");
        Serial.println(httpResponseCode);
        String payload = http.getString();
        int str_len = payload.length() + 1; 
        char json[str_len];
        payload.toCharArray(json, str_len);
        Serial.println(json);
        StaticJsonBuffer<200> jsonBuffer;
        JsonObject& root = jsonBuffer.parseObject(json);
        if(!root.success()) {
          Serial.println("parseObject() failed");
//          return false;
        }
        bool xstatus = root["status"];
        if(xstatus){
              int on0 = root["data"]["status"];
              if(on0 == 1){
                digitalWrite(pin,HIGH);
                Serial.println("true");
              }else{
                 digitalWrite(pin,LOW);
                 Serial.println("false");
              }
              int on1 = root["data"]["on1"];
              if(on1 ==1){
                 Serial.println("sensor getar dikirim");
                 if(vibrationSensorState==HIGH){
                    Serial.println("SIAP KIRIM ADA GETARAN");
                    HTTPClient http1;
                    String serverPath1 = so;
                    // Your Domain name with URL path or IP address with path
                    http1.begin(client, serverPath1.c_str());
                    // Send HTTP GET request
                    int httpResponseCode2 = http1.GET();
                    if (httpResponseCode==200) {
                       String response = http1.getString();
                       Serial.println(response);
                    }else{
                       Serial.println(httpResponseCode);
                    }
                }else{
                  Serial.println("TIDAK DIKIRIM TIDAK ADA GETARAN");
                }
              }else{
                Serial.println("sensor getar tidak di kirim");
              }
             
        }else{
            Serial.println("tidak di temukan perangkat");
        }
       
      }
      else {
        Serial.print("Error code: ");
        Serial.println(httpResponseCode);
        digitalWrite(LED_BUILTIN, HIGH);
//        digitalWrite(pin,LOW);
        delay(1000);
        digitalWrite(LED_BUILTIN, LOW);
      }
      // Free resources
      http.end();
    }
    else {
      Serial.println("WiFi Disconnected");
      digitalWrite(LED_BUILTIN, HIGH);
      delay(500);
      digitalWrite(LED_BUILTIN, LOW);
    }
    lt = millis(); 
   }
}

welcome to the forum.

Only one setup() is allowed in the sketch, but yours has three

Welcome to the forum. What are you trying to achieve. It looks like you have just copied loads of code and glued it together

As for the error - it occurs because all of your three setups are nested one inside the other. The C language does not allow nested functions.

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <ArduinoJson.h>
const char* s = "Yulius sulistyo";
const char* p = "12345678";
String sn = "http://random.skom.id/iot/api/iot?id_perangkat=1";
String so = "http://random.skom.id/iot/api/iot/notif?id_perangkat=1";
double lt = 0;
double td = 3000;
const int pin = 4;
const int pinsensor = 8;
int vibrationSensorState = 0;

void setup()
{
  pinMode(pin, OUTPUT);
  pinMode(pinsensor, INPUT);
  digitalWrite(pin, LOW);
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(115200);
  WiFi.begin(s, p);
  Serial.println("Connecting");
  while (WiFi.status() != WL_CONNECTED) {
    digitalWrite(LED_BUILTIN, HIGH);
    delay(500);
    digitalWrite(LED_BUILTIN, LOW);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
  Serial.println("Timer set to 5 seconds , it will take 5 seconds before publishing the first reading.");
}

is what I get after a clean up. most likely I missed something. The OP should be able to figure out the rest.

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