ending the temperature to a ThingSpeak channel using the TMP35 sensor.

This is a small and low cost implementation of IoT (Internet of Things, possibly one of the next technological revolutions). Mini-Client-Trinket-ThingSpeak The ESP8266 can be used alone thanks to the apparition of several environments like LUA, C, Micro Python etc.. In this case I wanted to keep the solution in the Arduino environment to take advantage of many ready-to-use libraries and examples. Then I realized, due the reduced space in the Trinket (5.310Kb for code), the usability of such libraries it’s not so simple. Programming the ATtiny85 with an ISP programmer will permit the use of the full 8K programming memory. Anyway was a fun project, is great to see the small ATtiny85 acting as a Web Client. Let’s make the Trinket do the job using just the Arduino IDE.


Power considerations: The ESP8266 is power hungry when is communicating with a Wi-Fi spot. It can reach 215mA in Transmit Mode. The 3.3Vregulator in the Trinket only supply 150mA max. Then we need to use another 3.3V power source. The L4931 (250 mA) is working at first. A 470 uF capacitors is needed at the output.


Wiring diagram: (….. on the works)


Making the ESP8266 breadboard friendly:  To modify the ESP8266 we need a 0.1″ 4-pin right-angle male header. The procedure is very simple:ESP8266-breadboard-09 First cut (remove) the row of pines closer to the board border. Then, using a breadboard as guide and support, sold the right-angle header. Use enough solder to obtain a good mechanical resistance.     More images in this post: https://arduining.com/2015/01/02/making-esp8266-breadboard-frienly/


Mini breadboard wiring:

MiniClient_wiring

Wire color identification:

Black: ground.

Red: +5V.

Orange: 3.3V from the L4931 regulator.

Yellow: Trinket RX data (ESP8266 TX).

Purple: Trinket TX data (ESP8266 RX).

Blue: ESP8266 reset.

Green: Trinket analog input (TMP36 Vout).


Final assembly:

MiniClient_assembled

Everything in it’s place… let’s coding…


Temperature Sensor TMP35: TMP35_chart


The Trinket Sketch: At first, just ported the code from a working example made with the Meduino NANO and the DHT22 temperature sensor, finding out that the code size will be a problem. open-style-packaging-2


Several changes were implemented to reduce the code size: Changing to the TMP35 temperature sensor because is easier to read (don’t need a library ). Avoiding the use of string manipulation functions. Avoiding the use of floating math operations. (Only integer math was used). Integrating CR and LF at the end of the strings and using print() function instead of println(). Many other changes until the compiler of the Arduino IDE announced:

Binary sketch size: 4,748 bytes (of a 5,310 byte maximum)


The Sketch:

/*ESP8266_Trinket_01.ino Arduining.com (29 APR 2015)
Sending temperature data to ThingSpeak.
Hardware:
 -Adafruit's Arduino Trinket (3.3V)
 -ESP8266(ESP-01)
 -Temperature sensor TMP35

Data update rate set by the SPERIOD value.
Using SoftwareSerial at 9600 bauds.
String and math functions avoided.
The temperature is not calibrated.
  -ATtiny85 internal reference differs a little from 1.1V
  -The TMP35 is affected by radio frequencies.

Sketch uses 4,748 bytes (89%) of program storage space. Maximum is 5,310 bytes.
-----------------------------------------------------------------------------*/

#include <SoftwareSerial.h> 

//Network parameters and server IP address:
#define mySSID    "XXXXXXXXX"           //WiFi network ID.
#define myPASS    "XXXXXXXXX"           //Password.
#define serverIP  "184.106.153.149"     //IP of ThingSpeak.com
#define ChannKEY  "XXXXXXXXXXXXXXXX"    //ThingSpeak channel key.

#define updField  "1"                   //Updating Field( 1,2,3,4,5...)
#define SPERIOD   30000                 //Sampling period (milliseconds).
#define MAXFAILS  10                    //Number of fails to RESET the ESP8266.

//Concatenating the string messages:
#define mode_MSG  "AT+CWMODE=3\r\n"       //Dual mode (Station and Access Point).
#define join_MSG  "AT+CWJAP=\"" mySSID "\",\"" myPASS "\"\r\n"      //set data to join the AP.
#define conn_MSG  "AT+CIPSTART=\"TCP\",\"" serverIP "\",80\r\n"     //TCP, ThingSpeak, port 80.
#define send_MSG  "AT+CIPSEND=46\r\n"     //Send data, size=46 (4 bytes for the value)
#define upda_MSG  "GET /update?key=" ChannKEY "&field" updField "=" //Updating Field data.
#define clos_MSG  "AT+CIPCLOSE\r\n"       //Close TCP connection.


//Trinket pins assignament:
#define RXDATA    1      // SoftwareSerial receive pin (RED LED connected).
#define TXDATA    0      // SoftwareSerial transmit pin.
#define HRESET    3      // Pin to hardware Reset the ESP8266
#define ANATEMP   1      // Analog channel 1 for temperature measurement.

int i,j;
int fails=100;           // set failures>MAXFAILS to generate initial RESET.
unsigned int temp;       // Use positive range values (0-32768)
String tempVal="00.0";   // Four characters (fixed format number).

SoftwareSerial ESPSerial(RXDATA, TXDATA);   // Creates SoftwareSerial channel.

//====================== Setup ================================================
void setup(){
  
  analogReference(INTERNAL);        //Use the 1.1V internal reference.
  pinMode(HRESET,OUTPUT);           //hardware reset for the ESP8266.
  ESPSerial.begin(9600);
  initESP();                        // ESP8266 init.

}   //END of setup.

//====================== loop =================================================
void loop(){

  while((millis() % SPERIOD)>10){};   //Wait for the next multiple of SPERIOD.

  while(ESPSerial.read()!= -1){};     //Wait until no serial data available.

//------------- Here is the temperature reading for the TMP35 ------------------
/*-------------------------------------------------------------------
    For compactness of the code, integer math is used:
    TheTMP35 has 10 mV/°C scale factor:Temp in °C = (Vout in mV)/10 
    With Vref=1.1V the range is: 0°C to +60°C, (0.1 degree resolution)
    AnalogRead value (559) => 60 degree.
    32768/1024= 32 (maximum multiplier to keep the integer positive).
    Multiplier and divider factors are: 29/27= 1.0740 close to 1100/1024.
 --------------------------------------------------------------------*/
  temp= (analogRead(ANATEMP)* 29)/27;  //value in millivolts (1.1V internal ref.)

//------ formating the integer temp to a 4 character string ---------  
  tempVal[0]= '0'+(temp/100);
  tempVal[1]= '0'+(temp%100/10);
//  tempVal[2]= '.';   //decimal point already there.
  tempVal[3]= '0'+(temp%10);

  updateValue();
  if (fails > MAXFAILS) initESP();   //Reset and init ESP8266 after some fails.

}   // END of loop.

/*-----------------------------------------------------------------------------
Function updateValue() 
-----------------------------------------------------------------------------*/
void updateValue(){
  ESPSerial.print(conn_MSG);        //Open TCP connection with ThingSpeak.
  if(waitChar('K',10000)){          //Wait for 'K' of "OK".
    delay(250);                     //Delay to receive the message "Linked"
    ESPSerial.print(send_MSG);      //Send message AT+CIPSEND= and size.
    if(waitChar('>',10000)){        //Wait for '>'.
      ESPSerial.print(upda_MSG);    //Update Field value.
//      tempVal= "13.4";
      ESPSerial.print(tempVal);
//      ESPSerial.print("20.7");    // test value
      ESPSerial.print("\r\n");      //to replace println (less code).
      fails=0;                      // clear failure counter.
      return;                       // Unlink automatically....
    }
  }
  fails++;
  ESPSerial.print(clos_MSG);       //Close the TCP connection with ThingSpeak.  
}

/*-----------------------------------------------------------------------------
Function waitChar() 
-----------------------------------------------------------------------------*/
bool waitChar(char Wchar, int duration){
  for(i=duration;i>0;i--){
    delay(1);
    if(ESPSerial.read()== Wchar)return true;
  }
  return false;
}

/*-----------------------------------------------------------------------------
Function initESP() 
-----------------------------------------------------------------------------*/
void initESP(){
  while(1){
    if (fails > MAXFAILS){           //------Hardware reset for ESP8266.
      digitalWrite(HRESET,LOW);
      delay(100);
      digitalWrite(HRESET,HIGH);
      delay(5000);
      fails=0;
    }

    ESPSerial.print("AT\r\n");       //"\r\n" to replace println  (less code)
    delay(1000);
    if(waitChar('K',1000)){          //Wait for 'K' of "OK".
      ESPSerial.print(mode_MSG);     //Set Station mode.
      delay(250); 
      ESPSerial.print(join_MSG);     //Set name and password of the AP.
      if(waitChar('K',10000))return; //Wait for 'K' of "OK" to exit setup
    }
    fails++;
  }

}


Mini Client part list :

Adafruit-Part-List

Pictures from the Adafruit Industries web page.

1.- USB cable – A/MiniB. (1)
2.- Hook-up Wire 22AWG Solid Core (assorted colors) .(1)
3.- Tiny breadboard 170 contacts. (1)
4.- Adafruit’s Trinket – Mini Microcontroller – 3.3V Logic. (1)
5.- 3.3V 250mA Linear Voltage Regulator – L4931-3.3 TO-92. (1)
6.- 10uF 25V Electrolytic Capacitor. (1)
7.- 470uF 25V Electrolytic Capacitor. (1)
8.-TMP35 – Analog Temperature sensor. (1)
9.- ESP8266 WiFi Module. (1)

NOTE: A 0.1″ 4-pin right-angle male header is recommended to adapt the ESP8266 to the breadboard.


This Post Is Under Construction…

28 APR 2015 : New wiring,  Sketch posted.

… More info when requested and (or) available.

Thanks for reading.


Advertisements