Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By Erni
#32185 From the serial output you posted it look like the Esp dosn't connect to your WiFi network, is that correct?
if that's the case I would assume that your ssid,password maybe wrong.

Also remember that the ESP8266 is a 3.3V device, so if you connect a 5V Arduino you will need a level converter.
User avatar
By bogdanioanliviu
#32318 Hello all
I have tried the code from Erni and it is working ok.
Now i have modified the code to be able to connect to mqtt and that it is working as well .
My problem is the following :
If on the esp that is the master i set up getValue like this:
Code: Select allint getValue() {
  Wire.beginTransmission(SlaveDeviceId);
  Wire.write(1); // Transfer command ("1") to get X sensor value;
  Wire.endTransmission();  // Moved up here by frippe75
  delay(100);
  // GET RESPONSE
  int receivedValue1 = 0;
  String valprimit = "";
  //String c = "";
  Wire.requestFrom(SlaveDeviceId, 1);
  receivedValue1 = Wire.read();// << 8 | Wire.read(); // combine two bytes into integer
  /*if (2 <= Wire.available()) {
      //receivedValue1 = Wire.read();
    //receivedValue1 = receivedValue1 << 8;
    //receivedValue1 |= Wire.read();
  }*/
  valprimit = String(receivedValue1);
  valprimit.toCharArray(valoare, 20);
  client.publish("outTopic", valoare);
  int error = Wire.endTransmission();
 
  return receivedValue1;
}


and on the arduino mini pro that is the slave i put this:

Code: Select allcase 1:{   // Return X sensor value
      returnValue = getValue();
      Serial.print("returnValue este:"); Serial.println(returnValue);
      byte b[1];
     
      b[0] = returnValue & 0xFF;
      Serial.println(b[0]);
       Wire.write(b, 2);
break;}
case 8: {  // Return Y sensor value
      returnValue = setPin();
      byte buffer[1];
      //buffer[0] = returnValue >> 8;
      buffer[0] = returnValue & 0xff;
      Wire.write(buffer, 1);
      Serial.print(buffer[0]);
                // null last Master's command
      break;}

int getValue() {
  float val1 = analogRead(SensorPin);
  val1 = (val1*5/1024.0) - 0.5;
  val1 = val1 / 0.01;
  int val = (int) val1;
  //int val = 22;  // with a static
  //Serial.println((int)val);
  //val = (val - .5) * 100;
  //int val = analogRead(SensorPin);
  return val;
}



SO if left the code like this when i call getValue on esp i will receive some random numbers
But if i assign a static value to val on arduino and comment out "case 8" then call getval on esp i will recive a correct value of 22 and 225 or 512 when i call setpin() on esp.

I think that i do something wrong on formating the byte for send from slave to master .
Can someone help me with this error.
The idea is when i call getpin() i should receive 0 or 1 that is the state for the pin, and when i call getValue() from esp i should receive a value from 20 to 40 in the receiveValue1.
I'm trying to do this from the last 2 days :shock: :?
Gracias
User avatar
By Erni
#32325 Hi bogdanioanliviu,

As far as I can see, you are calling command 1 to get a value (2 bytes), but you only read 1 byte on the esp8266
But case 1 returns 2 byte.

In my example I let both cases return 2 bytes to avoid confusing myself.
User avatar
By bogdanioanliviu
#32329
Erni wrote:Hi bogdanioanliviu,

As far as I can see, you are calling command 1 to get a value (2 bytes), but you only read 1 byte on the esp8266
But case 1 returns 2 byte.

In my example I let both cases return 2 bytes to avoid confusing myself.



on the arduino if i put to send 2 byte b[0] and b[1] and recive those 2 values when i try to send them are something like 1319 or 2 or 7 or value without sense.
but if i send only b[0] and read that on esp and "val" has to be fixed it will send correct value.
I'm really lost write now. Let me know if you like me to post the hole code for esp and arduino.
Thank you