Arduino Uno with ESP-01 - ThingSpeak and ThingTweet dont work together

Hello Everyone,
I am facing a lot of issue with this project. Basically it is IoT Garbage Monitoring System, the ultrasonic sensor checks the distance of the garbage from the lit. then it send the distance to Thing Speak and when the distance is less than 20 cm it sends me a tweet on my twitter account with the Bin ID.

Problem :
Everything is working good separately (Thing Speak and Thing Tweet), but when I combine the code, only Thing Speak works and Thing Tweet doesn’t. But when I comment the code of Thing Speak, only then the Thing Tweet works. I don’t know what the problem is may be its AT Command Issue or something I am not aware of. Please help me out on this.

Hardware :
Arduino UNO
ESP-01
Ultrasonic Sensor
DIP Switches ( for Bin Number )
LED Lights

Code :

#include <SoftwareSerial.h>
#include <stdlib.h>
#include <String.h>

String reset  = "AT+RST";
String twitterAPIKey = "3U8CQLNIVG8D0VOD";
String tweetURI = "/apps/thingtweet/1/statuses/update";
String thingSpeak = "api.thingspeak.com";
String urlEncoded = "application/x-www-form-urlencoded";
String cmd;
#define DEBUG true
String myAPI = "BR0FSB0E4DK6WYRE";   // API Key
String myHOST = "api.thingspeak.com";
String myPORT = "80";
String myFIELD = "field1";
int trigPin = 8;    // Trigger
int echoPin = 9;    // Echo
long duration, cm;
long sendVal;


SoftwareSerial ser(2, 3); // RX, TX
// constants won't change. They're used here to set pin numbers:
const int buttonPin1 = 13;     // the number of the pushbutton pin
const int buttonPin2 = 12;     // the number of the pushbutton pin
const int buttonPin3 = 11;     // the number of the pushbutton pin
const int buttonPin4 = 10;     // the number of the pushbutton pin
// variables will change:
int buttonState1 = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int binNum = 0;
const int red = 7;
const int green = 6;

void setup() {
  // put your setup code here, to run once:
  // enable debug serial
  Serial.begin(9600);
  // enable software serial
  ser.begin(9600);
  //AT+IPR=9600; set baud rate

  // reset ESP8266
  ser.println(reset);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  digitalWrite(red, LOW);
  digitalWrite(green, LOW);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
}

void loop() {
  // read from tmp36 sensor
  // read the state of the pushbutton value:
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  buttonState4 = digitalRead(buttonPin4);

  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);

  // Convert the time into a distance
  cm = (duration / 2) / 29.1;   // Divide by 29.1 or multiply by 0.0343


  sendVal = cm ; // Send the level of garbage in centimeters.

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState1 == HIGH) {
    // turn LED on:
    Serial.println("1 on");
    binNum = 1;
  } else {
    Serial.println("1 off");
  }

  if (buttonState2 == HIGH) {
    binNum = 2;
    // turn LED on:
    Serial.println("2 on");
  } else {
    Serial.println("2 off");
  }

  if (buttonState3 == HIGH) {
    binNum = 3;
    // turn LED on:
    Serial.println("3 on");
  } else {
    Serial.println("3 off");
  }
  if (buttonState4 == HIGH) {
    binNum = 4;
    // turn LED on:
    Serial.println("4 on");
  } else {
    Serial.println("4 off");
  }

  if (sendVal < 60) {
    digitalWrite(red, HIGH);
    digitalWrite(green, LOW);
    //create a POST request header structure
    String payload = postRequest(tweetURI, thingSpeak, urlEncoded, tempAlertTweet(sendVal, binNum));

    //TCP connect
    tcp_connect(cmd);

    // send data length
    if (postStatus(cmd, payload)) {

      // thingspeak needs at least 15 sec delay between updates
      delay(30000);
    } else {
      ser.println("AT+CIPCLOSE");
      // alert user
      Serial.println("AT+CIPCLOSE");
    }
  }
  else {
    digitalWrite(green, HIGH);
    digitalWrite(red, LOW);
  }
  delay(5000);

  //Sending Data to ThingSpeak Feild 1'
  String sendData = "GET /update?api_key=" + myAPI + "&" + myFIELD + "=" + String(sendVal);
  espData("AT+CIPMUX=1", 1000, DEBUG);       //Allow multiple connections
  espData("AT+CIPSTART=0,\"TCP\",\"" + myHOST + "\"," + myPORT, 1000, DEBUG);
  espData("AT+CIPSEND=0," + String(sendData.length() + 4), 1000, DEBUG);
  ser.find(">");
  ser.println(sendData);
  Serial.print("level of garbage in dustbin : ");
  Serial.println(sendVal);

}
// Loop Ended

//TCP connection
bool tcp_connect(String tcp_connect_cmd) {
  // TCP connection
  tcp_connect_cmd = "AT+CIPSTART=\"TCP\",\"";
  tcp_connect_cmd += "184.106.153.149"; // api.thingspeak.com
  tcp_connect_cmd += "\",80";
  ser.println(tcp_connect_cmd);
  Serial.println(tcp_connect_cmd);
  //check for connection error
  if (ser.find("Error") || ser.find("closed")) {
    Serial.println("AT+CIPSTART error");
    return;
  }
}

//POST request structure
String postRequest(String url, String host, String content_type, String message) {
  String postString = "POST " + url + " HTTP/1.1\r\n" +
                      "Host: " + host + "\r\n" +
                      "Content-Type: " + content_type + "\r\n" +
                      "Connection: " + "close\r\n" +
                      "Content-Length: " + message.length() + "\r\n" +
                      "Cache-Control: " + "no-cache\r\n" +
                      + "\r\n" + message;
  return postString;
}

//tweet message + api key + bin Number
String tempAlertTweet(float temp, int bin) {
  //tweet message
  String tempAlert = "api_key=" + twitterAPIKey +
                     "&status= The Garbage Bin is Full... " +
                     temp + " cm. " + "In Bin Number " + bin;
  return tempAlert;
}

//send payload to server via esp8266
bool postStatus(String command, String message) {
  command = "AT+CIPSEND=";
  command += String(message.length());
  Serial.println(command);
  ser.println(command);
  if (ser.find(">")) {
    ser.print(message);
    Serial.print(message);
    return;
  }
}

String espData(String command, const int timeout, boolean debug)
{
  Serial.print("AT Command ==> ");
  Serial.print(command);
  Serial.println("     ");

  String response = "";
  ser.println(command);
  long int time = millis();
  while ( (time + timeout) > millis())
  {
    while (ser.available())
    {
      char c = ser.read();
      response += c;
    }
  }
  if (debug)
  {
    //Serial.print(response);
  }
  return response;
}

Intelligent_Bin_V3.ino (6.42 KB)

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.