/* _ _ _ _____ _ _ _____ _ _ * /_\ | | |_ _| |_ (_)_ _ __ _ __|_ _|_ _| | |__ * / _ \| | | | | | ' \| | ' \/ _` (_-< | |/ _` | | / / * /_/ \_\_|_| |_| |_||_|_|_||_\__, /__/ |_|\__,_|_|_\_\ * |___/ * AllThingsTalk * EPS8266 (NodeMCU) Example * Description: This example checks and uploads the value of ESP8266's analog port to AllThingsTalk but it * also has a counter that increments by one when the analog port is touched and the value of that counter is uploaded to AllThingsTalk * * You'll see "millis" appearing throughout the code. Millis actually is the number of * milliseconds that have passed since NodeMCU began running the program. * Since Arduino/NodeMCU/ESP8266 doesn't support threading, only one thing can be done at a time, * we're going to use millis to check if a specified amount of time (variable "period" below) has passed * so we can run a specific command. This way, we're not using the "delay(time)" function, which would * cause the whole device to hang for the specified amount of time. */ #include // Load (include) the AllThingsTalk WiFi SDK - Read more about it on https://gitub.com/allthingstalk/arduino-wifi-sdk auto wifiCreds = WifiCredentials("Your_WiFi", "Your_WiFi_Password"); // Your WiFi Network Name and Password auto deviceCreds = DeviceConfig("Your_Device_ID", "Your_Device_Token"); // Go to AllThingsTalk Maker > Devices > Your Device > Settings > Authentication to get your Device ID and Token auto device = Device(wifiCreds, deviceCreds); // Create "device" object char* sensorAsset = "analog-example"; // Name of asset on AllThingsTalk to which you'll receive the analog value (automatically created below) char* counterAsset = "counter-example"; // Name of asset on AllThingsTalk to which you'll receive the counter value (automatically created below) const long period = 1000; // Change this to change how many milliseconds you want between analog port readings int analogValue = 0; // Variable that will store the value of the analog port int analogCounter = 0; // Initial value of the counter int analogTrigger = 40; // If exceeded, it's considered a touch and the counter increments. Change this to "calibrate" it int counterMax = 10; // Maximum value the counter can reach before resetting itself unsigned long startMillis; // Used to keep track of send intervals unsigned long currentMillis; // Used to keep track of send intervals void setup() { // This function is only called once, when the device boots Serial.begin(115200); // Starts the Serial port for debugging (at baud rate 115200) device.debugPort(Serial); // Enable debug output from AllThingsTalk SDK. device.wifiSignalReporting(true); // Enable AllThingsTalk WiFi SDK's feature that sends NodeMCU's WiFi Signal Strength to your AllThingsTalk Maker device.createAsset(sensorAsset, "Analog Value", "sensor", "integer"); // Create asset on AllThingsTalk to send analog value to device.createAsset(counterAsset, "Counter", "sensor", "integer"); // Create asset on AllThingsTalk to send counter value to device.init(); // Initialize WiFi and AllThingsTalk startMillis = millis(); // Saves the initial millis value at boot to startMillis variable } void analogCheck() { // This is the function that checks the value of analog port currentMillis = millis(); // Saves the value of "millis()" at the time of execution of this line if (currentMillis - startMillis >= period) { // If current time minus the last saved startMillis time is bigger than the period defined above, it'll run the code below analogValue = analogRead(A0); // Reads the analog port A0 of ESP8266 (NodeMCU) and saves it to "analogButton" variable Serial.print("Current Analog Value: "); // Prints to Serial port Serial.println(analogValue); // Prints to Serial port if (analogValue >= analogTrigger) { // Checks if the current analog port value is bigger than the threshold we've set for trigger Serial.println("Counter triggered! Incrementing..."); // Prints to Serial port analogCounter++; // If it is, increment the analogCounter value if (analogCounter > counterMax) { // Checks if the current analogCounter value is bigger than the maximim we've set analogCounter = 1; // If it is, reset the counter to 1 Serial.println("Counter has been reset!"); // Prints to Serial port } device.send(counterAsset, analogCounter); // Publish the counter value to AllThingsTalk Serial.print("Published counter value: "); // Prints to Serial port Serial.println(analogCounter); // Prints the current counter number to Serial port delay(1000); // Creates a 1 second dalay between this and next message } device.send(sensorAsset, analogValue); // Publish the analog value to AllThingsTalk startMillis = currentMillis; // Resets the startMillis by assigning it the value of currentMillis } } void loop() { // Main code that'll be run in loop all the time device.loop(); // Keep AllThingsTalk and WiFi connection alive analogCheck(); // Runs our "analogCheck" function, which checks the value of analog port and publishes it }