WTA : nodeMCU DHT11/22 send to EasyIoT server

9 years 6 months ago - 9 years 6 months ago #1459 by lesjaw
Halo guys, after a couple days learning how this microcontroler work and EasyIoT server, i am so frustated :-(.. i'm trying to build temperature and humidity sensor with dht11 but so far no luck..

but there is a couple on sample code i found that work for me..the question how do i send this data to EasyIoT server??

code
--init.lua
print("Setting up WIFI...")
wifi.setmode(wifi.STATION)
--modify according your wireless router settings
wifi.sta.config("MiFi","nexusdroit")
wifi.sta.connect()
tmr.alarm(1, 1000, 1, function() 
if wifi.sta.getip()== nil then 
print("IP unavaiable, Waiting...") 
else 
tmr.stop(1)
print("Config done, IP is "..wifi.sta.getip())
dofile("dht11.lua")
end 
end)

-
- Measure temperature, humidity and post data to thingspeak.com
-- 2014 OK1CDJ
-- DHT11 code is from esp8266.com
---Sensor DHT11 is conntected to GPIO0
pin = 3

Humidity = 0
HumidityDec=0
Temperature = 0
TemperatureDec=0
Checksum = 0
ChecksumTest=0


function getTemp()
Humidity = 0
HumidityDec=0
Temperature = 0
TemperatureDec=0
Checksum = 0
ChecksumTest=0

--Data stream acquisition timing is critical. There's
--barely enough speed to work with to make this happen.
--Pre-allocate vars used in loop.

bitStream = {}
for j = 1, 40, 1 do
     bitStream[j]=0
end
bitlength=0

gpio.mode(pin, gpio.OUTPUT)
gpio.write(pin, gpio.LOW)
tmr.delay(20000)
--Use Markus Gritsch trick to speed up read/write on GPIO
gpio_read=gpio.read
gpio_write=gpio.write

gpio.mode(pin, gpio.INPUT)

--bus will always let up eventually, don't bother with timeout
while (gpio_read(pin)==0 ) do end

c=0
while (gpio_read(pin)==1 and c<100) do c=c+1 end

--bus will always let up eventually, don't bother with timeout
while (gpio_read(pin)==0 ) do end

c=0
while (gpio_read(pin)==1 and c<100) do c=c+1 end

--acquisition loop
for j = 1, 40, 1 do
     while (gpio_read(pin)==1 and bitlength<10 ) do
          bitlength=bitlength+1
     end
     bitStream[j]=bitlength
     bitlength=0
     --bus will always let up eventually, don't bother with timeout
     while (gpio_read(pin)==0) do end
end

--DHT data acquired, process.

for i = 1, 8, 1 do
     if (bitStream[i+0] > 2) then
          Humidity = Humidity+2^(8-i)
     end
end
for i = 1, 8, 1 do
     if (bitStream[i+8] > 2) then
          HumidityDec = HumidityDec+2^(8-i)
     end
end
for i = 1, 8, 1 do
     if (bitStream[i+16] > 2) then
          Temperature = Temperature+2^(8-i)
     end
end
for i = 1, 8, 1 do
     if (bitStream[i+24] > 2) then
          TemperatureDec = TemperatureDec+2^(8-i)
     end
end
for i = 1, 8, 1 do
     if (bitStream[i+32] > 2) then
          Checksum = Checksum+2^(8-i)
     end
end
ChecksumTest=(Humidity+HumidityDec+Temperature+TemperatureDec) % 0xFF

print ("Temperature: "..Temperature.."."..TemperatureDec)
print ("Humidity: "..Humidity.."."..HumidityDec)
print ("ChecksumReceived: "..Checksum)
print ("ChecksumTest: "..ChecksumTest)
end

--- Get temp and send data to thingspeak.com
function sendData()
getTemp()
-- conection to thingspeak.com
print("Sending data to thingspeak.com")
conn=net.createConnection(net.TCP, 0) 
conn:on("receive", function(conn, payload) print(payload) end)
-- api.thingspeak.com 184.106.153.149
conn:connect(80,'192.168.5.157') 
conn:send("GET /field1="..Temperature.."."..TemperatureDec.."&field2="..Humidity.."."..HumidityDec.." HTTP/1.1\r\n") 

conn:on("sent",function(conn)
                      print("Closing connection")
                      conn:close()
                  end)
conn:on("disconnection", function(conn)
                      print("Got disconnection...")
  end)
end
-- send data every X ms to thing speak
tmr.alarm(2, 60000, 1, function() sendData() end )

so here the code for esp8266 and DHT11 using Arduino IDE, i fix the code so it will send data only if it has new value..
/*
  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>

// EasyIoT server definitions
#define ssid  "MiFi"
#define password  "nexusdroit"
#define host "192.168.5.157"
#define port 80
#define nodet "N1S0"
#define nodeh "N2S0"
#define user "admin"
#define userpassword "test"

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

#define USER_PWD_LEN 40
char unameenc[USER_PWD_LEN];

float oldTemp;
float oldHum;


void setup() {
  Serial.begin(115200);
  delay(10);
  dht.begin();
  
 wifiConnect();
  
  char uname[USER_PWD_LEN];
  String str = String(user)+":"+String(userpassword);  
  str.toCharArray(uname, USER_PWD_LEN); 
  memset(unameenc,0,sizeof(unameenc));
  base64_encode(unameenc, uname, strlen(uname));
  
  oldTemp = -1;
  oldHum = -1;
}

void loop() {
 
  float t = dht.readTemperature();
  
  if (isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
    
   if (t != oldTemp){
   sendInputStateT(t);
   oldTemp = t;
   }
   
  float h = dht.readHumidity();
  
  if (isnan(h)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  
  if (h != oldHum){
   sendInputStateH(h);
   oldHum = h;
   }
   
}

void wifiConnect(){
      Serial.print("Connecting to AP");
      WiFi.begin(ssid, password);
      while (WiFi.status() != WL_CONNECTED) {
      delay(100);
      Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");  
}

void sendInputStateT(float t){
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  while(!client.connect(host, port)) {
    Serial.println("connection failed");
    wifiConnect(); 
  } 
  
  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");
         
  // 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);
  }      
  delay(5000);
}
  
void sendInputStateH(float h){
  // Use WiFiClient class to create TCP connections
  WiFiClient client2;
  while(!client2.connect(host, port)) {
    Serial.println("connection failed");
    wifiConnect(); 
  } 
  
  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);
  
  client2.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(5000);
  
   while(client2.available()){
    String line = client2.readStringUntil('\r');
    Serial.print(line);
  }

  Serial.println();
  Serial.println("closing connection");
  
  delay(100);
}

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

9 years 6 months ago #1463 by Dennis
You could try to use the arduino IDE for esp8266, I've the imprwssion that it runs much more stable, and C is also easier for me than writing in lua...

see
github.com/esp8266/Arduino

regards
The following user(s) said Thank You: lesjaw

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

9 years 6 months ago #1465 by lesjaw

Dennis wrote: You could try to use the arduino IDE for esp8266, I've the imprwssion that it runs much more stable, and C is also easier for me than writing in lua...

see
github.com/esp8266/Arduino

regards


but it was you who the one made nodeMCU with DHT11 in another thread..oh man i'm so frustated with it.. only fw 9.5.6 run stable but it give me nil for temp and humi..

btw i found another esp8266+DHT11 but it for thingSpeak.. please modified it for EasyIoT server..
// www.arduinesp.com 
//
// Plot DTH11 data on thingspeak.com using an ESP8266 
// April 11 2015
// Author: Jeroen Beemster
// Website: www.arduinesp.com
 
 
#include <DHT.h>
#include <ESP8266WiFi.h>
 
// replace with your channel's thingspeak API key, 
//String apiKey = "4XZ9NA14HQZ7BYD7";
const char* ssid = "MiFi";
const char* password = "nexusdroit";
 
//const char* server = "api.thingspeak.com";
#define DHTPIN 2 // what pin we're connected to
 
DHT dht(DHTPIN, DHT11,15);
WiFiServer server(8);
   
 
void setup() {                
  Serial.begin(115200);
  delay(10);
  dht.begin();
  
  WiFi.begin(ssid, password);
 
  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");
  
}
 
 
void loop() {
   
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

     Serial.print("Temperature: ");
     Serial.print(t);
     Serial.print(" degrees Celcius Humidity: "); 
     Serial.print(h);
     Serial.println("% send to Thingspeak");    
 
   
  Serial.println("Waiting...");    
  
  delay(20000);  
}

it works flawlesy..

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

9 years 6 months ago #1467 by Dennis
I used a nodeMCU firmware 0.9.4 (will have to check build number; see first post of my nodeMCU thread for a link), and also 0.9.5 (20150213) successfully.

Did you upload all three file from the github repo to the nodeMCU module, then rebooted it?

regards

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

9 years 6 months ago #1468 by lesjaw

Dennis wrote: I used a nodeMCU firmware 0.9.4 (will have to check build number; see first post of my nodeMCU thread for a link), and also 0.9.5 (20150213) successfully.

Did you upload all three file from the github repo to the nodeMCU module, then rebooted it?

regards


yes of course..it upload succed but no temp and hum

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

9 years 6 months ago #1479 by EasyIoT
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/
The following user(s) said Thank You: ArneiO

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

Time to create page: 0.280 seconds

Forum latest

  • No posts to display.