Home > Exploring Ideas, Hardware testing, Internet of Things, Trinket Projects > Mini Client with the Arduino TRINKET and the ESP8266

Mini Client with the Arduino TRINKET and the ESP8266

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:


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:


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.
 -Adafruit's Arduino Trinket (3.3V)
 -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  ""     //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.
  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);

  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("20.7");    // test value
      ESPSerial.print("\r\n");      //to replace println (less code).
      fails=0;                      // clear failure counter.
      return;                       // Unlink automatically....
  ESPSerial.print(clos_MSG);       //Close the TCP connection with ThingSpeak.  

Function waitChar() 
bool waitChar(char Wchar, int duration){
    if(ESPSerial.read()== Wchar)return true;
  return false;

Function initESP() 
void initESP(){
    if (fails > MAXFAILS){           //------Hardware reset for ESP8266.

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


Mini Client 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.

  1. February 16, 2015 at 4:43 PM

    If you could post the code that would be great. I am trying to get something like this set up and I need to make sure I do it properly. Your code would be a good reference!

  2. mt.olympus
    March 16, 2015 at 4:21 PM

    is there any possibility to contact you? our arduino guy ran away from our college class project. we need help.

  3. Pierrot
    April 18, 2015 at 2:41 PM

    It should be great to publish the code even if it’s not documented an cleaned…. just to have an idea how it works.

    • Pierrot
      April 29, 2015 at 4:35 PM

      Many thanks for the sketch. I’ll try it soon.

  4. ngpha
    April 27, 2015 at 1:30 PM

    I have pro mini and esp8266 upload 4 field to thingspeak but it run only aome hours then esp not connect , i use ams 1117 modul to supply voltage , i unplug this power source then plug it so esp connect again but some hours later it get hang.how can i do to solve esp not connect

    • April 28, 2015 at 1:20 AM

      I had the same problems at first, use 470uF capacitor in The output of the 1117.

  5. April 29, 2015 at 1:01 PM

    Can you send me the sketch? Because i have problem with software serial on trinket 5v…

  6. tomm3
    April 29, 2015 at 1:02 PM

    Hi, i have problem making software serial on trinket 5v.
    Can you send me your sketch?
    What are the pins for interrupts? 0, 1?

    • April 29, 2015 at 4:23 PM

      Interrupts are not used in this project.
      The ESP8266’s reset is connected to the pin 3 of the Trinket.

  7. Sam
    May 7, 2015 at 1:28 AM

    Where did “6.- 10uF 25V Electrolytic Capacitor. (1)” go?

    I have been trying to convert your tiny breadboard to a schematic.

    • May 7, 2015 at 8:15 AM

      It’s recommended at the input of the voltage regulator, I had good results not using it and keeping the USB cable short ( under 4′).

  8. Ian
    September 26, 2015 at 8:22 AM

    Thanks for the tutorial – it’s great. I have some questions, though:

    1. What were the security settings on your network? Does it matter if it’s WEP/WPA/etc?
    2. I’ve read about flashing the ESP8266 itself with code that would offload much of the functionality of say a RESTful client – have you experimented with this?

    Thank you!

  1. January 23, 2015 at 8:37 PM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: