i'm not so newbie and not advanced... my problem is that i can not send a string of length 700 to my phone.
Of course, upto length about 200, it is OK.
Descrition:
it is a local wifi that is connected by rs485 to a line. The line calls it by name 'LO1' or 'GE1'. when called by the former, it passes request from app to line and when the latter, it saves the so-called "massive data".
Then maybe app calls it through wifi http and requests a report or an ON/OFF on a switch. For the former, it should pass the saved massive data (not able now!?) and for the latter, it waits until it is called by name 'LO1' and send the app request. That's all.
here is my code:
#include <ESP8266WiFi.h>
String request_onWiFi;
String request_fromApp;
const char *ssid = "Greenhouse";
const char *password = "784951623";
WiFiServer server(80);
const char *myRRID = "GE1"; //for receive
const char *myTTID = "LO1"; //for response
String myTID = "LO1"; //for transmit
String myRID = "GE1"; //for receive
String falseID = "GE1HM1"; //for receive
String r1ID;
String r2ID;
String safetyID;
String responseData;
String appData;
String rData;
int led;
int n;
byte mac[6];
unsigned long firstmillis;
unsigned long secondmillis;
int ledPin = D3;
void setup()
{
IPAddress ip(192, 168, 4, 1);
// IPAddress gateway(192, 168, 0, 1);
// IPAddress subnet(255, 255, 255, 0);
WiFi.mode(WIFI_AP);
// u can remove the password if you want the AP to be open.
WiFi.softAP(ssid, password);
server.begin();
Serial.begin(115200);
// WiFi.config(ip, gateway, subnet);
// IPAddress myIP = WiFi.softAPIP();
// WiFi.macAddress(mac);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
}
int value = 0;
void App_request_onWiFi()
{
WiFiClient client = server.available();
if (!client)
{
return;
}
//Read what the browser has sent into a String class
//and print the request to the monitor
//if u want just first line, use readStringUntil('\r')
//instead of readString()
request_onWiFi = client.readString();
int untill1 = request_onWiFi.indexOf("s/");
int untill2 = request_onWiFi.indexOf("/e");
//Serial.println(sign);
request_fromApp += request_onWiFi.substring(untill1 + 2, untill2);
request_fromApp += (",");
// Serial.println(request_fromApp);
// Handle the Request
String content;
if (request_fromApp.indexOf("RP") != -1)
{
content = responseData;
// Serial.print("content :");
// Serial.println(content);
}
else
{
content = "OK";
}
// Respond to it
client.flush(); //clear previous info in the stream
String response = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"; //<!DOCTYPE HTML>\r\n<html>\r\n";
response += content;
// response += "</html>\n";
client.print(response); // Send the response to the client
delay(1);
}
void feedback()
{
digitalWrite(LED_BUILTIN, LOW);
delay(100);
digitalWrite(LED_BUILTIN, HIGH);
}
void loop()
{
if (Serial.available()) {
firstmillis = millis();
rData = "";
char chars;
while (Serial.available())
{
chars = Serial.read();
delayMicroseconds(68);
rData = rData + chars;
}
// rData=Serial.readString();
// Serial.println(rData);
int until1 = rData.indexOf("LO1");
r1ID = rData.substring(until1, until1 + 3);
int until2 = rData.indexOf("GE1");
r2ID = rData.substring(until2, until2 + 3);
int until3 = rData.indexOf("GE1HM1");
safetyID = rData.substring(until3, until3 + 6);
// Serial.println(r1ID);
// Serial.print("r2ID : ");
// Serial.println(r2ID);
if (until1 <= 4 ) {
if (r1ID == myTID) {
secondmillis = millis();
if ((secondmillis - firstmillis) < 90) {
String news;
news = "UUUU";
news += myTID;
news += ",";
news += request_fromApp;
Serial.print(news);
request_fromApp = "";
news = "";
}
}
}
if (safetyID != falseID) {
if (until2 <= 4 ) {
if (r2ID == myRID) {
responseData = rData;
int untilsemi = responseData.indexOf(";");
int untiltag = responseData.indexOf("#");
responseData = responseData.substring(untilsemi + 1, untiltag);
// Serial.print("responseData :");
// Serial.println(responseData);
}
}
}
}
App_request_onWiFi();
Then I changed my 'String-based' code to a char array version, like this:
everything is OK. but it does not send the char array to phone!
char myID[4] = {"LO1"};
//------------------------------------------------------------------------
#include <ESP8266WiFi.h>
String request_onWiFi;
//char data_on_WiFi[170]; //any data on wifi
String request_fromApp;
//char request_fromApp[100]; //data from greenhouse app
char ssid[25] = {"Greenhouse"};
char password[25] = {"784951623"};
WiFiServer server(80);
char globalID[4] = {"GE1"};
char falseID[7] = {"GE1HM1"};
char greenhouse_data[710];
//byte mac[6];
unsigned int j = 0;
unsigned int k = 0;
String content;
char response2app[730] = {"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"};
//-------------------------------------------------------------------------
void setup()
{
IPAddress ip(192, 168, 4, 1);
// IPAddress gateway(192, 168, 0, 1);
// IPAddress subnet(255, 255, 255, 0);
WiFi.mode(WIFI_AP);
// u can remove the password if you want the AP to be open:
WiFi.softAP(ssid, password);
server.begin();
Serial.begin(115200);
// WiFi.config(ip, gateway, subnet);
// IPAddress myIP = WiFi.softAPIP();
// WiFi.macAddress(mac);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
}
//-------------------------------------------------------------------
void loop()
{
look_at_line();
listen2WiFi();
}
//-------------------------------------------------------------------
void look_at_line() {
unsigned int i = 0;
while (Serial.available() > 0) {
greenhouse_data[i] = Serial.read();
Serial.print(greenhouse_data[i]);
if (i >= 3) {
if (greenhouse_data[i] != falseID[3] & greenhouse_data[i - 1] == globalID[2] && greenhouse_data[i - 2] == globalID[1] &&
greenhouse_data[i - 3] == globalID[0]) { //if it is GE1, not GE1HM1
j = 0;
k = 0;
while (Serial.available() > 0) {
greenhouse_data[i] = Serial.read();
Serial.print(greenhouse_data[i]);
if (greenhouse_data[i] == ';' && j == 0) j = i + 1; //start of useful data
else if (greenhouse_data[i] == '#' && k == 0) k = i; //end of useful data
i++;
}
greenhouse_data[i] = '\0';
}
else if (greenhouse_data[i] == myID[2] && greenhouse_data[i - 1] == myID[1] &&
greenhouse_data[i - 2] == myID[0]) { //if it is LO1
Serial.print('U');
Serial.print('U');
Serial.print('U');
Serial.print('U');
Serial.print(myID[0]);
Serial.print(myID[1]);
Serial.print(myID[2]);
Serial.print(',');
Serial.print(request_fromApp);
/* for (i = 0; ; i++) {
Serial.print(request_fromApp[i]);
}*/
}
}
i++;
}
}
//-------------------------------------------------------------------
void listen2WiFi()
{
WiFiClient client = server.available();
if (!client)
{
return;
}
unsigned int i = 0;
byte report_needed = 0;
byte is_app = 0;
while (client.available() > 0) {
data_on_WiFi[i] = client.read();
delay(10);
if (i > 0) {
if (data_on_WiFi[i] == '/' && data_on_WiFi[i - 1] == 's') {
is_app = 1; //it is not a stranger
unsigned int until1 = ++i; //start of data from greenhouse app
unsigned int until2 = 0;
while (client.available() > 0) {
data_on_WiFi[i] = client.read();
delay(10);
if (data_on_WiFi[i] == 'P' && data_on_WiFi[i - 1] == 'R') {
report_needed = 1;
}
if (data_on_WiFi[i] == 'e' && data_on_WiFi[i - 1] == '/') {
until2 = i - 2; //end of data from greenhouse app
}
i++;
}
data_on_WiFi[i] = '\0';
for (i = until1; i <= until2; i++)
request_fromApp[i - until1] = data_on_WiFi[i];
data_on_WiFi[0] = '\0';
request_fromApp[i + 1] = ',';
}
}
i++;
}
Serial.print("request_from_app: ");
for (i = 0; i < 100; i++) Serial.print(request_fromApp[i]);
Serial.println();
// Handle the Request if it is from greenhouse app
if (is_app == 1) {
char content[100];
i = 0;
if (report_needed == 1) {
for (i = 0; i <= k; i++)
response2app[44 + i] = greenhouse_data[j++];
greenhouse_data[0] = '\0';
}
else
{
response2app[44 + i] = 'O';
response2app[45 + i] = 'K';
}
// Respond to it:
client.flush(); //clear previous info in the stream
for (i = 0; response2app[i] != '\0'; i++)
{
client.print(response2app[i]); // Send the response to the app
}
delay(1);
}
}