code is working but data is not receiving at thingspeak
古いコメントを表示
My code is working properly but i am not recevind the data at thingspeak if i can get any help ?? here is the code
#include <SoftwareSerial.h>
#include <SimpleDHT.h>
#include <Wire.h>
#include "ThingSpeak.h"
#include <LiquidCrystal_I2C.h>
#include <dht11.h>
#define RX 3
#define TX 2
#define dht_apin 11 // Analog Pin sensor is connected to
dht11 dhtObject;
String AP = "Khan"; // AP NAME
String PASS = "Pakistan2"; // AP PASSWORD
String API = "xxxxxxxxx"; // Write API KEY
String HOST = "api.thingspeak.com";
//String HOST = "184.106.153.149";
String PORT = "80";
String field = "field1";
int countTrueCommand;
int countTimeCommand;
boolean found = false;
int valSensor = 1;
int pinDHT11 = 11;
SimpleDHT11 dht11(pinDHT11);
LiquidCrystal_I2C lcd(0x27, 16, 2);
SoftwareSerial esp8266(RX,TX);
void setup() {
Serial.begin(9600);
esp8266.begin(115200);
sendCommand("AT",5,"OK");
sendCommand("AT+CWMODE=1",5,"OK");
sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");
}
void loop()
{
String getData = "GET /update?api_key="+ API +"&field1="+getTemperatureValue()+"&field2="+getHumidityValue();
sendCommand("AT+CIPMUX=1",5,"OK");
sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
sendCommand("AT+CIPSEND=0," +String(getData.length()+4),4,">");
esp8266.println(getData);delay(2000);countTrueCommand++;
sendCommand("AT+CIPCLOSE=0",5,"OK");
}
String getTemperatureValue(){
dhtObject.read(dht_apin);
Serial.print(" Temperature(C)= ");
int temp = dhtObject.temperature;
Serial.println(temp);
delay(50);
return String(temp);
}
String getHumidityValue(){
dhtObject.read(dht_apin);
Serial.print(" Humidity in %= ");
int humidity = dhtObject.humidity;
Serial.println(humidity);
delay(50);
return String(humidity);
}
void sendCommand(String command, int maxTime, char readReplay[]) {
Serial.print(countTrueCommand);
Serial.print(". at command => ");
Serial.print(command);
Serial.print(" ");
lcd.begin();
// Turn on the blacklight and print a message.
lcd.backlight();
lcd.print("Tempature ");
lcd.print((String)getTemperatureValue());
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Humidity ");
lcd.print((String)getHumidityValue());
lcd.print("%");
delay(1000);
while(countTimeCommand < (maxTime*1))
{
esp8266.println(command);//at+cipsend
if(esp8266.find(readReplay))//ok
{
found = true;
break;
}
countTimeCommand++;
}
if(found == true)
{
Serial.println("OYI");
countTrueCommand++;
countTimeCommand = 0;
}
if(found == false)
{
Serial.println("Fail");
countTrueCommand = 0;
countTimeCommand = 0;
}
found = false;
}
7 件のコメント
Christopher Stapels
2022 年 5 月 17 日
編集済み: Christopher Stapels
2022 年 5 月 18 日
I would look at the examples in the ThingSpeak library, which you are using. Try hardcoding a value to send to ThingSpeak, to remove issues of the sensors.
I would not use a static ip, and you also have that line commented out, so I dont think the code you shared ywould compile.
If you are still stuck after simplifying, make sure you share what you have tried and what you see on the serial monitor.
Sarfaraz Khan
2022 年 5 月 18 日
Christopher Stapels
2022 年 5 月 18 日
Any luck using the examples?
Sarfaraz Khan
2022 年 5 月 18 日
Christopher Stapels
2022 年 5 月 18 日
Sarfaraz Khan
2022 年 5 月 18 日
Christopher Stapels
2022 年 5 月 18 日
What happened when you tried the commands in that example? Can you show the output?
回答 (1 件)
Allam Bharath kumar
2022 年 5 月 18 日
編集済み: Christopher Stapels
2022 年 5 月 19 日
Same problem for me also with python program.
import paho.mqtt.publish as publish
import time
import random
server = "mqtt.thingsepak.com"
port = 1883
channel_id = "xxxxxxxxxxxxxx"
write_api_key = "xxxxxxxxxxx"
topic = "channels/"+channel_id+"/publish/"+write_api_key
mqtt_username = 'xxxxxxxxx'
mqtt_password = 'xxxxxxxxxxxxxx'
mqtt_auth = { 'username': mqtt_username, 'password': mqtt_password }
while True:
h = random.randint(30,100)
t = random.randint(10,50)
data = "field1="+str(t)+"&field2="+str(h)
publish.single(topic, data, qos=0, retain=False, hostname=server,
port=port, client_id=mqtt_username, keepalive=60, will=None, auth=mqtt_auth,
tls=None)
time.sleep(15)
1 件のコメント
Your code needs to be updated. Start with the example here. For starters, your server is incorrect: "mqtt.thingsepak.com" would never have worked.
コミュニティ
その他の回答 ThingSpeak コミュニティ
カテゴリ
ヘルプ センター および File Exchange で Read Data from Channel についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
