我正在使用通过 SoftwareSerial 连接到 Arduino Uno 的 ESP8266 向 API 发出发布请求。 ESP8266 能够连接到 WIFI 连接。当我尝试将一些数据(模块 DHT22 捕获的温度值)发送到服务器时,我总是被拒绝访问。当执行命令 "esp.println(tcpstart)" 并且我在串行监视器中收到响应 "--> 无法启动 TCP 连接" 时,总是会发生这种情况,这要归功于我添加的打印品。 “tcpstart” 是 AT 命令“AT+CIPSTART”。 Arduino Uno 刷入固件:ai-thinker-0.9.5.2-115200.bin。 这是我的代码:
#include <SoftwareSerial.h>
#include <DHT.h>;
#define RX_PIN 2
#define TX_PIN 3
#define DHTTYPE DHT22
#define DHTPIN 7
SoftwareSerial esp(RX_PIN, TX_PIN); // Rx, Tx
DHT dht(DHTPIN, DHTTYPE);
float temp;
String ssid = "XXXXX";
String pass = "YYYYY";
//Complete API URL to do the post: https://api.test.com:bbbb/zzz/zzzz/v2/z?zzzzzzz
String server = "https://api.test.com";
String ip = "aa.aaa.aa.aa";
int port = bbbb;
String uri = "/zzz/zzzz/v2/z?zzzzzzz";
bool connection;
String data;
String* response;
void setup(){
Serial.begin(9600);
esp.begin(115200);
esp.setTimeout(5000);
dht.begin();
}
void loop(){
reset();
readSensors();
while(connection == false)
connectWifi();
connection = false;
httppost();
}
void readSensors(){
temp = dht.readTemperature();
data = String(temp);
}
void reset(){
esp.println("AT+RST");
delay(300);
esp.println("AT+CWMODE=1");
delay(300);
esp.println("AT+RST");
delay(300);
esp.println("AT+CIPMUX=0");
delay(300);
}
void connectWifi(){
String cmd = "AT+CWJAP=\"" + ssid + "\",\"" + pass + "\"";
delay(300);
esp.println(cmd);
if(esp.find("OK")){
Serial.println("Wifi Connected");
connection = true;
}else{
Serial.println("--> Wifi not Connected");
connection = false;
}
}
void httppost(){
String postRequest =
"POST " + uri + " HTTP/1.1\r\n" +
"Host: " + server + ":" + port + "\r\n" +
"Accept: *" "/" "*\r\n" +
"Content-Length: " + data.length() + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"\r\n" +
data;
String tcpStart = "AT+CIPSTART=\"TCP\",\"" + ip + "\"," + port;
String sendCmd = "AT+CIPSEND=" + postRequest.length();
esp.println(tcpStart); //start TCP connection
delay(300);
if(esp.find("OK")){
Serial.println("TCP connection OK");
esp.println(sendCmd); //send the data over TCP connection
delay(300);
if(esp.find(">")){
Serial.println("Sending packet");
esp.println(postRequest);
delay(300);
if(esp.find("SEND OK")){
Serial.println("Packet sent");
while(esp.available()){ //number of bytes/char available for reading
char tmpResp = esp.read(); //read one char at the time
Serial.print(tmpResp);
if (tmpResp == '\0') continue; //terminate the while when end of the data
}
esp.println("AT+CIPCLOSE"); //close TCP connection
}else{
Serial.println("--> An error occured while sending packet");
}
}else{
Serial.println("--> ESP8266 is not listening for incoming data");
}
}else{
Serial.println("--> Cannot initiate TCP connection");
}
}
你知道它可能是什么问题吗?
最佳答案
试试这段代码。
#include <SoftwareSerial.h>
const byte rxPin = 2;
const byte txPin = 3;
SoftwareSerial ESP8266 (rxPin, txPin);
unsigned long lastTimeMillis = 0;
void setup() {
Serial.begin(9600);
ESP8266.begin(9600);
delay(2000);
}
void printResponse() {
while (ESP8266.available()) {
Serial.println(ESP8266.readStringUntil('\n'));
}
}
void loop() {
if (millis() - lastTimeMillis > 30000) {
lastTimeMillis = millis();
ESP8266.println("AT+CIPMUX=1");
delay(1000);
printResponse();
ESP8266.println("AT+CIPSTART=4,\"TCP\",\"192.168.1.19\",80");
delay(1000);
printResponse();
String cmd = "GET /test.html HTTP/1.1";
ESP8266.println("AT+CIPSEND=4," + String(cmd.length() + 4));
delay(1000);
ESP8266.println(cmd);
delay(1000);
ESP8266.println("");
}
if (ESP8266.available()) {
Serial.write(ESP8266.read());
}
}
关于c - Arduino 从服务器问题中得到响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57690525/
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po
尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub
我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search
由于fast-stemmer的问题,我很难安装我想要的任何rubygem。我把我得到的错误放在下面。Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingfast-stemmer:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcreatingMakefilemake"DESTDIR="cleanmake"DESTDIR=
最近,当我启动我的Rails服务器时,我收到了一长串警告。虽然它不影响我的应用程序,但我想知道如何解决这些警告。我的估计是imagemagick以某种方式被调用了两次?当我在警告前后检查我的git日志时。我想知道如何解决这个问题。-bcrypt-ruby(3.1.2)-better_errors(1.0.1)+bcrypt(3.1.7)+bcrypt-ruby(3.1.5)-bcrypt(>=3.1.3)+better_errors(1.1.0)bcrypt和imagemagick有关系吗?/Users/rbchris/.rbenv/versions/2.0.0-p247/lib/ru
在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo
我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie
当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub