Hi All
Could have a look on my code and explain why UNO doesn't send sensor data to ESP8266 by Serial (pin 0 and 1)? I've spent last few weeks trying to make it work, learned something but still they don't comunicate.
Both UNO and ESP while connected to my PC work ok (Arduino sends data and responds to feedback, ESP requests time, gets data and send it to ThingSpeak). So when I type what they should get in serial its ok, but when I connect one to another ThingSpeak is not updated...
The Idea:
Both UNO and ESP turn on about every 10 minutes (555 timer + relay - circut ready)
I'm sending 12 variables to ESP, It informs UNO what it already has, when it has everything or time is up it sends the data. Once a day on a hour I choose it asks ESP for NTP time, next time it's on it will get it updated. The LED pin will trigger turning off power.
Connection:
Both UNO and ESP have seperate 5V supplies (both connected to the same extension lead). Arduino gets 5V 1A, ESP 5V 500mA. ESP is plugged into a ESP-01 adapter(4 pins), so it can be powered with 5v and I have it connected to arduino serial directly (RX to TX, TX to RX).
So Far UNO:
- Gets sensor data
- Manages SD card (gets time from SD card and saves sensor data)
- Reads time from RTC and updates it's time
- Gives fedback in serial
ESP:
- Connects to Wifi
- Gets time from NTP Server
- Collects data from Serial and sends data to ThingSpeak
- Gives feedback in serial
I've tried today adding "<" and ">" To the char arrays that UNO sends but still nothing. (tutorial Serial Input Basics - Programming Questions - Arduino Forum)
On UNO the liblaries and code take over 95% storage space (below 70% dynamic memory), so ESP has to have it's own script... I tried using SoftwareSerial but it didn't help in finding the issue.
I have one UNO and a few NANO's only, and I'm not planning to buy a MEGA.
I left only two variables in to show what I mean (and not have 18 000 characters).
UNO code:
#include <DHT.h>
#include <DHT_U.h>
#include <SmoothThermistor.h>
#include <MS5611.h>
MS5611 baro;
int32_t pressure;
#include "RTClib.h"
RTC_DS1307 rtc;
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#define DHTPIN_INSIDE 3 // what digital pin we're connected to
#define DHTPIN_OUTSIDE 5 // what digital pin we're connected to
// Uncomment whatever type you're using!
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht_in(DHTPIN_INSIDE, DHTTYPE);
DHT dht_out(DHTPIN_OUTSIDE, DHTTYPE);
SmoothThermistor smoothThermistor_up(A1, ADC_SIZE_10_BIT, 10000, 10000, 3470, 25, 10);
SmoothThermistor smoothThermistor_front(A2, ADC_SIZE_10_BIT, 10000, 10000, 3470, 25, 10);
SmoothThermistor smoothThermistor_back(A3, ADC_SIZE_10_BIT, 10000, 10000, 3470, 25, 10);
float light = 0;
float temp_pressure = 0;
// SD Card IS 10
File gFile;
File timeFile;
// SERIAL
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
unsigned char incomingData = 0; //unsigned char
String newDate = "";
String this_day = "";
String this_time = "";
int running_time = 0;
int waitABit = 0;
boolean got_light = false;
boolean got_temp_pressure = false;
int oneSec = 0;
void setup() {
pinMode(PD2, OUTPUT);
Serial.begin(115200);
rtc.begin();
// Pin turning switching 555 therefore turning off power
baro = MS5611();
baro.begin();
dht_in.begin();
dht_out.begin();
if (!SD.begin(10)) {
//Serial.println("initialization failed!");
while (1);
}
//Serial.println("initialization done.");
if (SD.exists("TIME.txt")) {
timeFile = SD.open("TIME.txt");
newDate = timeFile.readString();
timeFile.close();
SD.remove("TIME.txt");
rtc.adjust(DateTime(newDate.substring(0, 4).toInt(), newDate.substring(5, 7).toInt(), newDate.substring(8, 10).toInt(), newDate.substring(11, 13).toInt(), newDate.substring(14, 16).toInt(), newDate.substring(17).toInt()));
}
timeFile = SD.open("NTP.txt");
newDate = timeFile.readString();
timeFile.close();
if (newDate.length() > 5) {
//Serial.println(newDate);
rtc.adjust(DateTime(newDate.substring(0, 4).toInt(), newDate.substring(5, 7).toInt(), newDate.substring(8, 10).toInt(), (newDate.substring(11, 13).toInt() - 1), newDate.substring(14, 16).toInt(), newDate.substring(17).toInt()));
}
else {
newDate = "";
}
}
void loop() {
delay(5000);
DateTime now = rtc.now();
int minutess = now.minute();
int hourss = now.hour();
int minuteNow = now.minute();
int hourNow = now.minute();
while (((minuteNow < minutess + 1) && (hourNow == hourss)) && (hourss == 18) && (minutess < 15)) {
Serial.write('T'); // get time
recvWithEndMarker();
showNewNumber();
now = rtc.now();
minuteNow = now.minute();
hourNow = now.minute();
}
running_time = 0;
if (incomingData == 0) {
this_day = String(now.year()) + "-" + String(now.month()) + "-" + String(now.day()) + " ";
newDate = this_day + this_time;
//Serial.println(newDate);
timeFile = SD.open("NTP.txt", FILE_WRITE);
timeFile.print(newDate);
timeFile.close();
this_time = "";
}
//Serial.println(this_time);
this_day = String(now.year()) + "-" + String(now.month()) + "-" + String(now.day()) + " ";
//Serial.println(this_day);
newDate = this_day + this_time;
this_time = "";
light = analogRead(A0);
// Serial.println(light);
// Read temperatures as Celsius (the default in DHT22)
//Read thermistors (10 samples)
// Read pressure [hPa] and temperature
temp_pressure = baro.getTemperature() / 100;
pressure = baro.getPressure() / 10; // Should be 100, but I want one more place to delete from substring
gFile = SD.open("GDATA.txt", FILE_WRITE);
// ...
gFile.close();
String string_light = "A" + String(light);
String string_temp_pressure = "B" + String(temp_pressure);
// From "A" to "L"
string_light = "<" + string_light.substring(0, 5) + ">";
string_temp_pressure = "<" + string_temp_pressure.substring(0, 5) + ">";
// From "A" to "L"
char array_light[string_light.length() + 1]; // char array_light[string_light.length() + 1];
string_light.toCharArray(array_light, string_light.length() + 1); // string_light.toCharArray(array_light, string_light.length() + 1);
char array_temp_pressure[string_temp_pressure.length() + 1];
string_temp_pressure.toCharArray(array_temp_pressure, string_temp_pressure.length() + 1);
Serial.println("begin");
running_time = 0;
running_time = millis();
now = rtc.now();
minutess = now.minute();
hourss = now.minute();
minuteNow = now.minute();
hourNow = now.minute();
while ((minuteNow < minutess + 1) && (hourNow == hourss)) {
now = rtc.now();
minuteNow = now.minute();
hourNow = now.minute();
if (got_light == false) {
Serial.write(array_light);
recvWithEndMarker();
showNewNumber();
}
if (got_temp_pressure == false) {
Serial.write(array_temp_pressure);
recvWithEndMarker();
showNewNumber();
}
if (running_time > 1000) {
running_time = 0;
running_time = millis();
oneSec++;
//Serial.println(oneSec);
}
}
now = rtc.now();
minutess = (int)now.minute();
hourss = (int)now.minute();
while ((minuteNow < minutess + 1) && (hourNow == hourss)) {
now = rtc.now();
minuteNow = (int)now.minute();
hourNow = (int)now.minute();
recvWithEndMarker();
showNewNumber();
if (incomingData == 'X') {
digitalWrite(PD2, HIGH);
}
}
digitalWrite(PD2, HIGH);
delay(5000);
digitalWrite(PD2, LOW);
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
if (Serial.available() > 0) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewNumber() {
if (newData == true) {
incomingData = 0;
String chArray(receivedChars);
incomingData = chArray.charAt(0);
switch (incomingData) {
case 'A':
got_light = true;
oneSec++;
break;
case 'B':
got_temp_pressure = true;
oneSec++;
break;
case 'T':
this_time = chArray.substring(1);
break;
}
newData = false;
}
}
ESP8266 code is below (over 9000)


