New here, so be gentle.
I've just done my first sketch (Arduino IDE > ESP32 WROOM) and everything was fine, however when I came to do the final deploy I removed the Serial.Println statements that I was using to test with, uploaded and within seconds the Arduino IDE was locked up and my pc processors were sounding like they were going to explode.
Killed the process, put a couple of random Serial.Println statements back in, uploaded and all was fine. Am I doing something stupid or is there something special you have to do if you've no Serial.Println statements in the code?
I can't find anything on the internet for a question this woolly. The code works with the println statements in, but locks up the IDE when they are removed. I've gone through the process of remming out the code and I can't find anything wrong except this Serial.Println issue. I'm of a mind just to leave a couple in the code, but I'd rather know if there is a solution to this. TIA
This sounds very much like the sort of problem that occurs when you write to memory that you shouldn't be writing to. The classic way of doing this is to write outside of the bounds of an array
Have you got the IDE compiler warnings set to verbose and, if so, are warnings of any kind displayed ?
Please post your full sketch, using code tags when you do
Since everyone seems to want to speculate instead of waiting for the code, I'll go ahead and throw my hat in the ring. My hypothesis is @nomadros is experiencing this bug:
This would happen if their code still produces serial output without line breaks. The Serial.println calls they removed were preventing their sketch from producing the "extremely long line" condition that causes Arduino IDE to use excessive system resources.
Thanks for all the replies...here's the code (at the stage where the problem occurred and actual values replaced with **** as appropriate.)
I'm not used to C++, I usually code in c# or javaScript. The function names should make what's happening pretty clear, but basically wake up every hour, make sure bluetooth is off, turn on Wifi, check the time (if midnight or new upload sync time else if it's a send hour (4 x a day) ) gather data and send it to an api end point, turn off Wifi and go back to sleep.
#include <ArduinoJson.h>
#include <WiFi.h>
#include "esp_bt.h"
#include <time.h>
#include <HTTPClient.h>
// DEFINE PINS
#define MOISTURE_PIN 36 //GPIO36
#define TEMPHUM_PIN 35 //GPIO35
#define LUMENS_PIN 34 //GPIO34
//DEFINE SLEEP CYCLE
#define uS_TO_S_FACTOR 1000000ULL
#define TIME_TO_SLEEP 360//0
// delare constants....stuff you can chamge
const char* sensorName="*****";
const char* sensorType ="*****";
const char* WiFiSSID="*****";
const char* WiFiPassword="*****";
const char* ntpServer1 = "pool.ntp.org";
const char* TargetURL="https://*****.com/sensorData";
const int WiFiTries=30;
const int sendTimes[5]={4,10,16,22,0}; //HOURS ARE GMT
// sendTimes[4] = 0 is the ntpServer1 time service being updated daily at 00:00 (midnight) GMT or when this code is updated on the ESP32
//declare global variables
JsonDocument doc;
//_____________________________________
void setup() {
analogSetAttenuation(ADC_11db);
//********************************************************
Serial.begin(9600);
delay(1000); // Take some time to open up the Serial Monitor
//********************************************************
MakeSureBluetoothIsOFF();
if (TurnOnWifi()){
if(SetUpTime()){
SetJSONObj();
GetTempHumData();
GetMoistureData();
GetLightLevelData();
SendData();
}
TurnOffWifi();
}
GoToSleep();
}
void SendData(){
HTTPClient http;
http.begin(TargetURL);
http.addHeader("Content-Type", "application/json");
// http.addHeader("X-API-Key", "your_api_key_here");
Serial.println("SENDING DATA....");
serializeJson(doc, Serial);
String jsonBody;
serializeJson(doc, jsonBody);
int responseCode = http.POST(jsonBody);
Serial.println("POST response: " + String(responseCode));
http.end();
}
bool TurnOnWifi(){
int countit=0;
WiFi.mode(WIFI_STA);
WiFi.begin(WiFiSSID, WiFiPassword);
Serial.println(" ");
while ((WiFi.status() != WL_CONNECTED) && (countit<WiFiTries)) {
countit++;
Serial.print(".");
delay(500);
}
if(WiFi.status() == WL_CONNECTED){
Serial.println("connected");
return true;}
return false;
}
void TurnOffWifi(){
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
}
void MakeSureBluetoothIsOFF(){
btStop();
esp_bt_controller_disable();
esp_bt_controller_deinit();
}
void GoToSleep(){
// PUT IN DEEP SLEEP
delay(1000);
Serial.flush();
delay(1000);
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
esp_deep_sleep_start();
}
void SetJSONObj(){
doc["SType"]= sensorType;
doc["SName"] = sensorName;
doc["FOne"] = NULL;
doc["FTwo"] = NULL;
doc["IThree"] = NULL;
doc["IFour"] = NULL;
}
void GetMoistureData(){
int val =analogRead(MOISTURE_PIN);
doc["IThree"]=val;
}
void GetTempHumData(){
//TEST DATA
doc["FOne"]= 55.01;
doc["FTwo"]= 66.02;
}
void GetLightLevelData(){
int ldrValue = analogRead(LUMENS_PIN);
doc["IFour"]=ldrValue;
}
bool SetUpTime(){
tm tm;
getLocalTime(&tm);
int currHour= tm.tm_hour;
Serial.println(currHour);
if(currHour==sendTimes[4]) { configTime(0, 0, ntpServer1);}
getLocalTime(&tm);
currHour= tm.tm_hour;
Serial.println(currHour);
if((currHour==sendTimes[0])||(currHour==sendTimes[1])||(currHour==sendTimes[2])||(currHour==sendTimes[3])){return true;}
return false;
}
//_____________________________________________________
void loop() {} //NOT NEEDED
//_____________________________________________________
yeah...sorry, I moved the code to libreOffice to rem out assorted identifiers and the indentation got lost. Now I've posted I'll do any edit on the site.
The hypothesis sounds really similar. The IDE was solidly locked or very, very slow to respond, however I could still watch a youtube video (no cats) in an adjacent window. My PC doesn't have fans but you could really hear a noise coming from it. First couple of times I rebooted the pc. then worked out it was just the IDE, xkilled it and everything went back to normal. Remmed out code and that didn't help, so the only other thing I'd changed was removing the Serial.Println statements and I put a couple back in and everything was normal. I really appreciate all the replies...thanks.
The "ArduinoJson" library's serializeJson function does not add a newline, so when this code is executed repeatedly without any other code adding a newline, it will produce extremely long lines in Serial Monitor
Here is a minimal demonstration of this code producing extremely long lines:
Thanks all... I hadn't got around to trying the actual sending part live as I need to change the api receiving class, the SP and the DB table (.NET, MS SQL server), but that's good to know,
I also want to change the if statement for a for loop in the SetUpTime function, so I can expand and contract the SendTimes array as I need.
I'll get back to this Monday. Off topic: this has turned into a 'mare as everything I'm doing has work off grid, in a forest with very little mobile signal. I really appreciate how easy Arduino & the general ESP32 community has made ESP32 coding. Once I get through this, the I'll try the ESPNOW library. Many thanks.
OK...quick update. I've been trying to chase this "IDE grinds to a halt" issue down and I don't think it has anything to do with code. The only way I can replicate the issue is if I have the sketch loaded on the ESP32, give the board power before the Arduino IDE has finished loading, upload the sketch again and then the whole thing grinds to a halt. I might be talking rubbish but that's what makes it happen on my set up (Linux Mint + Arduino IDE (the most recent version which updated last week))
Anyway code below....
#include <WiFi.h>
#include "esp_bt.h"
#include <time.h>
#include <HTTPClient.h>
#include <DHT.h>
#include <Adafruit_Sensor.h>
// DEFINE PINS
#define MOISTURE_PIN 36 //GPIO36
#define TEMPHUM_PIN 32 //GPIO32
#define LUMENS_PIN 35 //GPIO35
#define DHTTYPE DHT11
//DEFINE SLEEP CYCLE
#define uS_TO_S_FACTOR 1000000ULL
#define TIME_TO_SLEEP 36//00
// delare constants....stuff you can chamge
const char* sensorName="*******************";
const char* sensorType ="*******************";
const char* WiFiSSID="*************************";
const char* WiFiPassword="**********************";
const char* ntpServer1 = "pool.ntp.org";
const char* TargetURL="https://*********************/***********";
const int WiFiTries=30;
const int sendTimes[]={0,4,8,12,18,20}; //HOURS ARE GMT
const int sendTimesSize=6;
// sendTimes[0] = 0 is the ntpServer1 time service being updated daily at 00:00 (midnight) GMT or when this code is updated on the ESP32
//declare global variables
JsonDocument doc;
//_____________________________________
void setup() {
analogSetAttenuation(ADC_11db);
//********************************************************
Serial.begin(9600);
delay(1000); // Take some time to open up the Serial Monitor
//********************************************************
MakeSureBluetoothIsOFF();
if (TurnOnWifi()){
if(SetUpTime()){
SetJSONObj();
GetTempHumData();
GetMoistureData();
GetLightLevelData();
SendData();
}
TurnOffWifi();
}
GoToSleep();
}
void SendData(){
//Serial.println("SENDING DATA....");
serializeJson(doc, Serial);
}
bool TurnOnWifi(){
int countit=0;
WiFi.mode(WIFI_STA);
WiFi.begin(WiFiSSID, WiFiPassword);
Serial.println(" ");
while ((WiFi.status() != WL_CONNECTED) && (countit<WiFiTries)) {
countit++;
delay(500);
}
if(WiFi.status() == WL_CONNECTED){return true;}
return false;
}
void TurnOffWifi(){
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
}
void MakeSureBluetoothIsOFF(){
btStop();
esp_bt_controller_disable();
esp_bt_controller_deinit();
}
void GoToSleep(){
// PUT IN DEEP SLEEP
delay(1000);
Serial.flush();
delay(1000);
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
esp_deep_sleep_start();
}
void SetJSONObj(){
doc["SType"]= sensorType;
doc["SName"] = sensorName;
doc["FOne"] = NULL;
doc["FTwo"] = NULL;
doc["IThree"] = NULL;
doc["IFour"] = NULL;
}
void GetMoistureData(){
doc["IThree"] =(int)analogRead(MOISTURE_PIN);
}
void GetTempHumData(){
DHT dht(TEMPHUM_PIN,DHTTYPE);
doc["FOne"] = (float) dht.readTemperature();
doc["FTwo"] = (float) dht.readHumidity();
}
void GetLightLevelData(){
doc["IFour"] = (int) analogRead(LUMENS_PIN);
}
bool SetUpTime(){
tm tm;
getLocalTime(&tm);
int currHour= tm.tm_hour;
if(currHour==sendTimes[0]) { configTime(0, 0, ntpServer1);}
getLocalTime(&tm);
currHour= tm.tm_hour;
for(int i=1;i<sendTimesSize; i++){
if(currHour==sendTimes[i]){return true;break; }
}
Serial.println("bad hour");
return false;
}
//_____________________________________________________
void loop() {} //NOT NEEDED
//_____________________________________________________
Then upload the updated sketch to the board again. Does the fault still occur after you do that?
On a side note, it is not very respectful to the forum helpers to post poorly formatted code. In the future, please select Tools > Auto Format from the Arduino IDE menus before copying sketch code to share on the forum. Properly formatted code is easier to work with, so doing that regularly will benefit you as well.
The sketch does nothing to the IDE. A device connected to the host computer must be putting a demand on the host, either in serial handshaking, CPU time or power demand. All the processing is offloaded to the controller.
Your sketch needs more "debug" statements, for example, "Serial.println("Entering SendData() function."); in all the functions. Then, show the Serial Monitor output to determine the last debug statement.
Your sketch does not monitor if WiFi or BT return an error, it just assumes both work.