WTA : nodeMCU DHT11/22 send to EasyIoT server

8 years 11 months ago - 8 years 11 months ago #1488 by lesjaw

EasyIoT wrote: In LUA example you can see how to send data to EasyIoT server:

iot-playground.com/2-uncategorised/36-es...demcu-lua-no-arduino

For analog signal use ControlLevel. For example:
http://192.168.1.8/Api/EasyIoT/Control/Module/Esp8266/N1S12/ControlLevel/14.5/


Hi EasyIoT,

Yes..2 days i haven't sleep, i'm comparing all the code on how to send data to EasyIoT server.. i have figure it..there are two method i saw..

right now i'm trying the method how iot-playground.com/2-uncategorised/40-es...y-switch-arduino-ide ..

i have seen server send data of the gpi0 value.. (in automation)
private void sendCommand(string value)
{
  sendToServer("/gpio/"+value);
}
private void sendToServer(String message)
{
  try
  {
  //Console.WriteLine("TCP client command:" + message);
   Int32 port = 80;
   System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient( ESP8266_IP_ADDRESS, port);
   Byte[] data = System.Text.Encoding.ASCII.GetBytes(message); 
   System.Net.Sockets.NetworkStream stream = client.GetStream();
   stream.Write(data, 0, data.Length);
   // Close everything.
   stream.Close();         
   client.Close();
  }
  catch(Exception e)
  {
    Console.WriteLine(e.StackTrace);
  }
}
then the esp8266 code accepting the value
// Wait until the client sends some data
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }
  
  // Read the first line of the request
  String req = client.readStringUntil('\r');
  Serial.println(req);
  client.flush();
  
  // Match the request
  int val;
  if (req.indexOf("/gpio/0") != -1)
    val = 0;
  else if (req.indexOf("/gpio/1") != -1)
    val = 1;
  else {
    Serial.println("invalid request");
    client.stop();
    return;
  }

  // Set GPIO2 according to the request
  digitalWrite(2, val);
  
  client.flush();
and this is how esp8266 code sending value to server right?
// Prepare the response
  String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now ";
  s += (val)?"high":"low";
  s += "</html>\n";

  // Send the response to the client
  client.print(s);
  delay(1);
  Serial.println("Client disonnected");

i will try to send the value of temp and hum from esp8266 directly to server, without server need to send data..

but i wonder where the code (in automati0n) of accepting value from esp8266?

still working on it now...

btw i will try
http://192.168.1.8/Api/EasyIoT/Control/Module/Esp8266/N1S12/ControlLevel/14.5/

it seem so much easier..
thanks EasyIoT

Please Log in or Create an account to join the conversation.

8 years 11 months ago - 8 years 11 months ago #1490 by Dennis
hello,
the server>>sensor part looks right, but...

you do not need any custom code in eâsyiot server to accept data sent from sensor to server - instead, the sensor connects to the servers built-in internal REST API via http request, and it needs to authenticate the request using basic auth.

I too recommend to check the LUA example provided by Mr. easyIot, there you can see pretty good how to connect using basic auth.
iot-playground.com/2-uncategorised/36-es...demcu-lua-no-arduino


regards (and good luck, i am familiar with the no-sleep problem) :)

Please Log in or Create an account to join the conversation.

8 years 11 months ago - 8 years 11 months ago #1492 by lesjaw
so i have made a progress..
/*
  Created by Igor Jarc
 See http://iot-playground.com for details
 Please use community fourum on website do not contact author directly
 
 Code based on https://github.com/DennisSc/easyIoT-ESPduino/blob/master/sketches/ds18b20.ino
 
 External libraries:
 - https://github.com/adamvr/arduino-base64
 - https://github.com/milesburton/Arduino-Temperature-Control-Library
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 version 2 as published by the Free Software Foundation.
 */
#include <ESP8266WiFi.h>
#include <DHT.h>
#include <Base64.h>

const char* ssid = "MiFi";
const char* password = "nexusdroit";
const char* host = "192.168.5.157";
const char* nodet = "N1S0";
const char* nodeh = "N2S0";
const char* user = "admin";
const char* userpassword = "test";

// Create an instance of the server
// specify the port to listen on as an argument
//WiFiServer server(80);


#define DHTPIN 2 // what pin we're connected to
 
DHT dht(DHTPIN, DHT11,15);

#define USER_PWD_LEN 40
char unameenc[USER_PWD_LEN];

void setup() {
  Serial.begin(115200);
  delay(10);
  dht.begin();
  
 // Connect to WiFi network
  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(WiFi.localIP());
  
  char uname[USER_PWD_LEN];
  String str = String(user)+":"+String(userapassword);  
  str.toCharArray(uname, USER_PWD_LEN); 
  memset(unameenc,0,sizeof(unameenc));
  base64_encode(unameenc, uname, strlen(uname));
}

void loop() {
  Serial.print("Connecting to ");
  Serial.println(host);
  
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }
  
  float h = dht.readHumidity();
  
  if (isnan(h)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
 
 String urlh = "";
  urlh += "/Api/EasyIoT/Control/Module/Virtual/"+ String(nodeh) + "/ControlLevel/"+String(h); // generate EasIoT server node URL

  Serial.print("POST data to URL: ");
  Serial.println(urlh);
 
  client.print(String("POST ") + urlh + " HTTP/1.1\r\n" +
               "Host: " + String(host) + "\r\n" + 
               "Connection: close\r\n" + 
               "Authorization: Basic " + unameenc + " \r\n" + 
               "Content-Length: 0\r\n" + 
               "\r\n");
  delay(1000);
  
    // Read all the lines of the reply from server and print them to Serial
//    while(client.available()){
//   String line = client.readStringUntil('\r');
//    Serial.print(line);
//  }
  
  float t = dht.readTemperature();
  
  if (isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  
  String urlt = "";
  urlt += "/Api/EasyIoT/Control/Module/Virtual/"+ String(nodet) + "/ControlLevel/"+String(t); // generate EasIoT server node URL

  Serial.print("POST data to URL: ");
  Serial.println(urlt);
  
  client.print(String("POST ") + urlt + " HTTP/1.1\r\n" +
               "Host: " + String(host) + "\r\n" + 
               "Connection: close\r\n" + 
               "Authorization: Basic " + unameenc + " \r\n" + 
               "Content-Length: 0\r\n" + 
               "\r\n");
  delay(1000);
  
  // Read all the lines of the reply from server and print them to Serial
    while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }

  Serial.println();
  Serial.println("closing connection");
  
  // Repeat every 60 seconds
  delay(60000);
}

this is the serial output
Connecting to MiFi
......
WiFi connected
192.168.5.178
Connecting to 192.168.5.157
POST data to URL: /Api/EasyIoT/Control/Module/Virtual/N2S0/ControlLevel/68.0
POST data to URL: /Api/EasyIoT/Control/Module/Virtual/N1S0/ControlLevel/29.0
HTTP/1.1 200 OK
Content-Length: 27
Content-Type: application/json
Server: HttpGateway Microsoft-HTTPAPI/2.0
Date: Wed, 13 May 2015 16:44:23 GMT
Connection: close

[{ "ResponseValue" : "1" }]
closing connection

but only the first POST of URL get updated in EasyIoT server which is n this case is

/Api/EasyIoT/Control/Module/Virtual/N2S0/ControlLevel/68.0
POST data to URL:

other node N1S0 didnot get the data update..

why?

and what is the meaning of [{ "ResponseValue" : "1" }] ?

thanks

Please Log in or Create an account to join the conversation.

8 years 11 months ago #1493 by Dennis
i think you should try to read the dht22/11 a few times before sending data, because the first read sometimes gives garbage. But im just guessing here...

regards

Please Log in or Create an account to join the conversation.

8 years 11 months ago #1494 by lesjaw

Dennis wrote: hello,
the server>>sensor part looks right, but...

you do not need any custom code in eâsyiot server to accept data sent from sensor to server - instead, the sensor connects to the servers built-in internal REST API via http request, and it needs to authenticate the request using basic auth.

I too recommend to check the LUA example provided by Mr. easyIot, there you can see pretty good how to connect using basic auth.
iot-playground.com/2-uncategorised/36-es...demcu-lua-no-arduino


regards (and good luck, i am familiar with the no-sleep problem) :)


Hello Dennis..

yes..right now i'm using the suggested method, but i have problem.. because there 2 value i need to send to 2 node.. but it seem only can run one POST..

btw i also looking at your nodeMCU Lua method to send data to EasyIoT server.. from what i saw you are making it like this :

in esp8266
1. create a server
2. make listen to some port
3. getting data of the sensor and label it (GETTEMP and GETHUM)

in EasyIoT server
1. create automation for a node
2. making that automation request data for a label (GETTEMP or GETHUM) with a spesific port

Please Log in or Create an account to join the conversation.

8 years 11 months ago #1495 by lesjaw

Dennis wrote: i think you should try to read the dht22/11 a few times before sending data, because the first read sometimes gives garbage. But im just guessing here...

regards


yes but thats another case, because already get the right data to send..

the problem is sending the data to EasyIoT server.. i already make 2 POST into the right folder..but only one got executed/updated by EasyIoT server

Please Log in or Create an account to join the conversation.

Time to create page: 0.488 seconds

Forum latest

  • No posts to display.