ESP8266 - analogRead(A0) return 65535



  • Hello,
    Something stange with MyControllerDevice Lib, I cannot read analogRead(A0) correctly, always return 65535 value .
    Is anybody have any idea of why ?

    void sendLUX(){
    McMessage _message;
    Serial.print("Envoie de Luxmeter:");
    Serial.println(analogRead(A0));
    _message.update(SENSOR_LUXMETRE, C_SET, V_LEVEL);
    send(_message.set(analogRead(A0)));
    }

    MC[I]: Initialization done...
    Envoie de Luxmeter:65535


  • ADMIN



  • @jkandasa
    In fact i don't really know where i have to post ... what is your suggest about it ?
    Thanks


  • ADMIN

    @Stephan35 If you see any issues and do you think it is bug? Please raise it on GitHub. If you queries please raise it here.



  • @jkandasa
    In fact To be fair, for now I am not able to judge if this is a bug or not, so I will not post anymore on github. :laughing:



  • Can you show your exact working example?



  • @njbuch said:

    Can you show your exact working example?

    Here is my full example :

    #include <MyController.h>
    
    MyController mc;
    
    
    
    
    #define SENSOR_LED_BLEU "LED BLEU"
    #define SENSOR_LED_ROUGE "LED ROUGE"
    #define SENSOR_LED_VERTE "LED VERTE"
    #define SENSOR_LUXMETRE "LUXMETRE"
    #define BP_GPIO4 "Bouton RESET"
    #define BP_GPIO5 "Bouton Test 1"
    #define BP_GPIO14 "Bouton Test 2"
    
    //Relay pins
    #define PIN_RELAY_1 2
    #define PIN_BP_GPIO4 4
    #define PIN_BP_GPIO5 5
    #define PIN_LED_VERTE 12
    #define PIN_LED_BLEU 13
    #define PIN_BP_GPIO14 14
    #define PIN_LED_ROUGE 15
    
    
    
    //Sensors
    #define SENSOR_RELAY_1 "relay-1"
    
    
    
    
    unsigned long lastRecu = 0;
    bool Gpio5_Checked = false;
    bool Gpio14_Checked = false;
    unsigned long Gpio5_millis = 0;
    unsigned long Gpio14_millis = 0;
    
    unsigned long Scan_Gpio = 0;
    int Gpio4_S = 0; // Gpio 4 state
    int Gpio5_S = 0; // Gpio 5 state
    int Gpio14_S = 0; // Gpio 14 state
    int Gpio4_LS = 0; // Gpio 4 Last state
    int Gpio5_LS = 0; // Gpio 5 Last state
    int Gpio14_LS = 0; // Gpio 14 Last state
    
    
    //in_esp/ESP_8C0125/LUXMETRE/C_SET/V_LIGHT_LEVEL/0
    //n_esp/ESP_8C0125/LUXMETRE/C_SET/V_LEVEL/0
    
    //out_esp/ESP_8C0125/SENSOR_BC/C_INTERNAL/LUXMETRE/0
    //_message.update(BC_SENSOR, C_INTERNAL, SENSOR_LUXMETRE);
    
    
    void sendLUX(){
      McMessage _message;
      //_message.update(SENSOR_LUXMETRE, C_SET, "V_LIGHT_LEVEL");
      //char _luxmeter[21];
     // snprintf_P(_luxmeter, 20, "%s", analogRead(A0) );
      Serial.print("Envoie de Luxmeter:");
      Serial.println(analogRead(A0));
      //snprintf_P(_luxmeter, 20, "%d", String(analogRead(A0)));
      ////send(_message.set(_luxmeter));
      _message.update(SENSOR_LUXMETRE, C_SET, V_LEVEL);
      //snprintf_P(_luxmeter, 20, "%d", analogRead(A0));
      send(_message.set(analogRead(A0)));
    }
    
    //in_esp/ESP_8C0125/Bouton Test/C_SET/V_TRIPPED/0
    /*
    void sendPin5(){
      McMessage _message;
      _message.update(BP_GPIO5, C_SET, V_TRIPPED);
      if (digitalRead(PIN_BP_GPIO5) == HIGH){
      	send(_message.set(1));
      	Serial.println("BP = 1");
      }
      if (digitalRead(PIN_BP_GPIO5) == LOW){
      	send(_message.set(0));
      	Serial.println("BP = 0");
      }
    }
    void sendPin14(){
      McMessage _message;
      _message.update(BP_GPIO14, C_SET, V_TRIPPED);
      if (digitalRead(PIN_BP_GPIO14) == HIGH){
      	send(_message.set(1));
      	Serial.println("BP2 = 1");
      }
      if (digitalRead(PIN_BP_GPIO14) == LOW){
      	send(_message.set(0));
      	Serial.println("BP2 = 0");
      }
    }
    */
    
    void sendPin4(int val){
      	McMessage _message;
      	_message.update(BP_GPIO4, C_SET, V_STATUS);
      	send(_message.set(val));
      	Serial.print("Changement de Pin4 : ");
      	Serial.println(val);
    }
    
    void sendPin5(int val){
      	McMessage _message;
      	_message.update(BP_GPIO5, C_SET, V_STATUS);
      	send(_message.set(val));
      	Serial.print("Changement de Pin5 : ");
      	Serial.println(val);
    }
    void sendPin14(int val){
      	McMessage _message;
      	_message.update(BP_GPIO14, C_SET, V_STATUS);
      	send(_message.set(val));
      	Serial.print("Changement de Pin14 : ");
      	Serial.println(val);
    }
    
    /*
    void Pin5Change(){
    	if (millis() - Gpio5_millis > 75){
        	delayMicroseconds(32000);
        	Serial.println("\n*** Pin5 Interrupt");
        	Gpio5_Checked = true;
        	Gpio5_millis = millis();
        	detachInterrupt(PIN_BP_GPIO5);
        }
    }
    void Pin14Change(){
    	if (millis() - Gpio14_millis > 75){
    	    delayMicroseconds(32000);
    	    Serial.println("\n*** Pin14 Interrupt");
    	    Gpio14_Checked = true;
    	    Gpio14_millis = millis();
    	    detachInterrupt(PIN_BP_GPIO14);
    	}
    }
    */
    
    
    
    void before(){
      //Your code before this library inialize
    }
    
    void presentation(){
      //Send node name and version information
      sendSketchInfo("Module de test", "1.0.0");
    
      //Send sensor name and type
      present(S_BINARY, SENSOR_RELAY_1, "Relay 01");
      //request current status
      request(SENSOR_RELAY_1, V_STATUS);
    
    
      present(S_BINARY, SENSOR_LED_BLEU, SENSOR_LED_BLEU);
      //request current status
      request(SENSOR_LED_BLEU, V_STATUS);
    
      present(S_BINARY, SENSOR_LED_ROUGE, SENSOR_LED_ROUGE);
      //request current status
      request(SENSOR_LED_ROUGE, V_STATUS);
    
      present(S_BINARY, SENSOR_LED_VERTE, SENSOR_LED_VERTE);
      //request current status
      request(SENSOR_LED_VERTE, V_STATUS);
    
      //present(S_LIGHT, SENSOR_LUXMETRE, SENSOR_LUXMETRE);
      //request(SENSOR_LUXMETRE, "V_LIGHT_LEVEL");
    
    
      present(S_DIMMER, BP_GPIO4, BP_GPIO4);
      request(BP_GPIO4, V_STATUS);
    
      present(S_DOOR, BP_GPIO5, BP_GPIO5);
      request(BP_GPIO5, V_STATUS);
    
      present(S_DOOR, BP_GPIO14, BP_GPIO14);
      request(BP_GPIO14, V_STATUS);
    
    }
    
    void receiveTime(unsigned long mcTimestamp){
      //Do something with received time
    }
    
    void receive(McMessage &message) {
    	if(message.isSensorOf(SENSOR_RELAY_1) && message.isSubTypeOf(V_STATUS)){
    	    digitalWrite(PIN_RELAY_1, message.getUInt());
    	}
    	if(message.isSensorOf(SENSOR_LED_BLEU) && message.isSubTypeOf(V_STATUS)){
    	    digitalWrite(PIN_LED_BLEU, message.getUInt());
    	}
    	if(message.isSensorOf(SENSOR_LED_ROUGE) && message.isSubTypeOf(V_STATUS)){
    	    digitalWrite(PIN_LED_ROUGE, message.getUInt());
    	}
    	if(message.isSensorOf(SENSOR_LED_VERTE) && message.isSubTypeOf(V_STATUS)){
    	    digitalWrite(PIN_LED_VERTE, message.getUInt());
    	}
    }
    
    
    void setup() {
        pinMode(A0, INPUT);
      	pinMode(PIN_BP_GPIO4, INPUT);
      	pinMode(PIN_BP_GPIO5, INPUT);
      	pinMode(PIN_BP_GPIO14, INPUT);
      	pinMode(PIN_RELAY_1, OUTPUT);
      	pinMode(PIN_LED_BLEU, OUTPUT);
      	pinMode(PIN_LED_ROUGE, OUTPUT);
      	pinMode(PIN_LED_VERTE, OUTPUT);
      	if(!isSystemConfigured()){
        //You can check, does this node/device configured. If no you can display/blink some led from here.
      	}
      //attachInterrupt(PIN_BP_GPIO5, (Pin5Change), CHANGE);
      //attachInterrupt(PIN_BP_GPIO14, (Pin14Change), CHANGE); //digitalPinToInterrupt(x)
    }
    
    void loop() {
    	mc.loop();
    	long now = millis();
    	if (now - Scan_Gpio > 200 ){
    		Gpio4_S = digitalRead(PIN_BP_GPIO4);
    		Gpio5_S = digitalRead(PIN_BP_GPIO5);
    		Gpio14_S = digitalRead(PIN_BP_GPIO14);
    		if (Gpio4_S != Gpio4_LS ){
    			sendPin4(((Gpio4_S == HIGH) ? 1 : 0));
    			Gpio4_LS = Gpio4_S;
    		}
    		if (Gpio5_S != Gpio5_LS ){
    			sendPin5(((Gpio5_S == HIGH) ? 1 : 0));
    			Gpio5_LS = Gpio5_S;
    		}
    		if (Gpio14_S != Gpio14_LS ){
    			sendPin14(((Gpio14_S == HIGH) ? 1 : 0));
    			Gpio14_LS = Gpio14_S;
    		}
    		Scan_Gpio = now;
    		Serial.print(".");
    
    	}
      
      /*
      if (Gpio5_Checked) {
      	Gpio5_Checked = false;
      	sendPin5();
      	yield();
      	attachInterrupt(PIN_BP_GPIO5, (Pin5Change), CHANGE);
      }
      if (Gpio14_Checked) {
      	Gpio14_Checked = false;
      	sendPin14();
      	yield();
      	attachInterrupt(PIN_BP_GPIO14, (Pin14Change), CHANGE);
      }
      */
      //You can add your logic here.
      	if (now - lastRecu > 30000 ) {
        	lastRecu = now;
        	//sendRSSI();
        	sendLUX();
        	//send(SENSOR_LUXMETRE, analogRead(A0));
      	}
    }
    
    

    Take care of this : need to be improve , i am a beginer with MyController , and this example is not clean !

    Hope will help you

    Best regards.



  • Thanks will try out a variant of this! :)

    @Stephan35 I dont see any presentation of the SENSOR_LUXMETRE it seems to have been removed from your code, why is that?

    Do you have a light-color sensor? And what is the exact sensor-type? Do you have a link?



  • I have tried, and I am now realizing that the ADC on ESP8266 has a smaller voltage span at 0.0 - 1.0V.

    I will need to adjust my voltage divider and retry, I am guessing thats why the reading keeps very high, because it never goes below 1.0V. Let us know what happens in your lab....



  • @njbuch
    You have to adjust electonic input with a input divide voltage :
    0_1489325656696_photo_bb.gif.png

    You will find full of example to explain how to.

    A i tried to explain, my example need to be improve , tested, and modified as need.

    Best regards.



  • @Stephan35 Indeed but my voltage divider used was aiming for 0.0 - 3.3 and resulting output was over 1 volt most of the time, which means that I am reading 65535 all the time. I need a new one.

    I am looking in my bins after a few resistors that matches my photo-resistor specs. Will revert when successfull. I am using: http://www.ohmslawcalculator.com/voltage-divider-calculator to find the right combination.



  • @njbuch said:

    @Stephan35 Indeed but my voltage divider used was aiming for 0.0 - 3.3 and resulting output was over 1 volt most of the time, which means that I am reading 65535 all the time. I need a new one.

    I am looking in my bins after a few resistors that matches my photo-resistor specs. Will revert when successfull. I am using: http://www.ohmslawcalculator.com/voltage-divider-calculator to find the right combination.

    Sorry,
    answer is there : analogRead(A0) return 65535

    You have to modify Myconfig.h

    #define ENABLE_READ_VCC
    

Log in to reply
 

8
Online

439
Users

231
Topics

1378
Posts

Looks like your connection to MYCONTROLLER.ORG was lost, please wait while we try to reconnect.