Hello all!
I am trying to send some simple data from one esp8266 to another, but I can't seem to get it!
I have one ESP that acts as a Access Point and sends distance, temperature, humidity, and battery level to the other ESP that is connected to its network. The other just receives all of the data and prints it on a very simple web server.
I know my sender is working, because when I connect to it, I can see the data when checking each webpage (for example, "http://192.168.4.1/temperature").
My receiver though, is only getting an error code -1 for each data piece.
Here is the AP (transmitter) code:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <SoftwareSerial.h>
#include <DHT.h>
#define DHTPIN D7
#define batterypin A0
unsigned char data[4]={};
float distance;
float dist;
const char* ssid = "Solar-ESP";
const char* password = "makerslikethis";
bool error = false;
float calibration = 0.11;//.13
// Create AsyncWebServer object on port 127; 80
ESP8266WebServer server(80);
DHT dht(DHTPIN, DHT11);
SoftwareSerial mySerial(14,12); // RX, TX
void readTemp() {
server.send(200, "text/html", String((dht.readTemperature())+32));
}
void readHumi() {
server.send(200, "text/html", String(dht.readHumidity()));
}
void readDist() {
server.send(200, "text/html", String((dist/10) / 2.54));
}
void readErr() {
server.send(200, "text/html", String(error));
}
void readBat() {
float voltage = (((analogRead(batterypin) * 3.3) / 1024) * 2 - calibration);
Serial.println(voltage);
server.send(200, "text/html", String(voltage));
}
void setup(){
// Serial port for debugging purposes
Serial.begin(115200);
mySerial.begin(9600);
pinMode(batterypin, INPUT);
Serial.println();
// Setting the ESP as an access point
Serial.print("Setting AP (Access Point)…");
// Remove the password parameter, if you want the AP (Access Point) to be open
WiFi.softAP(ssid, password, 1, true, 1);
IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);
server.on("/temperature", readTemp);
server.on("/humidity", readHumi);
server.on("/distance", readDist);
server.on("/error", readErr );
server.on("/battery", readBat );
server.begin();
}
void loop(){
server.handleClient();
do{
for(int i=0;i<4;i++)
{
data[i]=mySerial.read();
}
}while(mySerial.read()==0xff);
mySerial.flush();
if(data[0]==0xff){
int sum;
sum=(data[0]+data[1]+data[2])&0x00FF;
if(sum==data[3]){
error = false;
distance=(data[1]<<8)+data[2];
if(distance>30){
dist = distance;
Serial.println(dist);
}else Serial.println("Below the lower limit");
}else Serial.println("ERROR"); error = true;
}
delay(100);
}
And the receiver code:
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
//#include <ESP8266WebServer.h>
#include <WiFiClient.h>
#include <ESP8266WiFiMulti.h>
ESP8266WiFiMulti WiFiMulti;
//ESP8266WebServer server(80);
#define buzzer 4
int cardist = 40;//inches
const char* ssid = "Solar-ESP";
const char* password = "makerslikethis";
const char* apssid = "Driveway-Alarm";
const char* appass = "makerawesome";
//Your IP address or domain name with URL path
const char* serverNameTemp = "http://192.168.4.1/temperature";
const char* serverNameHumi = "http://192.168.4.1/humidity";
const char* serverNamePres = "http://192.168.4.1/distance";
const char* serverNameErr = "http://192.168.4.1/error";
const char* serverNameBat = "http://192.168.4.1/battery";
String temperature;
String humidity;
String distance;
String error;
String battery;
unsigned long previousMillis = 0;
const long interval = 1000;
/*void serverStandard(){
String Content = "<html><font face='Arial'><head><title>Solar ESP Data (Driveway)</title></head><br><p style=color: '#07f747'>";
Content += "Temperature: " + temperature + "˚F<br>";
Content += "Humidity: " + humidity + "%<br>";
Content += "Distance: " + distance + "in<br>";
Content += "Battery Voltage: " + battery + "V</p></font>";
server.send(200, "text/html", Content);
}*/
void setup() {
Serial.begin(115200);
Serial.println();
pinMode(buzzer, OUTPUT);
digitalWrite(buzzer, LOW);
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_AP_STA);
WiFi.softAP(apssid, appass);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("Connected to WiFi");
//server.begin();
//server.on("/", serverStandard);
}
void loop() {
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval) {
// Check WiFi connection status
if ((WiFiMulti.run() == WL_CONNECTED)) {
temperature = httpGETRequest(serverNameTemp);
humidity = httpGETRequest(serverNameHumi);
distance = httpGETRequest(serverNamePres);
error = httpGETRequest(serverNameErr);
battery = httpGETRequest(serverNameBat);
Serial.print("RSSI: ");
Serial.println(WiFi.RSSI());
Serial.println("Temperature: " + temperature + " *F - Humidity: " + humidity + " % - Distance: " + distance + " in - Error: " + error + " - Battery: " + battery + " v");
previousMillis = currentMillis;
}
else {
Serial.println("WiFi Disconnected");
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("Connected to WiFi");
}
}
if (distance.toInt() <= cardist && distance.toInt() > 1){
digitalWrite(buzzer, HIGH);
delay(4000);
digitalWrite(buzzer, LOW);
delay(300);
}
}
String httpGETRequest(const char* serverName) {
WiFiClient client;
HTTPClient http;
// Your IP address with path or Domain name with URL path
http.begin(client, serverName);
// Send HTTP POST request
int httpResponseCode = http.GET();
String payload = "--";
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
payload = http.getString();
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
return payload;
}
Thanks in advance for any tips on why this is happening!