Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Java
#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(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
  rtc.begin();
  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }

  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); // set a timer of length 100000 microseconds (or 0.1 sec - or 10Hz => the led will blink 5 times, 5 cycles of on-and-off, per second)
  Timer1.attachInterrupt( timer ); // attach the service routine here

  lcd.clear();

}

void loop() {


  if (ack) {
    date_time();
    send_data();
    Serial.flush();
    delay(1);
    rf_read = read_data();
    pros();
    lcd_data();

    ack = 0;
  }


  // Serial.flush();



  while (esp8266.available()) // check i f the esp is sending a message
  {

    lcd.clear();
    lcd.print("Connecting to PC");
    if (esp8266.find("+IPD,"))
    {
      delay(1000);
      int connectionId = esp8266.read() - 48; // subtract 48 because the read() function returns
      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();
      // cipSend += "\r\n";

      esp8266.println(cipSend);
      read_back();
      esp8266.println(webpage);
      read_back();

      String closeCommand = "AT+CIPCLOSE=";
      closeCommand += connectionId; // append connection id
      //closeCommand += "\r\n";
      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;
  }




  /*

    lcd.setCursor(0, 0);
    lcd.print(esp_in[0]);
    lcd.print(" ");
    lcd.print(esp_in[1]);
    lcd.print(" ");
    lcd.print(esp_in[2]);
    lcd.print(" ");
    lcd.print(esp_in[3]);

    lcd.setCursor(0, 1);
    lcd.print(node_1[0]);
    lcd.print(" ");
    lcd.print(node_1[1]);
    lcd.print(" ");
    lcd.print(node_1[2]);
    lcd.print(" ");
    lcd.print(node_1[3]);
  */

  /*
    esp_in[0] = buff[0];
    esp_in[1] = in_data.substring(4 , 7).toInt();

    esp_in[2] = in_data.substring(13 , 18).toInt();
    esp_in[3] = in_data.substring(18 , 25).toInt();

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(esp_in[0]);
    lcd.print(" ");
    lcd.print(esp_in[1]);

    lcd.setCursor(0, 1);
    lcd.print(esp_in[2]);
    lcd.print(" ");
    lcd.print(esp_in[3]);
   */
}

//****************************************************************************

String read_data() {

  while (!Serial.available()) {
    //    lcd.setCursor(15, 1);
    //    lcd.print("w");
  }

  String temp;
  while (Serial.available()) {
    //    lcd.setCursor(15, 1);
    //    lcd.print("r");
    char c = Serial.read();
    if (c == '\n') {
      /*
      byte n = temp.indexOf("AC");
      if (n > 0) {
        node_1_cur = (temp.substring(n + 2, n + 5)).toInt();
      }

           n = temp.indexOf("BC");
            if (n > 0) {
              node_2_cur = (temp.substring(n + 2, n + 5)).toInt();
            }


            n = temp.indexOf("CC");
            if (n > 0) {
              node_3_cur = (temp.substring(n + 2, n + 5)).toInt();
            }

      */
      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());
  }

  /*
    if (String(now.second()).length() == 1) {
      temp += "0";
      temp += String(now.second());
    }
    else {
      temp += String(now.second());
    }
  */
  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>
Posted
Updated 12-Nov-15 15:49pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900