Code
This page is for the better understanding of the firmware of Gepetto.
GepettoWiFi v1.0
//GepettoWiFi v1.0 by Levitate
//Last Update 28.06.2019
/***********************************************************************/
//NodeMCU sleeps until a pulse comes to its reset pin
//Sends a 'R'eady message on the serial and waits for a 'C'onnect order
//Connects to the internet on the declared ssid and password
//Sends a 'C'onnected message on
//Reads and stores the data on the json file at the declared url
//Waits for 'S'end order to send the data on the serial
//Sends the data and goes back to sleep
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
// WiFi Parameters
const char* ssid = "Levitate_Gidasi";
const char* password = "uranyumgidasi";
const char* urlJson = "http://levitate.com.tr/stepper.json";
//The time allowed to wait for the internet connection to be established
//This is generally much more then the messageWaitingLimit
const int wifiWaitingLimit = 30000;
const int messageWaitingLimit = 2000;
unsigned long initialTime = millis();
bool waitingFlag = true;
void setup() {
Serial.begin(9600);
}
void loop() {
//Send a 'R'eady signal to arduino so it can ask for internet connection
Serial.write('R');
initialTime = millis();
//Wait for a 'C'onnect answer for an interval of messageWaitingLimit
while((waitingFlag) && ((millis() - initialTime) < messageWaitingLimit)){
if(Serial.read() == 'C')
waitingFlag = false;
yield();
}
//If 'C'onnect is acknowledged, start internet connection
if(waitingFlag == false){
waitingFlag = true;
WiFi.begin(ssid, password);
initialTime = millis();
while (waitingFlag && ((millis() - initialTime) < wifiWaitingLimit)){
if(WiFi.status() == WL_CONNECTED){
waitingFlag = false;
}
yield();
}
//If the connection is established send a 'C'onnected signal to serial
if(waitingFlag == false){
waitingFlag = true;
Serial.write('C');
//Read and store the data from the server
HTTPClient http;
http.begin(urlJson);
int httpCode = http.GET();
if (httpCode > 0){
//Buffer capacity is decided according to the data on the server
//This data can be calculated automatically on the https://arduinojson.org/v6/assistant/
const size_t capacity = 8*JSON_ARRAY_SIZE(4) + JSON_OBJECT_SIZE(8) + 270;
DynamicJsonBuffer jsonBuffer(capacity);
JsonObject& root = jsonBuffer.parseObject(http.getString());
//Debugging part to see data read from the server
/*
JsonArray& ms_delays_at_the_start = root["ms_delays_at_the_start"];
int ms_delays_at_the_start_0 = ms_delays_at_the_start[0]; // 0
int ms_delays_at_the_start_1 = ms_delays_at_the_start[1]; // 1200
int ms_delays_at_the_start_2 = ms_delays_at_the_start[2]; // 0
int ms_delays_at_the_start_3 = ms_delays_at_the_start[3]; // 0
JsonArray& max_speeds1 = root["max_speeds1"];
int max_speeds1_0 = max_speeds1[0]; // 372
int max_speeds1_1 = max_speeds1[1]; // 372
int max_speeds1_2 = max_speeds1[2]; // 0
int max_speeds1_3 = max_speeds1[3]; // 0
JsonArray& accels1 = root["accels1"];
int accels1_0 = accels1[0]; // 2000
int accels1_1 = accels1[1]; // 2000
int accels1_2 = accels1[2]; // 0
int accels1_3 = accels1[3]; // 0
JsonArray& ms_delays_at_the_end_of_fig1 = root["ms_delays_at_the_end_of_fig1"];
int ms_delays_at_the_end_of_fig1_0 = ms_delays_at_the_end_of_fig1[0]; // 1200
int ms_delays_at_the_end_of_fig1_1 = ms_delays_at_the_end_of_fig1[1]; // 1200
int ms_delays_at_the_end_of_fig1_2 = ms_delays_at_the_end_of_fig1[2]; // 0
int ms_delays_at_the_end_of_fig1_3 = ms_delays_at_the_end_of_fig1[3]; // 0
JsonArray& move_to_meters1 = root["move_to_meters1"];
int move_to_meters1_0 = move_to_meters1[0]; // 0
int move_to_meters1_1 = move_to_meters1[1]; // 0
int move_to_meters1_2 = move_to_meters1[2]; // 0
int move_to_meters1_3 = move_to_meters1[3]; // 0
JsonArray& max_speeds2 = root["max_speeds2"];
int max_speeds2_0 = max_speeds2[0]; // 372
int max_speeds2_1 = max_speeds2[1]; // 372
int max_speeds2_2 = max_speeds2[2]; // 0
int max_speeds2_3 = max_speeds2[3]; // 0
JsonArray& accels2 = root["accels2"];
int accels2_0 = accels2[0]; // 2000
int accels2_1 = accels2[1]; // 2000
int accels2_2 = accels2[2]; // 0
int accels2_3 = accels2[3]; // 0
JsonArray& ms_delays_at_the_end_of_fig2 = root["ms_delays_at_the_end_of_fig2"];
int ms_delays_at_the_end_of_fig2_0 = ms_delays_at_the_end_of_fig2[0]; // 1200
int ms_delays_at_the_end_of_fig2_1 = ms_delays_at_the_end_of_fig2[1]; // 1200
int ms_delays_at_the_end_of_fig2_2 = ms_delays_at_the_end_of_fig2[2]; // 0
int ms_delays_at_the_end_of_fig2_3 = ms_delays_at_the_end_of_fig2[3]; // 0
*/
//Wait for a 'S'end signal on the serial before sending the data
//'S'end message takes more time to come then the other messages
initialTime = millis();
while(waitingFlag && ((millis() - initialTime) < messageWaitingLimit)){
if(Serial.read() == 'S'){
waitingFlag = false;
root.printTo(Serial);
Serial.flush();
}
yield();
}
waitingFlag = true;
}
//End the http connection disconnect from the internet
http.end();
}
WiFi.disconnect(true);
}
//Go back to deepsleep
ESP.deepSleep(0);
}Update Choreograpy
Read From EEPROM
Waking Up NodeMCU
Last updated
Was this helpful?