Arduino freezes while sending data to ThingSpeak

Hello guys,

I'm rather new, both here and in Arduino niche, and while I do have significant background in programming, some things are just brand new to me, so I need some help from more experienced people.
I have procured some cheap components online, and they work well, the problem I am experiencing is sending data to ThingSpeak. I have created all neccesary accounts and created the channel to my liking, and made the aparatus that I am satisfied with, and which works well without said ThingSpeak. The line ThingSpeak.writeFields just freezes all operations on arduino. Even the extended command with debugging options stays silent.

HW in use:
generic arduino board
MQ2 gas sensor
DHT11 Temp and humidity sensor
ESP8266 WiFi module

Here is the code:

#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiUdp.h>
#include <SoftwareSerial.h>
#include <stdlib.h>
#include <DHT.h>
#include <DHT_U.h>
#include <ThingSpeak.h>

#define sensor A0

WiFiClient client;
unsigned long myChannelNumber = ******;
const char* myWriteAPIKey = "****************";
SoftwareSerial ESP8266(8, 9);
unsigned char check_connection = 0;
unsigned char times_check = 0;
DHT dht(7, DHT11);
static const int DHT_SENSOR_PIN = 2;
int gasLevel = 0;

void setup() {
  Serial.begin(115200);
  ESP8266.begin(115200);
  Serial.println("Connecting to Wifi");
  while (check_connection == 0) {
    Serial.print(".");
    ESP8266.print("AT+CWJAP=\"****\",\"****\"\r\n");
    ESP8266.setTimeout(5000);
    if (ESP8266.find("WIFI CONNECTED\r\n") == 1) {
      Serial.println("WIFI CONNECTED");
      break;
    }
    times_check++;
    if (times_check > 3) {
      times_check = 0;
      Serial.println("Trying to Reconnect..");
    }
  }
  ESP8266.print("***VER:");
  delay(2000);
  ESP8266.println("AT+RST");
  delay(1000);
  ESP8266.println("AT+GMR");
  delay(1000);
  ESP8266.println("AT+CWMODE=3");
  delay(1000);
  ESP8266.println("AT+CWLAP");
  delay(1000);
  dht.begin();
  ThingSpeak.begin(client);
}

void loop() {
  Serial.println("Preparation . . .");
  ThingSpeak.setField(1, dht.readTemperature());
  ThingSpeak.setField(2, dht.readHumidity());
  ThingSpeak.setField(3, analogRead(sensor));
  Serial.println("Sending . . .");
  ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
  Serial.println("Sent . . .");
  delay(30000);
}

Serial output is as follows:

Connecting to Wifi
...WIFI CONNECTED
Prepration . . .
Sending . . .

And stuck there for all eternity . . .

Pieces of code have been stolen from all around and reworked to fit my needs, and I know that I might not need all #includes that I have, but that is my secondary issue, and one for after the code starts working properly.

Have you tried a ThingSpeak example code from the ThingSpeak library?

1 Like

What is your "generic board"?

By the way, SoftwareSerial can't connect on 115200 baud:

I wanted to, but it seems too complicated to make to work. I was hoping that I was in the wrong, and that was something obvious to someone here, rather than going through the example . . . And, I'm still hoping :slight_smile: . . .

Generic is a not branded board. It has the shape and functionality of arduino uno.

And actually, when I change baud rate on this line (ESP8266.begin(115200);), I do not connect to wifi at all . . .

Here are a few steps to help troubleshoot this problem:

  1. Check Library Compatibility: Ensure that the ThingSpeak library version you are using is compatible with your hardware and the other libraries in your code. Sometimes, library versions can conflict and cause issues.
  2. Verify API Key and Channel Number: Double-check that the myChannelNumber and myWriteAPIKey are correctly entered. Incorrect credentials can cause the function to hang while trying to communicate with ThingSpeak.
  3. Adjust ESP8266 Communication: Ensure that the AT commands for the ESP8266 are correctly configured. Try adding more delays after AT commands to ensure that the module has enough time to process them.
  4. Debugging Serial Output: Add more debugging output before and after the ThingSpeak.writeFields call to narrow down where it’s freezing. For example, print messages before and after each field setting.
  5. Test Without Sensors: Try simplifying the code by removing the sensors and only sending a static value to ThingSpeak. This can help determine if the issue is related to the sensors or the network communication.
  6. Check Network Connection: Ensure your WiFi connection is stable. Unstable connections can sometimes cause timeouts and freezes in data transmission.
  7. Update Firmware and Libraries: Make sure the firmware of your ESP8266 and all your libraries are up to date, as bugs and compatibility issues are often fixed in newer versions.

These steps should help you diagnose and fix the issue with sending data to ThingSpeak.

There might be another problems in your code, but using SoftwareSerial with 115200 baudrate is definitely way to dead end.
SoftwareSerial on Uno can be used on rates not much as 38400, the recommended values are 9600 or 19200.