Hi,
Yeah I have a code.
Here it is. If anybody has suggestions to improve it, I would be really happy !
Specially, I don't know exactly how to use flush, yield, setting the esp8266 speed, signal power,
I'm encountering some lags when I use it. The reception stops for half second sometimes.
Besides, the signal slow down randomly and I don't know why !
NB:
The general idea is to sense physical interaction with the uno, send collected data via UART to ESP8266 which will transfer over an existing WIFI router to computer (running MaxMSP for example).
The specifications would be:
- Use of esp8266-12
- Use TCP protocol (to make sure that the transferred data is consistent)
- Go as fast as possible (I'm struggling using UART faster than 9600 bauds)
- Robust set up (it has to run for month without any reset because it will be in remote area)
So to make sure before introducing sensing in my project, I'm only generating a signal from 0 to 1024 with arduino Uno and sending these integers over UART and then wifi.
I visualise on computer the signal (triangle waves). So I notice when signal goes slower by wider triangles.
And finally, I visualize signal interruptions with the button in MaxMSP which turns off when no signal is received.
Signal visualisation on MaxMSP:
MaxMSP visualisation
=> We can see on this figure that the second triangle slowed down a bit at the beginning. The more I let the set up running, the worst it is in term of signal speed.
Arduino Uno side :
// ARD UNO ESP8266
// D10 <-> TX // Unhook before upload sketch
// D11 <-> RX // Unhook before upload sketch
// GND <-> GND
#include <SoftwareSerial.h>
#define RXpin 10 // MOD
#define TXpin 11 // MOD
String UARTsendData; // AUTO
SoftwareSerial espSerial(RXpin, TXpin);
void setup() { // -----------------------------------------------------------------------
Serial.begin(57600); // For com with computer
espSerial.begin(9600); // For com with ESP
}
void loop() { // ------------------------------------------------------------------------
// Generating a triangle wave forms
for (int i = 0; i < 1024; i++) {
UARTsendData = i;
UARTsend();
}
for (int i = 1023; i > 0; i--) {
UARTsendData = i;
UARTsend();
}
}
void UARTsend() { // --------------------------------------------------------------------
espSerial.print(UARTsendData);
espSerial.println("F");
}
ESP side:
// ARD UNO ESP8266
// D10 <-> TX // Unhook before upload sketch
// D11 <-> RX // Unhook before upload sketch
// GND <-> GND
#include <ESP8266WiFi.h>
#define RcvCount 10
#define Tempo 1
const char* ssid = "Z3K"; // MOD
const char* pwd = "pwd"; // MOD
const char* hostIP = "192.168.43.220"; // MOD
const int sendPort = 8889; // MOD
const int recvPort = 8890; // MOD
String UARTrcvData; // AUTO
char UARTrcvBuffer; // AUTO
int UARTrcvCount; // AUTO
String WIFIsendData; // AUTO
String WIFIrcvData; // AUTO
char WIFIrcvBuffer; // AUTO
int WIFIrcvCount; // AUTO
WiFiServer server(recvPort);
WiFiClient client;
void setup() { // -----------------------------------------------------------------------
Serial.begin(9600); // For com with ARD
WIFIconnect();
WIFIserverConnect();
server.begin();
client.setNoDelay(1);
Serial.println("\nInitialized");
}
void loop() { // -----------------------------------------------------------------------
UARTreceive();
WIFIsendData = UARTrcvData;
WIFIsend();
WIFIreceive();
}
void WIFIconnect() { // ----------------------------------------------------------------
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pwd);
while (WiFi.status() != WL_CONNECTED) {
delay(Tempo);
}
}
void WIFIserverConnect() { // ----------------------------------------------------------
if (!client.connect(hostIP, sendPort)) {
}
}
void WIFIreceive() { // ----------------------------------------------------------------
WIFIrcvCount = 0;
WIFIrcvBuffer = 'N';
WIFIrcvData = "";
WiFiClient client = server.available();
if (client.connected()) {
while (WIFIrcvBuffer != 'F' && WIFIrcvCount < RcvCount) {
WIFIrcvBuffer = client.read();
if (WIFIrcvBuffer != 'F') {
WIFIrcvData += WIFIrcvBuffer;
WIFIrcvCount++;
}
}
if (debug == true) {
Serial.println(WIFIrcvData);
}
}
client.flush();
}
void WIFIsend() { // -------------------------------------------------------------------
if (WiFi.status() != WL_CONNECTED) {
WIFIconnect();
}
client.flush();
if (client.connected()) {
client.println(WIFIsendData);
}
}
void UARTreceive() { // ----------------------------------------------------------------
UARTrcvCount = 0;
UARTrcvBuffer == 'N';
UARTrcvData = "";
while (UARTrcvBuffer != 'F' && UARTrcvCount < RcvCount) {
while (Serial.available() > 0) {
UARTrcvCount = UARTrcvCount + 1;
UARTrcvBuffer = (char)Serial.read();
if (UARTrcvBuffer != 'F') {
UARTrcvData += UARTrcvBuffer;
}
}
}
}