带有Arduino Uno的ESP8266没有连接并向服务器Ubidots发送数据。
我需要像Ubidots一样将数据发送到服务器,我需要你的帮助。下面的代码,我正在使用,我没有发布数据,也无法连接到服务器。
我正在使用AT命令连接到服务器。我的ESP8266可以连接到WiFi并可以将数据发送到ThingSpeak服务器。由于我需要在我的项目中添加GPS,所以现在我正在使用Ubidots,但我的问题是我无法连接并将数据发布到服务器。
#include <DHT.h>
#include <SoftwareSerial.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(13,12,11,10,9,8);
SoftwareSerial esp(2, 3); //RX,TX
#define DHTPIN 5 // Digital Pin 5
#define DHTTYPE DHT11 // We are Using DHT11
DHT dht(DHTPIN, DHTTYPE); // Initialising Pin and Type of DHT
String ssid = "JioFi2_05"; //wifi name
String password = "80199102"; //wifi password
String server = "things.ubidots.com"; //ubidots server name
String token = "A1E-IcAmTnS42aTf79zQsmiNny"; //ubidots token
String esp8266_d_id = "5a8adf76c03fee"; //esp8266device id
String temperature_id = "5a8e9268c0ebcfede"; //temperature variable id
String humidity_id = "5a8a73fb8083e6b"; //humidity variable id
String heartbeat_id = "5a83f614e9049"; //heartbeat variable id
void connectWifi() //wificonnection
{
String cmd = "AT+CWJAP=\"" +ssid+"\",\"" + password + "\"";
esp.println(cmd);
delay(4000);
if(esp.find("OK")){
Serial.println("WIFI Connected!");
lcd.setCursor(0, 0);
lcd.println("WIFI Connected!");
delay(1000);
}
else {
connectWifi();
Serial.println("Cannot connect to wifi");
}
}
void reset() {
esp.println("AT+RST");
delay(1000);
if(esp.find("OK") ) Serial.println("Module Reset");
}
void esp_buad_rate_change()
{
esp.println("AT+CIOBAUD=9600"); //change buad rate
Serial.println("AT+CIOBAUD=9600");
}
void wifisetup()
{
esp.println("AT");
esp.println("AT+CWMODE=3");
Serial.println("Wifi ready");
}
void ubi_send_data(String, String);
void setup()
{
Serial.begin(115200);
esp.begin(115200);
reset();
esp_buad_rate_change(); //changing buad rate
Serial.begin(9600);
esp.begin(9600);
lcd.begin(16,2);
lcd.clear();
lcd.setCursor(0,0);
lcd.println("Wifi Setup");
wifisetup(); //wifi setup
connectWifi();
}
void loop() {
float hum = dht.readHumidity(); // Reading Humidity Value
float tem = dht.readTemperature(); // Reading Temperature Value
ubi_send_data(String(temperature_id), String(tem));
ubi_send_data(String(humidity_id), String(hum));
ubi_send_data(String(heartbeat_id), String(heart));
}
void ubi_send_data(String id, String values){
top:
String cmd = "AT+CIPSTART=\"TCP\",\"" + server + "\",80";
esp.println(cmd);
delay(3000);
if(esp.find("OK"))
{
Serial.println("server found");
String cal="{\"value\": "+values+"}";
int len =cal.length();
String post = "POST /api/v1.6/variables/"+id+"/values HTTP/1.1\r\n";
post+= "Content-Type: application/json\r\n";
post+= "Content-Length: ";
post+= String(len)+"\r\n";
post+= "X-Auth-Token: "+token+"\r\n";
post+= "Host: things.ubidots.com\r\n\r\n";
post+= "{\"value\": "+values+"}\r\n\r\n";
String sendData = "AT+CIPSEND="+post;
esp.print(sendData);
Serial.println(sendData);
delay(6000);
if (esp.find(">"))
{
Serial.println("SENDING data");
}
else
{
Serial.println("NOT SENDING");
esp.println("AT+CIPCLOSE");
}
}
else
{
Serial.println("server not found");
goto top;
}
}
答案 0 :(得分:0)
请问您为什么使用AT命令?
您可以尝试实现一个简单的Arduino / ESP8266 Ubidots HTTP客户端。