i made some code for esp8266 wifi module using AT command. this is working normally. but i cant put some varible in to this code. how i do it. i had 6 integer type varibles.
this is the code
#include <liquidcrystal.h>
#include <timerone.h>
#include <softwareserial.h>
SoftwareSerial esp8266(8, 9);
long timeout = 1500, now_time = 0;
#include <wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
bool read_write = 0;
byte node_1_vol = 0, node_1_cur = 0, node_1_pow = 0, node_2_vol = 0, node_2_cur = 0, node_2_pow = 0, node_3_vol = 0, node_3_cur = 0, node_3_pow = 0;
String node, tim, in_data, rf_read;
bool ack = 0;
int esp_in[4];
int node_1[4], node_2[4], node_3[4];
int count = 0;
String time_24;
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
String from_esp;
void setup() {
Serial.begin(115200);
esp8266.begin(9600);
#ifdef AVR
Wire.begin();
#else
Wire1.begin();
#endif
rtc.begin();
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
lcd.begin(16, 2);
lcd.noCursor();
delay(1000);
lcd.print("Init wifi...");
esp8266.println("AT");
read_back();
esp8266.println("AT+RST");
read_back();
esp8266.println("AT+CWMODE=2");
read_back();
esp8266.println("AT+CIFSR");
read_back();
esp8266.println("AT+CIPMUX=1");
read_back();
esp8266.println("AT+CIPSERVER=1,80");
read_back();
lcd.clear();
lcd.print("Wifi OK1!");
delay(1000);
Timer1.initialize(500000);
Timer1.attachInterrupt( timer );
lcd.clear();
}
void loop() {
if (ack) {
date_time();
send_data();
Serial.flush();
delay(1);
rf_read = read_data();
pros();
lcd_data();
ack = 0;
}
while (esp8266.available())
{
lcd.clear();
lcd.print("Connecting to PC");
if (esp8266.find("+IPD,"))
{
delay(1000);
int connectionId = esp8266.read() - 48;
String webpage =
"<!DOCTYPE html>"
""
""
"<fieldset>"
"<h1>Main Controler</h1>"
"<form method = GET>"
"Node and Val "
"<input type=\"text\" name=\"node\" value=\"0\">"
"<br>"
"<br>"
"Timming "
"<input type=\"text\" name=\"time\" value=\"0\">"
"<br>"
"<br>"
"<input type=submit value=submit></form>"
"<br>"
"</body></html>";
String cipSend = "AT+CIPSEND=";
cipSend += connectionId;
cipSend += ",";
cipSend += webpage.length();
esp8266.println(cipSend);
read_back();
esp8266.println(webpage);
read_back();
String closeCommand = "AT+CIPCLOSE=";
closeCommand += connectionId;
esp8266.println(closeCommand);
read_back();
}
}
}
void read_back() {
long now_times = millis();
while ( (now_times + timeout) > millis())
{
while (esp8266.available())
{
char c = esp8266.read();
from_esp += c;
}
}
String response = from_esp;
if (response.indexOf("node=") > 0) {
node = response.substring(response.indexOf("node=") + 5 , response.indexOf("node=") + 12);
}
if (response.indexOf("time=") > 0) {
tim = response.substring(response.indexOf("time=") + 5 , response.indexOf("time=") + 15);
}
Serial.println(from_esp);
from_esp = "";
}
void pros()
{
esp_in[0] = node.substring(0 , 1).toInt();
esp_in[1] = node.substring(4 , 9).toInt();
esp_in[2] = tim.substring(0 , 5).toInt();
esp_in[3] = tim.substring(5 , 10).toInt();
if ( esp_in[0] == 1) {
node_1[0] = (esp_in[1]);
node_1[1] = (esp_in[2]);
node_1[2] = (esp_in[3]);
}
else if ( esp_in[0] == 2) {
node_2[0] = (esp_in[1]);
node_2[1] = (esp_in[2]);
node_2[2] = (esp_in[3]);
}
else if ( esp_in[0] == 3) {
node_3[0] = (esp_in[1]);
node_3[1] = (esp_in[2]);
node_3[2] = (esp_in[3]);
}
if ( rf_read.indexOf("AC") > 0) {
byte n = rf_read.indexOf("AC");
node_1_cur = (rf_read.substring(n + 2, n + 5)).toInt();
return;
}
else if ( rf_read.indexOf("BC") > 0) {
byte n = rf_read.indexOf("BC");
node_2_cur = (rf_read.substring(n + 2, n + 5)).toInt();
return;
}
else if ( rf_read.indexOf("CC") > 0) {
byte n = rf_read.indexOf("CC");
node_3_cur = (rf_read.substring(n + 2, n + 5)).toInt();
return;
}
}
String read_data() {
while (!Serial.available()) {
}
String temp;
while (Serial.available()) {
char c = Serial.read();
if (c == '\n') {
return temp;
}
else {
temp += c;
}
}
}
void send_data() {
if (node_1[1] == 0 && node_1[2] == 0) {
Serial.print("NAB");
Serial.println(node_1[0]);
}
else if (node_1[1] == time_24.toInt()) {
Serial.print("NAB");
Serial.println(node_1[0]);
}
else if (node_1[2] == time_24.toInt()) {
Serial.print("NAB");
Serial.println("0");
}
if (node_2[1] == 0 && node_2[2] == 0) {
Serial.print("NBB");
Serial.println(node_2[0]);
}
else if (node_2[1] == time_24.toInt()) {
Serial.print("NBB");
Serial.println(node_2[0]);
}
else if (node_2[2] == time_24.toInt()) {
Serial.print("NBB");
Serial.println("0");
}
if (node_3[1] == 0 && node_3[2] == 0) {
Serial.print("NCB");
Serial.println(node_3[0]);
}
else if (node_3[1] == time_24.toInt()) {
Serial.print("NCB");
Serial.println(node_3[0]);
}
else if (node_3[2] == time_24.toInt()) {
Serial.print("NCB");
Serial.println("0");
}
}
void date_time() {
DateTime now = rtc.now();
if ((now.hour()) < 9) {
time_24 = "0";
time_24 += String(now.hour());
}
else {
time_24 = String(now.hour());
}
if (String(now.minute()).length() == 1) {
time_24 += "0";
time_24 += String(now.minute());
}
else {
time_24 += String(now.minute());
}
time_24 += "H";
}
void timer() {
ack = 1;
count++;
}
void lcd_data() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(node_1_cur);
lcd.print("mA ");
lcd.print(node_2_cur);
lcd.print("mA ");
lcd.print(node_3_cur);
lcd.print("mA ");
lcd.setCursor(0, 1);
lcd.print(node_1_pow);
lcd.print("W ");
lcd.print(node_2_pow);
lcd.print("W ");
lcd.print(node_2_pow);
lcd.print("W ");
lcd.print(time_24);
}</br></br></br></br></br></fieldset></wire.h></softwareserial.h></timerone.h></liquidcrystal.h>