Arduino + ESP8266

Hi guys, I'm having a little bit of trouble to unify 2 codes that i have found on Internet.
One is

#include <SoftwareSerial.h>
#include <DHT.h>
#define DHTPIN 5
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
// ThingSpeak Write API key, this can be found in your channel under API Keys and you'll want the "Write API Key".
String apiKey = "D29G5QSIL6X7L6NF";
#define SSID "VIVO-5051" // Remove the text but keep the ""
#define PASS "1604020062"// Same as above, keep the ""
// connect pin 11 to TX of the ESP8266 (Yellow wire if you've been following the videos.)
// connect pin 10 to RX of the ESP8266 (Blue wire if you've been following the videos.)
SoftwareSerial SoftSer(6, 7); // Everywhere from here on when you see SoftSer, it is being used as a command
// in the same way you'd use Serial.print() or Serial.begin() only over pins 11 and 10 instead of the USB port.
void setup()
{
  Serial.begin(9600);
  dht.begin();
  SoftSer.begin(9600);
}

void loop()
{
  float t = dht.readTemperature();
  float h = dht.readHumidity();
  float temp = dht.readTemperature();
  // convert float to string-------------*
  char buf[16]; // buf array can hold up to 16 characters.
  String strTemp = dtostrf(temp, 4, 1, buf); // "buf" will be the actual name of the array and strTemp is being equalled to it as a string.
  String strTemp2 = dtostrf(h, 4, 1, buf);
  // dtostrf(floatvar, StringLengthIncDecimalPoint, numVarsAfterDecimal, charbuf);
  Serial.println(strTemp); // Prints the recorded temperature to the serial print.

  // TCP connection
  String cmd = "AT+CIPSTART=\"TCP\",\"";
  cmd += "184.106.153.149"; // api.thingspeak.com
  cmd += "\",80";
  SoftSer.println(cmd);
  if (SoftSer.find("Error"))
  {
    Serial.println("AT+CIPSTART error");
    return;
  }
  // Putting together the GET string, this command structure can be found on your
  // ThingSpeak channel in Data Import/Export, under "Update Channel Feed - GET".
  String getStr = "GET /update?api_key=";
  getStr += apiKey;
  getStr += "&field1=";
  getStr += String(strTemp);
  getStr += "&field2=";
  getStr += String(strTemp2);
  getStr += "\r\n\r\n";

  // send data length
  cmd = "AT+CIPSEND=";
  cmd += String(getStr.length());
  SoftSer.println(cmd);
  if (SoftSer.find(">"))
  {
    SoftSer.print(getStr); // Send data.
  }
  else
  {
    SoftSer.println("AT+CIPCLOSE");

    Serial.println("AT+CIPCLOSE"); // If this shows on the serial monitor the data did not send.
  }
  // The AT+RST command resets the ESP8266, I'm doing this because data is uploaded much more reliably.
  SoftSer.println("AT+RST");
  // ThingSpeak needs on average, a 15 second delay between updates. Change this to whatever suits your application
  // e.g: delay(600000) is ten minutes.
  delay(15000);
}

}

And the other one is

.
#include <SoftwareSerial.h>
#define SSID "VIVO-5051"  //name of wireless access point to connect to
#define PASS "1604020062"  //wifi password
#define DST_IP "213.186.33.19" // IP of api.pushingbox.com maybe it changes, so take DST_HOST
#define DST_HOST "api.pushingbox.com"
#define LED 13
String devid = "vDABE40AD726780F&tempData=35";  //MUDAR AQUI VALORES PARA TEMPERATURA
String sMessage_1 = "vxyxyxaxjasdasdas";
String sMessage_2 = "vsfdkjfkjsdhfkdsjfh";
String sMessage_3 = "sdkfhshfkdshfksdas";
SoftwareSerial SoftSer(6, 7);
void setup()  //initialise device & connect to access point in setup
{
  pinMode(LED, OUTPUT);

  //softwarereset();
  //  reset();

  SoftSer.begin(9600);
  Serial.begin(9600); // usb serial connects to to pc
  delay(4000);    //wait for usb serial enumeration on 'Serial' & device startup
}

void loop()
{
  String cmd = "AT+CIPSTART=\"TCP\",\"";
  cmd += DST_HOST;
  cmd += "\",80";

  SoftSer.println(cmd);  //send command to device

  delay(2000);  //wait a little while for 'Linked' response - this makes a difference

  cmd =  "GET /pushingbox?devid=";
  cmd += devid;
  cmd += " HTTP/1.1\r\n";  //construct http GET request
  cmd += "Host: api.pushingbox.com\r\n\r\n";
  SoftSer.print("AT+CIPSEND=");
  SoftSer.println(cmd.length());  //esp8266 needs to know message length of incoming message - .length provides this
  if (SoftSer.find(">"))   //prompt offered by esp8266
  {
    SoftSer.println(cmd);  //this is our http GET request
  }
  else
  {
    SoftSer.println("AT+CIPCLOSE");  //doesn't seem to work here?
    Serial.println("No '>' prompt received after AT+CPISEND");
  }

  SoftSer.println("AT+CIPCLOSE");
  delay(15000); //Tempo de demora para publicar mais uma vez
}

What can I do to my arduino connect in those 2 sites in just one code?
Thanks :slight_smile:

Please correct your post above and add code tags around your code:
[code]`` [color=blue]// your code is here[/color] ``[/code].

It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)


you just have to acquire the data and send it to the 2 different places

Sorry about that, So is it wrong if I just copy and paste one below the other?

Start by editing your first post to include the tags...

Done, thanks

(deleted)

Thx looks much better :slight_smile:

So do you understand what’s happening with the lines of code starting with SoftSer.print(?

they are used to send AT commands (like AT+CIPSTART or AT+CIPSEND) to your ESP following a strict grammar to tell the ESP to do something —> you need to study those commands and then just execute them in a row by strictly following the grammar

spycatcher2k:
Connect to one, close the connection,connect to the other, send the same data, close the connection, repeat!

That is what I'm trying to do, but still doesn't work :(.

#include <SoftwareSerial.h>
#define SSID "xxxx"  //name of wireless access point to connect to
#define PASS "xxx"  //wifi password
#define DST_IP "213.186.33.19" // IP of api.pushingbox.com maybe it changes, so take DST_HOST
#define DST_HOST "api.pushingbox.com"
#define LED 13
String devid = "vDABE40AD726780F&tempData=35";  //MUDAR AQUI VALORES PARA TEMPERATURA DO EXCEL
String apiKey = "D29G5QSIL6X7L6NF"; //DADOS PARA THINGSPEAK
SoftwareSerial SoftSer(6, 7);
#include <DHT.h>
#define DHTPIN 5                // Connect the signal pin of DHT11 sensor to digital pin 5
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

void setup()  //initialise device & connect to access point in setup
{
  dht.begin();
  SoftSer.begin(9600);
  Serial.begin(9600); // usb serial connects to to pc
}

void loop()
{
  //-- Parte do DHT11 e transformação--
  float t = dht.readTemperature();
  float h = dht.readHumidity();
  float temp = dht.readTemperature();
  char buf[16]; // buf array can hold up to 16 characters.
  String strTemp = dtostrf(temp, 4, 1, buf); // "buf" will be the actual name of the array and strTemp is being equalled to it as a string.
  String strTemp2 = dtostrf(h, 4, 1, buf);
  // dtostrf(floatvar, StringLengthIncDecimalPoint, numVarsAfterDecimal, charbuf);

  //-- Parte da conexão do thingspeak--
  String cmd2 = "AT+CIPSTART=\"TCP\",\"";
  cmd2 += "184.106.153.149"; // api.thingspeak.com
  cmd2 += "\",80";
  SoftSer.println(cmd2);

  if (SoftSer.find("Error"))
  {
    Serial.println("AT+CIPSTART error");
    return;
  }
  String getStr2 = "GET /update?api_key=";
  getStr2 += apiKey;
  getStr2 += "&field1=";
  getStr2 += String(strTemp);
  getStr2 += "&field2=";
  getStr2 += String(strTemp2);
  getStr2 += "\r\n\r\n";

  // send data length
  cmd2 = "AT+CIPSEND=";
  cmd2 += String(getStr2.length());
  SoftSer.println(cmd2);

  if (SoftSer.find(">"))
  {
    SoftSer.print(getStr2); // Send data.
    SoftSer.println("AT+CIPCLOSE");
    Serial.println("AT+CIPCLOSE");
  }
  else
  {
    SoftSer.println("AT+CIPCLOSE");

    Serial.println("AT+CIPCLOSE"); // If this shows on the serial monitor the data did not send.
  }
  // The AT+RST command resets the ESP8266, I'm doing this because data is uploaded much more reliably.
  SoftSer.println("AT+RST");
    SoftSer.println("AT+CIPCLOSE");
    Serial.println("AT+CIPCLOSE");

  //-- Conexão com o excel !! ------------------
  String cmd = "AT+CIPSTART=\"TCP\",\"";
  cmd += DST_HOST;
  cmd += "\",80";

  SoftSer.println(cmd);  //send command to device

  delay(2000);  //wait a little while for 'Linked' response - this makes a difference

  cmd =  "GET /pushingbox?devid=";
  cmd += devid;
  cmd += " HTTP/1.1\r\n";  //construct http GET request
  cmd += "Host: api.pushingbox.com\r\n\r\n";
  SoftSer.print("AT+CIPSEND=");
  SoftSer.println(cmd.length());  //esp8266 needs to know message length of incoming message - .length provides this
  if (SoftSer.find(">"))   //prompt offered by esp8266
  {
    SoftSer.println(cmd);  //this is our http GET request
  }
  else
  {
    SoftSer.println("AT+CIPCLOSE");  //doesn't seem to work here?
    Serial.println("No '>' prompt received after AT+CPISEND");
  }

  SoftSer.println("AT+CIPCLOSE");
  delay(15000); //Tempo de demora para publicar mais uma vez (no momento está em 35 segundos)
}

Forget coding for the web for the time being

Create a small program printing to the serial console your 3 values from

  float t = dht.readTemperature();
  float h = dht.readHumidity();
  float temp = dht.readTemperature();

once very 5 seconds

And post this code

Then - describe the sequence the AT commands you need to send assuming you have your 3 values. What would the exact AT commands should look like ? And what should the ESP answer every time you send one of those ?

For example you should (possibly) start with a

AT+CIPSTART="TCP","184.106.153.149",80 and not get error

Then
AT+CIPSEND=xxxx where xxx is the length of your ask and get a ‘>’ prompt

Then send
GET /update?api_key=D29G5QSIL6X7L6NF&field1=xxxxxx....

Etc

Like this?

  float t = dht.readTemperature();
  float h = dht.readHumidity();
  float temp = dht.readTemperature();

Serial.println(t);
Serial.println(h);
Serial.println(temp);

String strTemp1 = dtostrf(h, 4, 1, buf);
String strTemp2 = dtostrf(temp, 4, 1, buf);
String strTemp3 = dtostrf(h, 4, 1, buf);

delay (5000);

// Start
  String test = "AT+CIPSTART=\"TCP\",\"";
  test += "184.106.153.149";
  test += "\",80";
  SoftSer.println(test);

//GET
  String getStr = "GET /update?api_key=";
  getStr += apiKey;
  getStr += "&field1=";
  getStr += String(strTemp);
  getStr += "&field2=";
  getStr += String(strTemp2);
  getStr += "&field3=";
  getStr += String(strTemp3);
  getStr += "\r\n\r\n";

//SEND
  cmd = "AT+CIPSEND=";
  cmd += String(getStr.length());
  SoftSer.println(cmd);

No...

I would like to see a program which works without the ESP... just print the 3 variables
Get that first to work and post the whole thingy and what you see in the console

Then in plain text - no code - show me the AT commands

  float t = dht.readTemperature();
  float h = dht.readHumidity();
  float temp = dht.readTemperature();

  Serial.println(t);
  Serial.println(h);
  Serial.println(temp);

delay (5000);

Here it stabilish a conection to that IP
AT+CIPSTART="TCP","184.106.153.149",80

Here it send the lenght of the message
AT+CIPSEND= 64

And here it's to update with my values
GET /update?api_key=D29G5QSIL6X7L6NF&field1=t&field2=h&field3=temp

But I don't know what he needs to answer =/

I actually made it! Here is my code

#include <SoftwareSerial.h>
#define SSID "VIVO-5051"  //name of wireless access point to connect to
#define PASS "1604020062"  //wifi password
#define DST_IP "213.186.33.19" // IP of api.pushingbox.com maybe it changes, so take DST_HOST
#define DST_HOST "api.pushingbox.com"
String devid = "vDABE40AD726780F&tempData=35";  //MUDAR AQUI VALORES PARA TEMPERATURA DO EXCEL
String apiKey = "D29G5QSIL6X7L6NF"; //DADOS PARA THINGSPEAK
SoftwareSerial SoftSer(6, 7);
#include <DHT.h>
#define DHTPIN 5                // Connect the signal pin of DHT11 sensor to digital pin 5
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

void setup()  //initialise device & connect to access point in setup
{
  dht.begin();
  SoftSer.begin(9600);
  Serial.begin(9600); // usb serial connects to to pc
}

void loop()
{
  //-- Parte do DHT11 e transformação--
  float t = dht.readTemperature();
  float h = dht.readHumidity();
  float temp = dht.readTemperature();
  char buf[16]; // buf array can hold up to 16 characters.
  String strTemp = dtostrf(temp, 4, 1, buf); // "buf" will be the actual name of the array and strTemp is being equalled to it as a string.
  String strTemp2 = dtostrf(h, 4, 1, buf);
  // dtostrf(floatvar, StringLengthIncDecimalPoint, numVarsAfterDecimal, charbuf);

  //-- Parte da conexão do thingspeak--
  SoftSer.println("AT+CIPCLOSE");
  delay(5000);
  String cmd2 = "AT+CIPSTART=\"TCP\",\"";
  cmd2 += "184.106.153.149"; // api.thingspeak.com
  cmd2 += "\",80";
  SoftSer.println(cmd2);

  if (SoftSer.find("Error"))
  {
    Serial.println("AT+CIPSTART error");
    return;
  }
  String getStr2 = "GET /update?api_key=";
  getStr2 += apiKey;
  getStr2 += "&field1=";
  getStr2 += String(strTemp);
  getStr2 += "&field2=";
  getStr2 += String(strTemp2);
  getStr2 += "\r\n\r\n";

  // send data length
  cmd2 = "AT+CIPSEND=";
  cmd2 += String(getStr2.length());
  Serial.println (cmd2);
  SoftSer.println(cmd2);

  if (SoftSer.find(">"))
  {
    SoftSer.print(getStr2); // Send data.
    SoftSer.println("AT+CIPCLOSE");
    Serial.println("AT+CIPCLOSE");
  }
  else
  {
    SoftSer.println("AT+CIPCLOSE");

    Serial.println("AT+CIPCLOSE"); // If this shows on the serial monitor the data did not send.
  }
  // The AT+RST command resets the ESP8266, I'm doing this because data is uploaded much more reliably.
//  SoftSer.println("AT+RST");
//    SoftSer.println("AT+CIPCLOSE");
 //   Serial.println("AT+CIPCLOSE");

  //-- Conexão com o excel !! ------------------
  SoftSer.println("AT+CIPCLOSE");
  delay(5000);
  String cmd = "AT+CIPSTART=\"TCP\",\"";
  cmd += DST_HOST;
  cmd += "\",80";

  SoftSer.println(cmd);  //send command to device

  delay(2000);  //wait a little while for 'Linked' response - this makes a difference

  cmd =  "GET /pushingbox?devid=";
  cmd += devid;
  cmd += " HTTP/1.1\r\n";  //construct http GET request
  cmd += "Host: api.pushingbox.com\r\n\r\n";
  SoftSer.print("AT+CIPSEND=");
  SoftSer.println(cmd.length());  //esp8266 needs to know message length of incoming message - .length provides this
  if (SoftSer.find(">"))   //prompt offered by esp8266
  {
    SoftSer.println(cmd);  //this is our http GET request
  }
  else
  {
    SoftSer.println("AT+CIPCLOSE");  //doesn't seem to work here?
    Serial.println("No '>' prompt received after AT+CPISEND");
  }

  SoftSer.println("AT+CIPCLOSE");
  delay(15000); //Tempo de demora para publicar mais uma vez (no momento está em 35 segundos)
}

Thanks for help <3

1 Like

Great !