So i tried multiple times tries to upload but it keep giving the same error, the nodemcu esp8266 cant even connect. Anyway this is my code, sorry for the bad english.
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <ArduinoJson.h>
const char* s = "Riardu";
const char* p = "arduino6";
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 = 2;
const int pinsensor = 4;
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();
}
}
