For this project I'm using the nodeMCU development kit (NodeMCU devKit Github) to develope my code. The development kit contains a ESP8266 WiFi module, and I'm using this to wirelessly send data to the plotly streaming server. Plotly is a online browser based tool for visualizing data, which also has a streaming feature for displaying your data in real-time. Plotly is great because any device that has a browser and is connected to the internet (smartphone, PC, tablet, etc) can see the real-time data.
The nodeMCU devkit contains a ESP8266-12 WiFi module, a 3.3V regulator, two buttons (flash and user) and a USB to Serial converter. I purchased my board from aliexpress for around $6US. I chose to use this board because it contains all that is needed to develop with the ESP8266.
I have erased the nodeMCU firmware and downloaded my custom Arduino code using the Arduino IDE 1.6.5 and the ESP8266 Board Package installed (Arduino-Compatible IDE for ESP8266 Github page).
The Arduino Code
The Arduino code:
#include <ESP8266WiFi.h>
#define ssid "YourSSID"
#define pass "YourWiFiPassword"
#define userName "YourPlotlyUserName"
#define APIKey "xxxxxxxxxx"
#define fileName "test"
#define fileopt "overwrite"
#define nTraces 1
#define maxpoints "30"
#define world_readable true
#define convertTimestamp true
#define timezone "Australia/Melbourne"
char *tokens[nTraces] = {"xxxxxxxxxx"};
char stream_site[25] = {0};
WiFiClient client;
/* -------------- Setup ------------------ */
void setup() {
// Setup Serial
Serial.begin(9600);
delay(2000);
Serial.println();
Serial.println();
ESP8266_Init();
ESP8266_Connect("plot.ly", 80);
Serial.println("Initializing plot with Plot.ly server...");
plotly_init();
Serial.println("Making sure disconnected...");
client.stop();
Serial.println("Done!");
ESP8266_Connect("arduino.plot.ly", 80);
}
/* ------------- Loop -------------------- */
void loop() {
int val = analogRead(A0);
plotly_plot(millis(),val,tokens[0]);
}
/* --------------- Functions -------------------- */
void ESP8266_Init(){
Serial.println("-------------------------------------");
Serial.println(" INITIALIZING...");
Serial.println("-------------------------------------\r\n");
// Put WiFi into Station mode
Serial.println("1. Putting WiFi into station mode...");
WiFi.mode(WIFI_STA);
Serial.println(" Done!\r\n");
// Connect to WiFi
Serial.print("2. Connecting to: \"");
Serial.print(ssid);
Serial.print("\",\"");
Serial.print(pass);
Serial.println("\"...");
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println(" WiFi connected!");
Serial.print(" IP address: ");
Serial.println(WiFi.localIP());
Serial.println();
Serial.println("-------------------------------------");
Serial.println(" INITIALIZATION COMPLETE!");
Serial.println("-------------------------------------\r\n");
}
void ESP8266_Connect(char* url, int port){
Serial.print("Connecting to: \"");
Serial.print(url);
Serial.println("\" server...");
if (!client.connect(url, port)) {
Serial.println("*Connection Failed!*\r\n");
while(1){};
}
else{Serial.println("Connected!");}
}
void plotly_init(){
unsigned int i = 0;
char charbuffer;
// Calculate content length
unsigned int contentLength = 126 + strlen(userName) + strlen(fileopt) + nTraces*(87+strlen(maxpoints)) + (nTraces - 1)*2 + strlen(fileName);
if(world_readable){
contentLength += 4;
}
else{
contentLength += 5;
}
String contentLengthString = String(contentLength);
const char* contentLengthConstString = contentLengthString.c_str();
unsigned int postLength = contentLength + 94 + strlen(contentLengthConstString);
// Send Post and initalization data
client.print(
String("POST /clientresp HTTP/1.1\r\n") +
"Host: plot.ly:80\r\n" +
"User-Agent: Arduino/0.6.0\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"Content-Length: " + String(contentLength) + "\r\n\r\n" +
"version=2.3&origin=plot&platform=arduino&un="...
Read more »
hello
thanks a lot for sharing this project.
i see you made fixes in 2016. But now comes a new noob interested in this topic in 2018 and stumbles upon a (new?) error.
May i ask for help. i had read everything user "shyam.sunder91" wrote and my problem seems quite similar.
i have the "ERROR" on a similar moment.
sharing here the content of my serial
-------------------------------------
INITIALIZING...
-------------------------------------
1. Putting WiFi into station mode...
Done!
2. Connecting to: "MYWIFIID","MYCODE"...
.......
WiFi connected!
IP address: 192.168.1.X
-------------------------------------
INITIALIZATION COMPLETE!
-------------------------------------
Connecting to: "plot.ly" server...
Connected!
Initializing plot with Plot.ly server...
*ERROR!*
Soft WDT reset
ctx: cont
sp: 3ffefed0 end: 3fff0110 offset: 01b0
>>>stack>>>
3fff0080: 5815be23 3fff009c 3ffeee58 40202625
3fff0090: 00000000 00000000 00000000 00000000
3fff00a0: 00000000 00000000 00000000 00000000
3fff00b0: 00000000 00000000 00000000 00000000
3fff00c0: 3fff0f4c 0000000f 00000003 40203818
3fff00d0: 4020137a 3ffe8d30 3ffef0bc 3ffef0e8
3fff00e0: 3fffdad0 00000000 3ffef0bc 40202697
3fff00f0: feefeffe feefeffe 3ffef0e0 40203ed0
3fff0100: feefeffe feefeffe 3ffef0f0 40100710
<<<stack<<<
/⸮⸮⸮,⸮⸮⸮
Any ideas ?