As I have posted before, I have been playing with the super-cheap WI07C WiFi module based on the ESP8266 chip. I’ve now had sufficient success with it that I can publish a post on a working project. This simple setup uses an Arduino nano to read temperatures from to 18BS20 sensors, formats the data as JSON and then sends it over WiFi to a server on my home network. It’s cheap and simple. Here’s the fritzing diagram:
You may notice the Adafruit level shifter board in there too. That’s because the digital IO from the arduino is 5V, but the WiFi module needs 3.3V. The Adafruit module is a dead easy way of joining the two. The temperature sensors use a three-wire protocol which allows you to connect many in parallel and address each one individually. there’s a software library which takes care of this.
Here’s the Arduino sketch. It uses Miles Burton’s temperature control library to read from the sensors, so you’ll need to download that. NB: this code is not a shining exaple of style or completeness. It’s a quick hack to get something working. It does no error checking or reconnection if there are problems. You may find yourself pressing the reset button a lot.
You might also note that the hardware serial port is required for the WiFi module, which needs 115200 baud. This means that you can’t upload a new sketch to the arduino while it’s connected to the WiFi module. I just whip out the wires to the TXD and RXD pins on the arduino while I’m uploading, and all is well.
The sketch tries to join the WiFi network, and then tries to establish a simple TCP connection to a server IP address and port of your choice. Once the connection is established, it checks the temperature sensors every 10 seconds or so and sends the temperatures to the server in JSON format, thus: {"temp":[22.63,22.81]}
. That’s all it does. You’ll need a TCP server listening on your chosen IP address and port, of course. It seems to work quite reliably for me.
#include <SoftwareSerial.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define SSID "MyHomeSSID"
#define PASS "MyPassword"
#define TARGET_IP "192.168.1.xx"
#define TARGET_PORT 5000
#define TEMPERATURE_PIN 9
SoftwareSerial dbgSerial(10,11); // RX,TX
OneWire wire(TEMPERATURE_PIN);
DallasTemperature sensors(&wire);
void setup()
{
// WiFi module needs fast serial, so must use the hardware port which is also used for
// uploading sketches
Serial.begin(115200);
Serial.setTimeout(5000);
// For debugging, we therefore need a software serial port. This can be much slower.
dbgSerial.begin(9600);
dbgSerial.println("Starting");
delay(1000);
// Connect to the wirelsess network
dbgSerial.println("Joining network...");
Serial.print("AT+CWJAP="");
Serial.print(SSID);
Serial.print("","");
Serial.print(PASS);
Serial.println(""");
receive();
// Just check that the WiFi module is joined to the network
dbgSerial.println("Check connection...");
Serial.println("AT+CWJAP?");
receive();
dbgSerial.println("Initialising sensors...");
sensors.begin();
dbgSerial.println("Connecting to server...");
Serial.print("AT+CIPSTART="TCP","");
Serial.print(TARGET_IP);
Serial.print("",");
Serial.print(TARGET_PORT);
receive();
delay(5000);
dbgSerial.println("Ready to rumble!");
}
int incomingByte=0;
bool echoLocal = true;
// Get the data from the WiFi module and send it to the debug serial port
void receive(){
delay(500);
while (Serial.available() >0) {
incomingByte = Serial.read();
dbgSerial.write(incomingByte);
}
dbgSerial.println();
}
char temp1[10];
char temp2[10];
void loop()
{
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
dbgSerial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
dbgSerial.println("DONE");
dtostrf(sensors.getTempCByIndex(0),1,2,temp1);
dtostrf(sensors.getTempCByIndex(1),1,2,temp2);
String json="{"temp":[" + String(temp1) + "," + String(temp2) + "]}";
dbgSerial.print("Sending ");
dbgSerial.println(json);
// Send the data to the WiFi module
Serial.print("AT+CIPSEND=");
Serial.println(json.length());
delay(500);
Serial.print(json);
receive();
delay(10000);
}
This is a work in progress. I’ll be updating it soon. But for now, I’m very pleased with the simplicity of the WiFi modules, and even more pleased with their low cost (I am a Yorkshireman, after all).