sro bro ,
Actually i am not much aware of using this forum, I will definitely go through it.
Here is the full code, Using which it is working fine.
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <EEPROM.h>
WiFiClient client;
//<
// Configuration parameters for nodemcu as Access Point
char * ssid_ap = "server";
char * password_ap = "password";
//Varible to read from client
bool overwriteeeprom = 0;
String wifiname; //of router to which it will connect
String password;
//IPAddress ip(192, 168, 0, 1);
//IPAddress gateway(192, 168, 1, 0);
//IPAddress subnet(255, 255, 255, 0);
// Set up the server object
ESP8266WebServer server(80); //port
//>
//Router credentials to store data
String read_ssid;
String read_pwd;
//host credentials
String final_host_ip;
const uint16_t port = 5999;
//if need of static ip > credentials for ESP
//if you want to know router congif then connect with dynamic ip and get print of its ip gateway , dns , and subnet
IPAddress staticIP(192, 168, 0, 111); //ESP static ip
IPAddress gateway(192, 168, 0, 1); //IP Address of your WiFi Router (Gateway)
IPAddress subnet(255, 255, 255, 0); //Subnet mask
IPAddress dns(192, 168, 0, 1); //DNS
//defining int function
void ICACHE_RAM_ATTR isr1();
void ICACHE_RAM_ATTR isr2();
//digital input
#define digi_in1_pin A0
#define digi_in2_pin A0
#define digi_in3_pin A0
#define digi_in4_pin A0
#define start_pin D0
#define connected_led_pin A0
#define sending_data_led_pin A0
bool digi_in1_state;
bool digi_in2_state;
bool digi_in3_state;
bool digi_in4_state;
String digi_in1_str;
String digi_in2_str;
String digi_in3_str;
String digi_in4_str;
//counter
#define counter_in1_pin A0
#define counter_in2_pin A0
int counter_in1_value;
int counter_in2_value;
String counter_in1_string;
String counter_in2_string;
unsigned long current_button_pressed_time = 0;
unsigned long last_button_pressed_time = 0;
uint8_t counter_debounce_time = 50;
//for timer
unsigned long StartTime;
unsigned long CurrentTime;
unsigned long ElapsedTime;
String timer_val ;
String send_string = "";
void setup() {
Serial.begin(115200);
EEPROM.begin(100); //EEPROM.begin(Size)
pinMode(start_pin, INPUT);
while (!digitalRead(start_pin)) {
Serial.println("in startmode");
}
//Reading data from eeprom and printing
String ip_addr_of_host_1 = String(EEPROM.read(1));
String ip_addr_of_host_2 = String(EEPROM.read(2));
String ip_addr_of_host_3 = String(EEPROM.read(3));
String ip_addr_of_host_4 = String(EEPROM.read(4));
Serial.println("PRINTING HOST ADDRESS : ");
Serial.println(ip_addr_of_host_1);
Serial.println(ip_addr_of_host_2);
Serial.println(ip_addr_of_host_3);
Serial.println(ip_addr_of_host_4);
final_host_ip = ip_addr_of_host_1 + "." + ip_addr_of_host_2 + "." + ip_addr_of_host_3 + "." + ip_addr_of_host_4;
Serial.println(final_host_ip);
EEPROM.get(10, read_ssid);
EEPROM.get(50, read_pwd);
Serial.print("\nRead SSID from EEPROM : ");
Serial.print(read_ssid);
Serial.print("\nRead PWD from EEPROM : ");
Serial.print(read_pwd);
call_setup();
}
void loop() {
// put your main code here, to run repeatedly:
check_if_connected_to_server();
handle_input();
handle_timer();
handle_string();
digitalWrite(sending_data_led_pin, HIGH);
delay(500);
digitalWrite(sending_data_led_pin, LOW);
delay(500);
}
void call_setup() {
//Status LED pinMode
pinMode(connected_led_pin, OUTPUT);
pinMode(sending_data_led_pin, OUTPUT);
//digi and counter pinMode
pinMode(digi_in1_pin, INPUT);
pinMode(digi_in2_pin, INPUT);
pinMode(digi_in3_pin, INPUT);
pinMode(digi_in4_pin, INPUT);
pinMode(counter_in1_pin, INPUT_PULLUP);
pinMode(counter_in2_pin, INPUT_PULLUP);
//Enabling Interrupt
attachInterrupt(counter_in1_pin, isr1, FALLING);
attachInterrupt(counter_in2_pin, isr2, FALLING);
// Serial.begin(115200);
//Connecting to router
WiFi.begin(read_ssid, read_pwd);
WiFi.config(staticIP, gateway, subnet, dns); // YOU WILL HAVE TO CONFIG THE STATIC IP SETTING
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print("\nConnecting to router...");
}
//PRINT CONNECTED WIFI CREDENTIALS
Serial.print("WiFi connected with IP: ");
Serial.println(WiFi.localIP());
Serial.print("Subnet Mask: ");
Serial.println(WiFi.subnetMask());
Serial.print("Gateway IP: ");
Serial.println(WiFi.gatewayIP());
Serial.print("DNS 1: ");
Serial.println(WiFi.dnsIP(0));
Serial.print("DNS 2: ");
Serial.println(WiFi.dnsIP(1));
Serial.println(final_host_ip);
Serial.println(read_ssid);
Serial.println(read_pwd);
String final_host_ip1 = "192.168.0.2";
//connecting to host [Loop until host is not connected]
while (!client.connected()) {
client.connect(final_host_ip1, port);
Serial.println("connecting to host..");
delay(100);
}
Serial.println("Connected to server successful!");
// client.print("Hello from 82");
digitalWrite(connected_led_pin, HIGH);
StartTime = millis();
}
void check_if_connected_to_server (){
if (!client.connected()) {
digitalWrite(connected_led_pin, LOW);
Serial.println("Disconnected from server");
Serial.println("Connecting Again");
while (!client.connected()) {
client.connect(final_host_ip, port);
Serial.println("connecting to host..");
delay(100);
}
Serial.println("Connected to server successful Again");
digitalWrite(connected_led_pin, HIGH);
}
}
void handle_input (){
//Read pin state
digi_in1_state = digitalRead(digi_in1_pin);
digi_in2_state = digitalRead(digi_in2_pin);
digi_in3_state = digitalRead(digi_in3_pin);
digi_in4_state = digitalRead(digi_in4_pin);
//checking if there is any change
//INPUT1
if (digi_in1_state) {
digi_in1_str = "SSSSSS";
}
else {
digi_in1_str = "RRRRRR";
}
//INPUT2
if (digi_in2_state) {
digi_in2_str = "SSSSSS";
}
else {
digi_in2_str = "RRRRRR";
}
//INPUT3
if (digi_in3_state) {
digi_in3_str = "SSSSSS";
}
else {
digi_in3_str = "RRRRRR";
}
//INPUT4
if (digi_in4_state) {
digi_in4_str = "SSSSSS";
}
else {
digi_in4_str = "RRRRRR";
}
//for counter and counter value
//counter1
counter_in1_string = String(counter_in1_value);
//maintaining length of counter_in1_string
int counter_in1_string_len = counter_in1_string.length();
if (counter_in1_string_len != 6) {
if (counter_in1_string_len == 1) {
counter_in1_string = "00000" + counter_in1_string;
}
else if (counter_in1_string_len == 2) {
counter_in1_string = "0000" + counter_in1_string;
}
else if (counter_in1_string_len == 3) {
counter_in1_string = "000" + counter_in1_string;
}
else if (counter_in1_string_len == 4) {
counter_in1_string = "00" + counter_in1_string;
}
else if (counter_in1_string_len == 5) {
counter_in1_string = "0" + counter_in1_string;
}
}
//counter2
counter_in2_string = String(counter_in2_value);
//maintaining length of counter_in2_string
int counter_in2_string_len = counter_in2_string.length();
if (counter_in2_string_len != 6) {
if (counter_in2_string_len == 1) {
counter_in2_string = "00000" + counter_in2_string;
}
else if (counter_in2_string_len == 2) {
counter_in2_string = "0000" + counter_in2_string;
}
else if (counter_in2_string_len == 3) {
counter_in2_string = "000" + counter_in2_string;
}
else if (counter_in2_string_len == 4) {
counter_in2_string = "00" + counter_in2_string;
}
else if (counter_in2_string_len == 5) {
counter_in2_string = "0" + counter_in2_string;
}
}
}
void handle_timer(){
//timer
CurrentTime = millis();
ElapsedTime = CurrentTime - StartTime;
ElapsedTime = ElapsedTime / 1000;
timer_val = String(ElapsedTime);
}
void handle_string() {
send_string = digi_in1_str + "," + digi_in2_str + "," + digi_in3_str + "," + digi_in4_str + "," + counter_in1_string + "," + counter_in2_string + "," + timer_val;
Serial.println(send_string);
client.print(send_string);
}
//counterinterrupt
void ICACHE_RAM_ATTR isr1() {
current_button_pressed_time = millis();
if (current_button_pressed_time - last_button_pressed_time > counter_debounce_time)
{
counter_in1_value++;
last_button_pressed_time = current_button_pressed_time;
}
}
//interrupt
void ICACHE_RAM_ATTR isr2() {
current_button_pressed_time = millis();
if (current_button_pressed_time - last_button_pressed_time > counter_debounce_time)
{
counter_in2_value++;
last_button_pressed_time = current_button_pressed_time;
}
}
Whenever you are free plz try to resolve this,
I will be waiting for your response here
Thankyou