データの保存、更新、取得ができるWebサービス「milkcocoa」に接続し、データのプッシュ、送信、取得ができるライブラリです。EthernetIF版 https://mlkcca.com/
Dependents: MilkcocoaSample_Eth_tmp MilkcocoaSample_Eth uhuru_hallowin2
Fork of Milkcocoa by
Milkcocoa.cpp
00001 #include "Milkcocoa.h" 00002 00003 #if 0 00004 #if 0 00005 #include "SoftSerialSendOnry.h" 00006 extern SoftSerialSendOnry pc; 00007 #else 00008 extern Serial pc; 00009 #endif 00010 #define DBG(x) x 00011 #else 00012 #define DBG(x) 00013 #endif 00014 00015 DataElement::DataElement() { 00016 json_msg[0] = '\0'; 00017 strcpy(json_msg,"{\"params\":{"); 00018 } 00019 00020 DataElement::DataElement(char *json_string) { 00021 json_msg[0] = '\0'; 00022 strcpy(json_msg,json_string); 00023 } 00024 00025 void DataElement::setValue(const char *key, const char *v) { 00026 char json_string[64]; 00027 if( json_msg[strlen(json_msg)-1] != '{' ) 00028 { 00029 strcat(json_msg,","); 00030 } 00031 sprintf(json_string,"\"%s\":\"%s\"",key,v); 00032 strcat(json_msg,json_string); 00033 } 00034 00035 void DataElement::setValue(const char *key, int v) { 00036 char json_string[64]; 00037 if( json_msg[strlen(json_msg)-1] != '{' ) 00038 { 00039 strcat(json_msg,","); 00040 } 00041 sprintf(json_string,"\"%s\":\"%d\"",key,v); 00042 strcat(json_msg,json_string); 00043 } 00044 00045 void DataElement::setValue(const char *key, double v) { 00046 char json_string[64]; 00047 if( json_msg[strlen(json_msg)-1] != '{' ) 00048 { 00049 strcat(json_msg,","); 00050 } 00051 sprintf(json_string,"\"%s\":\"%f\"",key,v); 00052 strcat(json_msg,json_string); 00053 } 00054 00055 char *DataElement::getString(const char *key) { 00056 static char _word[64]; 00057 char *p; 00058 int i=0; 00059 00060 strcpy(_word , "\"\0"); 00061 strcat(_word , key ); 00062 strcat(_word , "\"" ); 00063 00064 p = strstr( (char*)json_msg , _word ) + 2 + strlen(_word); 00065 00066 while( (p[i] != ',')&&(p[i] != '\n')&&(p[i] != '\"') ) 00067 { 00068 _word[i] = p[i]; 00069 i++; 00070 } 00071 _word[i] = '\0'; 00072 00073 return _word; 00074 } 00075 00076 int DataElement::getInt(const char *key) { 00077 return atoi(getString(key)); 00078 } 00079 00080 float DataElement::getFloat(const char *key) { 00081 return atof(getString(key)); 00082 } 00083 00084 char *DataElement::toCharArray() { 00085 if( json_msg[strlen(json_msg)-1] != '{' ) 00086 { 00087 strcat(json_msg,"}"); 00088 } 00089 strcat(json_msg,"}"); 00090 00091 return(json_msg); 00092 } 00093 00094 Milkcocoa::Milkcocoa(MClient *_client, const char *host, uint16_t port, const char *_app_id, const char *client_id) { 00095 00096 client = _client; 00097 strcpy(servername,host); 00098 portnum = port; 00099 strcpy(app_id,_app_id); 00100 strcpy(_clientid,client_id); 00101 strcpy(username,"sdammy"); 00102 strcpy(password,app_id); 00103 00104 } 00105 00106 Milkcocoa::Milkcocoa(MClient *_client, const char *host, uint16_t port, const char *_app_id, const char *client_id, char *_session) { 00107 00108 client = _client; 00109 strcpy(servername,host); 00110 portnum = port; 00111 strcpy(app_id,_app_id); 00112 strcpy(_clientid,client_id); 00113 strcpy(username,_session); 00114 strcpy(password,app_id); 00115 00116 } 00117 00118 Milkcocoa* Milkcocoa::createWithApiKey(MClient *_client, const char *host, uint16_t port, const char *_app_id, const char *client_id, char *key, char *secret) { 00119 char session[60]; 00120 sprintf(session, "k%s:%s", key, secret); 00121 return new Milkcocoa(_client, host, port, _app_id, client_id, session); 00122 } 00123 00124 int Milkcocoa::getConnectStatus() { 00125 return client->isConnected(); 00126 } 00127 00128 void Milkcocoa::connect() { 00129 00130 if(client->isConnected()) 00131 return; 00132 00133 if(client->connect(servername, portnum)!=0) { 00134 DBG(pc.printf("Network connect err\r\n");) 00135 return; 00136 } 00137 00138 MQTTPacket_connectData data = MQTTPacket_connectData_initializer; 00139 data.keepAliveInterval = 20; 00140 data.cleansession = 1; 00141 data.MQTTVersion = 4; 00142 data.clientID.cstring = _clientid; 00143 data.username.cstring = username; 00144 data.password.cstring = password; 00145 00146 if (client->connect(data) != 0) { 00147 DBG(pc.printf("Milkcocoa connect err\r\n");) 00148 return; 00149 } 00150 } 00151 00152 bool Milkcocoa::push(const char *path, DataElement dataelement) { 00153 char topic[100]; 00154 char *buf; 00155 MQTT::Message message; 00156 00157 sprintf(topic, "%s/%s/push", app_id, path); 00158 00159 message.qos = MQTT::QOS0; 00160 message.retained = 0; 00161 message.dup = false; 00162 buf = dataelement.toCharArray(); 00163 message.payload = (void*)buf; 00164 message.payloadlen = strlen(buf); 00165 if(client->publish(topic, message)!=0) 00166 return(false); 00167 00168 return true; 00169 } 00170 00171 bool Milkcocoa::send(const char *path, DataElement dataelement) { 00172 char topic[100]; 00173 char *buf; 00174 MQTT::Message message; 00175 00176 sprintf(topic, "%s/%s/send", app_id, path); 00177 message.qos = MQTT::QOS0; 00178 message.retained = 0; 00179 message.dup = false; 00180 buf = dataelement.toCharArray(); 00181 message.payload = (void*)buf; 00182 message.payloadlen = strlen(buf); 00183 if(client->publish(topic, message)!=0) 00184 return false; 00185 00186 return true; 00187 } 00188 00189 void Milkcocoa::loop() { 00190 connect(); 00191 client->yield(RECV_TIMEOUT); 00192 } 00193 00194 bool Milkcocoa::on(const char *path, const char *event, GeneralFunction cb) { 00195 MilkcocoaSubscriber *sub = new MilkcocoaSubscriber(cb); 00196 sprintf(sub->topic, "%s/%s/%s", app_id, path, event); 00197 00198 if (client->subscribe(sub->topic, MQTT::QOS0, cb) != 0) { 00199 DBG(pc.printf("Milkcocoa subscribe err\r\n");) 00200 return false; 00201 } 00202 for (int i=0; i<MILKCOCOA_SUBSCRIBERS; i++) { 00203 if (milkcocoaSubscribers[i] == sub) { 00204 return false; 00205 } 00206 } 00207 for (int i=0; i<MILKCOCOA_SUBSCRIBERS; i++) { 00208 if (milkcocoaSubscribers[i] == 0) { 00209 milkcocoaSubscribers[i] = sub; 00210 return true; 00211 } 00212 } 00213 return true; 00214 } 00215 00216 MilkcocoaSubscriber::MilkcocoaSubscriber(GeneralFunction _cb) { 00217 cb = _cb; 00218 }
Generated on Thu Jul 14 2022 01:24:08 by
1.7.2
