- Posts: 55
- Thank you received: 6
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/
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);
}
}
// 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();
// 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");
http://192.168.1.8/Api/EasyIoT/Control/Module/Esp8266/N1S12/ControlLevel/14.5/
Please Log in or Create an account to join the conversation.
Please Log in or Create an account to join the conversation.
/*
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);
}
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
Please Log in or Create an account to join the conversation.
Please Log in or Create an account to join the conversation.
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)
Please Log in or Create an account to join the conversation.
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
Please Log in or Create an account to join the conversation.