Go Down

Topic: AT+Close Issue while Sending Data to Thingspeak (Read 390 times) previous topic - next topic

Badabing112

Hi All,


I'm exploring and working on an arduino IOT-based project that reads values from sensors (pulse rate sensor and LM35DZ) then send the values to Thingspeak channel then get the values from Thingspeak and post these values to Google Sheet using IFTTT

The idea behind this project is as follow:

  • Connect both sensors to Arduino Uno R3
  • Get readings from pulse rate and LM35DZ sensor
  • Establish a wireless connection using ESP8266 ESP-01S
  • Connect to Thingspeak channel using write API key then send sensors' readings to Thingspeak channnel every 30 seconds
  • Write sensors' readings from Thingspeak Channel to Google Sheet using IFTTT



As of now, everything is working as expected, except the STEP 4 and consequently STEP 5.
Observing the serial monitor, the sensors do get the values but sending them to Thingspeak channel is a hit or miss.

Snippet from serial monitor:

Quote
19:00:11.683 -> Temp is:
19:00:11.716 -> 25.42
19:00:11.716 -> BPM is:
19:00:11.716 -> 204
19:00:12.708 -> AT+CIPSTART="TCP","184.106.153.149",80
19:00:15.718 -> AT+CIPSEND=59
19:00:17.739 -> GET /update?key=E4GF8L6XWGZ947Q3&field1=204.0&field2=25.4
19:00:41.793 -> Temp is:
19:00:41.793 -> 19.06
19:00:41.793 -> BPM is:
19:00:41.793 -> 138
19:00:42.806 -> AT+CIPSTART="TCP","184.106.153.149",80
19:00:45.826 -> AT+CIPSEND=59
19:00:48.823 -> AT+CIPCLOSE
19:01:12.845 -> Temp is:
19:01:12.845 -> 16.62
19:01:12.887 -> BPM is:
19:01:12.887 -> 100
19:01:13.864 -> AT+CIPSTART="TCP","184.106.153.149",80
19:01:16.863 -> AT+CIPSEND=59
19:01:19.886 -> AT+CIPCLOSE
19:01:43.927 -> Temp is:
19:01:43.927 -> 23.95
19:01:43.927 -> BPM is:
19:01:43.927 -> 217
19:01:44.954 -> AT+CIPSTART="TCP","184.106.153.149",80
19:01:47.949 -> AT+CIPSEND=59
19:01:49.944 -> GET /update?key=E4GF8L6XWGZ947Q3&field1=217.0&field2=23.9
19:02:13.977 -> Temp is:
19:02:14.013 -> 23.95
19:02:14.013 -> BPM is:
19:02:14.013 -> 168
19:02:15.032 -> AT+CIPSTART="TCP","184.106.153.149",80
19:02:18.035 -> AT+CIPSEND=59
19:02:21.055 -> AT+CIPCLOSE
19:02:45.062 -> Temp is:
19:02:45.062 -> 16.62
19:02:45.098 -> BPM is:
19:02:45.098 -> 211
19:02:46.107 -> AT+CIPSTART="TCP","184.106.153.149",80
19:02:49.079 -> AT+CIPSEND=59
19:02:52.085 -> AT+CIPCLOSE
Green means the data was successfully captured, sent to Thingspeak, and posted on Google Sheet using IFTTT
Red Means the data was successfully captured but wasn't sent to thinkspeak, and consequently did not get posted on Google Sheet.


Below is a diagram of Arduino connection:




This is my code:

Code: [Select]


#define USE_ARDUINO_INTERRUPTS true
#define DEBUG true
#define SSID "XXXXXXXX"     // "SSID-WiFiname"
#define PASS "XXXXXXXX" // "password"
#define IP "184.106.153.149"      // thingspeak.com ip

#include <SoftwareSerial.h>
#include "Timer.h"
#include <PulseSensorPlayground.h>     // Includes the PulseSensorPlayground Library.  
Timer t;
PulseSensorPlayground pulseSensor;

String msg = "GET /update?key=XXXXXXXXXXXX";
SoftwareSerial esp8266(10, 11);

//Variables
const int PulseWire = A0;       // PulseSensor PURPLE WIRE connected to ANALOG PIN 0
int Threshold = 550;           //for heart rate sensor
float myTemp;
int myBPM;
String BPM;
String temp;
int error;
int raw_myTemp;
float Voltage;
float tempC;
void setup()
{

  Serial.begin(9600);
  esp8266.begin(115200);
  pulseSensor.analogInput(PulseWire);
  pulseSensor.setThreshold(Threshold);

  // Double-check the "pulseSensor" object was created and "began" seeing a signal.
  if (pulseSensor.begin()) {
    Serial.println("We created a pulseSensor Object !");  //This prints one time at Arduino power-up,  or on Arduino reset.
  }
  Serial.println("AT");
  esp8266.println("AT");

  delay(3000);

  if (esp8266.find("OK"))
  {
    connectWiFi();
  }
  t.every(30000, getReadings);
  t.every(30000, updateInfo);
}

void loop()
{
start: //label
  error = 0;
  t.update();
  //Resend if transmission is not completed
  if (error == 1)
  {
    goto start; //go to label "start"
  }
  delay(4000);
}

void updateInfo()
{
  String cmd = "AT+CIPSTART=\"TCP\",\"";
  cmd += IP;
  cmd += "\",80";
  delay(1000);  //added

  Serial.println(cmd);
  esp8266.println(cmd);
  delay(2000);
  if (esp8266.find("Error"))
  {
    return;
  }
  cmd = msg ;
  cmd += "&field1=";    //field 1 for BPM
  cmd += BPM;
  cmd += "&field2=";  //field 2 for temperature
  cmd += temp;
  cmd += "\r\n";
  Serial.print("AT+CIPSEND=");
  esp8266.print("AT+CIPSEND=");
  delay(1000);  //added

  Serial.println(cmd.length());
  esp8266.println(cmd.length());
  if (esp8266.find(">"))
  {
    delay(1000); //added
    Serial.print(cmd);
    esp8266.print(cmd);
  }
  else
  {
    delay(1000);  //added
    Serial.println("AT+CIPCLOSE");
    esp8266.println("AT+CIPCLOSE");
    //Resend...
    error = 1;
  }
}

boolean connectWiFi()
{
  Serial.println("AT+CWMODE=1");
  esp8266.println("AT+CWMODE=1");
  delay(2000);
  String cmd = "AT+CWJAP=\"";
  cmd += SSID;
  cmd += "\",\"";
  cmd += PASS;
  cmd += "\"";
  delay(1000);  //added

  Serial.println(cmd);
  esp8266.println(cmd);
  delay(5000);
  if (esp8266.find("OK"))
  {
    return true;
  }
  else
  {
    return false;
  }
}

void getReadings() {
  raw_myTemp = analogRead(A1);
  Voltage = (raw_myTemp / 1023.0) * 5000; // 5000 to get millivots.
  tempC = Voltage * 0.1;
  myTemp = tempC;
  Serial.println("Temp is: ");
  Serial.println(myTemp);
  int myBPM = pulseSensor.getBeatsPerMinute();  // Calls function on our pulseSensor object that returns BPM as an "int".
  // "myBPM" hold this BPM value now.
  if (pulseSensor.sawStartOfBeat()) {            // Constantly test to see if "a beat happened".
    Serial.println("BPM is: ");
    Serial.println(myBPM);                        // Print the value inside of myBPM.
  }

  delay(20);
  char buffer1[10];
  char buffer2[10];
  BPM = dtostrf(myBPM, 4, 1, buffer1);
  temp = dtostrf(myTemp, 4, 1, buffer2);
}






My question is, why are my data are randomly sent to thingspeak ?
If there is a major error in what i'm doing, it should not send ANYTHING at all to Thingspeak, instead i'm getting unreliable results.

ieee488

1. SoftwareSerial is not capable of 115200.

2. ESP8266 requires a robust 3.3V supply when transmitting that the Arduino Uno is not capable of providing.



.



.

Badabing112

1. SoftwareSerial is not capable of 115200.

2. ESP8266 requires a robust 3.3V supply when transmitting that the Arduino Uno is not capable of providing.



.



.
Thank you for your time ieee488.



1. After a bit of research i found how inefficient SoftwareSerial is compared to other libraries.
Specifically, HardwareSerial and AltSoftSerial libraries (need to read their documentation sometimes this weekend).

2. I got my hands on AMS1117 and LD1117 regulators and i'll work on them this weekend


Again, thank you for taking the time. Hopefully i'll get back to this post within the weekend with a positive results.


Cheers


ieee488

Unfortunately, most people myself included start with the Uno then add the ESP-01 module for wifi when the much better option is to buy one module that has wifi built in.
One regular here zoomkat reports excellent results using Wemos D1 Mini Pro.

Badabing112

#4
Feb 25, 2020, 04:38 pm Last Edit: Feb 25, 2020, 04:39 pm by Badabing112
Unfortunately, most people myself included start with the Uno then add the ESP-01 module for wifi when the much better option is to buy one module that has wifi built in.
One regular here zoomkat reports excellent results using Wemos D1 Mini Pro.
I've been thinking the same, but i already have the uno and esp8266 and i prefer to exhaust all possible solution before buying ARDUINO UNO WiFi REV2.

I borrowed an ESP32 from a friend, but to tell the truth, i don't know the difference (yet) between the two (esp32 & esp 8266)

I added WeMos D1 Mini Pro to my research list, and i will see how things pan out this weekend


Cheers

zoomkat

"I added WeMos D1 Mini Pro to my research list, and i will see how things pan out this weekend"

For the price of a house martini and a bar tip you can get the below board. The below vendor is in the US with ~5 day delivery time by USPS. Shipping time from china is getting long now.

https://www.ebay.com/itm/OTA-WeMos-D1-CH340-WiFi-Arduino-UNO-R3-Development-Board-ESP8266-ESP-12E/383124613310?hash=item593401a8be:g:K~4AAOSwEExdZaRd
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

ieee488

"I added WeMos D1 Mini Pro to my research list, and i will see how things pan out this weekend"

For the price of a house martini and a bar tip you can get the below board. The below vendor is in the US with ~5 day delivery time by USPS. Shipping time from china is getting long now.

https://www.ebay.com/itm/OTA-WeMos-D1-CH340-WiFi-Arduino-UNO-R3-Development-Board-ESP8266-ESP-12E/383124613310?hash=item593401a8be:g:K~4AAOSwEExdZaRd
Is that a Wemos D1 in an Arduino Uno form?
Or are there other differences?
.

ieee488

I've been thinking the same, but i already have the uno and esp8266 and i prefer to exhaust all possible solution before buying ARDUINO UNO WiFi REV2.

I borrowed an ESP32 from a friend, but to tell the truth, i don't know the difference (yet) between the two (esp32 & esp 8266)

I added WeMos D1 Mini Pro to my research list, and i will see how things pan out this weekend


Cheers
The ESP8266 is actually the IC.

If you have a module, then it is most like the ESP-01.
The ESP32 is a more powerful module with the ESP8266.

.

Badabing112

"I added WeMos D1 Mini Pro to my research list, and i will see how things pan out this weekend"

For the price of a house martini and a bar tip you can get the below board. The below vendor is in the US with ~5 day delivery time by USPS. Shipping time from china is getting long now.

https://www.ebay.com/itm/OTA-WeMos-D1-CH340-WiFi-Arduino-UNO-R3-Development-Board-ESP8266-ESP-12E/383124613310?hash=item593401a8be:g:K~4AAOSwEExdZaRd
Hey Zoomkat

Thanks for popping in :D, i'm definitely considering the WeMos D1 Mini if all else fails.



The ESP8266 is actually the IC.

If you have a module, then it is most like the ESP-01.
The ESP32 is a more powerful module with the ESP8266.

.
Thanks for the clarification.

Your guess is correct, i have ESP-01 (1 MB) module (the black/dark one)

Badabing112

I come bearing good news! :D



I have added the LD1117 regulator to power up the ESP8266 with a stable 3.3v and it's working. (as per ieee488 suggestion)
Although, i started measuring the voltage while the ESP8266 is connected without the regulator and the 3.3v seems to be stable with no issue.

Yet, My issue was not solved.

A friend of mine hooked me up with 360 squirt programmer for the purpose of monitoring the ESP8266 pin through Putty. Turns out the problem was caused by my code.

After few hours of head scratching and aimless debugging, we found out the root cause of the issue:

Code: [Select]



 if (esp8266.find(">"))   //problem is here
  {
    delay(1000); //added
    Serial.print(cmd);
    esp8266.print(cmd);
  }
  else
  {
    delay(1000);  //added
    Serial.println("AT+CIPCLOSE");
    esp8266.println("AT+CIPCLOSE");
    //Resend...
    error = 1;
  }




Before this IF statement, the ESP8266 receives "AT+CIPSEND" command, and if all goes well, it should response with "ok   >"  (as per espressif documentation)

for some reasons, the "esp8266.find(">")" does not execute properly within time before the "updateInfo" function is called again. (remember, i have "t.every(30000, updateInfo);" in the setup function).
That cause the function to send another "AT+CIPSEND" command while the esp8266 is waiting for the payload to be send to the server.



I tried to change the Serial.find timeout duration, but the issue was not solved.
Then i changed the esp8266.find to esp8266.readStringUntil('62') and it is working without any issue :D



Right now, my aimless debugging and head scratching is directed to other issues with my project LOL

garrettlynchirl

I come bearing good news! :D

I have added the LD1117 regulator to power up the ESP8266 with a stable 3.3v and it's working. (as per ieee488 suggestion)

Can you share a diagram or photo of your setup please so we can see how the LD1117 is used in the circuit?

Go Up