Howdy!
I'd like to apologize in advance for my lack of concentration and understanding. Too mentally exhausted from IRL responsibilities and such, but i don't mind providing as much details as possible regarding my issue. Regardless, here's my story behind this issue.
Context: i'd received Arduino Uno ATmega328P WiFi R3 with built-in ESP8266 module. With this thing in my posession, i started working on my project. The goal of this project is to provide sensor data (i.e. current light level recieved via the light sensor - "MH-Sensor-Series" is written on the back of the board) by sending it from Arduino (acts as a data transmitter) to ESP8266 (acts as a data receiver). ESP8266 itself is supposed to act as an access point. And it does well in that regard after some tinkering. ESP8266 outputs HTML page where sensor data can be located.
Issue: no matter what i do, i get gibberish output out of this whole thing. I'd been searching the forums here and there until i realized that nothing suggested is working in my case. I'd tried multiple things:
- Checked wiring - everything is intact and wired correctly;
- Checked all devices - everything is stable and works flawlessly; i even tried using my other Arduino Uno connected to standalone ESP8266-01 module - no dice whatsoever;
- Set baud rates on both devices to the exact same value (9600);
- Trial and error - tried various combinations of the setup;
- Used
Serial.read()command - data is inadequate (i do understand that i get data as a series of bytes); - Used
Serial.readString()command - data is completely unreadable; - Used
Serial.parseInt()command - the value is always returned as zero.
Question: what should i do in order to get a readable data? Obviously i'm missing something trivial in this setup, but i can't see it.
This is what my wiring currently looks like:
Arduino (A0) --> Sensor (A0)
Arduino (D2) --> Sensor (D0)
Arduino (GND) --> Sensor (GND)
Arduino (5V) --> Sensor (VCC)
Arduino (GND) --> Voltage Divider (IN-)
Arduino (D4) as TX --> Voltage Divider (IN+)
Voltage Divider (OUT-) --> ESP8266 (GND)
Voltage Divider (OUT+) --> ESP8266 (RX)
Arduino (transmitter) code:
#include <SoftwareSerial.h>
SoftwareSerial esp8266(3, 4);
void setup() {
//
// Initialize serial communications at 9600 bps:
//
Serial.begin(9600);
esp8266.begin(9600);
return;
}
void run() {
int analogSensorValuePrevious = 0;
int analogSensorValueCurrent = 0;
//
// Receive data from the sensor.
//
analogSensorValueCurrent = analogRead(A0);
//
// Do not print the current sensor value if it's the same as the previous one that can be found in the previous tick.
//
if (analogSensorValuePrevious != analogSensorValueCurrent) {
analogSensorValuePrevious = analogSensorValueCurrent;
Serial.print("Current light level is ");
Serial.println(analogSensorValueCurrent);
esp8266.write(analogSensorValueCurrent);
}
delay(5000);
return;
}
void loop() {
run();
return;
}
ESP8266 (receiver) code:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#ifndef APSSID
#define APSSID "<ssid>"
#define APPSK "<password>"
#endif
const char *ssid = APSSID;
const char *password = APPSK;
String data = "";
String body = "";
ESP8266WebServer server(80);
void respond() {
//
// Get sensor data if available.
//
while (Serial.available() > 0) {
int digit = Serial.read();
if (isDigit(digit)) {
data += (char)digit;
}
}
Serial.print("Current light level is ");
Serial.println(data);
if (data.isEmpty()) {
body = "<div style=\"color: white; background-color: maroon; border-radius: 8px; box-shadow: 0 0 10px black inset; display: flex; align-items: center; height: 100%; justify-content: center; width: 100%;\"><h1>Sensor data is unreachable. Check connection.</h1></div>";
} else {
body = "<div style=\"color: white; background-color: green; border-radius: 8px; box-shadow: 0 0 10px black; 8px; display: flex; align-items: center; height: 100%; justify-content: center; width: 100%;\"><h1>Current light level is " + data + "</h1></div>";
}
Serial.print("Currently, the body contains the following: ");
Serial.println(body);
server.send(200, "text/html", body);
//
// Don't forget to clear the string.
//
data = "";
return;
}
void setup() {
delay(1000);
Serial.begin(9600);
Serial.println();
Serial.print("Configuring access point...");
WiFi.softAP(ssid, password);
IPAddress myIPAddress = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIPAddress);
server.on("/", respond);
server.begin();
Serial.println("Server started");
return;
}
void loop() {
server.handleClient();
return;
}
