hi,
i want my program to send data, wich it got with a moisture and humidity/temperatur sensor(DHT22), to a server.
Also it is using a DS1307 RTC to get the current time(sometimes there is no network connection).
Everything is fine but as soon as I call the wire library(I2C for the RTC), the timer library(for a tick event wich calls the put_() function) or when I remove the analog pins A6 and A7 from the int Array(used for reading the moisture senors) the espwifi is failing to connect to the server or it is sending weird characters(like when the baud rate of the serial monitor and the arduino is different).
I am using an Arduino Nano(with Old bootloader).
I already tried removing the Strings because i read it is maybe a problem with memory or something like that but it did not seem to help much. ![]()
And as i want to add more code, any memory saving recommendations are appreciated. ![]()
I have now idea why the three things together are not working ![]()
//-------------------------------------------------------------------------
void(* reset) (void) = 0; //declare reset function @ address 0
void add_data(String str, float val = 0, int i = 0);
void init_wifi(bool already_called = false);
//-------------------------------------------------------------------------
//--------------------------------------------RTC---------------------------------------------
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC;
//--------------------------------------------RTC---------------------------------------------
//--------------------------------------------WIFI--------------------------------------------
#include "WiFiEsp.h"
#include "SoftwareSerial.h"
SoftwareSerial Serial1(11, 12); // RX, TX // Emulate Serial1 on pins 6/7 if not present
char ssid[] = "TP-LINK_"; // your network SSID (name)
char pass[] = "blablabla"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
char server[] = "example.com";
WiFiEspClient client;// Initialize the Ethernet client object
char data[255];
bool print_wifi_ = false;
//--------------------------------------------WIFI--------------------------------------------
//-------------------------------------------DHT----------------------------------------------
#include "DHT.h"
DHT dhts[] = {DHT(3, DHT22), DHT(4, DHT22)};
//-------------------------------------------DHT----------------------------------------------
//-----------------------------------------TIMER----------------------------------------------
#include "Timer.h"
Timer timer;
int tick_event[4];
//-----------------------------------------TIMER----------------------------------------------
//----------------------------------------VARIABLES-------------------------------------------
bool light = true;
int m_pins[] = {A0, A1, A2, A3/*, A6, A7*/};
int m[sizeof(m_pins)];
float h[sizeof(dhts)];
float t[sizeof(dhts)];
//----------------------------------------VARIABLES-------------------------------------------
//------------------------------------------------------SETUP------------------------------------------------------
void setup(){
Serial.begin(115200);
init_wifi();
init_dht();
init_rtc();
tick_event[0] = timer.every(5000, put_);
Serial.println(F("start\n"));
}
void loop(){
timer.update();
// put_();
delay(5000);
}
//----------------------------------------------------------------------------------------------------
void check_sens(){
add_data("light", light);
for(int i = 0; i < sizeof(m_pins)/sizeof(m_pins[0]); i++){
m[i] = analogRead(m_pins[i]);//moisture
add_data("moisture", m[i], i);
}
for(int i = 0; i < sizeof(dhts)/sizeof(dhts[0]); i++){
h[i] = dhts[i].readHumidity();//humidity
t[i] = dhts[i].readTemperature();//temperature
add_data("humidity", dhts[i].readHumidity(), i);
add_data("temperatur", dhts[i].readTemperature(), i);
}
Serial.println();
Serial.print("CHAR: ");
Serial.println(data);
Serial.println();
}
void add_data(String str, float val, int i = 0){
char tmp[50];
if(str != "light"){
strcat(data, "&");
str.toCharArray(tmp, str.length()+1);
strcat(data, tmp);
strcat(data, "_");
snprintf(tmp, sizeof(tmp), "%d", i);
strcat(data, tmp);
strcat(data, "=");
dtostrf(val, 5, 2, tmp);
strcat(data, tmp);
}
else{
strcpy(data, "");
str.toCharArray(tmp, str.length()+1);
strcat(data, tmp);
strcat(data, "=");
snprintf(tmp, sizeof(tmp), "%d", val);
strcat(data, tmp);
}
}
void init_rtc(){
Wire.begin();
RTC.begin();
if(!RTC.isrunning()){
Serial.println(F("RTC is NOT running!"));
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
DateTime now = RTC.now();
//will be removed later and sent to the server too
Serial.print("time: ");
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
}
void init_dht(){
for(int i = 0; i < sizeof(dhts)/sizeof(dhts[0]); i++){ dhts[i].begin(); }
}
//--------------------------------------------------WIFI--------------------------------------------------
void filter(String _buffer){
Serial.println(F("\nbuffer:"));
Serial.print(_buffer);
// convert_to_ts(_buffer);
static int wifi_err_counter = 0;
static int wifi_err_counter_ges = 0;
if(_buffer.indexOf("oki")!= -1){
Serial.println(F("\nserver received data!"));
wifi_err_counter_ges = 0;
wifi_err_counter = 0;
}
else{
Serial.println(F("\nerror!"));
if(WiFi.status() != "WL_CONNECTED"){
if(wifi_err_counter >= 5){
reconnect_wifi();
wifi_err_counter = 0;
}
else{
wifi_err_counter++;
}
}
wifi_err_counter_ges++;
}
if(wifi_err_counter_ges >= 20){
reset();
}
}
String read_(){
char c;
String _buffer;
// if there are incoming bytes available
// from the server, read them and print them
if(client.available()){
delay(35);
while(client.available()){
c = client.read();
_buffer.concat(c);
//Serial.print(c);
}
}
client.flush();
client.stop();
return _buffer;
}
void put_(){
check_sens();
if(client.connectSSL(server, 443)){
// Make a HTTP request
client.println("POST /put.php HTTP/1.1");
client.println("Host: example.com");
client.println("Authorization: Basic supersecret=");
client.println("Content-Type: application/x-www-form-urlencoded");
client.println("Connection:close");
client.print("Content-Length:");
String str = "";
str = String(str + data);
client.println(str.length());
client.println();
client.print(data);
}
strcpy(data, "");
filter(read_());
}
void reconnect_wifi(){
WiFi.disconnect();
Serial.println(F("\nreconnecting..."));
if(WiFi.status() == WL_DISCONNECTED){
init_wifi(true);
}
}
void init_wifi(bool already_called = false){
// initialize serial for ESP module
Serial.println(F("init wifi"));
if(already_called == false){
Serial1.begin(9600);
WiFi.init(&Serial1);
}
else if(already_called == true){
status = 0;
}
if(WiFi.status() == WL_NO_SHIELD){// check for the presence of the shield
// don't continue
while(true);
}
while(status != WL_CONNECTED){// attempt to connect to WiFi network
// Connect to WPA/WPA2 network
status = WiFi.begin(ssid, pass);
}
}
//--------------------------------------------------WIFI--------------------------------------------------
Sorry for bad english ![]()