我正在编写一个家庭物联网项目。主服务器使用Express JS用Node.js编写,所有外围设备都使用ESP8266通过WiFi网络连接到服务器。
我想要完成的事情非常简单。其中一个组件是RGB灯泡。用户可以更改灯泡的颜色或功率(打开和关闭)。
为了更改灯泡的颜色,我想从Node.js服务器向ESP8266发送一个POST请求,其中包含非常简单的主体,包括:
{"red" : 255, "green" : 128, "blue" : 0}
要从Node.js服务器发送请求,我使用request
npm包。请求如下所示:
const options = {
url: 'http://192.168.1.226:80/changeBulbColor',
method: 'POST',
json: {
"red": 255,
"green": 255,
"blue": 255
},
headers: {
"Content-Type": "application/json"
}
};
request(options, callback);
我还尝试了一个版本的请求,我将json
切换为body
并添加了一个额外的字段json: true
。
当我尝试使用curl
发送请求时,一切都很好。这是有效的curl命令:
curl -vv -X POST -d "{"power":false}" "Content-Type: application/json" http://192.168.1.226/changeBulbPower
当我尝试使用request
库并从Node.js服务器发送POST时,来到ESP8266的body
始终为空。当我试图记录身体参数的数量时。我得到了0。
ESP8266的代码:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ArduinoJson.h>
#include <ESP8266HTTPClient.h>
const char* ssid = "ssid";
const char* password = "password";
bool isOn = false;
const int redPin = 0;
const int bluePin = 1;
const int greenPin = 2;
struct Color {
int R;
int G;
int B;
};
bool isInSetupMode = false;
Color* bulbColor;
ESP8266WebServer server(80);
void handleRoot() {
String response = "<html><head></head><body>";
response+= "Lightbulb is ";
response+= ((isOn == 1) ? "on" : "off");
response += "</br>";
response += "Value of red: " + String (bulbColor -> R );
response += "</br>Value of green: " + String (bulbColor -> G );
response += "</br>Value of blue: " + String (bulbColor -> B );
IPAddress myIP = WiFi.localIP();
response+= "<br> Device's IP address: ";
response+= String(myIP[0]);
response+= ".";
response+= String(myIP[1]);
response+= ".";
response+= String(myIP[2]);
response+= ".";
response+= String(myIP[3]);
response+= "</body></html>";
server.send(200,"text/html",response);
}
void handleBulbRequest() {
Serial.println(server.arg("plain"));
Serial.println(server.args());
for(int i = 0; i < server.args(); i++ ) {
Serial.print(server.argName(i) + ": ");
Serial.println(server.arg(i));
}
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(server.arg("plain"));
if(!root.success()){
server.send(400);
return;
}
bulbColor->R = root["red"];
bulbColor->G = root["green"];
bulbColor->B = root["blue"];
if(isOn) {
analogWrite(redPin, bulbColor->R);
analogWrite(greenPin, bulbColor->G);
analogWrite(bluePin, bulbColor->B);
}
server.send(200);
}
void onPowerChange() {
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(server.arg("plain"));
if(!root.success()){
server.send(400);
return;
}
isOn = root["power"];
if(isOn) {
analogWrite(redPin, bulbColor->R);
analogWrite(greenPin, bulbColor->G);
analogWrite(bluePin, bulbColor->B);
} else {
analogWrite(redPin, 1);
analogWrite(greenPin, 1);
analogWrite(bluePin, 1);
}
server.send(200);
}
void onAssignDevice() {
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(server.arg("plain"));
if(!root.success()) {
server.send(400);
return;
}
isInSetupMode = root["highlight"];
server.send(200);
}
void handleNotFound(){
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET)?"GET":"POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i=0; i<server.args(); i++){
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
void setup(void) {
Serial.begin(74880);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
server.on("/", HTTP_GET , handleRoot);
server.on("/changeBulbColor", HTTP_POST, handleBulbRequest);
server.on("/highlightDevice", onAssignDevice);
server.on("/changeBulbPower", onPowerChange);
server.onNotFound(handleNotFound);
bulbColor = new Color();
bulbColor->R = 1;
bulbColor->G = 1;
bulbColor->B = 1;
sendMessageToServerDebug();
server.begin();
}
void loop(void) {
server.handleClient();
if(isInSetupMode) {
digitalWrite(greenPin, HIGH);
delay(250);
digitalWrite(greenPin, LOW);
delay(750);
}
}
void sendMessageToServer() {
HTTPClient http;
http.begin("http://192.168.43.197:4000/registerDevice"); //HTTP
http.addHeader("Content-Type", "application/json");
IPAddress myIP = WiFi.localIP();
String json = "{\"ip\""" + String(myIP[0]) + "." + String(myIP[1]) + "." + String(myIP[2]) + "." + String(myIP[3]) + "\",";
json += "\"mac\""";
json += WiFi.macAddress() + "\",\"deviceType\""LIGHTS\"}";
int httpCode = http.POST(json);
if(httpCode > 0) {
if(httpCode == HTTP_CODE_OK) {
String payload = http.getString();
}
} else {
}
http.end();
}
void sendMessageToServerDebug() {
HTTPClient http;
http.begin("http://192.168.1.79:8080/registerDevice"); //HTTP
http.addHeader("Content-Type", "application/json");
IPAddress myIP = WiFi.localIP();
String json = "{\"ip\""" + String(myIP[0]) + "." + String(myIP[1]) + "." + String(myIP[2]) + "." + String(myIP[3]) + "\",";
json += "\"mac\""";
json += WiFi.macAddress() + "\",\"deviceType\""LIGHTS\"}";
int httpCode = http.POST(json);
if(httpCode > 0) {
if(httpCode == HTTP_CODE_OK) {
String payload = http.getString();
}
} else {
}
http.end();
}