receive and export data at the same time

4 views (last 30 days)
I have two different data that I need to get from the internet and send to the internet. When I use Thingspeak, it only performs the function of one of them (the one that is written first in the code). I cannot make two operations on the same code. Is there a way to do this?( I am using arduino UNO)
  2 Comments
Christopher Stapels
Christopher Stapels on 18 Jan 2024
Can you share some relevant parts of the code?
best
best on 18 Jan 2024
Edited: Christopher Stapels on 18 Jan 2024
Serial.println(nema()); //TO SEND DATA
Serial.println(hum()); //TO GET DATA
}
String nema(){
String veri = "GET https://api.thingspeak.com/update?api_key=XXXXXXXXXXXXXXXXX";
veri += "&field1=";
veri += String(nem3);
veri += "\r\n\r\n";
esp.print("AT+CIPSEND=");
esp.println(veri.length()+2);
delay(2000);
if(esp.find(">")){
esp.print(veri);
Serial.println(veri);
Serial.println("Veri gonderildi.");
delay(1000);
}
Serial.println("Baglantı Kapatildi.");
esp.println("AT+CIPCLOSE");
delay(1000);
}
String hum()
{
String rest = "AT+CIPSEND=90";
rest += "\r\n";
sendData(rest, 2000, 0);
String hostt = "GET /apps/thinghttp/send_request?api_key=YYYYYYYYYYYYYYYYYYY";
hostt += "\r\n";
hostt += "Host:api.thingspeak.com";
hostt += "\r\n\r\n\r\n\r\n\r\n";
String hum = sendData(hostt, 2000, 1);
return (hum);
I can see both of functions are working, tested with serial.print. one of the ***api key lines is working and the other get ERROR on the monitor. "
***beginning with GET
""which one is first, it works only.""" Thanks.

Sign in to comment.

Accepted Answer

Christopher Stapels
Christopher Stapels on 19 Jan 2024
I think your questions are fundamentally programming questions, they dont have much to do with ThingSpeak exactly. I reccomend you start over with your code, have it do one simple thing at a time, and make sure you understand what it is doing and why. If you want to practice with ThingSpeak, you can use the API syntax from the API keys tab of your channel view to change the data in your channel and to read it back. Then once you can make a function in your code that does that predictably, then you can add the second function and build up from there. Also I still wonder what action the ThingHTTP is doing for you. I guess you said at the top that it is fetching some other data form the internet. You can probably use a different route to do that more efficiently, perhaps using MATLAB code and TimeControl.
  2 Comments
best
best on 20 Jan 2024
Edited: best on 20 Jan 2024
As you said, I reviewed it again and solved the problem in a very funny way. I added the necessary command to reconnect ESP to the beginning of the 2nd function and it was solved. Even if there is no shutdown command in the first function, ESP shuts down itself and it is necessary to activate it again. Thanks very much for your help.
Rene Chan
Rene Chan on 23 Jan 2024
I think you closed the connection in the first function with:
esp.println("AT+CIPCLOSE");

Sign in to comment.

More Answers (1)

Christopher Stapels
Christopher Stapels on 18 Jan 2024
Moved: Christopher Stapels on 18 Jan 2024
You should be able to complete both operationg in the same code. I have succeeded in both of these in the same code before (though not with AT commands)
There are functions to interact with ThingSpeak by writing AT commands in the ThingSpeak library. That might help you somewhat.
You have provided the functions, but you arent showing how you are calling the functions, so I'm not sure how they are triggered.
Also, I dont know what your ThingHTTP does. If it writes to the same channel as the first function, it would be denied if they are too close in time.
I think there are very many parts to your question. I would reccommend simplyfying your process and testing just one thing at a time.
  1 Comment
best
best on 18 Jan 2024
Edited: best on 18 Jan 2024
Almost all of the code is like this: (I tried to explain it in the images below.)
String networkname = "wifi-22222";
String networkpassword ="11111111";
String ip = "184.106.153.149";
SoftwareSerial esp(10, 11);
const int prob = A0;
float nem, nem2, nem3;
int ledPin = 9;
void setup() {
Serial.begin(9600);
Serial.println("Initializing Connection");
esp.begin(9600);
esp.println("AT");
Serial.println("AT Command Sent");
while(!esp.find("OK")){
esp.println("AT");
Serial.println("Failed to Connect to ESP8266.");
}
Serial.println("OK Answer Received");
esp.println("AT+CWMODE=1");
while(!esp.find("OK")){
esp.println("AT+CWMODE=1");
Serial.println("Setting Module Type..");
}
Serial.println("Set to client");
Serial.println("Connecting to the Network...");
esp.println("AT+CWJAP=\""+networkname+"\",\""+networkpassword+"\"");
while(!esp.find("OK"));
Serial.println("Connected to the Network.");
delay(1000);
}
void loop() {
esp.println("AT+CIPSTART=\"TCP\",\""+ip+"\",80");
if(esp.find("Error")){
Serial.println("AT+CIPSTART Error");
}
delay(500);
nem=analogRead(prob);
delay(500);
nem2=nem/10.2;
nem3=100-nem2;
Serial.println(nem3);
String data = "GET https://api.thingspeak.com/update?api_key=XXXXXXXXXXXXXXXXX";
data += "&field1=";
data += String(nem3);
data += "\r\n\r\n";
esp.print("AT+CIPSEND=");
esp.println(veri.length()+2);
delay(2000);
if(esp.find(">")){
esp.print(data);
Serial.println(data);
Serial.println("Data sent.");
delay(1000);
}
Serial.println("Connection Closed.");
esp.println("AT+CIPCLOSE");
delay(1000);
Serial.println(hum());
}
String hum()
{
String rest = "AT+CIPSEND=90";
rest += "\r\n";
sendData(rest, 2000, 0);
String hostt = "GET /apps/thinghttp/send_request?api_key=YYYYYYYYYYYYYYYYYYY";
hostt += "\r\n";
hostt += "Host:api.thingspeak.com";
hostt += "\r\n\r\n\r\n\r\n\r\n";
String hum = sendData(hostt, 2000, 1);
int baslangic_2=hum.indexOf('%');
hum=hum.substring(baslangic_2,baslangic_2-2);
int a = hum.toInt();
return (hum);
}
String sendData(String komut, const int zamangecen, boolean debug)
{
String response = "";
esp.print(komut); // komut satırını espnin çıkışına yazdırıyoruz
long int Zaman = millis();
while ( (Zaman + zamangecen) > millis())
{
while (esp.available())
{
char c = esp.read();
response += c; //
}
}
if (debug)
{
Serial.print(response);
}
return response;
}

Sign in to comment.

Communities

More Answers in the  ThingSpeak Community

Categories

Find more on Read Data from Channel in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!