receive and export data at the same time

조회 수: 4 (최근 30일)
best
best 2024년 1월 17일
댓글: Rene Chan 2024년 1월 23일
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
Christopher Stapels
Christopher Stapels 2024년 1월 18일
Can you share some relevant parts of the code?
best
best 2024년 1월 18일
편집: Christopher Stapels 2024년 1월 18일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Christopher Stapels
Christopher Stapels 2024년 1월 19일
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
best
best 2024년 1월 20일
편집: best 2024년 1월 20일
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 2024년 1월 23일
I think you closed the connection in the first function with:
esp.println("AT+CIPCLOSE");

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Christopher Stapels
Christopher Stapels 2024년 1월 18일
이동: Christopher Stapels 2024년 1월 18일
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
best
best 2024년 1월 18일
편집: best 2024년 1월 18일
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;
}

댓글을 달려면 로그인하십시오.

커뮤니티

더 많은 답변 보기:  ThingSpeak 커뮤니티

카테고리

Help CenterFile Exchange에서 Read Data from Channel에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by