0

I am having some problems with retrieving data from another esp. I have two esp8266, one is the access point which has a nextion lcd hooked up to it. The access point reads button presses from the lcd then sends messages to the other esp(the station point) in order to turn on the respcetive digital pins. The station point has two dht22 sensors hooked up to it. I am now trying to sens the temperature data from one of the sensor to the access point to print the data to the screen but I am having some problems(either sending or retrieving the sensor data, not sure how to test it). Here is the code for the access point:

//this is the acces point
#include <doxygen.h>
#include <NexButton.h>
#include <NexCheckbox.h>
#include <NexConfig.h>
#include <NexCrop.h>
#include <NexDualStateButton.h>
#include <NexGauge.h>
#include <NexGpio.h>
#include <NexHardware.h>
#include <NexHotspot.h>
#include <NexNumber.h>
#include <NexObject.h>
#include <NexPage.h>
#include <NexPicture.h>
#include <NexProgressBar.h>
#include <NexRadio.h>
#include <NexRtc.h>
#include <NexScrolltext.h>
#include <NexSlider.h>
#include <NexText.h>
#include <NexTimer.h>
#include <Nextion.h>
#include <NexTouch.h>
#include <NexUpload.h>
#include <NexVariable.h>
#include <NexWaveform.h>

#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
#include <Nextion.h>


//wifi stuff 
WiFiServer server(80);
IPAddress IP(192, 168, 4, 15);
IPAddress mask = (255, 255, 255, 0);

//booleans for check
bool Red1=false;
bool Green1=false;
bool Blue1=false;
bool Red2=false;
bool Green2=false;
bool Blue2=false;
bool Relay1=false;
bool Relay2=false;
bool AmbientTemp=false;
//ints for temp data
//String tempOutside=" ";
String tempInside=" ";
String humOutside=" ";
String humInside=" ";
//buttons
// page id,id,""name"
NexButton b0 = NexButton(1,2,"b0");//red1
NexButton b1 = NexButton(1,3,"b1");//green1
NexButton b2 = NexButton(1,4,"b2");//blue1
NexButton b7 = NexButton(3,2,"b7");//relay1
NexButton b8 = NexButton(3,3,"b8");//relay2
NexButton b9 = NexButton(2, 14, "b9");
NexText t8 = NexText(2,10,"t8");//tremperature inside
NexText t9 = NexText(2,11,"t9");//temperature outside
NexText t10 = NexText(2,12,"t10");//humidity inside
NexText t11 = NexText(2,13,"t11");//humidity outside

NexTouch *nex_listen_list[] = {
  &b0,
  &b1,
  &b2,
  &b7,
  &b8,
  &b9,
  NULL
};
//led strip 1
void b0PushCallback(void *ptr) {
  Red1=true;

  }
void b1PushCallback(void *ptr) {
  Green1=true;
  Serial.println("Green1");
  }
void b2PushCallback(void *ptr) {
  Blue1=true;
  }
//relays
void b7PushCallback(void *ptr) {
  Relay1=true;
  }
void b8PushCallback(void *ptr) {
  Relay2=true;
  }
void b9PushCallback(void *ptr) {
  AmbientTemp=true;
  }

void setup() {
  Serial.begin(9600);
  nexInit();
  b0.attachPush(b0PushCallback,&b0);
  b1.attachPush(b1PushCallback,&b1);
  b2.attachPush(b2PushCallback,&b2);
  b7.attachPush(b7PushCallback,&b7);
  b8.attachPush(b8PushCallback,&b8);
  b9.attachPush(b9PushCallback,&b9);
  //wifi part
  WiFi.mode(WIFI_AP);
  WiFi.softAP("Wemos_AP", "Wemos_comm");
  WiFi.softAPConfig(IP, IP, mask);
  server.begin();
  Serial.println();
  Serial.println("accesspoint_bare_01.ino");
  Serial.println("Server started.");
  Serial.print("IP: "); Serial.println(WiFi.softAPIP());
  Serial.print("MAC:"); Serial.println(WiFi.softAPmacAddress());
}

void loop() {
  nexLoop(nex_listen_list);
  //wifi stuff again
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  //String request = client.readStringUntil('\r');
  Serial.println("********************************");
  //Serial.println("From the station: " + request);
  client.flush();
  //strip 1
  if (Red1==true){
    client.print("redOne\r");
    Serial.println("Red1");
    Red1=false;
    }
  if (Green1==true){
    client.println("greenOne\r");
    Green1=false;
    }
  if (Blue1==true){
    client.println("blueOne\r");
    Blue1=false;
    }
  //relays
  if (Relay1==true){
    client.println("relayOne\r");
    Relay1=false;
    }
  if (Relay2==true){
    client.println("relayTwo\r");
    Serial.println("Relay2");
    Relay2=false;
    }
  if (AmbientTemp==true){
    client.println("AmbientTemp\r");
    Serial.println("ambient temp");
    AmbientTemp=false;
    }
  String tempOutside=client.readStringUntil('\r');
  Serial.println("tempOutside: ");
  Serial.println(tempOutside);
  //t8.setText(tempOutside);
  //tempInside=client.readStringUntil('\b');
  //t9.setText(tempOutside);
  //humInside=client.readStringUntil('\e');
  //t10.setText(humInside);
  //humOutside=client.readStringUntil('\f');
  //t11.setText (humOutside);//halp
  //Serial.println(humOutside);
  //Serial.println("humoutside: ");
  //Serial.print("t11.txt=");
  //Serial.print(humOutside.toInt());
  //Serial.write(0xff);
  //Serial.write(0xff);
  //Serial.write(0xff);
  Serial.println("Byte sent to the station: ");
  //Serial.println(client.println("reeeeeeeeeeeeeeeeeeeee\r"));
  //Serial.println(client.println("IamAlive\r"));
  }

And here is the code for the access point:

#include <DHT.h>
#include <DHT_U.h>
#include <ESP8266WiFi.h>

#define DHTPIN 3 //outside
#define DHTPIN2 0 //inside
#define DHTTYPE DHT22//type of sensor that I am using
DHT dht(DHTPIN, DHTTYPE); //outside
DHT dht2(DHTPIN2, DHTTYPE); //inside

//pins
const int Red = 5;
const int Green = 4;
const int Blue = 14;
const int Relay1= 12;
const int Relay2= 13;
//booleans for on/off(state)
bool RedS=false;
bool GreenS=false;
bool BlueS=false;
bool Relay1S=false;
bool Relay2S=false;
bool AmbientTempS=false;

char ssid[] = "Wemos_AP";           // SSID of your AP
char pass[] = "Wemos_comm";         // password of your AP
IPAddress server(192,168,4,15);     // IP address of the AP
WiFiClient client;
void setup() {
  Serial.begin(9600);
  dht.begin();
  dht2.begin();
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, pass);           // connects to the WiFi AP
  Serial.println();
  Serial.println("Connection to the AP");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println();
  Serial.println("Connected");
  Serial.println("station_bare_01.ino");
  Serial.print("LocalIP:"); Serial.println(WiFi.localIP());
  Serial.println("MAC:" + WiFi.macAddress());
  Serial.print("Gateway:"); Serial.println(WiFi.gatewayIP());
  Serial.print("AP MAC:"); Serial.println(WiFi.BSSIDstr());
  //pins
  pinMode(Red, OUTPUT);
  pinMode(Green, OUTPUT);
  pinMode(Blue, OUTPUT);
  pinMode(Relay1, OUTPUT);
  pinMode(Relay2, OUTPUT);
  digitalWrite(Red, LOW);
  digitalWrite(Green, LOW);
  digitalWrite(Blue, LOW);
  digitalWrite(Relay1, LOW);
  digitalWrite(Relay2, LOW);
}
void loop() {
  client.connect(server, 80);
  Serial.println("********************************");
  Serial.print("Byte sent to the AP: ");
  Serial.println(client.print("Anyo\r"));
  String answer = client.readStringUntil('\r');
  Serial.println("From the AP: "+answer);
  int h = dht.readHumidity(); //humidity for Outside
  int t = dht.readTemperature(); //temperaure for Outside
  int h2 = dht2.readHumidity(); //humidity for Outside
  int t2 = dht2.readTemperature(); //temperaure for Outside
  Serial.println("temp outside: ");
  Serial.println(t);
  Serial.println("Thing: ");
  //Serial.println("temp inside:");
  //Serial.println(t2);
  //client.print(String(t2)+'\b');
  //Serial.println("hum outside:");
  //Serial.println(h);
  //client.print(String(h)+'\e');
  //Serial.println("hum inside:");
  //Serial.println(h2);
  //Serial.println(client.print(String(h2)+"\f"));
  client.flush();
  //stuff
  client.println(String(t)+'\r');
  //stuff again
  if(answer=="redOne"){
    if (RedS==false){
      digitalWrite(Red, HIGH);
      RedS=true;
      Serial.println("Red1 ON");
      }
    else{
      digitalWrite(Red, LOW);
      RedS=false;
      Serial.println("Red1 OFF");
      }
    }
  if(answer=="greenOne") {
    if (GreenS==false){
      Serial.println("Green1 ON");
      GreenS=true;
      digitalWrite(Green, HIGH);
      }
    else{
      Serial.println("Green1 OFF");
      GreenS=false;
      digitalWrite(Green, LOW);
      }


    }
  if(answer=="blueOne") {
    if (BlueS==false){
      Serial.println("Blue1 ON");
      BlueS=true;
      digitalWrite(Blue, HIGH);
      }
    else{
      Serial.println("Blue1 OFF");
      BlueS=false;
      digitalWrite(Blue, LOW);
      }


    }
  if(answer=="relayOne") {
    if (Relay1S==false){
      Serial.println("Relay1 ON");
      Relay1S=true;
      digitalWrite(Relay1, HIGH);
      }
    else{
      Serial.println("Relay1 OFF");
      Relay1S=false;
      digitalWrite(Relay1, LOW);
      }


    }
  if(answer=="relayTwo") {
    if (Relay2S==false){
      Serial.println("Relay2 ON");
      Relay2S=true;
      digitalWrite(Relay2, HIGH);
      }
    else{
      Serial.println("Relay2 OFF");
      Relay2S=false;
      digitalWrite(Relay2, LOW);
      }
  if(answer=="AmbientTemp") {
    if (AmbientTempS==false){
      Serial.println("AmbientTemp ON");
      AmbientTempS=true;
      }
    else{
      Serial.println("AmbientTemp OFF");
      AmbientTempS=false;
      digitalWrite(Red, LOW);
      digitalWrite(Green, LOW);
      digitalWrite(Blue, LOW);
      }
   while(AmbientTempS==true){
    Serial.println("Ambient temp is on!");
    }

    }  
  client.stop();
  delay(2000);
  }
}

Thank you and sorry for any mistakes, English is not my first language.

2
  • start at the beginning ... reduce your code to sending a single byte or character... when that works reliably, then expand to multiple bytes/characters ... keep adding code in small increments Commented Apr 4, 2020 at 21:59
  • Will definitely try, had a similar problem and it worked in the end, I still posted the question to see if I made some obvious mistake I didn't catch. Commented Apr 4, 2020 at 22:24

1 Answer 1

1

Figured it out, not too sure what was causing it to not send messages but below is the code that is working.

#include <ESP8266WiFi.h>
#include <DHT.h>
#include <DHT_U.h>

#define DHTPIN 3 //outside
#define DHTPIN2 0 //inside
#define DHTTYPE DHT22//type of sensor that I am using
DHT dht(DHTPIN, DHTTYPE); //outside
DHT dht2(DHTPIN2, DHTTYPE); //inside

//pins
const int Red = 5;
const int Green = 4;
const int Blue = 14;
const int Relay1= 12;
const int Relay2= 13;
//booleans for on/off(state)
bool RedS=false;
bool GreenS=false;
bool BlueS=false;
bool Relay1S=false;
bool Relay2S=false;
bool AmbientTempS=false;

char ssid[] = "Wemos_AP";           // SSID of your AP
char pass[] = "Wemos_comm";         // password of your AP
IPAddress server(192,168,4,15);     // IP address of the AP
WiFiClient client;
void setup() {
  Serial.begin(9600);
  dht.begin();
  dht2.begin();
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, pass);           // connects to the WiFi AP
  Serial.println();
  Serial.println("Connection to the AP");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println();
  Serial.println("Connected");
  Serial.println("station_bare_01.ino");
  Serial.print("LocalIP:"); Serial.println(WiFi.localIP());
  Serial.println("MAC:" + WiFi.macAddress());
  Serial.print("Gateway:"); Serial.println(WiFi.gatewayIP());
  Serial.print("AP MAC:"); Serial.println(WiFi.BSSIDstr());
  //pins
  pinMode(Red, OUTPUT);
  pinMode(Green, OUTPUT);
  pinMode(Blue, OUTPUT);
  pinMode(Relay1, OUTPUT);
  pinMode(Relay2, OUTPUT);
  digitalWrite(Red, LOW);
  digitalWrite(Green, LOW);
  digitalWrite(Blue, LOW);
  digitalWrite(Relay1, LOW);
  digitalWrite(Relay2, LOW);
}
void loop() {
  int h = dht.readHumidity(); //humidity for Outside
  int t = dht.readTemperature(); //temperaure for Outside
  int h2 = dht2.readHumidity(); //humidity for Outside
  int t2 = dht2.readTemperature(); //temperaure for Outside
  client.connect(server, 80);
  Serial.println("********************************");
  Serial.print("Byte sent to the AP: ");
  Serial.println(client.print("Muie PSD\r")); //sending data to esp8266
  String answer = client.readStringUntil('\r');
  Serial.println("From the AP: " + answer);
  if(answer=="redOne"){
    if (RedS==false){
      digitalWrite(Red, HIGH);
      RedS=true;
      Serial.println("Red1 ON");
      }
    else{
      digitalWrite(Red, LOW);
      RedS=false;
      Serial.println("Red1 OFF");
      }
    }
  if(answer=="greenOne") {
    if (GreenS==false){
      Serial.println("Green1 ON");
      GreenS=true;
      digitalWrite(Green, HIGH);
      }
    else{
      Serial.println("Green1 OFF");
      GreenS=false;
      digitalWrite(Green, LOW);
      }


    }
  if(answer=="blueOne") {
    if (BlueS==false){
      Serial.println("Blue1 ON");
      BlueS=true;
      digitalWrite(Blue, HIGH);
      }
    else{
      Serial.println("Blue1 OFF");
      BlueS=false;
      digitalWrite(Blue, LOW);
      }


    }
  if(answer=="relayOne") {
    if (Relay1S==false){
      Serial.println("Relay1 ON");
      Relay1S=true;
      digitalWrite(Relay1, HIGH);
      }
    else{
      Serial.println("Relay1 OFF");
      Relay1S=false;
      digitalWrite(Relay1, LOW);
      }


    }
  if(answer=="relayTwo") {
    if (Relay2S==false){
      Serial.println("Relay2 ON");
      Relay2S=true;
      digitalWrite(Relay2, HIGH);
      }
    else{
      Serial.println("Relay2 OFF");
      Relay2S=false;
      digitalWrite(Relay2, LOW);
      }
  if(answer=="AmbientTemp") {
    if (AmbientTempS==false){
      Serial.println("AmbientTemp ON");
      AmbientTempS=true;
      }
    else{
      Serial.println("AmbientTemp OFF");
      AmbientTempS=false;
      digitalWrite(Red, LOW);
      digitalWrite(Green, LOW);
      digitalWrite(Blue, LOW);
      }
   while(AmbientTempS==true){
    Serial.println("Ambient temp is on!");
    }

    }
  client.flush();
  client.stop();
  delay(2000);
  }
}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.