Offline LCD + Realtime Clock + Servo integration to thinger.io for catfeeder


#1

Hi there,

I am trying my first project for an automated cat feeder, I have some older code which works perfectly now I need to integrate it with thinger.io and I think I need some parallel proccesing I think to create a nice online dashboard.

I have a servo, a LCD and a Real time Clock. In the current code the servo responds when I press a button and the clock displays fine on my LCD. I can’t use delays in my thinger code. So my question is how can I write the code to have the LCD and clock work in Thinger? And integrate the LCD within thinger? I need parallel processing right? I cant use a delay in my main code i mean?

My current code I want to migrate to thinger.io

#include <virtuabotixRTC.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <Time.h>
#include <TimeAlarms.h>
#include <Servo.h>
#include <SPI.h>

 #define I2C_ADDR    0x3F // <<----- Add your address here.  Find it from I2C Scanner
 #define BACKLIGHT_PIN     3
 #define En_pin  2
 #define Rw_pin  1
 #define Rs_pin  0
 #define D4_pin  4
 #define D5_pin  5
 #define D6_pin  6
 #define D7_pin  7

Servo servo1;
int n = 1;
const int buttonPin = 2; // analog pin used to connect the button
int buttonState = 0;

int pos = 0; // variable to store the servo position
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

// Creation of the Real Time Clock Object
virtuabotixRTC myRTC(5, 6, 7);

void setup() {
Serial.begin(9600);

pinMode(buttonPin, INPUT);

//servo setup
servo1.attach(A0); //analog pin 0
servo1.write(1); //set servo to position 0

// Set the current date, and time in the following format:
// seconds, minutes, hours, day of the week, day of the month, month, year
//myRTC.setDS1302Time(00, 14, 18, 5, 13, 11, 2016);

//set time for alarm and set alarm

// setTime(8,29,0,1,1,11); // set time to Saturday 8:29:00am Jan 1 2011
//setTime(myRTC.hours,myRTC.minutes,0,myRTC.dayofmonth,myRTC.month,16); // set time to Saturday 8:29:00am Jan 1 2011

myRTC.updateTime(); //|

setTime(myRTC.hours,myRTC.minutes,myRTC.seconds,0,0,0); // set time to Saturday 8:29:00am Jan 1 2011

Alarm.alarmRepeat(17,5,0, MorningAlarm); // 8:30am every day
digitalClockDisplay();
//Alarm.delay(1000); // wait one second between clock display NOT VERY GOOD FOR THINGER.IO

lcd.begin (16,2); // <<----- My LCD was 16x2

// Switch on the backlight
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
lcd.home (); // go home

lcd.print(“Pieter’s FoodSyS”);

}

//=======================================================================================================//|
// //|
// Printout by accessing Single Element objects BEGIN //|
// //|
//=======================================================================================================//|
// //|
// This example utilizes the Serial.print function to access individual data elements, this allows for //|
// user defined output format. //|
// //|
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//|
void loop() { //|

//button control

buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
// turn LED on:
lcd.home (); // go home
lcd.clear();
lcd.print(“Serv.Food Manual”);
servo1.write(179);
}
else {
// turn LED off:
servo1.write(1);
lcd.home (); // go home
lcd.clear();
lcd.print(“Pieter’s FoodSyS”);
}

// This allows for the update of variables for time or accessing the individual elements. //|
myRTC.updateTime(); //|

lcd.setCursor (0,1); // go to start of 2nd line
lcd.print(myRTC.hours);
lcd.print(":");
lcd.print(myRTC.minutes);
lcd.print(":");
lcd.print(myRTC.seconds);
lcd.setBacklight(HIGH); // Backlight on

//digitalClockDisplay();
//|
// Start printing elements as individuals //|
//Serial.print(“Current Date / Time: “); //|
// Serial.print(myRTC.dayofmonth); //|
// Serial.print(”/”); //|
// Serial.print(myRTC.month); //|
// Serial.print("/"); //|
// Serial.print(myRTC.year); //|
// Serial.print(" “); //|
// Serial.print(myRTC.hours); //|
// Serial.print(”:"); //|
// Serial.print(myRTC.minutes); //|
// Serial.print(":"); //|
// Serial.println(myRTC.seconds); //|
//|
// Delay so the program doesn’t print non-stop //|
Alarm.delay(1000); //|
} //|
//|
//=======================================================================================================//|
// //|
// Printout using BUFFER objects BEGIN //|
// //|
//=======================================================================================================//|

void MorningAlarm(){
Serial.println(“event triggered”);
Food();
}

void digitalClockDisplay()
{
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();
}

void printDigits(int digits)
{
Serial.print(":");
if(digits < 10)
Serial.print(‘0’);
Serial.print(digits);
}

void Food() {
lcd.home (); // go home
lcd.clear();
lcd.print(“Serving Food”);
servo1.write(170);
Alarm.delay(3000);
servo1.write(1);
Alarm.delay(1500);
lcd.home (); // go home
lcd.print(“Pieter’s FoodSyS”);
}

I need to integrate it with the thinger framework code somehow:

#include <Adafruit_CC3000.h>
#include <SPI.h>
#include <ccspi.h>
#include <ThingerCC3000.h>
#include <Servo.h> 
#define USERNAME "your_username"
#define DEVICE_ID "your_device_id"
#define DEVICE_CREDENTIAL "your_device_credential"

#define SSID "your_ssid"
#define SSID_PASSWORD "your_ssid_password"

#define SERVO_PIN 9

ThingerCC3000 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
Servo servo;

void setup() {
    servo.attach(SERVO_PIN);
    
    // configure wifi network
    thing.add_wifi(SSID, SSID_PASSWORD);

    // controlling servo position
    thing["servo"]["pos"] << [](pson& in){
      servo.write((int)in);
    };

    // reseting servo position
    thing["servo"]["reset"] = [](){
      servo.write(0);
    };
}

void loop() {
  thing.handle();
}

What would be the best approach to have my LCD and clock working inside thinger code? (running independently I think)? My LCD code seems to break the thinger.io code.

Many thanks for any advice! I will make a tutorial of this when I get it to work with pictures!


Remote Servo Control with Adafruit CC3000
#2

First of all I want a picture of the cat feeder! That is a so cool gadgetl! :sunglasses:

Then, I would like to know what hardware you are using. So we can start building a sketch that integrates functionality step by step, like controlling the servo to even replicate your LCD content in the dashboard.

The delay is not a good idea while using thinger, at least in the loop, but a delay of some seconds is not really a pain, however the device become less responsive while interacting from the Internet. Anyway, we can solve the main delay that you are using to update the screen, as we can store the latest update timestamp, and only update again after passing a 1 second threshold. This should do the trick. For reference you can see the Arduino example “02. Digital > Blink Without Delay” if you want to test it in the meanwhile.


#3

Hi Alvarolb!

Thanks for responding I appreciate your hard work! Love to help you with some tutorials.

Picture is coming soon. Girlfriend and me are using an IKEA closet, and hack the cat feeder system in the closet! Its getting great as we will be weighting the cats aswel with the Sparkfun loadsensors later.The idea came about when one cat became really fat and they woke us up in the morning! With this we can stay in bed without the cats waking us up! (We need to have some sleep haha) or we can go somewhere and leave the cats in the weekend home alone and feed them remotely for a few days :wink:

Would love to make some pics of it when its finished and write a good tutorial with pictures and all. Yes the gadget is great! Girlfriend building the pipe system for the food. I am coding the software.

Anyway, I am using the CC3000! Okay you are pointing me towards the blink without delay. That is great.But still maybe you have a better idea.

I am just getting started in C++ ( never programmed in it) so I got to learn all this by trail and error! Even managed to get the dht11 sensor to work and put a 12 volt Led Lamp using the internet.

This is a more advanced project so I got the LCD, Servo (standard servo working) and realtime clock working.

Hardware details:

-Mega2560 R3 ATmega2560-16AU
-I2C 1602 (LCD)
-DS1302 clock (RTC CR2032 battery)
-Standard servo from futaba S3003 (for testing)

  • CC3000 Keyes wifi shield

I can’t share the source code in this community right? ( i can only upload images right now)

General feeding procedure:
-I want the servo to rotate on a certain time .e.g. rotate to pos 180 , stay there 5 sec and then return to 0.

Your ideas:
-I can see the time online?
-I can set the time for the feeding?
-I can set the duration for the pause example start from pos 0 rotate to 180 pauze 5 sec and return to 0.

Lets first get the LCD and RTC to work and I would be really happy!

So my above code is working perfectly but when adding the standard thinger.io framework my clock stops working. Think the loops are not good. Would be great if you can help me so we can showcase this for all the people out there.


#4

Great description @Pieter. Yes, you can share the code in the community, just write your code between ``` and ```. It will display much more better.

I am working in a code to some get some of the functionality you have in your sketch. I will share it soon. Did you managed to connect your Mega to thinger.io with the CC3000?

Yes, you will be able to see the time online, feed by pressing a button in the dashboard on in the phone, also configure the servo delays, program the alarm, etc. :slight_smile:


#5

Hi Alvarob,

The CC3000 works I have tested your demo code. The shield is compatible with the mega which is great.
I found out that I need an Mega for the display, clock, servo and thinger framework, since the Arduino uno memory got full quite fast because of all the libraries. Now the code takes up 10% memory which is great.

I am very curious about the approach and learning new things because my code has subroutines and making usage of the alarm function. I just started in C++ and only 2 months in electronics and Arduino. Learned a lot on the way!

Let me know how I can best integrate the LCD with the thinger framework! Its a great gadget case for your project demos! I can’t seem to figure out how to implement the Display with thinger and handle the procedures and routines.
But the challenge is of-course what makes it fun…Many times I find myself thinking when finishing a project: hmmm what to do next :wink:

Goodluck! and hope to hear from you soon. When we got this working I will write a extensive tutorial about it explaining which parts were used and which libraries and why. (have to do some code cleanup as well)


#6

Here are the software downloads I used to build your own LCD, Servo, RTC kit for a cat feeder.
It should be your standard arsenal when coding LCDs, Gadgets or building the more advanced projects which serve some display and clocks.

Source libs download:

Realtime Clock
Building your own Realtime clock
https://virtuabotix-virtuabotixllc.netdna-ssl.com/core/wp-content/uploads/2014/01/virtuabotixRTC.zip

LCD I2C interface:
This allows you to connect an LCD with I2C interface (easy to connect using 4 wires instead of soldering)

Time alarms
This allows you to trigger an event when you have a clock or you are using the interal time since the device has been turned on.

Wifi Shield CC3000
https://github.com/adafruit/Adafruit_CC3000_Library/archive/master.zip
optional:
https://github.com/sparkfun/SFE_CC3000_Library/archive/master.zip

libraries are used for below hardware

-Arduino Mega2560 R3 ATmega2560-16AU
-I2C 1602 (LCD)
-DS1302 clock (RTC CR2032 battery)
-Standard servo from futaba S3003 (for testing)

  • CC3000 Keyes wifi shield

#7

Many thanks for the detailed info @Pieter, now I can compile the sketch I am working on. Also saw some details about the alarms that wanted to know! :slight_smile: Will keep you updated with a tentative sketch tomorrow!


#8

Ok great Alvaro!

You have cats yourself too? :wink: Let me know werther you need more information. Great that you help out with this.

I am curious how I should write the (sub)routines for the Display as I don;t understand how to design the main loop completely. Thank you! Thinger.io is so cool!


#9

Hi @Pieter! I have been so busy these days! I have some time now, so I will try to design a preliminary code!

By the way, I don’t have cats yet! But I really like them, so it is possible i need some system like this in the future :wink:


#10

Hi @Pieter, I have completed a preliminary code for now. But needs testing with your hardware. I have compiled it successfully with the libraries you mentioned, but the LCD library seems to have changed a little bit, as your code is not completely compatible with my library version. You may need to update your libraries to compile this code.

At this moment, the code should have the following functionality:

  • Work as usual by triggering the alarm to serve food
  • Change the Alarm Time from the Internet
  • Enable or disable the alarm functionality
  • Possibility to serve food as demand (by clicking a button in the dashboard)
  • Get feeder time both in text or JSON. The text will be very useful for displaying the feeder time in a dashboard.
  • Enable or disable feeder backlight

Here comes the code (may not work fine as I have not your hardware for testing):

#include <virtuabotixRTC.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Time.h>
#include <TimeAlarms.h>
#include <Adafruit_CC3000.h>
#include <SPI.h>
#include <ccspi.h>
#include <ThingerCC3000.h>
#include <Servo.h>

#define USERNAME "your_username"
#define DEVICE_ID "your_device_id"
#define DEVICE_CREDENTIAL "your_device_credential"

#define SSID "your_ssid"
#define SSID_PASSWORD "your_ssid_password"

#define I2C_ADDR          0x3F
#define BACKLIGHT_PIN     3
#define SERVO_PIN         9

// instances
ThingerCC3000 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
Servo servo1;
LiquidCrystal_I2C lcd(I2C_ADDR, 16, 2); // need to change this for the new library
virtuabotixRTC myRTC(5, 6, 7);

int alarm_hour=8, alarm_minute=30;
bool alarm_enabled = true;

void setup() {
  Serial.begin(9600);

  //servo setup
  servo1.attach(SERVO_PIN);
  servo1.write(1); //set servo to position 0

  // initialize default alarm
  Alarm.alarmRepeat(alarm_hour, alarm_minute, 0, FoodAlarm);

  // LCD initialization
  //  not available in my library
  pinMode(BACKLIGHT_PIN, OUTPUT);
  //lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.home();
  lcd.print("Pieter's FoodSyS");  

  // configure wifi network
  thing.add_wifi(SSID, SSID_PASSWORD);

  // here come the thinger resources

  // servo control to control the servo remotelly
  thing["servo"] << servo(servo1);

  // enable disable backlight (not sure it will work)
  thing["backlight"] << digitalPin(BACKLIGHT_PIN);

  // read current time
  thing["time"] >> [](pson& out){
    out["hour"] = hour();
    out["minute"] = minute();
    out["second"] = second();
  };

  // allows creating a resource to display time as text (for a Text dashboard Widget)
  thing["time_text"] >> [](pson& out){
      String timeFormat = hour() + String(":") + minute() + String(":") + second(); 
      out = timeFormat.c_str();
  };

  // allows to configure alarm time from internet
  thing["alarm_time"] << [](pson& in){
    if(in.is_empty()){
      in["hour"] = alarm_hour;
      in["minute"] = alarm_minute;
    }else{
      alarm_hour = in["hour"];
      alarm_minute = in["minute"];
      Alarm.alarmRepeat(alarm_hour, alarm_minute, 0, FoodAlarm);
    }
  };

  // for creating a instant food button in the dashboard
  thing["serve_food"] << [](pson& in){
    if(!in.is_empty()){
      Food();
    }
    in = false;
  };
}

void update_lcd_time(){
  lcd.setCursor(0,1);                                                                                     
  lcd.print(hour());     
  lcd.print(":");                                                                                                     
  lcd.print(minute());  
  lcd.print(":");        
  lcd.print(second());  
}

void FoodAlarm(){
  if(alarm_enabled){
    Food();
  }
}

void Food() {
  lcd.home();
  lcd.clear();
  lcd.print("Serving Food");  
  servo1.write(170);
  delay(3000);
  servo1.write(1);
  delay(1500);
  lcd.home ();
  lcd.print("Pieter's FoodSyS");  
}

unsigned long lastClockUpdate = 0;

void handle_time_update(){
   unsigned long currentTime = millis();
  if(currentTime - lastClockUpdate >= 1000 || lastClockUpdate == 0){
    // update time from RTC
    myRTC.updateTime();                                                                               
    setTime(myRTC.hours, myRTC.minutes, myRTC.seconds, 0,0,0);
    
    // update time in lcd
    update_lcd_time(); 
  
    // call alarm delay to handle possible alarms triggers (even with 0)
    Alarm.delay(0);

    // update timestamp
    lastClockUpdate = currentTime;
  }
}

void loop() {
  // update time in lcd and handle alarm triggers
  handle_time_update();
  
  // handle thinger function
  thing.handle();
}

Sure we can improve the code step by step. For now we need to test if this basic functionality works as expected. Let me know if you can compile the code, and it connects to the platform… You may start creating also a dashboard for controlling the feeder, like showing the RTC time, or creating a button for serve food inmediatly :sunglasses:


#11

WOW great I have been waiting for this!!!

I will test your code this week I have been building the mechanical parts together with my girlfriend this week!! Looks promising! Yes we can improve this step by step. I also have pictures of the cat feeder which is also a work in progress. I will upload some pics when we have finished it. I will test soon.


#12

Hey man I am trying to get this to work now with the CC3000 and I noticed that I cant get connected with

#include <Adafruit_CC3000.h>
#include <SPI.h>
#include <ccspi.h>
#include <ThingerCC3000.h>

ThingerCC3000 thing("secret", "catfeeder", "secret");

void setup() {
    thing.add_wifi("name", "pass");
}

void loop() {
  thing.handle();
}

Has the library changed?

I did some testing with the keysshield. Has this stopped working?


#13

the standard Adafruit_cc3000 examples seem to work fine.


#14

Hi! The library did not change too much from the last time. I have an Arduino+Adafruit CC3000. Let me test it this afternoon!


#15

Ok many thanks! Picking this project up again finally!! Thanks alot for this code!!! Eager to test!!


#16

Hi Alvarolb,

Did the CC3000 work for you? I have the Wemos d1 works perfect but the CC3000 doesn’t work anymore, but the samples do of the standard CC3000 library (i tested the chat example)

Many thanks in advance!


#17

Hi @Pieter I had no time to test it. Hope I can take a few minutes this night! Thanks for remembering it!


#18

Hi @Pieter finally got some time to test it. I have tried the stock example for the Adafruit CC3000 with an Arduino Nano and everything seems to work as expected. I recommend you to test the stock example, and to enable the debug output to see what happens, this is my debug output with the cc3000:

[NETWORK] Starting connection...
[NETWORK] CC3000 initialized!
[NETWORK] Connecting to network Meeble
[NETWORK] Getting IP Address...
[NETWORK] Connected!
[_SOCKET] Connecting to iot.thinger.io:25200...
[_SOCKET] Error while connecting!
[_SOCKET] Connecting to iot.thinger.io:25200...
[_SOCKET] Connected!
[THINGER] Authenticating. User: alvarolb Device: cc3000
[THINGER] Writing bytes: 33 [OK]
[THINGER] Authenticated

#19

Hi thank you so much for the response!

I finally tried this debug mode. My CC3000 is not faulty but it seems to be stuck:

[NETWORK] Starting connection…
[NETWORK] CC3000 initialized!
[NETWORK] Connecting to network FRITZ!Box 5490 YH
[NETWORK] Getting IP Address…
[NETWORK] Connected!
[_SOCKET] Connecting to iot.thinger.io:25200
[_SOCKET] Using secure TLS/SSL connection: no
[_SOCKET] Error while connecting!
[_SOCKET] Connecting to iot.thinger.io:25200
[_SOCKET] Using secure TLS/SSL connection: no
[_SOCKET] Error while connecting!
[_SOCKET] Connecting to iot.thinger.io:25200
[_SOCKET] Using secure TLS/SSL connection: no
[_SOCKET] Error while connecting!
[_SOCKET] Connecting to iot.thinger.io:25200
[_SOCKET] Using secure TLS/SSL connection: no
[_SOCKET] Error while connecting!
[_SOCKET] Connecting to iot.thinger.io:25200
[_SOCKET] Using secure TLS/SSL connection: no
[_SOCKET] Error while connecting!

You have any idea? The standard cc3000 examples work so i guess something can’t connect to the thinger.io framework? I am clueless.

Looking forward to hearing from you!


#20

I’m having the same problem with my connections. I have one project that has been running for Months and works great, but I’m not able to create any new ones with different end points. I’m using a NODEMCU with ESP8266.