The breadboard version of the HelvePic32 has the same pin layout as the HelvePic32 PTH.
It can be powered via the USB cable or via the 5V pin by an external 5V source.
This version comes soldered so you can plug it on the breadboard immediately
All projects and examples that are described with the normal HelvePic32 PTH are applicable to the breadboard version. So we use the occasion to describe, how to build a data logger that send the data to the internet.
(Of course, this example works as well with the HelvePic32 PTH)
It is perfectly OK to monitor the data logger using the serial interface and display the activities on the serial monitor. But a nice TFT is always an improvement and the SPI based 1.8" TFTs are easy to handle and pretty cheap to obtain.
The SPI pins vary from model to model, so the picture only shows a common TFT that I use.
First, connect the TFT to power
Next, connect Reset with the Reset of the HelvePic32 BB
Now connect RS. This pin is often labeled CD on several TFT modules. (Not to confuse with CS)
RS is short for Register Select
CD is short for Command/Data
The next pin to connect is SDA, also known as MOSI (Master Out, Slave In)
Followed by the clock pin, usually labeled CLK or SCK
Finally we connect CS, often labelled SS as well.
CS is short for Chip Select
SS is short for Slave Select
The TFT is now read to be used. We tested it with UTFT and our TFT had a ST7735 chip.
As data source we use a DHT22 chip on a breakout that has the pull-up resistors for the device already on board.
It gets powered by 3.3 V and some sources write that the sensor is more stable when powered by 5V. I cannot confirm this, it worked fine on 3.3V.
To read the sensor we used the DHT library
For the internet connection we use a ESP8266 WiFi module that comes on a breakout to address the module like a simple serial terminal.
This demonstrates a great advantage of the HelvePic32: it has two serial ports that can be used independently from the normal USB based serial port. Using Serial0 gives us access to the ESP8266 without interfering with the code upload.
The following code uses www.thingspeak.com as data logging web service. The code needs to be adjusted to the credentials of your WiFi router and the API key you obtain from thingspeak.com
#include "HelvePic32.h"
#include <DHT22.h>
#define DHT22_PIN nP[RIGHT][2]
DHT22 myDHT22(DHT22_PIN);
#define ESPgppd nP[RIGHT][1] // SMD
#define ESPreset nP[LEFT][0]
#define SSID "xxxx SSID xxxx"
#define PASS "xxxx passwort xxxx"
#define IP "184.106.153.149" // thingspeak.com
String GET = "GET /update?key=xxxx_API_key_xxx&field1=";
String F2 = "&field2=";
String inputString = ""; // a string to hold incoming data
const char* header = "Sensor Monitor";
const char* footer = "HelvePic32.org";
boolean debug = false; // if true: ignore DHT
float avgT, avgH;
uint8_t cnt;
void setup() {
pinMode(ESPgppd,OUTPUT);
pinMode(ESPreset,OUTPUT);
digitalWrite(ESPgppd,HIGH);
digitalWrite(ESPreset,HIGH);
avgT = 0.0;
avgH = 0.0;
cnt = 0;
Serial.begin(9600);
Serial0.begin(115200);
doWait(10);
Serial.println("Send AT to Esp8266");
Serial0.println("AT");
Serial.println("Wait for 5 seconds");
doWait(5);
while (Serial0.available()){
char inChar = (char)Serial0.read();
inputString += inChar;
}
Serial.print(inputString);
if(inputString.indexOf("OK")>0){
Serial.println("ESP8266: OK");
connectWiFi();
} else {
Serial.println("Send AT+RST to ESP8266");
Serial0.println("AT+RST");
Serial.println("Wait for 5 seconds");
doWait(5);
if(Serial0.find("ready")){
Serial.println("ESP8266: OK");
connectWiFi();
} else {
Serial.println("ESP8266 not responding as expected");
}
}
Serial.println("Wait for DHT ");
doWait(2);
}
void loop() {
DHT22_ERROR_t errorCode;
char bt[10];
char bh[10];
String tempC, hum;
if (debug) {
if (cnt==0) {
Serial.print("Debug Mode - no Data --- OK --- ");
Serial.println(millis());
}
Serial.print(".");
avgT += 23.5;
avgH += 48;
cnt++;
if (cnt>29){
Serial.println();
avgT = (float)avgT/cnt;
avgH = (float)avgH/cnt;
Serial.println("Debug Mode - Sending Data to ESP8266");
tempC = dtostrf(avgT, 5, 2, bt);
hum = dtostrf(avgH, 5, 2, bh);
updateDHT(tempC, hum);
cnt = 0;
avgT = 0;
avgH = 0;
}
} else {
errorCode = myDHT22.readData();
switch(errorCode)
{
case DHT_ERROR_NONE:
Serial.print("Humidity: ");
Serial.print(myDHT22.getHumidity());
Serial.print(" %");
Serial.print("Temperature: ");
Serial.print(myDHT22.getTemperatureC());
Serial.print(" C ");
Serial.print("---- OK ----");
Serial.print(millis());
avgT += myDHT22.getTemperatureC();
avgH += myDHT22.getHumidity();
cnt++;
if (cnt>59){
avgT = (float)avgT/cnt;
avgH = (float)avgH/cnt;
Serial.println(" -----> Sending averages to WEB Server");
Serial.print("Humidity: ");
Serial.print(avgH);
Serial.print(" %");
Serial.print("Temperature: ");
Serial.print(avgT);
Serial.print(" C ");
tempC = dtostrf(avgT, 5, 2, bt);
hum = dtostrf(avgH, 5, 2, bh);
updateDHT(tempC, hum);
cnt = 0;
avgT = 0;
avgH = 0;
}
break;
case DHT_ERROR_CHECKSUM:
Serial.print("Error");
Serial.print("check sum");
break;
case DHT_BUS_HUNG:
Serial.print("Error");
Serial.print("BUS Hung ");
break;
case DHT_ERROR_NOT_PRESENT:
Serial.print("Error");
Serial.print("Not Present ");
break;
case DHT_ERROR_ACK_TOO_LONG:
Serial.print("Error");
Serial.print("ACK time out ");
break;
case DHT_ERROR_SYNC_TIMEOUT:
Serial.print("Error");
Serial.print("Sync Timeout ");
break;
case DHT_ERROR_DATA_TIMEOUT:
Serial.print("Error");
Serial.print("Data Timeout ");
break;
case DHT_ERROR_TOOQUICK:
Serial.print("Error");
Serial.print("Polled to quick ");
break;
}
Serial.println();
}
delay (2000); // DHT needs 2 s for next measurment/poll
}
boolean connectWiFi(){
Serial.println("AT+CWMODE=1");
Serial0.println("AT+CWMODE=1");
doWait(2);
String cmd="AT+CWJAP=\"";
cmd+=SSID;
cmd+="\",\"";
cmd+=PASS;
cmd+="\"";
Serial.println(cmd);
Serial0.println(cmd);
Serial.println("Waiting 8 seconds to set WiFi credentials");
doWait(8);
if(Serial0.find("OK")){
Serial.println("ESP8266: OK");
return true;
}else{
Serial.println("ESP8266: failed");
return false;
}
}
void updateDHT(String tC, String hU){
Serial.println("Send Data to WEB ");
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += IP;
cmd += "\",80";
Serial.println(cmd);
Serial0.println(cmd);
Serial.println("Waiting 2 s to connect to Web Server");
doWait(2);
if(Serial0.find("CONNECT")){
// if(Serial0.find("Linked")){
Serial.println("ESP8266: Linked");
cmd = GET;
cmd += tC;
cmd += F2;
cmd += hU;
cmd += "\r\n";
Serial.print("AT+CIPSEND=");
Serial.println(cmd.length());
Serial.println(cmd);
Serial0.print("AT+CIPSEND=");
Serial0.println(cmd.length());
delay(500);
if(Serial0.find(">")){
Serial0.print(cmd);
if(Serial0.find("SEND")){
Serial.println("ESP8266: Send OK");
}
}else{
Serial.println("ESP8266: Send failed");
Serial.println("AT+CIPCLOSE");
Serial0.println("AT+CIPCLOSE");
}
return;
} else {
Serial.println("ESP8266: cannot connect to WEB Server");
}
}
void doWait(int s){
for (int i=0; i<s; i++){
Serial.print(".");
delay(1000);
}
Serial.println();
}