Some of you may be aware that there’s been an interesting development lately that allows you to develop, compile, and upload software for the ESP boards using the Arduino IDE.

Although I’m no great fan of the Arduino IDE, I’ve had a play around with it, and it certainly has the potential to make development much easier!

The project is in the early stages, but if development continues, there’s potential for it to make development of the ESP boards much easier. Further details can be found here: https://github.com/esp8266/Arduino

As I’m starting to struggle with the ESP SDK (I’ve managed to get some things working, but I don’t have the time to research into it as much as I’d like), I decided to see if I could convert my current wireless temperature sensor into “Arduino” code. It took a bit of work, but the code below seems to work quite well using the exact same board.

The MQTT works well (though I’ve yet to try subscribing and I’ve read there are some issues with it), and the i2c communication seems to be working fine. The code is pretty rough at this point, but I’ll tidy it up later and develop it further into my own project.

#include 
#include 
#include 
#include 

void * __dso_handle;

const char* ssid = "SSID";
const char* password = "PASSWORD";

char* topic = "TOPIC";
char* server = "MQTT SERVER";

// PIN adresses are set to GND
#define DS1631_ADDR 0x90 >> 1

WiFiClient wifiClient;
PubSubClient client(server, 1883, callback, wifiClient);

void callback(char* topic, byte* payload, unsigned int length) {
  // handle message arrived
}
String clientName;

String macToStr(const uint8_t* mac)
{
  String result;
  for (int i = 0; i < 6; ++i) {
    result += String(mac[i], 16);
    if (i < 5)
      result += ':';
  }
  return result;
}



// SETUP
void setup() {

  Serial.begin(9600);


  Wire.pins(2, 0);

  Wire.begin();             // join I2C bus
  Wire.pins(2, 0);


  // Stop conversion to be able to modify "Access Config" Register
  Wire.beginTransmission(DS1631_ADDR);
  Wire.write((int)(0x22)); // Stop conversion
  Wire.endTransmission();

  // Read "Access Config" regsiter
  Wire.beginTransmission(DS1631_ADDR);
  Wire.write((int)(0xAC)); // @AC : Acces Config
  Wire.endTransmission();
  Wire.requestFrom(DS1631_ADDR, 1); //Reade 1 byte
  Wire.available();
  int AC = Wire.read(); // receive a byte

  Serial.print("Acces Config (Before): "); Serial.print(AC);  Serial.println("");

  // WRITE into "Access Config" Register
  Wire.beginTransmission(DS1631_ADDR);
  Wire.write(0xAC); // @AC : Acces Config
  Wire.write(0x0C); // Continuous conversion & 12 bits resolution
  Wire.endTransmission();

  // READ "Access Config" register
  Wire.beginTransmission(DS1631_ADDR);
  Wire.write((int)(0xAC)); // @AC : Acces Config
  Wire.endTransmission();
  Wire.requestFrom(DS1631_ADDR, 1);
  Wire.available();
  AC = Wire.read();

  Serial.print("Acces Config (AFTER): "); Serial.print(AC);  Serial.println("");

  // START conversion to get T°
  Wire.beginTransmission(DS1631_ADDR);
  Wire.write((int)(0x51)); // Start Conversion
  Wire.endTransmission();



  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  // Generate client name based on MAC address and last 8 bits of microsecond counter

  clientName += "esp8266-";
  uint8_t mac[6];
  WiFi.macAddress(mac);
  clientName += macToStr(mac);
  clientName += "-";
  clientName += String(micros() & 0xff, 16);

  Serial.print("Connecting to ");
  Serial.print(server);
  Serial.print(" as ");
  Serial.println(clientName);

  if (client.connect((char*) clientName.c_str())) {
    Serial.println("Connected to MQTT broker");
    Serial.print("Topic is: ");
    Serial.println(topic);


  }

  else {
    Serial.println("MQTT connect failed");
    Serial.println("Will reset and try again...");
    abort();
  }

  // Setup Serial connection
  Serial.begin(9600);
  Serial.println("");
  Serial.println("-----------------------------------");
  Serial.println("DS1631 test: Temp. sensor");
  Serial.println("-----------------------------------");
  Serial.println("");
}

int itoa(int value, char *sp, int radix)
{
  char tmp[16];// be careful with the length of the buffer
  char *tp = tmp;
  int i;
  unsigned v;

  int sign = (radix == 10 && value < 0);
  if (sign)
    v = -value;
  else
    v = (unsigned)value;

  while (v || tp == tmp)
  {
    i = v % radix;
    v /= radix; // v/=radix uses less CPU clocks than v=v/radix does
    if (i < 10)       *tp++ = i + '0';     else       *tp++ = i + 'a' - 10;   }   int len = tp - tmp;   if (sign)   {     *sp++ = '-';     len++;   }   while (tp > tmp)
    *sp++ = *--tp;

  return len;
}

// Main Loop
void loop() {

  //READ T°
  Wire.beginTransmission(DS1631_ADDR);
  Wire.write((int)(0xAA)); // @AA : Temperature
  Wire.endTransmission();
  Wire.requestFrom(DS1631_ADDR, 2); // READ 2 bytes
  Wire.available(); // 1st byte
  uint8_t Th = Wire.read(); // receive a byte
  Wire.available(); // 2nd byte
  uint8_t Tl = Wire.read(); // receive a byte


  // T° processing
  //if(Th>=0x80) //if sign bit is set, then temp is negative
  // Th = Th - 256;


  uint16_t temp = (uint16_t)((Th << 8) + Tl);   temp >>= 4;

  float T_dec = temp * 0.0625;

  // Display T° on "Serial Monitor"
  Serial.print("Temperature : ");
  Serial.print(T_dec);   Serial.print(" degC / ");
  Serial.print("Th register: "); Serial.print(Th);  Serial.print(" / ");
  Serial.print("Tl register: "); Serial.print(Tl);  Serial.println("");

  if (client.connected()) {
    Serial.print("Sending payload: ");
    Serial.println(Th);

    char buf[10];
    itoa(temp, buf, 10);

    if (client.publish(topic, buf)) {
      Serial.println("Publish ok");
    }
    else {
      Serial.println("Publish failed");
    }
  }
  else {
    if (client.connect((char*) clientName.c_str())) {
      Serial.println("Connected to MQTT broker");
      Serial.print("Topic is: ");
      Serial.println(topic);
      Serial.print("Sending payload: ");
      Serial.println(Th);

      char buf[10];
      itoa(temp, buf, 10);

      if (client.publish(topic, buf)) {
        Serial.println("Publish ok");
      }
      else {
        Serial.println("Publish failed");
      }

    }

    else {
      Serial.println("MQTT connect failed");
      Serial.println("Will reset and try again...");
      abort();
    }
  }

  // Wait 1s before restart
  delay(60000);
}

I’ve had the above working for a few hours now and so far it’s all working well.

I’ve also managed to change the rx and tx pins to outputs which I’ll be experimenting with more very soon.

Advertisements