Newbie questions??
Posted: Mon Dec 26, 2016 9:55 am
Hi guys,
I'm new to ESP8266. I've got nodeMCU ESP12E V1.0: http://www.ebay.com/itm/V1-0-NodeMcu-LU ... 1393372661
My task is to send data from ESP (get temperature and humidity from DHT22) to Intel Edison server (node.JS server). I'm using Arduino IDE to code. First, let see my code:
Client (ESP8266):
Only for sending data, not receive anything!
The server-side code is:
The server is run correctly, because when I open the browser, then type "192.168.1.102:3000/25/30" to test the server, the terminal logout "25" and "30". But when I use the ESP, it's not work!
How can I use the ESP to send HTTP request the same way the browser does?
I'm new to ESP8266. I've got nodeMCU ESP12E V1.0: http://www.ebay.com/itm/V1-0-NodeMcu-LU ... 1393372661
My task is to send data from ESP (get temperature and humidity from DHT22) to Intel Edison server (node.JS server). I'm using Arduino IDE to code. First, let see my code:
Client (ESP8266):
Only for sending data, not receive anything!
Code: Select all
#include <DHT.h>
#include <ESP8266WiFi.h>
#define DHTPIN 5
#define DHTTYPE DHT22
const char* ssid = "xxxxxx";
const char* password = "xxxxxxxx";
const char* host = "xxxxxxxxxxxxxxxx";
const char* streamId = "....................";
const char* privateKey = "....................";
WiFiClient client;
DHT dht(DHTPIN,DHTTYPE);
void setup() {
Serial.begin(115200);
delay(100);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
const int httpPort = 3000;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
else Serial.println("Server connected!");
dht.begin();
}
void loop(){
float t = dht.readTemperature();
float h = dht.readHumidity();
int temp = (int) (t*100);
int humid = (int) (h*100);
String tempchar = String(temp);
String humidchar = String(humid);
String packet = "GET /sensor/" + tempchar+ "/" + humidchar + " HTTP/1.1";
client.println(packet);
Serial.println(packet);
delay(2000);
}
The server-side code is:
Code: Select all
var express = require('express');
var app = express();
app.listen(3000);
app.get('/', function (req,res) {
console.log('connected');
})
app.get('/sensor/:temp/:hmdt', function(req, res) {
var t = req.params.temp;
var h = req.params.hmdt;
console.log(t);
console.log(h);
});
The server is run correctly, because when I open the browser, then type "192.168.1.102:3000/25/30" to test the server, the terminal logout "25" and "30". But when I use the ESP, it's not work!
How can I use the ESP to send HTTP request the same way the browser does?