just to note I`m using arduino uno + esp8266-01. Esp is getting voltage from separate voltage regulator 3.3v min 3.5v max measured,1.5A max output and filtered with 100uf and 0.1uf caps. Logic level converting did with 2 n-type mosftets with resistors. ESP tested with softserial works great. now code:
#define max_attempts 15
#define input_buffer 250
#define ipd 2
#define restart 1
#define at 2
#define uart 3
#define cwmode 4
#define cwdhcp 5
#define cipmux 6
#define cipservermaxconn 7
#define cipserver 8
#define cipsto 9
#define cwdhcps 10
#define cwsap 11
#define websend 12
#define sendok 13
#define homepagelength 235
#define ledPin 13
const char RST[] PROGMEM = "AT+RST";
const char AT[] PROGMEM = "AT";
const char UART[] PROGMEM = "AT+UART_DEF=115200,8,3,1,0";
const char CWMODE[] PROGMEM = "AT+CWMODE_DEF=3";
const char CWDHCP[] PROGMEM = "AT+CWDHCP_DEF=2,1";
const char CIPMUX[] PROGMEM = "AT+CIPMUX=1";
const char CIPSERVERMAXCONN[] PROGMEM = "AT+CIPSERVERMAXCONN=1";
const char CIPSERVER[] PROGMEM = "AT+CIPSERVER=1,80";
const char CIPSTO[] PROGMEM = "AT+CIPSTO=60";
const char CWDHCPS[] PROGMEM = "AT+CWDHCPS_DEF=1,5,\"192.168.4.10\",\"192.168.4.15\"";
const char CWSAP[] PROGMEM = "AT+CWSAP_DEF=\"mq2\",\"poseidon\",10,3";
const char CIPSEND[] PROGMEM="AT+CIPSEND=";
const char CIPCLOSE[] PROGMEM="AT+CIPCLOSE=";
byte process_text(char a[], byte _size);
byte receive_command(void);
byte id = 0;
byte send_command(byte command);
bool esp_init(void);
void input_buffer_flush(void);
bool client_checker(void);
void setup() {
pinMode(ledPin,OUTPUT);
digitalWrite(ledPin,LOW);
Serial.begin(115200); //if 9600 then delay(2)
while (!Serial);
delay(100);
while (!esp_init()) {digitalWrite(ledPin,!digitalRead(ledPin));};
// Serial.println("everything went ok");
}
void loop() {
if(client_checker()); //Serial.println("connection available");
}
bool esp_init(void){
bool success=1;
success&=send_command(restart);
success&=send_command(at);
success&=send_command(uart);
success&=send_command(cwmode);
success&=send_command(cwsap);
success&=send_command(cwdhcp);
success&=send_command(cwdhcps);
success&=send_command(cipmux);
success&=send_command(cipservermaxconn);
success&=send_command(cipserver);
success&=send_command(cipsto);
return success;
}
void input_buffer_flush(void){
char c=0;
while (Serial.available()){
if(Serial.available()>0) c=Serial.read();
delayMicroseconds(100);
digitalWrite(ledPin,!digitalRead(ledPin));}
return ;
}
byte process_text(char a[],byte _size){
byte answer=0;
if(a[0]=='S' && a[1]=='E' && a[2]=='N' && a[3]=='D' && a[5]=='O' && a[6]=='K') answer=13;
else if((a[0]=='+' && a[1]=='I' && a[2]=='P' && a[3]=='D')&&((a[5])-48)<5) {id=((a[5])-48);answer=2;}//needs to be checked ID is digit or ascii
else if((a[1]==',' && a[2]=='C' && a[3]=='O' && a[4]=='N' && a[5]=='N' && a[6]=='E' && a[7]=='C' && a[8]=='T')&&((a[0])-48)<5) {
id=((a[0])-48);
answer=3;}
else if(a[0]=='C' && a[1]=='L' && a[2]=='O' && a[3]=='S' && a[4]=='E' && a[5]=='D') {id=0;answer=4;}
else if(a[0]=='>') answer=12;
else if((a[0]=='O' && a[1]=='K') || (a[0]=='N' && a[1]=='O' && a[3]=='C' && a[4]=='H') || (a[0]=='r' && a[1]=='e' && a[2]=='a' && a[3]=='d' && a[4]=='y')) answer=1;
return answer;
}
byte receive_command(void) {
char c = 0;
char a[input_buffer] = {0};
byte b = 0;
while (!Serial.available());
for (byte i = 0; i < input_buffer && Serial.available(); i++) {
c = Serial.read();
digitalWrite(ledPin,!digitalRead(ledPin));
a[i] = c;
if (c == '\n') {
if ((b = process_text(a, i)) > 0) {
//input_buffer_flush();
break;
}
}
delayMicroseconds(100);
}
return b;
}
byte send_command(byte command) {
byte timout = 10;
bool text_response = 0;
char _command[100] = {0};
switch (command) {
case restart:
for (byte i = 0; i < strlen_P(RST); i++) {
_command[i] = pgm_read_byte_near(RST + i);
}
break;
case uart:
for (byte i = 0; i < strlen_P(UART); i++) {
_command[i] = pgm_read_byte_near(UART + i);
}
break;
case at:
for (byte i = 0; i < strlen_P(AT); i++) {
_command[i] = pgm_read_byte_near(AT + i);
}
break;
case cwmode:
for (byte i = 0; i < strlen_P(CWMODE); i++) {
_command[i] = pgm_read_byte_near(CWMODE + i);
}
break;
case cwsap:
for (byte i = 0; i < strlen_P(CWSAP); i++) {
_command[i] = pgm_read_byte_near(CWSAP + i);
}
break;
case cwdhcp:
for (byte i = 0; i < strlen_P(CWDHCP); i++) {
_command[i] = pgm_read_byte_near(CWDHCP + i);
}
break;
case cwdhcps:
for (byte i = 0; i < strlen_P(CWDHCPS); i++) {
_command[i] = pgm_read_byte_near(CWDHCPS + i);
}
break;
case cipmux:
for (byte i = 0; i < strlen_P(CIPMUX); i++) {
_command[i] = pgm_read_byte_near(CIPMUX + i);
}
break;
case cipservermaxconn:
for (byte i = 0; i < strlen_P(CIPSERVERMAXCONN); i++) {
_command[i] = pgm_read_byte_near(CIPSERVERMAXCONN + i);
}
break;
case cipserver:
for (byte i = 0; i < strlen_P(CIPSERVER); i++) {
_command[i] = pgm_read_byte_near(CIPSERVER + i);
}
break;
case cipsto:
for (byte i = 0; i < strlen_P(CIPSTO); i++) {
_command[i] = pgm_read_byte_near(CIPSTO+i);
}
break;
default:
break;
}
do {
if (timout > max_attempts) return text_response;
input_buffer_flush();
timout++;
Serial.println(_command); //println will add \r\n at the end of command
delayMicroseconds(100);
text_response = receive_command();
} while (!text_response);
return text_response;
}
void home_page(void){
Serial.print(F("HTTP/1.1 200 OK\n"));
Serial.print(F("Accept-Ranges: bytes\n"));
Serial.print(F("Content-Type: text/html\n"));
Serial.print(F("\n"));
Serial.print(F("<!DOCTYPE HTML>"));
Serial.print(F("<html>"));
Serial.print(F("<title> room 1 </title>"));
Serial.print(F("<head>"));
Serial.print(F("</head>"));
Serial.print(F("<body>"));
Serial.print(F("<p>"));
Serial.print(F("Temperature:"));
//Serial.println temperature variable
Serial.print(F("</p>"));
Serial.print(F("<p>"));
// Serial.println humidity variable
Serial.print(F("</p>"));
Serial.print(F("<p>"));
//Serial.println co level variable
Serial.print(F("</p>"));
Serial.print(F("</body>"));
Serial.print(F("</html>"));
Serial.print(F("Client disonnected"));
Serial.print(F("\n"));
return;
}
bool client_checker(void){
byte c=0;
char _command[100]={0};
if(Serial.available()>0){
c=(receive_command());
}
switch(c){
case ipd:
for (byte i = 0; i < strlen_P(CIPSEND); i++) {
_command[i] = pgm_read_byte_near(CIPSEND + i);
}
Serial.print(_command);
Serial.print(id);
Serial.print(',');
Serial.println(homepagelength);
break;
case websend:
home_page();
break;
case sendok:
for (byte i = 0; i < strlen_P(CIPCLOSE); i++) {
_command[i] = pgm_read_byte_near(CIPCLOSE + i);
}
Serial.print(_command);
Serial.println(id);
break;
default:
break;
}
return c;
}