Coding the Colonel

As I’m going about reloading my old (working) code back onto the Colonel, I thought that I would post a copy of it here, while I had it in front of me, as I haven’t done that yet.

Before I get into it, I need to say that I can’t take credit for any of this code. Given my lack of in-depth programming know-how, I went on a hunting trip of sorts and collected various bits of open source and tutorial code which I was rearranging and stringing together in an attempt to understand how it works, while make something that actually did.

To get started the first thing I did was take to the search engines and look into “How to post Arduino data to a spreadsheet”, which eventually let me to Ishan Ajay’s tutorial (and code) for uploading Temp/Humidity data to a google spreadsheet. ( https://create.arduino.cc/projecthub/24Ishan/log-temperature-data-to-google-sheets-0b189b?f=1 )

After only being to get so far on my own, on account of some complications/conflicts I was facing as a result of the way I was passing the data prior to posting, I went looking for help in the Arduino forums and received some help with my code from someone using the handle 6v6gt. They supplied with the piece of code I needed to resolve the communication conflict, and I would like to take a second a quickly say “Thank you” to you both, for your assistance.

Mustard_Arduino V1.0

#include <EEPROM.h>

#include "GravityTDS.h"

#define TdsSensorPin A0

GravityTDS gravityTds;

float temperature = 25, tdsValue = 0;

char tdsTransfer [20]; // Buffer big enough for 10 characters

void setup()

{

Serial.begin(9600);

gravityTds.setPin(TdsSensorPin);

gravityTds.setAref(5.0); //reference voltage on ADC, default 5.0V on Arduino UNO

gravityTds.setAdcRange(1024); //1024 for 10bit ADC;4096 for 12bit ADC

gravityTds.begin(); //initialization

}

void loop()

{

//temperature = readTemperature(); //add your temperature sensor and read it

gravityTds.setTemperature(temperature); // set the temperature and execute temperature compensation

gravityTds.update(); //sample and calculate

tdsValue = gravityTds.getTdsValue(); // then get the value

dtostrf (tdsValue, 5, 2, tdsTransfer); // Leave room for too large numbers!

Serial.write (tdsTransfer ); // Serial.Print and Serial.write glitch when both used for same variable

delay(1000);

}

The Mustard_Arduino V1.0 code is responsible for operating all the sensors.

 

The top (untitled) portion of the code is responsible for telling the computer which sensors are installed and which values it will need to retain while the program runs. Currently, I only have the TDS sensor installed.

 

void setup()

This next part of the code is identified by the phrase “void setup”.
This is where you give the computer a set of one-off instructions do prep/prime whatever is going to need prepping or priming.

In my code, I have instructed it that there is a serial connection (for the NodeMCU), and have it initializing the TDS sensor.

 

void loop ()

The void loop are where you give the operating instructions and tell the microcomputer what exactly you want it to do, which it will repeat until unable.

In my code, the computer is checking the value coming off the TDS sensor, and write it to the serial connection, before waiting 1 second and doing it again.

 

Mustard Wifi V1.0

#include <ESP8266WiFi.h>

#include "HTTPSRedirect.h"

#include "DebugMacros.h"

int h ; //int will ned to change to float for mustard? dstTransfer

int t ; //int will ned to change to float for mustard?

char tdsTransfer[7]; // 6+1 characters reserved for tdsTransfer

String sheetHumid = "";

String sheetTemp = "";

const char* ssid = "WIFI"; //replace with our wifi ssid

const char* password = "PASSWORD"; //replace with your wifi password

const char* host = "script.google.com";

const char *GScriptId = "GOOGLE"; // Replace with your own google script id

const int httpsPort = 443; //the https port is same

const char* fingerprint = "";

String url = String("/macros/s/") + GScriptId + "/exec?value=Temperature"; // Write Teperature to Google Spreadsheet at cell A1

String url2 = String("/macros/s/") + GScriptId + "/exec?cal"; // Write to Cell A continuosly

//replace with sheet name not with spreadsheet file name taken from google

String payload_base = "{\"command\": \"appendRow\", \

\"sheet_name\": \"SHEETNAME\", \

\"values\": ";

String payload = "";

HTTPSRedirect* client = nullptr;

void setup() {

delay(1000);

Serial.begin(9600);

Serial.println();

Serial.print("Connecting to wifi: ");

Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {

delay(500);

Serial.print(".");

}

Serial.println("");

Serial.println("WiFi connected");

Serial.println("IP address: ");

Serial.println(WiFi.localIP());

// Use HTTPSRedirect class to create a new TLS connection

client = new HTTPSRedirect(httpsPort);

client->setInsecure();

client->setPrintResponseBody(true);

client->setContentTypeHeader("application/json");

Serial.print("Connecting to ");

Serial.println(host); //try to connect with "script.google.com"

// Try to connect for a maximum of 5 times then exit

bool flag = false;

for (int i = 0; i < 5; i++) {

int retval = client->connect(host, httpsPort);

if (retval == 1){

flag = true;

break;

}

else

Serial.println("Connection failed. Retrying...");

}

if (!flag) {

Serial.print("Could not connect to server: ");

Serial.println(host);

Serial.println("Exiting...");

return;

}

// Finish setup() function in 1s since it will fire watchdog timer and will reset the chip.

//So avoid too many requests in setup()

delay(3000);

Serial.println("\nWrite into cell 'A1'");

Serial.println("------>");

// fetch spreadsheet data

client->GET(url, host);

Serial.println("\nGET: Fetch Google Calendar Data:");

Serial.println("------>");

client->GET(url2, host);

Serial.println("\nStart Sending Sensor Data to Google Spreadsheet");

// delete HTTPSRedirect object

delete client;

client = nullptr;

}

void loop() {

// static uint32_t lastSendAtMs = millis() ; //*-* part of communication fix *-*

static int error_count = 0;

static int connect_count = 0;

const unsigned int MAX_CONNECT = 20;

static bool flag = false;

h = 123456 ; // Reading temperature or humidity takes about 250 milliseconds!

t = 999999 ; // Read temperature as Celsius (the default)

if (isnan(h) || isnan(t)) { // Check if any reads failed and exit early (to try again).

Serial.println(F("Failed to read from DHT sensor!"));

return;

}

//*-* This is where the data is put together

Serial.readBytes (tdsTransfer, 6); // read first 6 bytes

Serial.flush();

sheetHumid = String(h) + String("%"); //convert integer humidity to string humidity

sheetTemp = String(tdsTransfer) + String(" ppm");

// *-* for upload to spreadsheet

static uint32_t lastSendAtMs = millis() ; //*-* part of communication fix *-*

//*-* Data contained in this if statement only runs prior to upload to prevent conflicts *-*

if ( millis() - lastSendAtMs > 3000 ) { // 3 seconds

lastSendAtMs = millis() ; //

Serial.print("TDS: "); Serial.print(tdsTransfer); Serial.flush(); Serial.println(" uploaded ");

Serial.print("Holder: "); Serial.print(h);

payload = payload_base + "\"" + sheetTemp + "," + sheetHumid + "\"}";

if (!flag) {

client = new HTTPSRedirect(httpsPort);

client->setInsecure();

flag = true;

client->setPrintResponseBody(true);

client->setContentTypeHeader("application/json");

}

if (client != nullptr) {

if (!client->connected()) {

client->connect(host, httpsPort);

client->POST(url2, host, payload, false);

Serial.print("Sent : "); Serial.println("Temp and Humid");

}

}

else {

DPRINTLN("Error creating client object!");

error_count = 5;

}

if (connect_count > MAX_CONNECT) {

connect_count = 0;

flag = false;

delete client;

return;

}

Serial.println("POST or SEND Sensor data to Google Spreadsheet:");

if (client->POST(url2, host, payload)) {

;

}

else {

++error_count;

DPRINT("Error-count while connecting: ");

DPRINTLN(error_count);

}

if (error_count > 3) {

Serial.println("Halting processor...");

delete client;

client = nullptr;

Serial.printf("Final free heap: %u\n", ESP.getFreeHeap());

Serial.printf("Final stack: %u\n", ESP.getFreeContStack());

Serial.flush();

ESP.deepSleep(0);

}

} //*-* part of communication fix. Data in above if statement only runs prior to upload to prevent conflicts *-*

}

Mustard Wifi 1.0

This is the code that runs the NodeMCU

in the untitled section of the wifi’s code there are significantly more things to keep track of, as it is also being used to hold my log in credentials and information about the spreadsheet that I will be loading the information.

References here to temperature and humidity are because this code was originally used for posting temp and humidity data, not TDS. For the sake of my sanity, I left the variables named as they were, not knowing if changing them would have an impact on the information going to the spreadsheet or not.

 

Void Setup()

First I am connecting it to the serial, so it is aware of the Uno, then I am connected it to the wifi and logging into google to make sure the sp

 

Void Loop()

The NodeMCU receives, and copies the TDS sensor from Uno (via the serial connection), adds the units (ppm) and sends the information to be posted on the spreadsheet.

Cannabal

Part time mad scientist, gardener, tinker and dreamer.

https://www.cannabalurges.com
Previous
Previous

Happy find in the script

Next
Next

Wait a sec