in past I uploaded the below code to Wemos R2 Mini :
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <DHT.h>
#define DHTTYPE DHT22 // DHT 11
#define DHTPIN D5
DHT dht(DHTPIN, DHTTYPE);
const char *ssid = " Hello_IOT";
const char *pass = "12345678";
String command;
ESP8266WebServer server(8080);
void respond() {
int h = dht.readHumidity();
int t = dht.readTemperature();
server.send(200, "text/html", "<h1>Hello from Wemos R1 Mini</h1>");
}
void outputJson() {
char temp[400];
int h = dht.readHumidity();
int t = dht.readTemperature();
int tf = dht.readTemperature(true);
snprintf ( temp, 400,
"{\"wittyCloud\" : [{\"location\" : \"any\" ,\
\"temperatureInC\" :%d ,\
\"temperatureInF\" :%d ,\
\"humidity\":%d,\
\"ldr\":%d\
}]}",
t, tf, h, analogRead(A0));
server.send ( 200, "text/json", temp );
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(BUILTIN_LED, OUTPUT);
Serial.println("Connect to access Point");
WiFi.softAP(ssid, pass);
IPAddress IPserver = WiFi.softAPIP();
Serial.println(" IP address server : ");
Serial.println(IPserver);
server.on("/", respond);
server.on("/json", outputJson);
server.begin();
server.onNotFound([]() {
command = server.uri();
server.send(200, "text/plain", command);
});
}
void loop() {
// put your main code here, to run repeatedly:
server.handleClient();
if (command.equals("/mati")) {
digitalWrite(BUILTIN_LED, HIGH); //turn off BUILTIN LED
}
if (command.equals("/hidup")) {
digitalWrite(BUILTIN_LED, LOW); //turn on BUILTIN LED
}
}
for learning purpose, it worked, however when I modified it to the below code for wemos to connect to home wifi router it instead creates the access point, and on opening the URL after connecting to wemos access point it displays the web page of updated code.
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266mDNS.h>
#include <ESP8266WebServer.h>
#include "control.h"
#include "webpage.h"
#include <DHT.h>
ESP8266WebServer server ( 80 );
const char* sid = "router+ssid";
const char* passwd = "ssid_password";
#define DHTTYPE DHT22 // DHT 22
#define DHTPIN D5
DHT dht(DHTPIN, DHTTYPE);
const char* host = "nodemcu";
void outputJson() {
char temp[400];
int h = dht.readHumidity();
int t = dht.readTemperature();
int tf = dht.readTemperature(true);
snprintf ( temp, 400,
"{\"wemos\" : [{\"location\" : \"bangalore\" ,\
\"temperatureInC\" :%2d ,\
\"temperatureInF\" :%2d ,\
\"humidity\":%2d\
}]}",
t, tf, h);
server.send ( 200, "text/json", temp );
}
void handleRoot() {
const int nsize = 2300;
char temp[nsize];
delay(200);
int h = dht.readHumidity();
int t = dht.readTemperature();
snprintf ( temp, nsize,
"%s\n<!-- add btns here --></\div></body>\n\
</html>",webpage::html);
server.send ( 200, "text/html", temp );
}
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += ( server.method() == HTTP_GET ) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for ( uint8_t i = 0; i < server.args(); i++ ) {
message += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n";
}
server.send ( 404, "text/plain", message );
}
void setup ( void ) {
Serial.begin ( 9600 );
// config static IP
IPAddress ip(192, 168, 1, 88);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns(192, 168, 1, 1);
WiFi.config(ip, dns, gateway, subnet);
WiFi.begin(sid, passwd);
Serial.println ( "" );
EEPROM.begin(512);
// Wait for connection
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( "." );
}
Serial.println ( "" );
Serial.print ( "Connected to " );
Serial.println ( sid );
Serial.print ( "IP address: " );
Serial.println ( WiFi.localIP() );
if ( MDNS.begin ( host ) ) {
Serial.println ( "MDNS responder started" );
}
MDNS.addService("http", "tcp", 80);
server.on ( "/", handleRoot );
server.on("/json", outputJson);
server.on("/control", control::toggleRelay);
server.onNotFound (handleNotFound);
server.begin();
Serial.println (F("HTTP server started"));
}
void loop ( void ) {
server.handleClient();
}