How to - Standalone node on esp8266 using wifi only - sketch problem



  • Hi,

    I'm trying to use a nodemcu 0.9 as a standalone node using wifi and not the radio to connect to my Domoticz. Using the ESP8266 gateway i'm facing a problem. I've added the setup and loop part to it from a sketch I used earlier on a node with a traditional radio on it.
    My guess is that trying to use the wifi connection to send data the commands in a sketch must be slightly different, but I have no idea how and can't find useful examples. Using this code gives me an error compiling in the line MySensor gw; , saying 'MySensor' does not name a type.

    Can anyone help me out?
    Thanks in advance!!

    Insert Code Here
     #include <EEPROM.h>
    #include <SPI.h>
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    
    // Use a bit lower baudrate for serial prints on ESP8266 than default in MyConfig.h
    #define MY_BAUD_RATE 9600
    
    // Enables and select radio type (if attached)
    //#define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    #define MY_GATEWAY_ESP8266
    
    #define MY_ESP8266_SSID "myssid"
    #define MY_ESP8266_PASSWORD "wpa key"
    
    // Enable UDP communication
    //#define MY_USE_UDP
    
    // Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP)
    #define MY_IP_ADDRESS 192,168,2,242
    
    // If using static ip you need to define Gateway and Subnet address as well
    #define MY_IP_GATEWAY_ADDRESS 192,168,2,254
    #define MY_IP_SUBNET_ADDRESS 255,255,255,0
    
    // The port to keep open on node server mode 
    #define MY_PORT 5003      
    
    // How many clients should be able to connect to this gateway (default 1)
    #define MY_GATEWAY_MAX_CLIENTS 2
    
    // Controller ip address. Enables client mode (default is "server" mode). 
    // Also enable this if MY_USE_UDP is used and you want sensor data sent somewhere. 
    #define MY_CONTROLLER_IP_ADDRESS 192, 168, 2, 1
    
    // Enable inclusion mode
    #define MY_INCLUSION_MODE_FEATURE
    
    // Enable Inclusion mode button on gateway
    // #define MY_INCLUSION_BUTTON_FEATURE
    // Set inclusion mode duration (in seconds)
    #define MY_INCLUSION_MODE_DURATION 60 
    // Digital pin used for inclusion mode button
    #define MY_INCLUSION_MODE_BUTTON_PIN  3 
    
     
    // Flash leds on rx/tx/err
    // #define MY_LEDS_BLINKING_FEATURE
    // Set blinking period
    // #define MY_DEFAULT_LED_BLINK_PERIOD 300
    
    // Led pins used if blinking feature is enabled above
    #define MY_DEFAULT_ERR_LED_PIN 16  // Error led pin
    #define MY_DEFAULT_RX_LED_PIN  16  // Receive led pin
    #define MY_DEFAULT_TX_LED_PIN  16  // the PCB, on board LED
    
    #if defined(MY_USE_UDP)
      #include <WiFiUDP.h>
    #else
      #include <ESP8266WiFi.h>
    #endif
    
    #include <MySensor.h>
    
    #define CHILD_ID_LIGHT 0
    #define LIGHT_SENSOR_ANALOG_PIN 1
    #define MIN 5  // Liquid
    #define MAX 114 // Air
    
    int sensorPin = A0; // select the input pin for the potentiometer
    int sensorValue = 0; // variable to store the value coming from the sensor
    
    
    MySensor gw;
    MyMessage msg(CHILD_ID_LIGHT, V_HUM);
    int lastLightLevel;
    int oldBatteryPcnt = 0;
    
    
    void setup(){
    
    Serial.begin(9600);
    
    gw.begin();
    
    // Send the sketch version information to the gateway and Controller
    gw.sendSketchInfo("Mobile Sensor 1", "Kruiden");
    
    // Register all sensors to gateway (they will be created as child devices)
    gw.present(CHILD_ID_LIGHT, S_HUM);
    }
    
    void loop(){
    // read the value from the sensor:
    sensorValue = analogRead(sensorPin);
    delay(1000);
    int lastsensorValue = sensorValue;
     
    Serial.println(sensorValue);
    int sendValue =  map(sensorValue, 5, 114, 0, 100); 
      gw.send( msg.set( sendValue ) ); 
    gw.sleep(90);
    }
     
    ` ` `

  • Admin

    The sketch structure is slightly different in 2.0. You no longer need to construct the MySensors class and reference it using gw.

    How to convert old sketches has been described here:
    https://docs.google.com/document/d/1NKq5uuNdnxF5jWnum7VT32mbKLjuvqlx2A5D1qQ-H3Q/edit#heading=h.t5sdmpn6jz2i

    I've changed the things you need to do below (but I haven't verified it in any IDE).

    #include <EEPROM.h>
    #include <SPI.h>
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    
    // Use a bit lower baudrate for serial prints on ESP8266 than default in MyConfig.h
    #define MY_BAUD_RATE 9600
    
    // Enables and select radio type (if attached)
    //#define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    #define MY_GATEWAY_ESP8266
    
    #define MY_ESP8266_SSID "myssid"
    #define MY_ESP8266_PASSWORD "wpa key"
    
    // Enable UDP communication
    //#define MY_USE_UDP
    
    // Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP)
    #define MY_IP_ADDRESS 192,168,2,242
    
    // If using static ip you need to define Gateway and Subnet address as well
    #define MY_IP_GATEWAY_ADDRESS 192,168,2,254
    #define MY_IP_SUBNET_ADDRESS 255,255,255,0
    
    // The port to keep open on node server mode 
    #define MY_PORT 5003      
    
    // How many clients should be able to connect to this gateway (default 1)
    #define MY_GATEWAY_MAX_CLIENTS 2
    
    // Controller ip address. Enables client mode (default is "server" mode). 
    // Also enable this if MY_USE_UDP is used and you want sensor data sent somewhere. 
    #define MY_CONTROLLER_IP_ADDRESS 192, 168, 2, 1
    
    // Enable inclusion mode
    #define MY_INCLUSION_MODE_FEATURE
    
    // Enable Inclusion mode button on gateway
    // #define MY_INCLUSION_BUTTON_FEATURE
    // Set inclusion mode duration (in seconds)
    #define MY_INCLUSION_MODE_DURATION 60 
    // Digital pin used for inclusion mode button
    #define MY_INCLUSION_MODE_BUTTON_PIN  3 
    
     
    // Flash leds on rx/tx/err
    // #define MY_LEDS_BLINKING_FEATURE
    // Set blinking period
    // #define MY_DEFAULT_LED_BLINK_PERIOD 300
    
    // Led pins used if blinking feature is enabled above
    #define MY_DEFAULT_ERR_LED_PIN 16  // Error led pin
    #define MY_DEFAULT_RX_LED_PIN  16  // Receive led pin
    #define MY_DEFAULT_TX_LED_PIN  16  // the PCB, on board LED
    
    #if defined(MY_USE_UDP)
      #include <WiFiUDP.h>
    #else
      #include <ESP8266WiFi.h>
    #endif
    
    #include <MySensor.h>
    
    #define CHILD_ID_LIGHT 0
    #define LIGHT_SENSOR_ANALOG_PIN 1
    #define MIN 5  // Liquid
    #define MAX 114 // Air
    
    int sensorPin = A0; // select the input pin for the potentiometer
    int sensorValue = 0; // variable to store the value coming from the sensor
    
    
    MyMessage msg(CHILD_ID_LIGHT, V_HUM);
    int lastLightLevel;
    int oldBatteryPcnt = 0;
    
    
    void setup(){
    // Send the sketch version information to the gateway and Controller
    sendSketchInfo("Mobile Sensor 1", "Kruiden");
    
    // Register all sensors to gateway (they will be created as child devices)
    present(CHILD_ID_LIGHT, S_HUM);
    }
    
    void loop(){
    // read the value from the sensor:
    sensorValue = analogRead(sensorPin);
    delay(1000);
    int lastsensorValue = sensorValue;
     
    Serial.println(sensorValue);
    int sendValue =  map(sensorValue, 5, 114, 0, 100); 
      send( msg.set( sendValue ) ); 
    sleep(90);
    }```


  • Thanks very much for your quick reply! Now suddenly I get compiling errors saying there is no such file as espwifi8266.h, which is weird because it was compiling other sketches just fine. Reinstalling libraries and the board manager now....

    I'll let you know whether this works. Thanks again!


  • Admin

    You probably accidentally changed board type in the IDE.



  • Nope.. That what I thought too. Getting this error Arduino: 1.6.5 (Windows 8.1), Board:"NodeMCU 0.9 (ESP-12 Module), 80 MHz, 9600"

    In file included from C:\Users\braml\AppData\Roaming\Arduino15\packages\esp8266\hardware\esp8266\1.6.5-947-g39819f0\libraries\ESP8266WiFi\src/ESP8266WiFi.h:32:0,
    from esp_gateway_aangepast.ino:63:
    C:\Users\braml\AppData\Roaming\Arduino15\packages\esp8266\hardware\esp8266\1.6.5-947-g39819f0\libraries\ESP8266WiFi\src/WiFiClient.h: In instantiation of 'size_t WiFiClient::write(T&, size_t) [with T = char*; size_t = unsigned int]':
    C:\Users\braml\Documents\Arduino\libraries\MySensors/core/MyGatewayTransportEthernet.cpp:164:52: required from here
    C:\Users\braml\AppData\Roaming\Arduino15\packages\esp8266\hardware\esp8266\1.6.5-947-g39819f0\libraries\ESP8266WiFi\src/WiFiClient.h:113:36: error: request for member 'available' in 'source', which is of non-class type 'char*'
    size_t left = source.available();
    ^
    C:\Users\braml\AppData\Roaming\Arduino15\packages\esp8266\hardware\esp8266\1.6.5-947-g39819f0\libraries\ESP8266WiFi\src/WiFiClient.h:117:5: error: request for member 'read' in 'source', which is of non-class type 'char*'
    source.read(buffer.get(), will_send);
    ^
    Multiple libraries were found for "EEPROM.h"

    Used: C:\Users\braml\Documents\Arduino\libraries\EEPROM

    Not used: C:\Users\braml\AppData\Roaming\Arduino15\packages\esp8266\hardware\esp8266\1.6.5-947-g39819f0\libraries\EEPROM

    Multiple libraries were found for "SPI.h"

    Used: C:\Users\braml\Documents\Arduino\libraries\SPI

    Not used: C:\Users\braml\AppData\Roaming\Arduino15\packages\esp8266\hardware\esp8266\1.6.5-947-g39819f0\libraries\SPI

    Fout bij compileren.

    I've removed all libraries and reinstalled the board manager.. Have you seen this before?



  • Hi,

    After a day of trying I'm starting to drive mad... After several reinstalls of Arduino IDE, Java and my libraries it seems like I'm almost there, but I realy don't have a clue on how to fix this anymore..I'm trying to use this sketch:

    Insert Code Here

    #include <EEPROM.h>
    #include <SPI.h>
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    
    // Use a bit lower baudrate for serial prints on ESP8266 than default in MyConfig.h
    #define MY_BAUD_RATE 9600
    
    // Enables and select radio type (if attached)
    //#define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    #define MY_GATEWAY_ESP8266
    
    #define MY_ESP8266_SSID "ssid"
    #define MY_ESP8266_PASSWORD "wpa"
    
    // Enable UDP communication
    #define MY_USE_UDP
    
    // Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP)
    #define MY_IP_ADDRESS 192,168,2,242
    
    // If using static ip you need to define Gateway and Subnet address as well
    #define MY_IP_GATEWAY_ADDRESS 192,168,2,254
    #define MY_IP_SUBNET_ADDRESS 255,255,255,0
    
    // The port to keep open on node server mode 
    #define MY_PORT 5003      
    
    // How many clients should be able to connect to this gateway (default 1)
    #define MY_GATEWAY_MAX_CLIENTS 2
    
    // Controller ip address. Enables client mode (default is "server" mode). 
    // Also enable this if MY_USE_UDP is used and you want sensor data sent somewhere. 
    //#define MY_CONTROLLER_IP_ADDRESS 192, 168, 178, 68
    
    // Enable inclusion mode
    #define MY_INCLUSION_MODE_FEATURE
    
    // Enable Inclusion mode button on gateway
    // #define MY_INCLUSION_BUTTON_FEATURE
    // Set inclusion mode duration (in seconds)
    #define MY_INCLUSION_MODE_DURATION 60 
    // Digital pin used for inclusion mode button
    #define MY_INCLUSION_MODE_BUTTON_PIN  3 
    
     
    // Flash leds on rx/tx/err
    // #define MY_LEDS_BLINKING_FEATURE
    // Set blinking period
    // #define MY_DEFAULT_LED_BLINK_PERIOD 300
    
    // Led pins used if blinking feature is enabled above
    #define MY_DEFAULT_ERR_LED_PIN 16  // Error led pin
    #define MY_DEFAULT_RX_LED_PIN  16  // Receive led pin
    #define MY_DEFAULT_TX_LED_PIN  16  // the PCB, on board LED
    
    #if defined(MY_USE_UDP)
      #include <WiFiUDP.h>
    #else
      #include <ESP8266WiFi.h>
    #endif
    
    #include <MySensor.h>
    
    #define CHILD_ID_LIGHT 0
    #define LIGHT_SENSOR_ANALOG_PIN 1
    #define MIN 5  // Liquid
    #define MAX 114 // Air
    
    int sensorPin = A0; // select the input pin for the potentiometer
    int sensorValue = 0; // variable to store the value coming from the sensor
    
    void receive(const MyMessage &msg) {}
    MyMessage msg(CHILD_ID_LIGHT, V_HUM);
    
    void setup() { 
    }
    
    void presentation() {
      
      // Present locally attached sensors here    
      // Send the sketch version information to the gateway and Controller
          sendSketchInfo("Mobile Sensor 1", "Kruiden");
    
    // Register all sensors to gateway (they will be created as child devices)
          present(CHILD_ID_LIGHT, S_HUM);
    }
    
    
    void loop() {
      // Send locally attached sensors data here
      // read the value from the sensor:
    sensorValue = analogRead(sensorPin);
    delay(1000);
    int lastsensorValue = sensorValue;
     
    Serial.println(sensorValue);
    int sendValue =  map(sensorValue, 5, 114, 0, 100); 
    send( msg.set( sendValue ) ); 
    sleep(90);
    }
    

    Giving me this error trying to compile..

    Arduino: 1.6.5 (Windows 8.1), Board:"NodeMCU 0.9 (ESP-12 Module), 80 MHz, Serial, 115200, 4M (3M SPIFFS)"
    
    GatewayESP8266.ino: In function 'void presentation()':
    GatewayESP8266:153: error: 'sendSketchInfo' was not declared in this scope
    GatewayESP8266:156: error: 'present' was not declared in this scope
    GatewayESP8266.ino: In function 'void loop()':
    GatewayESP8266:169: error: 'send' was not declared in this scope
    GatewayESP8266:170: error: 'sleep' was not declared in this scope
    'sendSketchInfo' was not declared in this scope
    
    
    

    Maybe I'm just being stupid, but I don't see what to do anymore. Hope there is someone who can help me with this.


  • Admin

    What is your sketchbook location set to in the IDE/Preferences?

    Do you have a folder "libraries" in this location containing the MySensors library?

    <sketchbook location>/libraries/MySensors/...

    And you should be using this (as your sketch is based on 2.0.0):
    https://github.com/mysensors/Arduino/archive/development.zip



  • My sketchbook location is set to E:\documenten\Arduino and it contains the MySensors library. I think I was using 1.5 before so I removed all libraries and extracted everything from the 2.0 zip to that folder, Now compiling gives me this:

    Arduino: 1.6.5 (Windows 8.1), Board:"NodeMCU 0.9 (ESP-12 Module), 80 MHz, Serial, 115200, 4M (3M SPIFFS)"
    
    In file included from E:\Documenten\Arduino\libraries\MySensors/MySensor.h:219:0,
                     from GatewayESP8266.ino:133:
    E:\Documenten\Arduino\libraries\MySensors/core/MyGatewayTransportEthernet.cpp:46:25: error: 'WiFiClient' does not name a type
      #define EthernetClient WiFiClient
                             ^
    E:\Documenten\Arduino\libraries\MySensors/core/MyGatewayTransportEthernet.cpp:64:9: note: in expansion of macro 'EthernetClient'
      static EthernetClient clients[MY_GATEWAY_MAX_CLIENTS];
             ^
    E:\Documenten\Arduino\libraries\MySensors/core/MyGatewayTransportEthernet.cpp: In function 'bool gatewayTransportInit()':
    E:\Documenten\Arduino\libraries\MySensors/core/MyGatewayTransportEthernet.cpp:103:9: error: 'WiFi' was not declared in this scope
       (void)WiFi.begin(MY_ESP8266_SSID, MY_ESP8266_PASSWORD);
             ^
    E:\Documenten\Arduino\libraries\MySensors/core/MyGatewayTransportEthernet.cpp:107:27: error: 'WL_CONNECTED' was not declared in this scope
       while (WiFi.status() != WL_CONNECTED)
                               ^
    E:\Documenten\Arduino\libraries\MySensors/core/MyGatewayTransportEthernet.cpp: In function 'bool _readFromClient(uint8_t)':
    E:\Documenten\Arduino\libraries\MySensors/core/MyGatewayTransportEthernet.cpp:194:10: error: 'clients' was not declared in this scope
       while (clients[i].connected() && clients[i].available()) {
              ^
    Multiple libraries were found for "WiFiUdp.h"
    
     Used: C:\Users\braml\AppData\Roaming\Arduino15\packages\esp8266\hardware\esp8266\2.0.0\libraries\ESP8266WiFi
    
     Not used: C:\Program Files (x86)\Arduino\libraries\WiFi
    
    Fout bij compileren.
    
      Dit rapport zou meer informatie hebben met
      "Tijdens de compilatie uitgebreide uitvoer weergeven"
      ingeschakeld in Bestand > Voorkeuren.
    
    

    Edit:

    Manualy include the eps8266wifi library on top of the sketch did the trick. Despite the fact that these libraries where included later on in the sketch. @hek, thanks for puting me on the right track!



  • I am having the same problem... same exact error, but on a Sparkfun Thing Dev board 8266.. Trying to put the include up above in the GatewayESP8266 code didn't work..

    I've uninstalled everything arduino, including the Arduino15 directory under the AppData\Local directory, and have installed completely from scratch...

    I can't be the only one having this problem still, am I ? Anyone know what's going on?

    Thanks in advance.. I feel SO close to having this working!!





  • For the record, I have successfully built the code for a V2 MySensors ESP8266 gateway. The issue described in the first lines of this thread was caused by using V2.3 or less of the ESP8266 Arduino library, which didn't contain the most recent Wifi class refactoring.

    See the following post I made on GitHub
    https://github.com/mysensors/MySensors/issues/625



  • @bram81 Thank you so much ... I was nearly getting mad - without your "edit and the include esp8266wifi hint" I'd still be pulling out my beard in frustration !


 

374
Online

7.8k
Users

8.7k
Topics

93.1k
Posts