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<<<
Related
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.
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
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 .
I have a NodeMCU board that is reset after about 3 seconds of booting up. I do not have any external caps or resistors attached to my board as other posts have suggested. I tried several of these methods with no luck. I have attached my code below along with the output of the serial monitor. I am uploading code with the Arduino IDE #115200 baud (80mHz) with 4M (3M SPIFFS).
Terminal output:
tai
chx2d
csum 0x2d
v60000318
~ld
�� �� �� �� �� �� �� �� �� �� �� �� �� �� �� �� �� �� �� �� �� �� �� �� �� �� �� �� �� �� �� �� �� �� �� �� �load 0x4010f000, len 1384, room 16
�� �� �� �� �� �� �� �� �� �� �� �������������to Hardy
.....
Attempting MQTT connection...connected
Adafruit MPR121 Capacitive Touch sensor test
Soft WDT reset
ctx: cont
sp: 3ffef550 end: 3ffef7e0 offset: 01b0
>>>stack>>>
3ffef700: 00003a97 00000001 00000002 00000001
3ffef710: 3ffee5d6 00000150 00000004 4020120c
3ffef720: 3ffee5d6 0000005a 00000004 402013ca
3ffef730: 00000001 00000000 00000004 40201300
3ffef740: 0000005a 000d0462 3ffee630 3ffee7b0
3ffef750: 0000005d 3ffee594 00000001 40202c5c
3ffef760: 0000005d 3ffee594 3ffee5a0 40202c87
3ffef770: 4020153a 00000001 3ffee5a0 4020408b
3ffef780: 3fffdad0 0000005a 3ffee594 402040db
3ffef790: 3fffdad0 3ffee4c0 3ffee784 40202a2b
3ffef7a0: 00000000 00000000 00000000 40204ce8
3ffef7b0: 00000000 00000000 00000000 feefeffe
3ffef7c0: feefeffe 00000000 3ffee7a9 40204b18
3ffef7d0: feefeffe feefeffe 3ffee7c0 40100718
<<<stack<<<
������
Code:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "Adafruit_MPR121.h"
int led1 = D6;
int led2 = D3;
const char* inTopic = "/home/room1/switch1/in";
const char* outTopic = "/home/room1/switch1/out";
const char* ssid = "Hardy";
const char* password = "*****";
const char* mqtt_server = "192.168.1.199";
uint16_t lasttouched = 0;
uint16_t currtouched = 0;
Adafruit_MPR121 cap = Adafruit_MPR121();
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(9600);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
client.subscribe(inTopic);
Serial.println("Connecting to " + (String)ssid);
setup_wifi();
reconnect();
Serial.println("Adafruit MPR121 Capacitive Touch sensor test");
if (!cap.begin(0x5A)) {
Serial.println("MPR121 not found, check wiring?");
while (1);
}
Serial.println("MPR121 found!");
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop(); //Check for updated commands on topic
checkTouches(); //See if buttons were pressed
//delay(100);
}
void checkTouches() {
currtouched = cap.touched();
if ((currtouched & _BV(0)) && !(lasttouched & _BV(0)) ) {
publishCommand(outTopic, 0);
}
if ((currtouched & _BV(1)) && !(lasttouched & _BV(1)) ) {
publishCommand(outTopic, 1);
}
lasttouched = currtouched;
}
void publishCommand(String topic,float topic_val){
Serial.print("Newest topic " + topic + " value:");
Serial.println(String(topic_val).c_str());
client.publish(topic.c_str(), String(topic_val).c_str(), true);
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
int value = (int) payload[0];
value -= 48; //For byte to int conversion
Serial.println(value);
if (value == 0) { //Switch 1
//temp
analogWrite(led1, 0);
/*
if((char)payload[1] == '0') { //LED off
analogWrite(led1, 0);
}
else if((char)payload[1] == '1') { //LED on
analogWrite(led1, 200);
}
else if((char)payload[1] == '2') { //LED dimmed for night mode
analogWrite(led1, 100);
}
*/
}
if(value == 1) { //Switch 2
//temp
analogWrite(led1, 255);
/*
if((char)payload[1] == '0') { //LED off
analogWrite(led1, 0);
}
else if((char)payload[1] == '1') { //LED on
analogWrite(led1, 200);
}
else if((char)payload[1] == '2') { //LED dimmed for night mode
analogWrite(led1, 100);
}
*/
}
}
void setup_wifi() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("WiFi connected at " + WiFi.localIP());
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
// Once connected, publish an announcement...
// ... and resubscribe
client.subscribe(inTopic);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
delay(5000);
I don't think you can put long delays in code on the NodeMCU. From my experience, long delay calls create just the situation you describe.
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