Example code creating an AP for some reason
Posted: Fri Oct 20, 2017 4:32 pm
I have used the ESP (NodeMCU and Wemos D1) a small amount but am not anywhere close to an expert. I want to connect a DHT11 to the board and report the temp/humidity to MQTT. I found a sketch that does exactly this and have it working, however it seems to be creating an access point and I'm not sure what line is doing that. Could someone kindly explain what I'm not seeing? I know this is not complicated, but I really can't seem to see it even though it's surely in front of my face!
The source code is from Thingsboard on github:
https://raw.githubusercontent.com/thingsboard/thingsboard.github.io/master/docs/samples/esp8266/resources/esp8266-dht-mqtt.ino
My code which simply duplicates the 1 DHT into 2 and makes them DHT11 instead of DHT22 is:
The source code is from Thingsboard on github:
https://raw.githubusercontent.com/thingsboard/thingsboard.github.io/master/docs/samples/esp8266/resources/esp8266-dht-mqtt.ino
Code: Select all
#include "DHT.h"
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#define WIFI_AP "YOUR_WIFI_AP"
#define WIFI_PASSWORD "YOUR_WIFI_PASSWORD"
#define TOKEN "ESP8266_DEMO_TOKEN"
// DHT
#define DHTPIN 2
#define DHTTYPE DHT22
char thingsboardServer[] = "YOUR_THINGSBOARD_HOST_OR_IP";
WiFiClient wifiClient;
// Initialize DHT sensor.
DHT dht(DHTPIN, DHTTYPE);
PubSubClient client(wifiClient);
int status = WL_IDLE_STATUS;
unsigned long lastSend;
void setup()
{
Serial.begin(115200);
dht.begin();
delay(10);
InitWiFi();
client.setServer( thingsboardServer, 1883 );
lastSend = 0;
}
void loop()
{
if ( !client.connected() ) {
reconnect();
}
if ( millis() - lastSend > 1000 ) { // Update and send only after 1 seconds
getAndSendTemperatureAndHumidityData();
lastSend = millis();
}
client.loop();
}
void getAndSendTemperatureAndHumidityData()
{
Serial.println("Collecting temperature data.");
// Reading temperature or humidity takes about 250 milliseconds!
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
String temperature = String(t);
String humidity = String(h);
// Just debug messages
Serial.print( "Sending temperature and humidity : [" );
Serial.print( temperature ); Serial.print( "," );
Serial.print( humidity );
Serial.print( "] -> " );
// Prepare a JSON payload string
String payload = "{";
payload += "\"temperature\":"; payload += temperature; payload += ",";
payload += "\"humidity\":"; payload += humidity;
payload += "}";
// Send payload
char attributes[100];
payload.toCharArray( attributes, 100 );
client.publish( "v1/devices/me/telemetry", attributes );
Serial.println( attributes );
}
void InitWiFi()
{
Serial.println("Connecting to AP ...");
// attempt to connect to WiFi network
WiFi.begin(WIFI_AP, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to AP");
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
status = WiFi.status();
if ( status != WL_CONNECTED) {
WiFi.begin(WIFI_AP, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to AP");
}
Serial.print("Connecting to ThingsBoard node ...");
// Attempt to connect (clientId, username, password)
if ( client.connect("ESP8266 Device", TOKEN, NULL) ) {
Serial.println( "[DONE]" );
} else {
Serial.print( "[FAILED] [ rc = " );
Serial.print( client.state() );
Serial.println( " : retrying in 5 seconds]" );
// Wait 5 seconds before retrying
delay( 5000 );
}
}
}
My code which simply duplicates the 1 DHT into 2 and makes them DHT11 instead of DHT22 is:
Code: Select all
#include "DHT.h"
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#define WIFI_AP "Linksys_1"
//#define TOKEN "ESP8266_DEMO_TOKEN"
// DHT
#define DHTPIN1 D4
#define DHTPIN2 D5
#define DHTPIN3 D6
#define DHTTYPE DHT11
char MQTTServer[] = "192.168.0.200";
WiFiClient wifiClient;
// Initialize DHT sensor.
DHT dht1(DHTPIN1, DHTTYPE);
DHT dht2(DHTPIN2, DHTTYPE);
DHT dht3(DHTPIN3, DHTTYPE);
PubSubClient client(wifiClient);
int status = WL_IDLE_STATUS;
unsigned long lastSend;
int value = 0;
void setup()
{
Serial.begin(115200);
pinMode(DHTPIN1, INPUT_PULLUP);
pinMode(DHTPIN2, INPUT_PULLUP);
pinMode(DHTPIN3, INPUT_PULLUP);
dht1.begin();
dht2.begin();
dht3.begin();
delay(10);
InitWiFi();
client.setServer( MQTTServer, 1883 );
lastSend = 0;
}
void loop()
{
if ( !client.connected() ) {
reconnect();
}
if ( millis() - lastSend > 60000 ) { // Update and send only after 60 seconds
getAndSendTemperatureAndHumidityData();
lastSend = millis();
}
client.loop();
}
void getAndSendTemperatureAndHumidityData()
{
++value;
Serial.println("Collecting temperature data.");
// Reading temperature or humidity takes about 250 milliseconds!
float h1 = dht1.readHumidity();
float h2 = dht2.readHumidity();
float h3 = dht3.readHumidity();
// Read temperature as Celsius (the default)
float t1 = dht1.readTemperature();
float t2 = dht2.readTemperature();
float t3 = dht3.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h1) || isnan(t1)) {
Serial.println("Failed to read from DHT1 sensor!");
}
else
{
Serial.print("Humidity1: ");
Serial.print(h1);
Serial.print(" %\t");
Serial.print("Temperature1: ");
Serial.print(t1);
Serial.println(" *C ");
String temperature1 = String(t1);
String humidity1 = String(h1);
// Just debug messages
Serial.print( "Sending temperature1 and humidity1 : [" );
Serial.print( temperature1 ); Serial.print( "," );
Serial.print( humidity1 );
Serial.print( "] -> " );
// Prepare a JSON payload string DHT1
String payload = "";
payload += "temperature="; payload += temperature1; payload += ",";
payload += "humidity="; payload += humidity1;
// Send payload
char attributes[100];
payload.toCharArray( attributes, 100 );
client.publish( "Safe1", attributes );
Serial.println( attributes );
}
if (isnan(h2) || isnan(t2)) {
Serial.println("Failed to read from DHT2 sensor!");
}
else
{
Serial.print("Humidity2: ");
Serial.print(h2);
Serial.print(" %\t");
Serial.print("Temperature2: ");
Serial.print(t2);
Serial.println(" *C ");
String temperature2 = String(t2);
String humidity2 = String(h2);
// Just debug messages
Serial.print( "Sending temperature2 and humidity2 : [" );
Serial.print( temperature2 ); Serial.print( "," );
Serial.print( humidity2 );
Serial.print( "] -> " );
// Prepare a JSON payload string DHT2
String payload = "";
payload += "temperature="; payload += temperature2; payload += ",";
payload += "humidity="; payload += humidity2;
// Send payload
char attributes[100];
payload.toCharArray( attributes, 100 );
client.publish( "Safe2", attributes );
Serial.println( attributes );
}
if (isnan(h3) || isnan(t3)) {
Serial.println("Failed to read from DHT3 sensor!");
}
else
{
Serial.print("Humidity3: ");
Serial.print(h3);
Serial.print(" %\t");
Serial.print("Temperature3: ");
Serial.print(t3);
Serial.println(" *C ");
String temperature3 = String(t3);
String humidity3 = String(h3);
// Just debug messages
Serial.print( "Sending temperature3 and humidity3 : [" );
Serial.print( temperature3 ); Serial.print( "," );
Serial.print( humidity3 );
Serial.print( "] -> " );
// Prepare a JSON payload string DHT3
String payload = "";
payload += "temperature="; payload += temperature3; payload += ",";
payload += "humidity="; payload += humidity3;
// Send payload
char attributes[100];
payload.toCharArray( attributes, 100 );
client.publish( "Safe3", attributes );
Serial.println( attributes );
}
}
void InitWiFi()
{
Serial.println("Connecting to AP ...");
// attempt to connect to WiFi network
WiFi.begin(WIFI_AP);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to AP");
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
status = WiFi.status();
if ( status != WL_CONNECTED) {
WiFi.begin(WIFI_AP);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to AP");
}
Serial.print("Connecting to MQTT server ...");
// Attempt to connect (clientId, username, password)
if ( client.connect("ESP8266 Device") ) {
Serial.println( "[DONE]" );
} else {
Serial.print( "[FAILED] [ rc = " );
Serial.print( client.state() );
Serial.println( " : retrying in 5 seconds]" );
// Wait 5 seconds before retrying
delay( 5000 );
}
}
}