Assignment 3 (Person A Person B) - pedroorey/elektro GitHub Wiki

Description

Work in group of 2 people: person A and person B. Both A and B have a nodeMCU. Person A: Create a channel in ThingSpeak and call it “KABK_ex2”. Connect one of the sensor we used in class to nodeMCU, read the sensor and send the measurement to your channel “KABK_ex2”, in file 1. Document the exercise: description, schematic, code and video Person B: Use NodeMCU to read the data of your person B and print on Serial Monitor the data.

For this assignment I worked as person B, so my code displays the data from ThingSpeak and prints it on the Serial Monitor.

SerialMonitor

Schematic

Updated schematic of the circuit containing the numbers of the pins used.

SerialMonitor

Code

# include <ThingSpeak.h>
# include <ESP8266WiFi.h>
# include "secret.h"

//WiFi Variables
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;

//thingspeak variables
long myChannel_ID = SECRET_(H_ID);
char myReadAPIKey[] = SECRET_ReadAPIKey;
int FieldNumber_data = 2;

int data_download = 0;
int wait = 15000;
WiFiClient Clients;

void setup() {
  Serial.begin(9600);
  WiFi.mode(WIFI_STA);
  ThingSpeak.begin(Clients);

}

void loop() {
  if(WiFi.status() != WL_CONNECTED){
    
    Serial.println("TRY TO CONNECT");
    while(WiFi.status() != WL_CONNECTED){
      WiFi.begin(ssid, pass);
      Serial.println("...");
      delay(5000);
    }
    Serial.println("CONNECTED");
    
  }

  //READ DATA
  data_download = ThingSpeak.readLongField(myChannel_ID, 
  FieldNumber_data, myReadAPIKey);

  int statusCode = ThingSpeak.getLastReadStatus();
  if(statusCode == 200){
    Serial.println("IT WORKED");
    Serial.println(data_download);
  }
  delay(wait);
  }

Serial Monitor

⚠️ **GitHub.com Fallback** ⚠️