How to make an html page using ESP8266 - arduino

I have written a program on Arduino IDE to make an HTML page. It is getting compiled as well as getting upload on my nodemcu. But I get no any HTML page in browser whenever I try to enter into it with the IP address serial provided on the serial monitor. Kindly help me to find the problem if there is any either in my code or in the headerfile which is in current directory . Following is my code,
#include<ESP8266WiFi.h>
#include<WiFiClient.h>
#include<ESP8266WebServer.h>
#include "index.h"
const char * ssid = "Nadeem";
const char *password = "hamzalaiba";
ESP8266WebServer server(80);
void handleRoot() {
String s = MAIN_page; //Read HTML contents
server.send(200, "text/html", s); //Send web page
}
void setup(){
Serial.begin(9600);
delay(10);
Serial.print("Connecting to\n");
Serial.print(ssid);
WiFi.begin(ssid,password);
while(WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("Wifi connected");
delay(10);
Serial.print("Your server IP address: ");
Serial.println(WiFi.localIP());
server.on("/",handleRoot);
server.begin();
Serial.println("Server Started");
}
void loop(){
server.handleClient();
}
The headerfile 'index.h' contains following codes ,
const char MAIN_PAGE[] PROGMEM = R"=====(
<HTML>
<HEAD>
<TITLE>My first web page</TITLE>
</HEAD>
<BODY>
<CENTER>
<B>Hello Zain.... </B>
</CENTER>
</BODY>
</HTML>
)=====";
The output I get on serial monitor have following stuff,
......
Wifi connected
Your server IP address: 192.168.10.13
Server Started
--------------- CUT HERE FOR EXCEPTION DECODER ---------------
Exception (3):
epc1=0x4000bf64 epc2=0x00000000 epc3=0x00000000 excvaddr=0x4023d559 depc=0x00000000
.>>>stack>>>
ctx: cont
sp: 3ffffd00 end: 3fffffc0 offset: 0190
3ffffe90: 00000001 3ffee38c 4023d559 4020574b
3ffffea0: 3ffef450 00000001 3ffef24c 402031aa
3ffffeb0: 3fffff00 3fffff10 80fffef0 40201b50
3ffffec0: 00000001 40208230 3ffffef0 402081fe
3ffffed0: 3fffff10 3ffee3a8 3ffef24c 401000e1
3ffffee0: 3ffef24c 3ffee3a8 3ffef24c 40201b88
3ffffef0: 3ffe0000 3fff0000 80000001 80fe84ec
3fffff00: 3ffef24c 3ffee3a8 3ffee368 402031fe
3fffff10: 0000002f 80000000 81fefb00 0000008f
3fffff20: 80005054 388a21b4 40100200 00002ade
3fffff30: 3ffee3a8 00000000 00000001 00000001
3fffff40: 00000001 3ffef24c 3ffee368 3ffee500
3fffff50: 00000001 3ffee38c 3ffee368 3ffee500
3fffff60: 00000001 3ffee38c 3ffee368 40203497
3fffff70: 00000000 00000000 00001388 80efeffe
3fffff80: 00000000 00000000 00000001 40100170
3fffff90: 3fffdad0 00000000 3ffee4c0 40203538
3fffffa0: 3fffdad0 00000000 3ffee4c0 4020668c
3fffffb0: feefeffe feefeffe 3ffe84ec 40100ba1
<<<stack<<<
--------------- CUT HERE FOR EXCEPTION DECODER ---------------
V)⸮L⸮⸮D⸮⸮Connecting to
Nadeem...........
Wifi connected
Your server IP address: 192.168.10.13
Server Started

Related

Problem connecting BMP280 to ESP8266 NodeMCU - check wiring

I am connecting a GY BMP280 to an ESP8266 NodeMCU:
with the wiring:
VCC = 3.3V
GND = GND
SCL = D1 (GPIO5)
SDA = D2 (GPIO4)
I use the Adafruit BMP280 Library by Adafruit version 2.6.6 And my program is as follows:
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
Adafruit_BMP280 bmp; // create a BMP280 instance
#define SEALEVELPRESSURE_HPA 1013.25
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial console to open
}
if (!bmp.begin()) {
Serial.println("Could not find a valid BMP280 sensor, check wiring!");
while (1);
}
}
void loop() {
float temperature = bmp.readTemperature();
float pressure = bmp.readPressure() / 100.0F;
float altitude = bmp.readAltitude(SEALEVELPRESSURE_HPA);
Serial.print("Temperature = ");
Serial.print(temperature);
Serial.println(" *C");
Serial.print("Pressure = ");
Serial.print(pressure);
Serial.println(" hPa");
Serial.print("Altitude = ");
Serial.print(altitude);
Serial.println(" meters");
Serial.println();
delay(2000);
}
I have tried different ports and different code setups, this is in my opinion the most correct for what i need, but i keep getting the same output on my serial monitor:
Soft WDT reset
>>>stack>>>
ctx: cont
sp: 3ffffdf0 end: 3fffffc0 offset: 01a0
3fffff90: 3fffdad0 00000000 3ffee6fc 4020105c
3fffffa0: feefeffe feefeffe 3ffee750 40203268
3fffffb0: feefeffe feefeffe 3ffe85e0 40100ef5
<<<stack<<<
--------------- CUT HERE FOR EXCEPTION DECODER ---------------
�{���4��Could not find a valid BMP280 sensor, check wiring!
I really cant figure out the problem, does anyone here have an idea of what could cause the trouble?
The message
Soft WDT reset
means you're triggering the watchdog timer. The watchdog timer is part of the system which halts it with an error in case part of it runs for too long without allowing other parts to run.
In this case it's this code that triggers it:
if (!bmp.begin()) {
Serial.println("Could not find a valid BMP280 sensor, check wiring!");
while (1);
}
Code on an ESP8266 (or ESP32) should not run loops like that indefinitely without ever calling yield() or delay() to allow other things to run. That's what triggers the watchdog.
if (!bmp.begin()) {
Serial.println("Could not find a valid BMP280 sensor, check wiring!");
while (1)
yield();
}
As for why your code can't detect the sensor, have you used an I2C scanner to confirm that the device is even wired correctly?
If it is, the sensor has two possible I2C addresses, 0x76 and 0x77. The Adafruit library defaults to 0x77. Try 0x76.
if (!bmp.begin(0x76)) {
If that doesn't work then you have a hardware problem, which is off-topic here as Stack Overflow is for software. So if that doesn't work please post about it in the Arduino Stack Exchange.

Using BME280 with SNMP librarie

I'm trying to use SNMP library to read BME280 sensor on NODEMCU, but I get the following error after compiling sketch:
ERROR:
ets Jan 8 2013,rst cause:2, boot mode:(3,6)
load 0x4010f000, len 3584, room 16
tail 0
chksum 0xb0
csum 0xb0
v2843a5ac
~ld
MY CODE:
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <Arduino_SNMP.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme;
const char* ssid = "Andre";
const char* password = "12345678";
float temperature = bme.readTemperature();
float humidity = bme.readHumidity();
float pressure = bme.readPressure() / 100.0F;
float altitude = bme.readAltitude(SEALEVELPRESSURE_HPA);
WiFiUDP udp;
SNMPAgent snmp = SNMPAgent("public"); // Starts an SMMPAgent instance with the community string 'public'
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("");
bme.begin(0x76);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// give snmp a pointer to the UDP object
snmp.setUDP(&udp);
snmp.begin();
snmp.addFloatHandler(".1.3.6.1.4.1.12345.1", &temperature);
snmp.addFloatHandler(".1.3.6.1.4.1.12345.2", &humidity, true);
snmp.addFloatHandler(".1.3.6.1.4.1.12345.3", &pressure);
snmp.addFloatHandler(".1.3.6.1.4.1.12345.4", &altitude);
// you can accept SET commands with a pointer to an integer (or string)
}
void loop() {
snmp.loop(); // must be called as often as possible
}
I've been trying to figure out what's wrong with my code.
#ElefantPhace
Another error occurred:
--------------- CUT HERE FOR EXCEPTION DECODER ---------------
ets Jan 8 2013,rst cause:2, boot mode:(3,6)
load 0x4010f000, len 3584, room 16
tail 0
chksum 0xb0
csum 0xb0
v2843a5ac
~ld
--------------- CUT HERE FOR EXCEPTION DECODER ---------------
Exception (28):
epc1=0x40203c84 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000
stack>>>
ctx: cont
sp: 3ffffda0 end: 3fffffc0 offset: 0190
3fffff30: 40204528 3ffeebe0 3ffe8706 40204825
3fffff40: 3fffdad0 00000000 3ffeebe0 402048ec
3fffff50: 3fffdad0 3ffeea84 3ffeea2c 40203d68
3fffff60: 3fffdad0 3ffeea84 3ffeebe0 402025c5
3fffff70: feefeffe feefeffe feefeffe feefeffe
3fffff80: feefeffe feefeffe feefeffe feefeffe
3fffff90: feefeffe feefeffe feefeffe 3ffeec58
3fffffa0: 3fffdad0 00000000 3ffeec18 402050e0
3fffffb0: feefeffe feefeffe 3ffe84fc 40100c29
<<<stack<<<

Unintentional strange characters added to packets during udp communication in Arduino

Stackoverflow!
Unusual strange characters are added to packets during udp communication in Arduino.
Hi. Stack overflow. I encountered problems that I could not solve while using Udp with Arduino. I have looked up a lot of information to solve this problem. I also tested various cases. But I can not solve it.
The problem occurs from the 24th byte of the packet. The characters exceeding 23 characters are converted into strange characters and the subsequent sentences are not output. Is the packet transmission of udp communication limited to 24 bytes? If so, I am glad, but I want to know how to solve it. Please help me.
The test environment is as follows. Two Arduino are connected via a LAN cable. In addition, the following source code has been uploaded to Arduino. The serial monitor is as follows.
TX serial monitor
.
.
17:46:31.521 -> Sending UDP message
17:46:32.516 -> Sending UDP message
17:46:33.514 -> Sending UDP message
17:46:34.519 -> Sending UDP message
17:46:35.515 -> Sending UDP message
17:46:36.510 -> Sending UDP message
RX serial monitor
17:38:51.664 -> 010010010101001010101001K;⸮ <----------problem
17:38:52.662 -> Received packet of size 31
17:38:52.662 -> From 192.168.1.251, port 5678
17:38:52.662 -> Contents:
17:38:52.662 -> 01001001010100101010100j⸮ <------- problem
17:38:53.663 -> Received packet of size 31
17:38:53.663 -> From 192.168.1.251, port 5678
17:38:53.663 -> Contents:
17:38:53.663 -> 010010010101001010101001;ے <------------problem
17:38:56.770 -> Received packet of size 31
---------Source Code-------------
///////////TX
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#define MSG_INTERVAL 1000
// network parameters
byte mac[] = {
0x90, 0xA2, 0xDA, 0x0E, 0x05, 0x04}; // ethernet interface MAC address
IPAddress localIp(192, 168, 1, 251); // local ip address
IPAddress destIp(192, 168, 1, 15); // destination ip address
unsigned int port = 5678; // destination port
// EthernetUDP to send and receive messages.
EthernetUDP Udp;
// message string
char message[] = "0100100101010010101010010101010";
// timing
//unsigned long previousLedMillis;
//unsigned long ledInterval;
unsigned long previousSendMillis;
unsigned long sendInterval;
// setup the arduino and shields
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start ethernet and udp
Ethernet.begin(mac,localIp); // static ip version
// open UDP port
Udp.begin(port);
// show the local ip address (useful for dhcp)
Serial.print("Local IP: ");
Serial.println(Ethernet.localIP());
// initialize send interval
sendInterval = MSG_INTERVAL;
}
// do tasks
void loop() {
// check if udp string has to be sent
if(millis() - previousSendMillis >= sendInterval) {
sendMessage();
}
}
// send udp string
void sendMessage() {
Serial.println("Sending UDP message");
// store current millis
previousSendMillis = millis();
// send udp message
Udp.beginPacket(destIp, port);
Udp.write(message);
Udp.endPacket();
}
/////RX
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 15);
IPAddress remIp(92, 168, 1, 176);
unsigned int localPort = 5678; // local port to listen on
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
void setup()
{
// start the Ethernet and UDP:
Ethernet.begin(mac,ip);
Udp.begin(localPort);
pinMode(5, OUTPUT);
Serial.begin(115200);
Serial.print("Local IP: ");
Serial.println(Ethernet.localIP());
}
void loop()
{
int packetSize = Udp.parsePacket();
if(packetSize)
{
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remote = Udp.remoteIP();
for (int i =0; i < 4; i++)
{
Serial.print(remote[i], DEC);
if (i < 3)
{
Serial.print(".");
}
}
Serial.print(", port ");
Serial.println(Udp.remotePort());
// read the packet into packetBufffer
Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
//Udp.read(packetBuffer,1024);
Serial.println("Contents:");
Serial.println(packetBuffer);
//for (int i=0;i<packetSize;i++){
// Serial.print(packetBuffer[i]);
//}
//Serial.print('\n');
}
}
yes you have right, some problem with length 24!!
if you read inside the EthernetUDP.h file, you will see:
#define UDP_TX_PACKET_MAX_SIZE 24
so if you want use more characters, dont use this const, choose your personal size.
change
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
to
char packetBuffer[50];
or use another define:
#define UDP_MAX_BUFFER 50 //for example
char packetBuffer[UDP_MAX_BUFFER];

ESP8266 - Async DallasTemperature - Exception (28)

I am starting a temperature measurement (sensors.requestTemperatures) in a timer interrupt and I need a way to know whether it is ready. I tried using the sensors.isConversionAvailable method, but it throws an exception. I looked at the source of the isConversionAvailable method and tried to call the readScratchPad method myself, and an exception was thrown there.
Full code:
#include <DallasTemperature.h>
#include <OneWire.h>
#define TIMER_SECONDS 1
#define SENSOR_PIN 5
OneWire sensorWire(SENSOR_PIN);
DallasTemperature sensors(&sensorWire);
void setup() {
Serial.begin(115200);
Serial.println("Initializing...");
sensors.begin();
sensors.setResolution(10);
sensors.setWaitForConversion(false);
timer1_isr_init();
timer1_attachInterrupt([] {
sensors.requestTemperatures();
});
timer1_enable(TIM_DIV265, TIM_EDGE, TIM_LOOP);
timer1_write(F_CPU / 256 * TIMER_SECONDS);
Serial.println("Initialization complete");
}
void loop() {
Serial.println("Started the loop method");
//When this block is uncommented, the second exception is thrown
/*uint8_t number;
sensors.readScratchPad(0, &number);
Serial.print("Scratch pad number: ");
Serial.println(number);*/
if (sensors.isConversionAvailable(0)) {
Serial.print("Temperature: ");
Serial.println(sensors.getTempCByIndex(0));
} else {
Serial.println("The conversion is unavailable");
}
Serial.println("Finished the loop method");
}
Exception (isConversionAvailable):
Initializing...
Initialization complete
Started the loop method
Exception (28):
epc1=0x402025af epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000
ctx: cont
sp: 3ffef230 end: 3ffef460 offset: 01a0
>>>stack>>>
3ffef3d0: 00000000 3ffee354 00000000 402025ad
3ffef3e0: 3ffef420 00000000 3ffee338 40201e57
3ffef3f0: 00000000 00000017 3ffef4a0 40202bcd
3ffef400: 3ffe8588 00000000 3ffef4a0 3ffee42c
3ffef410: 3fffdad0 3ffee338 3ffef4a0 40202144
3ffef420: 40201052 88011627 3ffef4a0 40202c1c
3ffef430: 3fffdad0 00000000 3ffef4a0 40201d0e
3ffef440: feefeffe 00000000 3ffee424 40202888
3ffef450: feefeffe feefeffe 3ffee440 40100114
<<<stack<<<
ets Jan 8 2013,rst cause:2, boot mode:(1,6)
Exception (readScratchPad):
Initializing...
Initialization complete
Started the loop method
Exception (28):
epc1=0x402025cf epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000
ctx: cont
sp: 3ffef250 end: 3ffef470 offset: 01a0
>>>stack>>>
3ffef3f0: 00000000 3ffee364 00000000 402025cd
3ffef400: 3ffef440 00000000 3ffee348 40201e77
3ffef410: 00000000 3ffee348 3ffef4b0 40202c18
3ffef420: 3ffe8438 00000000 3ffef4b0 3ffee43c
3ffef430: 3fffdad0 3ffee348 3ffef4b0 40201d14
3ffef440: 3fffdad0 3ffee348 3ffef4b0 40201cd0
3ffef450: feefeffe 00000000 3ffee434 402028a8
3ffef460: feefeffe feefeffe 3ffee450 40100114
<<<stack<<<
ets Jan 8 2013,rst cause:2, boot mode:(1,6)
Looking at the function's code (a few comments omitted):
bool DallasTemperature::readScratchPad(const uint8_t* deviceAddress, uint8_t* scratchPad){
// send the reset command and fail fast
int b = _wire->reset();
if (b == 0) return false;
_wire->select(deviceAddress);
_wire->write(READSCRATCH);
for(uint8_t i = 0; i < 9; i++){
scratchPad[i] = _wire->read();
}
b = _wire->reset();
return (b == 1);
}
You call the function with
sensors.readScratchPad(0, &number);
Then deviceAddress = NULL. So then there is the call _wire->select(deviceAddress). The _wire object is of type OneWire*. The function select is implemented as follows:
void OneWire::select(const uint8_t rom[8])
{
uint8_t i;
write(0x55); // Choose ROM
for (i = 0; i < 8; i++) write(rom[i]);
}
And in rom[i] you have a dereference on the NULL pointer you gave it initially. I.e.: Crash.
Looking at another function in the lib shows how to obtain the a valid deviceAddress pointer.
float DallasTemperature::getTempCByIndex(uint8_t deviceIndex){
DeviceAddress deviceAddress;
if (!getAddress(deviceAddress, deviceIndex)){
return DEVICE_DISCONNECTED_C;
}
return getTempC((uint8_t*)deviceAddress);
}
So, if you want the address of your first sensor, you can get it by doing:
DeviceAddress deviceAddress;
if (!sensors.getAddress(deviceAddress, 0)){
Serial.println("No device address found for index 0!!!");
return;
}
//use deviceAddr in the next call
ScratchPad readScratchPad; //this is a typedef to uint8_t[9]
sensors.readScratchPad( (uint8_t*)deviceAddress, readScratchPad);
//do whatever you like with the values in the readScratchPad array..
For further references look at the library files at https://github.com/milesburton/Arduino-Temperature-Control-Library and https://github.com/PaulStoffregen/OneWire .

CIDR address calculation (network and broadcast address)

I'm a little stuck with something right now:
Trying to figure out how to calculate the network/broadcast address for the following CIDR IP: 10.20.30.45/13.
As far as I can see, the fact that the network part of this IP is 13 bits big, tells us that the address hast got to be changed beginning with the seccond oktett when determining the network address
-> 10.X.0.0
But what will X be, and how can I calculate X?
You can convert 10.20.30.45 to binary and /13 to binary, which is 255.248.0.0.
10.20.30.45 in binary would be 00001010 00010100 00011110 00101101
255.248.0.0 is 11111111 11111000 00000000 00000000
Then compare them:
00001010 00010100 00011110 00101101
11111111 11111000 00000000 00000000
______________________________________
00001010 00010000 00000000 00000000 => back to decimal is 10.16.0.0
To get the broadcast address you have to do a binary inversion of the CIDR or Netmask Address.
Or you can you this CIDR converter to automate it for you.
In C(++):
#ifdef __FreeBSD__
#include <sys/socket.h>
#endif
#include <arpa/inet.h>
#include <netinet/in.h>
#include <cassert>
#include <cstdint>
#include <iostream>
int main() {
in_addr net, broadcast;
int bits = inet_net_pton(AF_INET, "10.20.30.45/13", &net, sizeof(net));
assert((bits != -1)); // assert that inet_net_pton understood us
// Apply CIDR mask to address to get the network
if (bits > 0) { // u32 << 32 is undefined
uint32_t mask = htonl((0xFFFFFFFFu) << (32 - bits));
net.s_addr &= mask;
}
broadcast = net;
// Do the same for broadcast
if (bits < 32) {
uint32_t mask = htonl((0xFFFFFFFFu) >> bits);
broadcast.s_addr |= mask;
}
std::cout << "Network: " << inet_ntoa(net) << std::endl;
std::cout << "Broadcast: " << inet_ntoa(broadcast) << std::endl;
}
Outputs:
Network: 10.16.0.0
Broadcast: 10.23.255.255

Resources