A place users can post their projects. If you have a small project and would like your own dedicated place to post and have others chat about it then this is your spot.

User avatar
By ketsayay
#20114 i can't sand data to thingspeak with this code

#‎include‬<TEE_ESP_WIFI.h>
#include <SoftwareSerial.h>
‪#‎define‬ pinEN 9
#define ESP_Rx 10
#define ESP_Tx 11
#include "DHT.h"
DHT dht;
String apiKey = "3699EKAIV6AFTZ27";
String ssid="kimui";
String pwd="yaranika";
ESP wifi(ESP_Rx,ESP_Tx,pinEN);
SoftwareSerial ser(10, 11);
void print_debug(String data)
{ Serial.print(data);}
void setup()
{
dht.setup(13);
Serial.begin(9600);
wifi.begin(9600);
// ser.begin(9600); without this line i can connect to ap
Serial.println("ESP8226");
wifi.Event_debug = print_debug;
wifi.reset();
wifi.setmode(STATION);
wifi.disconnectAP();
wifi.connectAP(ssid,pwd);
wifi.multipleconnect(MULTIPLE);
String ip = wifi.myip();
}
void loop()
{
float temp = dht.getTemperature();

// convert to string
char buf[16];
String strTemp = dtostrf(temp, 4, 1, buf);
Serial.println(strTemp);
// TCP connection
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += "184.106.153.149"; // api.thingspeak.com
cmd += "\",80";
ser.println(cmd);
if(ser.find("Error")){
Serial.println("AT+CIPSTART error");
return;
}
// prepare GET string
String getStr = "GET /update?api_key=";
getStr += apiKey;
getStr +="&field1=";
getStr += String(strTemp);
getStr += "\r\n\r\n";
// send data length
cmd = "AT+CIPSEND=";
cmd += String(getStr.length());
ser.println(cmd);
if(ser.find(">")){
ser.print(getStr);
}
else{
ser.println("AT+CIPCLOSE");
// alert user
Serial.println("AT+CIPCLOSE");
}
// thingspeak needs 15 sec delay between updates
delay(16000);

}